Skip to main content
Tw Twintrinsic
  • Getting Started
  • Components
  • Theming
  • Completion
Examples
Data Dashboard Game Map
Core
App
Layout
Accordion AccordionItem Card Container Panel Separator Sidebar Splitter
Navigation
AppHeader BottomBar Breadcrumb BreadcrumbItem Menu MenuItem Tabs Tab TabList TabPanel TreeMenu
Data Display
Avatar AvatarGroup Badge Carousel CarouselItem Chip ChipGroup CodeBlock CodeBlockSpeed CodeEditor DataTable Map Progress Skeleton Table TableBody TableCell TableHead TableHeader TableRow Tag TagGroup Timeline TimelineItem Tooltip Tree TreeNode
Metrics
DonutChart PieChart LineChart BarChart HorizontalBarChart AreaChart StatsCard MetricGrid KPICard GaugeChart ProgressMetric MetricTrend
Form
AutoComplete Button ButtonGroup Calendar Checkbox ColorPicker Combobox Dropdown FileUpload FloatLabel Form FormField Input InputSwitch InvalidState Knob ListInput Listbox NumberInput Radio RadioGroup Rating Select SelectGroup Slider Switch TextInput Textarea
Feedback
Modal Stepper StepperStep Toast
Utility
Icon Lazy LazyPanel Masonry ThemeToggle

Switch

The Switch component provides a toggle switch alternative to checkboxes with support for labels, different sizes, and form integration. It's designed for binary choices where the user can toggle between enabled and disabled states.

Examples

Basic Usage

<Switch label="Enable notifications" />

With Initial Value

<Switch 
  label="Dark mode" 
  checked={true}
/>

Different Sizes

<Switch 
  label="Small" 
  size="sm"
/>
<Switch 
  label="Medium (default)" 
  size="md"
/>
<Switch 
  label="Large" 
  size="lg"
/>

Disabled State

<Switch 
  label="Disabled (off)" 
  disabled={true}
/>
<Switch 
  label="Disabled (on)" 
  checked={true}
  disabled={true}
/>

Required Field

<Switch 
  label="I agree to the terms" 
  required={true}
/>

Without Label

Airplane mode
<div class="flex items-center gap-2">
  <span>Airplane mode</span>
  <Switch ariaLabel="Toggle airplane mode" />
</div>

Props

PropTypeDefaultDescription
checkedbooleanfalseWhether the switch is checked
labelstringRequiredLabel text for the switch
namestringRequiredName attribute for the input
disabledbooleanfalseWhether the switch is disabled
requiredbooleanfalseWhether the switch is required
size'sm' | 'md' | 'lg''md'Size of the switch
ariaLabelstringRequiredARIA label for accessibility (used when no visible label is provided)
idstringauto-generatedHTML id for accessibility
classstring''Additional CSS classes

Events

EventDetail TypeDescription
onchange{ checked: boolean }Fired when the switch state changes

Accessibility

The Switch component follows accessibility best practices:

  • Uses a native <input type="checkbox"> for best compatibility
  • Proper focus states with visible indicators
  • Labels are properly associated with inputs
  • ARIA labels for switches without visible labels
  • Proper disabled states with visual indicators
  • High color contrast between track and thumb
  • Smooth transitions for state changes

Keyboard Interaction

  • Tab - Move focus to the switch
  • Space - Toggle the switch state