๐ธslotted nodes
a.k.a distributed nodes.
web โฉ component โฉ slot โฉ slotted nodes
ไปปไฝๅจ่ช่จ web component ๆจ็ฑค(ๅฆ๏ผ<my-component>
)ไน้็็ดๆฅๅญๅ
็ด (direct child)๏ผๅ
ๅซๆๅญ(text node)๏ผ้ฝๆฏ slotted nodes๏ผไพๅฆ๏ผ
Styling in Shadow DOM โฉ ::slotted()
Nodes (in light DOM) that can be inserted into <slot> are known as slottable nodes; when a node has been inserted in a <slot>, it is said to be slotted.
An unnamed
<slot>
will be filled with all of the custom element's top-level child nodes that do not have theslot
attribute. This includes text nodes. ๐ Using templates and slots
Only custom element's top-level children (in the light DOM) may have
[slot]
attributeโ๏ธ ๐ See "light DOM" tab in example code in Using Slots.::slotted() only select slotted element itself, not its descendants (content)โ๏ธ
Slotted Contents
slotted content come from light DOM, they use document stylesโ๏ธ
shadow DOM styles do not affect slotted contentโ๏ธ
::slotted() only select slotted element itself, not its descendants (content)โ๏ธ
::slotted() can't go inside light DOM, for example: ::slotted(div) p is invalidโ๏ธ
::slotted() can't be used in .querySelector()โ๏ธ
ๅจไธ้ข็็ฏไพ็ขผไธญ๏ผๆๅๅๅฅๅจๅค้จๅๅ
ง้จ CSS ๅฐ <span>
ๅ
็ด ๅใ็ฒ้ซใ่ใ็ด
่ฒใๆ ผๅผ่จญๅฎ๏ผๅฐๆผๆๅ
ฅ shadow DOM<slot>
็ <span>
่่จ๏ผๅชๆๅค้จ็ใ็ฒ้ซใ่จญๅฎๆๆ (๐ see: "codepen" tab below)ใ ๐พ replit
Styling Slotted Content
๐พ ::slotted() lab
use document (outer) styles (in light DOM).
style <slot> (in shadow DOM) and rely on CSS inheritance.
use ::slotted(ยซselectorยป) in shadow DOM.
โญ๏ธ use CSS variables: CSS variables exist on all levels, both in light and shadow. ๐ CSS hooks with custom properties
Last updated