Question presented to candidate:
"Our design system has a Card component, and every team wants something different in the header and footer. We also have a DataTable where each team formats cells differently. How would you use slots to support all of that without adding a prop for every variation?"
What a strong answer should cover:
- A slot is a placeholder in the child (
<slot />) that the parent fills with its own template content; the content is compiled in the parent's scope. - Named slots (
<slot name="footer">, filled with<template #footer>) for multiple regions, and fallback content between the<slot>tags. - Scoped slots: the child passes data back as slot props (
<slot :item="item">), and the parent receives it with#item="{ item }". This is how a list or table owns the loop while the parent owns the markup. - Slots compile to functions the child calls when it renders, so slot content is lazy and re-renders with the child.
$slots(oruseSlots()) lets the child check whether a slot was provided, for example to skip an empty header wrapper.- Typing slots with
defineSlots()in TypeScript, and the template rule that mixingv-sloton the component with named<template>slots is a compile error.
Clarifying questions expected:
- "Should the card render its header wrapper when no header is passed?"
- "Does the table need to hand row data back to the caller's cell template?"
Code / implementation expected: Yes β a component with a default and a named slot with fallback, and a scoped slot used by the parent.