Switch
An on/off control that takes effect immediately and works in native forms.
Not in 0.1.0 yet
Purpose
Switch turns a setting on or off. Use it for immediate, binary settings; use a checkbox for
choices confirmed with a submit button in long forms.
Import
import { Label, Switch } from "@neshel-ui/components";Usage
"use client";
import { Button, Label, Switch } from "@neshel-ui/components";
import { useState } from "react";
export default function SwitchForm() {
const [data, setData] = useState("");
return (
<form
onSubmit={(event) => {
event.preventDefault();
setData(JSON.stringify([...new FormData(event.currentTarget).entries()]));
}}
style={{ display: "grid", gap: 12, justifyItems: "start" }}
>
<div style={{ display: "flex", gap: 8, alignItems: "center" }}>
<Switch id="demo-news" name="news" />
<Label htmlFor="demo-news">Newsletter</Label>
</div>
<div style={{ display: "flex", gap: 8, alignItems: "center" }}>
<Switch id="demo-terms" name="terms" required />
<Label htmlFor="demo-terms" required>
Accept terms
</Label>
</div>
<Button type="submit" size="sm">
Save
</Button>
<output>{data}</output>
</form>
);
}API
SwitchProps
| Prop | Type | Default | Description |
|---|---|---|---|
| checked | boolean | — | Controlled state. |
| defaultChecked | boolean | false | Initial state when uncontrolled (default false). |
| disabled | boolean | — | — |
| form | string | — | — |
| id | string | — | Id of the switch button; associate a <Label htmlFor> with it. |
| invalid | boolean | — | Marks the switch aria-invalid. Describe the error with aria-describedby. |
| name | string | — | Form field name; submitted only while the switch is on. |
| onCheckedChange | (checked: boolean) => void | — | — |
| readOnly | boolean | — | Keeps focus and announcements but ignores toggling. |
| ref | Ref<HTMLButtonElement> | — | — |
| required | boolean | — | The form cannot be submitted while the switch is off. |
| uiSize | FieldSize | "md" | Size (default "md"), consistent with Input and Select. |
| value | string | "on" | Submitted value while on (default "on", like a checkbox). |
Also accepts the props of: React DOM attributes.
Controlled and uncontrolled
checked with onCheckedChange, or defaultChecked. In a form the switch submits name=value
(value defaults to "on") only while it is on; required blocks submission while it is off;
a form reset restores defaultChecked and, like native inputs, does not call onCheckedChange.
Files are synchronised.
"use client";
import { Label, Switch } from "@neshel-ui/components";
import { useState } from "react";
export default function SwitchControlled() {
const [enabled, setEnabled] = useState(true);
return (
<div style={{ display: "grid", gap: 12, justifyItems: "start" }}>
<div style={{ display: "flex", gap: 8, alignItems: "center" }}>
<Switch
id="demo-sync"
uiSize="lg"
checked={enabled}
onCheckedChange={setEnabled}
aria-describedby="demo-sync-hint"
/>
<Label htmlFor="demo-sync">Synchronise files</Label>
</div>
<p id="demo-sync-hint">
{enabled ? "Files are synchronised." : "Synchronisation is paused."}
</p>
<div style={{ display: "flex", gap: 8, alignItems: "center" }}>
<Switch id="demo-locked" checked readOnly uiSize="sm" />
<Label htmlFor="demo-locked">Managed by your organisation</Label>
</div>
</div>
);
}Sizes and states
uiSize is sm, md (default) or lg, like the other fields. readOnly keeps the value and stays
focusable; disabled removes it from the tab order and from the form data.
Accessibility
- A native
<button role="switch">witharia-checked; associate a<Label htmlFor>with itsid(clicking the label toggles it). invalidsetsaria-invalid; describe the error witharia-describedby.- Visible focus ring on keyboard focus, system colours in forced-colours mode, no thumb transition under reduced motion.
Keyboard
| Key | Action |
|---|---|
| Space / Enter | Toggle |
| Tab | Move focus |
Common mistakes
- A switch without a label, or a label that changes with the state: keep the name constant and let
aria-checkedtell the state. - A switch that only takes effect after pressing Save: use a checkbox.
- Reading
falsefrom the form data: an off switch is absent, like an unchecked checkbox.