Installation
Add Component
Copy the code snippet below into you components folder.
Integrate Recipe
Integrate this recipe in to your Panda config.
Usage
Shortcuts
The Switch component also provides a set of shortcuts for common use cases.
SwitchControl
The Switch.Control renders the Switch.Thumb within it by default.
This works:
This might be more concise, if you don't need to customize the thumb:
Examples
Sizes
Use the size prop to change the size of the switch.
Controlled
Use the checked and onCheckedChange prop to control the state of the switch.
Disabled
Use the disabled prop to disable the switch.
Invalid
Use the invalid prop to mark the switch as invalid.
Tooltip
Wrap the Switch.Root with a Tooltip to show a tooltip on hover or focus.
Track Indicator
Use the Switch.Indicator component to display different indicators based on the checked state.
Thumb Indicator
Use the Switch.ThumbIndicator component to add an icon to the switch thumb.
Closed Component
Here's how to setup the switch for a closed component composition.
Props
Root
| Prop | Default | Type | 
|---|---|---|
| variant | 'solid' | 'solid' | 
| size | 'md' | 'xs' | 'sm' | 'md' | 'lg' | 
| value | \on\ | string | numberThe value of switch input. Useful for form submission. | 
| asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. | |
| checked | booleanThe controlled checked state of the switch | |
| disabled | booleanWhether the switch is disabled. | |
| ids | Partial<{ root: string; hiddenInput: string; control: string; label: string; thumb: string }>The ids of the elements in the switch. Useful for composition. | |
| invalid | booleanIf `true`, the switch is marked as invalid. | |
| label | stringSpecifies the localized strings that identifies the accessibility elements and their states | |
| name | stringThe name of the input field in a switch (Useful for form submission). | |
| onCheckedChange | (details: CheckedChangeDetails) => voidFunction to call when the switch is clicked. | |
| readOnly | booleanWhether the switch is read-only | |
| required | booleanIf `true`, the switch input is marked as required, |