Switch21
An on/off setting that takes effect right away. The knob springs to the other side and squashes on the way. Comic turns it into a red button on yellow, Arcade into a joystick ball.
<Switch label="Autosave" />Usage
import { useState } from "react";
import { Switch } from "plonk-ui";
export function Autosave() {
const [on, setOn] = useState(true);
return (
<Switch
label="Autosave"
checked={on}
onChange={(event) => setOn(event.target.checked)}
/>
);
}A switch is a checkbox with role="switch", so it takes the same props:
checked and onChange, or defaultChecked.
Switch or checkbox?
Use a switch when the change happens immediately, like turning on autosave. Use a Checkbox when the choice waits for a submit button, like accepting terms.
Saving every change.
A small click when you press buttons.
Ask your admin to turn this on.
import { useState } from "react";
import { Switch } from "plonk-ui";
export default function Settings() {
const [autosave, setAutosave] = useState(true);
const [sounds, setSounds] = useState(false);
return (
<div className="flex flex-col gap-5">
<Switch
label="Autosave"
description={autosave ? "Saving every change." : "Save with ⌘S."}
checked={autosave}
onChange={(event) => setAutosave(event.target.checked)}
/>
<Switch
label="Sounds"
description="A small click when you press buttons."
checked={sounds}
onChange={(event) => setSounds(event.target.checked)}
/>
<Switch
label="Beta features"
description="Ask your admin to turn this on."
disabled
/>
</div>
);
}In a form
A switch still submits like a checkbox: its value (on by default) under its
name when it's on, and nothing when it's off.
import { useState, type FormEvent } from "react";
import { Button, Switch } from "plonk-ui";
export default function Form() {
const [sent, setSent] = useState<string>();
function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
const data = new FormData(event.currentTarget);
setSent(JSON.stringify(Object.fromEntries(data)));
}
return (
<form onSubmit={handleSubmit} className="flex flex-col items-start gap-4">
<Switch name="digest" label="Weekly digest" defaultChecked />
<Switch name="mentions" value="yes" label="Mentions" />
<Button type="submit">Save</Button>
<code className="empty:hidden">{sent}</code>
</form>
);
}Personalities
Sticker springs the knob across and squashes it like jelly. Retro jumps it in
three steps and Arcade in five frames. Comic turns the track yellow with a red knob and shouts a PAT!; Arcade
lights the track cyan under a pink joystick ball and throws points. Effects play
only when the switch turns on. Turn them off with --plonk-press-effect: none.
Accessibility
- Screen readers announce it as a switch, "on" or "off".
Spacetoggles it, and its label toggles it too.descriptionis linked witharia-describedby.- Pass
aria-labelwhen there's no visible label, like a switch in a table row.
API
All other <input> props are passed through.
| Prop | Type | Default | Description |
|---|---|---|---|
label | ReactNode | — | Visible label, linked to the switch. Without one, pass aria-label. |
description | ReactNode | — | Help text under the label, read by screen readers. |
checked | boolean | — | Controlled on state. Use with onChange. |
defaultChecked | boolean | — | On state on first render, when uncontrolled. |
onChange | ChangeEventHandler<HTMLInputElement> | — | Native change event. Read event.target.checked for the new state. |
name | string | — | Field name in the submitted form data. |
value | string | number | readonly string[] | — | Value sent with the form when on. Defaults to on. |
disabled | boolean | — | Disables the switch. The track turns dashed. |