Skip to main content

Shadow DOM Slots - Composition

Web Components: Shadow DOM Slots - Composition


What is the HTML named <slot> element used for in web development?

View Answer:
Interview Response: The HTML ‹slot› element is part of the Web Components technology suite, which acts as a placeholder inside a web component that you can fill with your markup. It lets you create separate DOM trees and present them together.

What is the definition of a flattened DOM?

View Answer:
Interview Response: The combination of the Light DOM and the shadow DOM is considered the flattened DOM result. The flattened DOM is viewable in the developer tools for inspection but unavailable in the source.

What is the slot attribute's principal limitation?

View Answer:
Interview Response: The slot="..." attribute is only valid for direct children of the shadow host. For nested elements, it gets ignored. If there are multiple elements in light DOM with the same slot name, they append into the slot.

Code Example:

<!-- invalid slot, must be direct child of user-card -->
<span slot="birthday">01.01.2001</span>
</div>
</user-card>

<!-- The slots below are appended in order -->
<user-card>
<span slot="username">John</span>
<span slot="username">Smith</span>
</user-card>

Can you explain what slot fallback content is in browser rendering?

View Answer:
Interview Response: If we put something inside a ‹slot›, it becomes the fallback, “default” content. The browser shows it if there is no corresponding filler in light DOM. If the slot is in the shadow DOM it renders if there is no slot available in the light DOM.

In the Shadow DOM, what is the default slot?

View Answer:
Interview Response: The first ‹slot› in shadow DOM that does not have a name is a “default” slot. The default slot gets all nodes from the light DOM that are not slotted elsewhere.

Code Example:

<script>
customElements.define(
'user-card',
class extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<div>Name:
<slot name="username"></slot>
</div>
<div>Birthday:
<slot name="birthday"></slot>
</div>
<fieldset>
<legend>Other information</legend>
<slot></slot>  first unnamed slot
</fieldset>
`;
}
}
);
</script>

<user-card>
<div>I like to swim.</div>
<span slot="username">John Smith</span>
<span slot="birthday">01.01.2001</span>
<div>...And play volleyball too!</div>
</user-card>

What are the three methods that handle HTML slot element assignment?

View Answer:
Interview Response: The HTML slot element has three main methods, including assignedSlot, assignedNodes, and assignedElements used to handle or assess element node assignment. The assignedSlot method returns the ‹slot› element that the node gets assigned. The assignedElements() method returns a sequence of the nodes assigned to this slot, and if the flatten option sets to true, the assigned nodes of any other slots are descendants of this slot. If no assigned nodes return, the slot's fallback content returns. The assignedSlot method returns an HTMLSlotElement representing the ‹slot› element the node is inserted in. These methods are useful when we need to show the slotted content and track it in JavaScript.

Code Example:

<custom-menu id="menu">
<span slot="title">Candy menu</span>
<li slot="item">Lollipop</li>
<li slot="item">Fruit Toast</li>
</custom-menu>

<script>
customElements.define(
'custom-menu',
class extends HTMLElement {
items = [];

connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `<div class="menu">
<slot name="title"></slot>
<ul><slot name="item"></slot></ul>
</div>`;

// triggers when slot content changes
this.shadowRoot.firstElementChild.addEventListener(
'slotchange',
(e) => {
let slot = e.target;
if (slot.name == 'item') {
this.items = slot
.assignedElements()
.map((elem) => elem.textContent);
alert('Items: ' + this.items);
}
}
);
}
}
);

// items update after 1 second
setTimeout(() => {
menu.insertAdjacentHTML('beforeEnd', '<li slot="item">Cup Cake</li>');
}, 1000);
</script>