Button Component
The Button component is a flexible, accessible button with multiple variants and sizes.Location
Features
Type-Safe Variants
Uses CVA for compile-time variant validation
Radix Slot Support
Can render as any element using
asChild propAccessible
Built-in focus states and ARIA support
Dark Mode Ready
Includes dark mode styling variants
Implementation
Full Button Component Code
Full Button Component Code
Props API
Button Props
The visual style variant of the button.Options:
default, destructive, outline, secondary, ghost, linkThe size of the button.Options:
default, sm, lg, iconWhen
true, the button will merge its props and behavior onto its immediate child using Radix Slot.Additional CSS classes to apply to the button.
All standard HTML button attributes are supported.
Variants
- default
- destructive
- outline
- secondary
- ghost
- link
The primary button style with solid background.Style: Primary color background with white text and hover darkening effect.
Sizes
Usage Examples
Basic Button
Button with Icon
The Button automatically styles SVG children:Button as Link (Slot Pattern)
UseasChild to render the button as a different element:
When
asChild={true}, the Button component merges its styling and props onto the child element. This is useful for maintaining button styling while rendering as links, router components, or custom elements.Disabled State
pointer-events-none and opacity-50 when disabled.
Styling System
Class Variance Authority (CVA)
The button uses CVA for managing variants:Utility Function: cn
The cn utility combines clsx and tailwind-merge for intelligent class merging:
- Merges Tailwind classes intelligently (e.g.,
px-4 px-6→px-6) - Handles conditional classes
- Prevents class conflicts
Custom Styling
Accessibility Features
Keyboard Navigation
Keyboard Navigation
Screen Readers
Screen Readers
- Semantic
<button>element by default - Supports ARIA attributes via props
data-slot="button"attribute for component identification
Error States
Error States
aria-invalidstyling support- Destructive variant has enhanced focus ring
- Disabled state prevents interaction
Related Components
Navigation
See how buttons are used in navigation
Component Overview
Learn about the component architecture