Toggle
A switch for a setting that applies as soon as it is flipped.
Examples
Playground
Size
md is the default; sm sits in a dense settings list.
sm
md
States
Every state side by side. Hover and focus are forced, so they can be reviewed without a pointer.
off
hover
focus
on
disabled
Content
A description under the label, and the label on either side.
description
label right
Install
npx shadcn add @hipuku/toggleUsage
import { Toggle } from '@/components/haus/components/Toggle'
<Toggle
label="Weekly digest"
description="A summary every Monday"
checked={digest}
onChange={setDigest}
/>API
Toggle also takes the input's own attributes and forwards its ref to the input. className lands
on the root.
| Prop | Type | Description |
|---|---|---|
onChange | ((checked: boolean, event: React.ChangeEvent<HTMLInputElement>) => void) | (checked, event), matching Checkbox and RadioGroup. See Checkbox for why
the event is there. |
label | string | |
description | string | |
size | "sm" | "md" | |
labelPosition | "left" | "right" |
Sizes and layout
sm and md, with md the default. The label sits before the switch; labelPosition="right"
puts it after. onChange gives you (checked, event).
A Toggle has no error, because a switch applies at once and there is nothing to submit later. For
a choice that is saved with a form, use Checkbox.
Accessibility
- Announced as a switch, on or off, and Space flips it.
- The label is its name and the description is read as its description.
- Inside a form with a
name, it posts a value only while it is on.