Switch
An on/off toggle you can tap, press or drag, with a thumb that stretches under your finger and an optional pending state for saves.
- Form components
- 1 parts
- 9 examples
- vs shadcn/ui
@base-ui/react@tabler/icons-reactclass-variance-authority
See it in a real product: Integrations, Memory, Model Compare, and 13 more
View blockpnpm dlx shadcn@latest add https://hextaui.com/r/switch.jsonAdds the component, the HextaUI theme tokens and any HextaUI components it depends on.
Add the theme tokens to your global CSS, if you haven’t yet.
Install the dependencies.
pnpm add @base-ui/react @tabler/icons-react class-variance-authority cnCopy and paste the following code into your project.
components/ui/switch.tsx components/ui/spinner.tsx lib/motion.ts Update the import paths to match your project setup.
Press and the thumb stretches toward the other side before springing across, so you feel the press land. You can also grab the thumb and drag it: past the middle it flips, short of it it snaps back.
variant="ios" uses a taller track, a capsule thumb and green when on. Press, drag and saving work the same.
Return a promise from onCheckedChange and the switch flips right away, even when it's controlled. A spinner shows in the thumb only if saving takes longer than 400ms. If it rejects, the switch flips back and shakes.
icons adds a check and a ring to the track, so the state doesn't rely on color.
sm, default and lg.
With Field, the label and description are linked to the switch.
readOnly keeps the value visible and focusable but won't change it.
Pass checked and onCheckedChange.
The thumb starts on the right and slides left, and dragging follows.
| Key | Action |
|---|---|
| SpaceEnter | Turns the focused switch on or off. |
- The switch is a
switchrole named by its label. Wrap it in a<label>or useField. - While saving, it's
aria-busyand ignores new presses. - The hit area is larger than the switch, and larger again on touch.
- With reduced motion, the thumb moves instantly and nothing shakes.
| Prop | Type | Default |
|---|---|---|
checked | boolean | – |
defaultChecked | boolean | false |
onCheckedChangeReturn a promise to show the saving state. On rejection an uncontrolled switch flips back. | (checked, details) => void | Promise | – |
variant | "default" | "ios" | "default" |
size | "sm" | "default" | "lg" | "default" |
iconsShow on and off marks in the track. | boolean | false |
name | string | – |
value | string | – |
disabled | boolean | false |
readOnly | boolean | false |
required | boolean | false |
render | ReactElement | (props, state) => ReactElement | <span> |
| Attribute | Description |
|---|---|
data-slot="switch" | The track, with data-size and data-variant. |
data-slot="switch-thumb" | The thumb. |
data-checked / data-unchecked | Whether the switch is on. |
data-dragging | Present while the thumb is being dragged. |
data-pending | Present while a returned promise is pending. |
--switch-w / --switch-h / --switch-thumb / --switch-thumb-w | Track and thumb sizes, set by size. |
- MotionThe easing curves, durations and reduced-motion check every component animates with, plus hooks for size morphs and sliding highlights.
- SpinnerA loading indicator with Apple-style ticks or a breathing ring that can wait before showing and stay long enough not to flicker.
- CalendarA date grid for single, range and multiple selection, with sliding months, range previews and touch-sized days.
- CheckboxA checkbox whose check draws in, with indeterminate parents, groups and labels that share its hover.
- ComboboxA filterable select with chips, groups and async results, in a popup that resizes as you type.
- Date pickerA button that opens a calendar in a popover, or a bottom sheet on phones, for single dates and ranges.
Used in Pro blocks
Blocks that build on Switch.
Every block comes with HextaUI Pro, and Prompt Input is free.