Forms
Switch
Free11.8 KB gzippedAn on/off toggle for settings that take effect immediately. Renders a native <input type="checkbox" role="switch"> under a pill track, so Space toggles it, forms submit it, and screen readers announce "on" or "off". Works standalone or inside a Field.
Accessibility: WCAG 2.2 AA · No A or AA violations in light or dark mode · Keys: Space · Full report
Import
import { Switch } from "@vjdesigns/vjdesigns-ui";
Importing only Switch adds about 11.8 KB of minified, gzipped JavaScript to your app (React not included). The stylesheet is shared by every component and loaded once: 75.1 KB gzipped.
Examples
Basic
For settings that take effect right away. Screen readers announce "on" or "off".
import { Stack, Switch } from "@vjdesigns/vjdesigns-ui";
export function Basic() {
return (
<Stack gap={4}>
<Switch label="Wi-Fi" defaultChecked />
<Switch label="Bluetooth" description="Visible to nearby devices as “Jordan's laptop”." />
</Stack>
);
}
Settings list
labelPosition="between" fills the row, which suits settings lists.
import { Card, Divider, Stack, Switch } from "@vjdesigns/vjdesigns-ui";
export function SettingsList() {
return (
<Card variant="outline" padding="md" style={{ width: "100%", maxWidth: "26rem" }}>
<Stack gap={4} divider={<Divider />}>
<Switch label="Email alerts" labelPosition="between" defaultChecked />
<Switch label="Push notifications" labelPosition="between" />
<Switch label="Weekly summary" labelPosition="between" defaultChecked />
</Stack>
</Card>
);
}
Saving state
loading blocks toggling while a change saves. showStateLabels adds visible On/Off text.
import { useState } from "react";
import { Switch } from "@vjdesigns/vjdesigns-ui";
export function SavingState() {
const [checked, setChecked] = useState(false);
const [saving, setSaving] = useState(false);
function toggle(next: boolean) {
setSaving(true);
setTimeout(() => {
setChecked(next);
setSaving(false);
}, 1200);
}
return <Switch label="Two-factor authentication" checked={checked} onCheckedChange={toggle} loading={saving} showStateLabels />;
}
Accessibility
showStateLabels adds visible on/off text, so the state doesn't rely on color or the thumb's position alone (SC 1.4.1). While a change is saved, loading sets aria-busy, blocks toggling and adds "Saving" to the description.
import { useState } from "react";
import { Switch } from "@vjdesigns/vjdesigns-ui";
export function Accessibility() {
const [on, setOn] = useState(true);
const [saving, setSaving] = useState(false);
return (
<Switch
label="Two-factor authentication"
description="Ask for a code when you sign in on a new device."
showStateLabels
checked={on}
loading={saving}
onCheckedChange={(next) => {
setSaving(true);
setTimeout(() => {
setOn(next);
setSaving(false);
}, 1200);
}}
/>
);
}
Translated
The state labels and the "Saving" screen-reader text come from the locale (Brazilian Portuguese here).
import { Stack, Switch, LocaleProvider } from "@vjdesigns/vjdesigns-ui";
import ptBR from "@vjdesigns/vjdesigns-ui/locales/pt-BR";
export function Translated() {
return (
<LocaleProvider locale="pt-BR" messages={ptBR}>
<Stack gap={4}>
<Switch label="Modo escuro" showStateLabels defaultChecked />
<Switch label="Sincronizar contatos" description="Salvando suas preferências" loading defaultChecked />
</Stack>
</LocaleProvider>
);
}
Right to left
The first example again, inside a right-to-left LocaleProvider (Arabic). Layout, spacing and arrow icons mirror, and the components' built-in text is translated. The example's own text stays in English.
Basic
import { LocaleProvider } from "@vjdesigns/vjdesigns-ui";
import ar from "@vjdesigns/vjdesigns-ui/locales/ar";
// Basic is the "Basic" example above.
<LocaleProvider locale="ar" messages={ar}>
<Basic />
</LocaleProvider>
API reference
Switch
| Prop | Type | Default |
|---|---|---|
checkedWhether the switch is on (controlled). | boolean | — |
defaultCheckedInitial on state when uncontrolled. | boolean | — |
onCheckedChangeCalled with the next on state whenever the user toggles the switch. | (checked: boolean) => void | — |
labelVisible label. The whole label is clickable. When omitted, give the switch a name with | ReactNode | — |
descriptionSupporting text under the label, linked with | ReactNode | — |
labelPositionWhere the label sits relative to the switch. | "start""end""between" | — |
sizeTrack, thumb, and label size. Defaults to the surrounding Field's size, then | "sm""md""lg" | — |
toneTrack color while on. | "neutral""primary""accent""success""warning""danger""info" | — |
invalidMarks the switch invalid ( | boolean | — |
disabledDisables the switch. Inherited from Field. | boolean | — |
requiredMarks the switch as required for form submission. Inherited from Field. | boolean | — |
readOnlyKeeps the switch focusable and announced, but blocks toggling ( | boolean | — |
thumbIconDecorative icons shown inside the thumb for each state, e.g. | Partial<SwitchStateContent> | — |
showStateLabelsShows visible state text next to the track, so the state doesn't rely on color or position alone. | boolean | — |
stateLabelsText for | SwitchStateContent | — |
loadingShows a spinner in the thumb, sets | boolean | — |
loadingLabelScreen-reader text added to the description while | string | — |
Also accepts native <input> attributes, plus className, style and ref.
See what changed in each version in the Changelog.
Accessibility report
Switch targets WCAG 2.2 AA. Below: a live axe-core audit of this page's examples, Lighthouse scores for the page in both modes, the accessibility behavior built into the component, and the checks only a person can do.
axe-core audit (live)
Checking the examples above…
Lighthouse audit
Light mode
Score 10036 passed · 0 failed · 29 not applicable · 10 to check by hand
Dark mode
Score 10036 passed · 0 failed · 29 not applicable · 10 to check by hand
Lighthouse 13.5.0 audits the whole docs page (header and sidebar included), not just the examples, in English. Last run 11/27/2026. Some failed audits carry no weight in the score, so a page can score 100 and still list one.
Built-in behavior
Keyboard
| Key | Action |
|---|---|
| Space | Turns the switch on or off. |
Roles and states
| Element | Role | States and properties |
|---|---|---|
<input type="checkbox"> | switch | On / off is the native checked state; aria-busy while loading, aria-invalid, aria-describedby |
Manual checks
Automated tools find roughly a third of accessibility issues. Test these in your app, with your content.
| Check | What to test | WCAG 2.2 |
|---|---|---|
| Keyboard | Every action works with Tab, Shift+Tab, Enter, Space and the arrow keys, and focus never gets stuck. | 2.1.1 (A), 2.1.2 (A) |
| Focus | Focus moves in a logical order, is always visible, and isn't hidden behind sticky headers. | 2.4.3 (A), 2.4.7 (AA), 2.4.11 (AA), 2.4.13 (AAA) |
| Screen readers | Names, roles and states are announced correctly, and status changes are announced without moving focus. | 1.3.1 (A), 4.1.2 (A), 4.1.3 (AA) |
| Color | Meaning never depends on color alone; your own colors meet contrast in both modes. | 1.4.1 (A), 1.4.3 (AA), 1.4.11 (AA), 1.4.6 (AAA) |
| Zoom and reflow | At 200% text size and at 320px wide (400% zoom), nothing is cut off and no sideways scrolling is needed. | 1.4.4 (AA), 1.4.10 (AA), 1.4.12 (AA) |
| Pointer targets | Click and tap targets are at least 24×24px (44×44px for AAA), or spaced so they don't overlap. | 2.5.8 (AA), 2.5.5 (AAA) |
| Motion | Animations respect the reduced-motion setting. | 2.3.3 (AAA) |