Slot

Slot

Utilities
Composition

Merges props onto its immediate child, enabling polymorphic component patterns. Allows components to render as different elements while preserving their styling and behavior.

Preview

Button rendering as different elements

How It Works

Understanding the asChild pattern

Without Slot (asChild=false)

Component renders its default element (e.g., Button renders a <button>)

With Slot (asChild=true)

Component's props merge onto its child element. The child becomes the rendered element.

Common Patterns

Typical use cases for asChild

Download Links

Download button.json

External Links

View on GitHub

Direct Slot Usage

Using Slot in custom components

import { Slot } from '@radix-ui/react-slot'
interface MyButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
asChild?: boolean
}
function MyButton({ asChild, ...props }: MyButtonProps) {
const Comp = asChild ? Slot : 'button'
return (
<Comp
className="px-4 py-2 bg-primary-default text-primary-foreground rounded-lg"
{...props}
/>
)
}
// Usage
<MyButton>Click me</MyButton> // Renders <button>
<MyButton asChild>
<Link href="/page">Go to page</Link> // Renders <a>
</MyButton>

Props

Slot API reference

PropTypeDefaultDescription
children*React.ReactElement—Single React element to merge props onto

Note: Slot requires exactly one child element. Multiple children or text nodes will cause errors.

Usage

Import and implementation examples

// Using with Button component
import { Button } from '@/components/ui/button'
import Link from 'next/link'
// Button as Next.js Link
<Button asChild>
<Link href="/dashboard">
Dashboard
</Link>
</Button>
// Button as anchor
<Button asChild variant="link">
<a href="mailto:hello@example.com">
Contact Us
</a>
</Button>
// Direct Slot import for custom components
import { Slot } from '@radix-ui/react-slot'
function Card({ asChild, ...props }) {
const Comp = asChild ? Slot : 'div'
return <Comp className="p-4 border rounded-xl" {...props} />
}
// Card as article
<Card asChild>
<article>Content</article>
</Card>

Features

Built-in functionality

  • Prop merging: Combines parent and child props intelligently
  • Event handling: Handlers from both parent and child are called
  • Ref forwarding: Refs work correctly on the rendered element
  • Style merging: className and style props are merged
  • Type safety: Full TypeScript support with proper inference
  • React 18+: Compatible with latest React features

Accessibility

Accessibility considerations

ARIA Attributes

ARIA attributes pass through to rendered elementrole attribute from either parent or child is preservedSemantic HTML elements maintain their accessibility

Keyboard Navigation

KeyAction
DependsKeyboard behavior follows rendered element

Notes

  • Links (a) and buttons have different semantics - use appropriately
  • Use asChild to render semantic elements (link for navigation, button for actions)
  • Screen readers announce based on the actual rendered element

Related Components