Switch
Slant / Controls
An accessible toggle with the slanted pixel as its knob.
Demo
Usage
import { Switch } from '@tuttn/slant'
// Controlled: pass checked and onCheckedChange.
const [sync, setSync] = useState(true)
<Switch checked={sync} onCheckedChange={setSync} aria-label="Sync checkpoints" />
// Uncontrolled: leave checked out; it keeps its own state from defaultChecked.
<Switch defaultChecked onCheckedChange={save} aria-labelledby="notify-label" />
// In a form: with a name it submits like a checkbox, its value only while on.
<form>
<Switch name="autosave" value="yes" size="sm" aria-label="Auto-save" />
</form>Props
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | — | Whether the switch is on. Pass it to control the switch; leave it out to let the switch keep its own state. |
defaultChecked | boolean | false | The state an uncontrolled switch starts in. |
onCheckedChange | (checked: boolean) => void | — | Called with the next state when a click, Space or Enter flips the switch. |
disabled | boolean | — | Dims the switch and ignores input. A disabled switch is not submitted with its form. |
size | 'sm' | 'md' | 'md' | `md` is 40×22px; `sm` is a 32×18px switch for dense rows. |
name | string | — | Submits the switch with a form: while it is on, a hidden input with this name carries `value`. |
value | string | 'on' | The value submitted under `name` while the switch is on. |
form | string | — | The id of the form the hidden input belongs to, when the switch sits outside it. |
aria-label / aria-labelledby | string | — | One of them is required: the switch has no text of its own. |
…rest | ButtonHTMLAttributes | — | Other button attributes and the ref reach the button, which has role="switch". |