Skip to content

Commit e479c53

Browse files
committed
docs(view): further detail and explanation of slot define attribute
1 parent d820601 commit e479c53

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

docs/1-essentials/02-views.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,17 @@ For instance, the snippet below implements a tab component that accepts any numb
579579

580580
### Define slot ownership in nested view components
581581

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+
582588
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.
583589
```html x-container.view.php
584590
<div class="w-full max-w-7xl mx-auto px-4 sm:px-6 lg:px-8"><x-slot/></div>
585591
```
586-
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>`.
587593
```html x-header.view.php
588594
<header>
589595
<x-slot name="top" />
@@ -607,7 +613,11 @@ At the callsite, you still use the `name` attribute to define which slot you're
607613
<x-slot name="left">Some content I want to insert</x-slot>
608614
</x-header>
609615
```
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:
611621
```html x-outer.view.php
612622
<div class="outer">
613623
<x-inner>
@@ -617,12 +627,13 @@ You can also push content into a child's named slot this way:
617627
</x-inner>
618628
</div>
619629
```
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:
621631
```html outercallsite.view.php
622632
<x-outer>
623633
<x-slot name="left">My override</x-slot>
624634
</x-outer>
625635
```
636+
And so, this places the literal string `My override` into `<x-innner>`'s `left` slot.
626637

627638
### Dynamic view components
628639

0 commit comments

Comments
 (0)