Skip to content

Commit 4414f74

Browse files
committed
Add @with_children section to common patterns docs
1 parent 552c90b commit 4414f74

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

docs/common-patterns.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,43 @@ print(
160160
)
161161
)
162162
```
163+
164+
### Components with children
165+
166+
When building their own set of components, some prefer to make their own
167+
components accept children nodes in the same way as the HTML elements provided
168+
by htpy.
169+
170+
Making this work correctly in all cases can be tricky, so htpy provides a
171+
decorator called `@with_children`.
172+
173+
With the `@with_children` decorator you can to convert a component like this:
174+
175+
```py
176+
from htpy import Element, Node
177+
178+
def my_component(*, title: str, children: Node) -> Element:
179+
...
180+
```
181+
182+
That is used like this:
183+
184+
```py
185+
my_component(title="My title", children=h.div["My content"])
186+
```
187+
188+
Into a component that is defined like this:
189+
190+
```py
191+
from htpy import Element, Node, with_children
192+
193+
@with_children
194+
def my_component(children: Node, *, title: str) -> Element:
195+
...
196+
```
197+
198+
And that is used like this, just like any HTML element:
199+
200+
```py
201+
my_component(title="My title")[h.div["My content"]]
202+
```

0 commit comments

Comments
 (0)