🔀
Slot
The use of slots improves the composition allowing to reflect the content exposed in the lightDOM of the component in the shadowDOM of this, example:
The use of slot is thanks to the use of the ShadowDOM api, to make use of the shadowDom you must declare it as a property of the host tag, example:
<host shadowDom>I exist inside the shadowDOM</host>

::slotted(<selector>)

The slotted selector allows the manipulation of the content exposed as a lightDOM slot from the shadowDOM, example:

Auto slot

With Atomico you can define a default slot for your components, this is useful if you want to maintain some compositional consistency without the need to declare the use of slot, example:
1
<my-component>
2
<my-component-header></my-component-header>
3
<my-component-footer></my-component-footer>
4
</my-component>
Copied!
To define a default slot you only have to declare the prop slot in the props, example:
1
myComponentHeader.props = {
2
slot: { type:String, value: "header"}
3
}
4
5
myComponentFooter.props = {
6
slot: { type:String, value: "footer"}
7
}
Copied!
Consider this practical only if the composition is leveraged to the container, this does not prevent you from modifying the slot property from the component instance.

Conditional slot

Atomico has the hook @atomico/hooks/use-slot that appends the slotchange event to a reference, this will allow you to hide slots if they do not declare content, example:
1
import { useRef } from "atomico";
2
import { useSlot } from "@atomico/hooks/use-slot";
3
4
function component(){
5
const ref = useRef();
6
const childNodes = useSlot(ref);
7
return <host shadowDom>
8
<header style={{
9
display: childNodes.length? "block": "none"
10
}}>
11
<slot name="header" ref={ref}/>
12
</header>
13
</host>
14
}
Copied!
Copy link
Edit on GitHub