Slots are a powerful feature when working with webcomponents that use the shadowDOM. And thanks to the hook @atomico/hooks/use-slot
you will be able to observe the state of the slot and know the childNodes of this to manage the logic of the template. For example:
Thus managing to easily condition parts of our template to define the slots.
Another trick that useSlot allows us is the interaction at the DOM level, example:
We can even apply filters per instance for a better identification of the node, example:
⚠️ In case of high ui computation use prefer to use slot modifiers inside a useEffect
.
Slots are not only limited to a static definition, you can use them to identify a node and use it to create new nodes according to the logic of the webcomponent, example:
Thanks to useSlot(refSlotTemplateUser)
we get the first node of the Template
slot to be used in the iteration of the results obtained by fetch, with this we have eliminated the leverage of the webcomponent in charge of rendering the user at the level of our webcomponent userFetch
Example
⚠️ the cloneNode
property is available from version atomico@1.37.0