liveBook · Manning

Named slot

As explained in Vanilla Web

A named slot is a slot with a name attribute that receives only child nodes whose slot attribute has the matching value.

A named slot is a <slot> with a name attribute in a shadow DOM. It receives child nodes from the custom element only when their slot attribute matches that name. section 8.4

Use named slots to let consumers provide content for specific regions while the component retains its own structure.

  • Each name should be unique within a shadow root; a component can define multiple named slots. section 8.4
  • Named slots can coexist with a default unnamed slot. An empty slot="" value is treated as content for a named slot projection. section 8.4 section 8.9

Component layout regions

A component can provide named regions for title, media, and actions, allowing consumers to supply content for each without exposing the component’s internal markup. section 8.9

Figure 8.3 This diagram shows how elements from the main DOM can be projected into the shadow DOM of a custom element using the <slot> element. When you use a <slot> in a shadow DOM, the children of the custom element in the main DOM are projected into it as slotted content.
ch08 F003 firtman

See how child elements from the main DOM are projected into slots in a custom element’s shadow DOM. section 8.4

How Vanilla Web covers it

How deep it goes
Defines name matching, uniqueness, coexistence with default slots, and the empty-string assignment case.
Examples
Uses title, media, and actions as named regions in component layouts.
What you can do after
Apply named slots to compose component layouts from consumer-provided children.
How it connects
Connects named slots to shadow DOM, custom elements, light DOM, and default slots.

Also explained in

Compare how 2 books explain named slot →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.