|
1 |
| -This tutorial chapter needs to be written! |
| 1 | +Components, _unlike_ native HTML elements, but similar to WebComponents, can have multiple zones or regions for differently named block contents. |
2 | 2 |
|
3 |
| -It could be written by you!, if you want <3 |
| 3 | +In the previous chapter, we learned that the default space between an opening and closing tag is the `:default` block. |
| 4 | + |
| 5 | +Here, we may pass multiple blocks via `<:namedBlock>` notation, |
| 6 | +```hbs |
| 7 | +<MyComponent> |
| 8 | + <:header>some heading</:header> |
| 9 | +
|
| 10 | + <:body> |
| 11 | + main content |
| 12 | + </:body> |
| 13 | +
|
| 14 | + <:footer>footer content</:footer> |
| 15 | +</MyComponent> |
| 16 | +``` |
| 17 | + |
| 18 | +`<:namedBlock>` notation can be identified by the preceeding `:` character in both the opening and closing tag. |
| 19 | + |
| 20 | +Inside `MyComponent`, we need to allow each of these blocks to be used via `{{yield to="blockName"}}` |
| 21 | + |
| 22 | +```gjs |
| 23 | +const MyComponent = <template> |
| 24 | + <header>{{yield to="header"}}</header> |
| 25 | +
|
| 26 | + <main> |
| 27 | + {{yield to="body"}} |
| 28 | + </main> |
| 29 | +
|
| 30 | + <footer>{{yield to="footer"}}</footer> |
| 31 | +</template>; |
| 32 | +``` |
| 33 | + |
| 34 | +<p class="call-to-play">Practice using named blocks within the playground area</p>. |
| 35 | + |
| 36 | +Take note, there _are_ some constraints when it comes to invoking components with named blocks to help with conceptual consistency: |
| 37 | +- _the order of named blocks regions does not matter._ |
| 38 | + ```glimmer |
| 39 | + <MyComponent> |
| 40 | + <:header>some heading</:header> |
| 41 | + <:body>body content here</:body> |
| 42 | + </MyComponent> |
| 43 | + ``` |
| 44 | + is the same as |
| 45 | + ```glimmer |
| 46 | + <MyComponent> |
| 47 | + <:body>body content here</:body> |
| 48 | + <:header>some heading</:header> |
| 49 | + </MyComponent> |
| 50 | + ``` |
| 51 | + This is because the placement of a block is determined by `<MyComponent>`, not the caller. This makes named blocks a good tool for design systems. |
| 52 | +- _free-form content may not exist outside of named-blocked invocation._ |
| 53 | + For example, this is invalid: |
| 54 | + ```glimmer |
| 55 | + <MyComponent> |
| 56 | + content here |
| 57 | + <:body>body content here</:body> |
| 58 | + </MyComponent> |
| 59 | + ``` |
| 60 | + This is because, syntactically, content directly within the `<MyComponent>` and `</MyComponent>` belongs to the `:default` block, which is the same as defining |
| 61 | + ```glimmer |
| 62 | + <MyComponent> |
| 63 | + <:default> |
| 64 | + content here |
| 65 | + </:default> |
| 66 | + <:body>body content here</:body> |
| 67 | + </MyComponent> |
| 68 | + ``` |
| 69 | +- _named blocks may not contain named blocks from the same parent component._ |
| 70 | + for example, this is invaild |
| 71 | + ```gjs |
| 72 | + const Demo = <template> |
| 73 | + <MyComponent> |
| 74 | + <:body> |
| 75 | + body content here |
| 76 | + <:header>some heading</:header> |
| 77 | + </:body> |
| 78 | + </MyComponent> |
| 79 | + </template> |
| 80 | + ``` |
| 81 | + This is invalid because the block content is **owned by the invoker*, so `<:header>` doesn't actually exist in the above component because there is no `{{yield to="header"}}` in `<Demo>`. You'll receive a build-time error similar to: |
| 82 | + ``` |
| 83 | + Unexpected named block inside <:body> named block: |
| 84 | + named blocks cannot contain nested named blocks: |
| 85 | + ``` |
| 86 | + _even_ if you try to defined `{{yield to="header"}}` |
| 87 | + ```gjs |
| 88 | + const Demo = <template> |
| 89 | + {{yield to="header"}} |
| 90 | + <MyComponent> |
| 91 | + <:body> |
| 92 | + body content here |
| 93 | + <:header>some heading</:header> |
| 94 | + </:body> |
| 95 | + </MyComponent> |
| 96 | + </template> |
| 97 | + ``` |
| 98 | +- _a component cannot pass content its own block._ |
| 99 | + For example, trying to pass content to the `:header` named block: |
| 100 | + ```gjs |
| 101 | + const Demo = <template> |
| 102 | + {{yield to="header"}} |
| 103 | + <:header>some heading</:header> |
| 104 | + </template> |
| 105 | + ``` |
0 commit comments