Skip to main content

Slot

The composition utility that powers the asChild pattern.

import { Slot, Slottable, createSlot, createSlottable } from '@gentleduck/primitives/slot'
import { Slot, Slottable, createSlot, createSlottable } from '@gentleduck/primitives/slot'

What it does

When <Primitive.button asChild> is used, the button element is replaced by a Slot, and the child element receives all the button's props (ARIA attributes, event handlers, refs, etc.).

How props merge

Prop typeBehavior
Event handlers (onClick, etc.)Both fire (child first, then slot)
styleMerged (child overrides slot)
classNameConcatenated
refBoth refs receive the element
Everything elseChild props override slot props

Creating named slots

Use createSlot and createSlottable to create named instances for better debugging in React DevTools:

const MySlot = createSlot('MyComponent')
const MySlottable = createSlottable('MyComponent')
const MySlot = createSlot('MyComponent')
const MySlottable = createSlottable('MyComponent')

Slottable

<Slot>
  <Slottable>
    <button>This replaces the Slot</button>
  </Slottable>
  <span>This is preserved as a sibling</span>
</Slot>
<Slot>
  <Slottable>
    <button>This replaces the Slot</button>
  </Slottable>
  <span>This is preserved as a sibling</span>
</Slot>