Switch

Slant / Controls

An accessible toggle with the slanted pixel as its knob.

Demo

Switches

Usage

@tuttn/slant
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

PropTypeDefaultDescription
checkedboolean—Whether the switch is on. Pass it to control the switch; leave it out to let the switch keep its own state.
defaultCheckedbooleanfalseThe state an uncontrolled switch starts in.
onCheckedChange(checked: boolean) => void—Called with the next state when a click, Space or Enter flips the switch.
disabledboolean—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.
namestring—Submits the switch with a form: while it is on, a hidden input with this name carries `value`.
valuestring'on'The value submitted under `name` while the switch is on.
formstring—The id of the form the hidden input belongs to, when the switch sits outside it.
aria-label / aria-labelledbystring—One of them is required: the switch has no text of its own.
…restButtonHTMLAttributes—Other button attributes and the ref reach the button, which has role="switch".
Full source