Performance: Named slots
Named slots affect how content is distributed inside components, impacting rendering and layout stability.
<MyComponent> <div slot="header">Header content</div> <div slot="default">Default content</div> <div slot="footer">Footer content</div> </MyComponent>
<MyComponent> <div slot="header">Header content</div> <div slot="footer">Footer content</div> <div>Default content</div> </MyComponent>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Unnamed default slot mixed with named slots | Multiple nodes inserted dynamically | Multiple reflows on content change | Higher paint cost due to layout shifts | [X] Bad |
| Explicitly named slots including default | Stable node insertion | Single reflow on initial render | Lower paint cost with stable layout | [OK] Good |
header inside a Svelte component?name, not id or custom tags.<MyComponent> <div slot="title">Welcome</div> <div slot="footer">Goodbye</div> </MyComponent>
MyComponent.svelte you have:<slot name="title">Default Title</slot> <slot name="footer">Default Footer</slot>
slot="title" fills the named slot "title" in the component.<div slot="title">Welcome</div> replaces the default title slot content, same for footer.<MyComponent> <div slot="header">Hello</div> <div slot="footer">Bye</div> <div slot="header">Again</div> </MyComponent>
MyComponent defines named slots header and footer.header content is replaced by the second because slots accept one content block per name.Card with named slots for header, content, and footer. You also want to provide default content for each slot if none is passed. Which code snippet correctly implements this?name attribute and default content inside the slot tags.Pass the test and this pattern moves unknown