You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/1-essentials/02-views.md
+14-3Lines changed: 14 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -579,11 +579,17 @@ For instance, the snippet below implements a tab component that accepts any numb
579
579
580
580
### Define slot ownership in nested view components
581
581
582
+
You can use `<x-slot name="mySlot" />` interchangeably to both *define* a slot with optional default content, or to provide content to *populate* the slot. Tempest will consider the hierarchy of the components from the AST to automatically detect your intent, however in more complex, especially nested, view components this can result in unexpected behaviour.
583
+
584
+
To override this behaviour and manually control in which view component your slots are considered to be *defined*, you can use `<x-slot define="mySlot" />` syntax instead. This causes the slot to be registered against the view component in which the keyword 'define' is used, instead of where the `slot` itself appears in the AST.
585
+
586
+
#### Extendable view component example using `define`
587
+
582
588
Let us assume you have an `x-container` view component, which is a `<div>` with formatting to act as a flex container for responsive sizing. You use this component repeatedly across your project, and it's effectively a macro to open and close the `<div>`; it doesn't have any slots or do anything special itself otherwise, with only a default `<x-slot/>` to render whatever it is given.
Now, assume we have an `x-header` in which we wish to use the `x-container`. Our `x-header` wishes to place slots `left` and `right` inside it; `x-header` owns these slots and wishes to expose these slots at the callsite in case they need custom content. Using the `define` keyword tells Tempest to treat these slots as *owned* by `x-header` instead of as a *slot to fill* inside `x-container`.
592
+
Now, assume we have an `x-header` in which we wish to use the `x-container`. Our `x-header` wishes to place slots `left` and `right` inside it; `x-header` owns these slots and wishes to expose these slots at the callsite in case they need custom content. Using the `define` keyword tells Tempest to treat these slots as *defined* by `x-header` instead of as a *slot to fill* inside `x-container`. Using `name` here instead of `define` would mean that Tempest falls back to the AST, and treats them as if they are slots of `<x-container>`.
587
593
```html x-header.view.php
588
594
<header>
589
595
<x-slotname="top" />
@@ -607,7 +613,11 @@ At the callsite, you still use the `name` attribute to define which slot you're
607
613
<x-slotname="left">Some content I want to insert</x-slot>
608
614
</x-header>
609
615
```
610
-
You can also push content into a child's named slot this way:
616
+
This example would replace the default content `<x-header-left />` instead with the literal string `Some content I want to insert` - or whatever you provide.
617
+
618
+
#### Populating a child's name slot using `define`
619
+
620
+
You can also push content into a child's named slot, not just the default slot, by creating a `define`d slot as follows:
611
621
```html x-outer.view.php
612
622
<divclass="outer">
613
623
<x-inner>
@@ -617,12 +627,13 @@ You can also push content into a child's named slot this way:
617
627
</x-inner>
618
628
</div>
619
629
```
620
-
At the callsite:
630
+
Again, the `define` keyword registers a `left` slot against the view component `<x-outer>` irrespective of it's position in the AST, and means that at the callsite:
621
631
```html outercallsite.view.php
622
632
<x-outer>
623
633
<x-slotname="left">My override</x-slot>
624
634
</x-outer>
625
635
```
636
+
And so, this places the literal string `My override` into `<x-innner>`'s `left` slot.
0 commit comments