Imperative slot assignment
Imperative slot assignment gives JavaScript direct control over which slotted content ends up in which part of a component. It helps when static slot names are not expressive enough for the component's composition rules.
Overview
Imperative slot assignment gives JavaScript direct control over which slotted content ends up in which part of a component. It helps when static slot names are not expressive enough for the component's composition rules.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 92 | 92 | 92 | 16.4 | 92 | 16.4 | |
| DOM API | ||||||
| The read-only slotAssignment property of the ShadowRoot interface returns the slot assignment mode for the shadow DOM tree. Nodes are either automatically assigned (named) or manually assigned (manual). The value of this property defined using the slotAssignment option when calling Element.attachShadow(). | 86 | 86 | 92 | 16.4 | 86 | 16.4 |
- Before Chrome 95, the method accepted any `Node` instead of just `Element` and `Text`.
- Accepted `sequence<Node>` instead of `(Element or Text)...`.
- This browser only partially implements this feature
- This feature was removed in a later browser version (92)
- Before Edge 95, the method accepted any `Node` instead of just `Element` and `Text`.
- Accepted `sequence<Node>` instead of `(Element or Text)...`.
- This browser only partially implements this feature
- This feature was removed in a later browser version (92)
- Before Chrome Android 95, the method accepted any `Node` instead of just `Element` and `Text`.
- Accepted `sequence<Node>` instead of `(Element or Text)...`.
- This browser only partially implements this feature
- This feature was removed in a later browser version (92)
Syntax
const shadow = host.attachShadow({ mode: 'open', slotAssignment: 'manual' });
shadow.innerHTML = '<slot></slot>';
const slot = shadow.querySelector('slot');
slot.assign(host.querySelector('.item')); Live demo
Fallback content
Use slot fallback content when the host does not provide a named assignment.
Dynamic reassignment
Move existing elements between slots to change layout without rebuilding the host element.
Use cases
Dynamic layout regions
Assign children into panels, headers, or side areas based on runtime rules instead of hard-coded markup structure.
Adaptive component layouts
Change where projected content appears when the component switches between compact and expanded modes.
Cautions
- Imperative slotting can make component structure harder to inspect, so document the assignment rules clearly.
- If the layout is mostly static, named slots remain easier to read and maintain.
Accessibility
- Moving content between slots must preserve reading order, heading relationships, and control associations.
- Avoid slot logic that surprises assistive technology users by changing structure without a visible reason.
Related links
Powered by web-features