Skip to content
Vertical Jump Designs-uiFront-End Studio
Get Pro

v3.9 is out.

Forms

Switch

Free11.8 KB gzipped

An 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

tsx
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".

tsx
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.



tsx
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.

tsx
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.

tsx
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).

Salvando
tsx
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

tsx
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

Switch props
PropTypeDefault
checked

Whether the switch is on (controlled).

boolean—
defaultChecked

Initial on state when uncontrolled.

boolean—
onCheckedChange

Called with the next on state whenever the user toggles the switch.

(checked: boolean) => void—
label

Visible label. The whole label is clickable. When omitted, give the switch a name with aria-label, aria-labelledby, or a surrounding Field.

ReactNode—
description

Supporting text under the label, linked with aria-describedby.

ReactNode—
labelPosition

Where the label sits relative to the switch. between fills the row and pushes them apart.

"start""end""between"—
size

Track, thumb, and label size. Defaults to the surrounding Field's size, then md.

"sm""md""lg"—
tone

Track color while on.

"neutral""primary""accent""success""warning""danger""info"—
invalid

Marks the switch invalid (aria-invalid and a danger border). Inherited from Field.

boolean—
disabled

Disables the switch. Inherited from Field.

boolean—
required

Marks the switch as required for form submission. Inherited from Field.

boolean—
readOnly

Keeps the switch focusable and announced, but blocks toggling (aria-readonly). Inherited from Field.

boolean—
thumbIcon

Decorative icons shown inside the thumb for each state, e.g. { on: <CheckIcon />, off: <CloseIcon /> }.

Partial<SwitchStateContent>—
showStateLabels

Shows visible state text next to the track, so the state doesn't rely on color or position alone.

boolean—
stateLabels

Text for showStateLabels. Defaults to "On" and "Off".

SwitchStateContent—
loading

Shows a spinner in the thumb, sets aria-busy, and blocks toggling while a change is saved.

boolean—
loadingLabel

Screen-reader text added to the description while loading. Defaults to "Saving".

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)

Running accessibility audit

Checking the examples above…

Lighthouse audit

Light mode

Score 100

36 passed · 0 failed · 29 not applicable · 10 to check by hand

Dark mode

Score 100

36 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

Keyboard
KeyAction
SpaceTurns the switch on or off.

Roles and states

Roles and states
ElementRoleStates and properties
<input type="checkbox">switchOn / 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.

Manual accessibility checks
CheckWhat to testWCAG 2.2
KeyboardEvery 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)
FocusFocus 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 readersNames, 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)
ColorMeaning 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 reflowAt 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 targetsClick 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)
MotionAnimations respect the reduced-motion setting.2.3.3 (AAA)