Components

Switch

Headless on/off switch with optional native form participation.

When to Use

Use Switch for a setting that becomes active or inactive immediately, such as notifications. Use Checkbox when the choice is part of a form that is applied later, and use Toggle for a pressed command like bold text.

Features

  • Renders a WAI-ARIA switch.
  • Can be controlled or uncontrolled.
  • Supports disabled, read-only, required, and invalid states.
  • Renders an optional hidden checkbox input for native form submission.
  • Includes a decorative thumb part that mirrors root state.
  • Supports asChild and render.

Import

import { Switch } from "@flowstack-ui/atom";

Anatomy

<Switch.Root>
  <Switch.Thumb />
</Switch.Root>

API Reference

Root

Owns the checked and form state and renders a native button by default. It also provides state to Thumb and renders a hidden checkbox when name is present.

PropTypeDefault
checkedboolean-
defaultCheckedbooleanfalse
onCheckedChange(checked: boolean) => void-
disabledbooleanfalse
readOnlybooleanfalse
invalidbooleanfalse
requiredbooleanfalse
namestring-
valuestring"on"
formstring-
ariaLabelstring-
asChildbooleanfalse
renderRenderProp-
ARIA attributeValues
role"switch"
aria-checkedCurrent checked state
aria-labelValue from ariaLabel when provided
aria-requiredtrue when required
aria-readonlytrue when read-only
aria-invalidtrue when invalid
Data attributeValues
[data-slot]"switch"
[data-state]"checked" | "unchecked"
[data-disabled]Present when disabled
[data-readonly]Present when read-only
[data-required]Present when required
[data-invalid]Present when invalid

Thumb

Mirrors Root state for the movable visual part while remaining hidden from assistive technology.

PropTypeDefault
asChildbooleanfalse
renderRenderProp-
ARIA attributeValues
aria-hiddenAlways true
Data attributeValues
[data-slot]"switch-thumb"
[data-state]"checked" | "unchecked"
[data-disabled]Present when disabled
[data-readonly]Present when read-only
[data-required]Present when required
[data-invalid]Present when invalid

Advanced compound parts can read useSwitchContext or use the public SwitchContextProvider.

Examples

Form Submission

import { Switch } from "@flowstack-ui/atom";

export default function NotificationSetting() {
  return <Switch.Root name="notifications" value="enabled" ariaLabel="Notifications"><Switch.Thumb /></Switch.Root>;
}

Controlled

import { useState } from "react";
import { Switch } from "@flowstack-ui/atom";

export default function ControlledSwitch() {
  const [enabled, setEnabled] = useState(false);
  return <Switch.Root checked={enabled} onCheckedChange={setEnabled} ariaLabel="Notifications"><Switch.Thumb /></Switch.Root>;
}

Accessibility

Switch follows the WAI-ARIA switch pattern. Root owns the switch role and checked state; Thumb is decorative.

KeyDescription
EnterToggles checked state.
SpaceToggles checked state.

Provide visible text, ariaLabel, or aria-labelledby. Read-only switches remain focusable but cannot toggle. Disabled switches use native button disabled behavior. Non-native asChild and render switches receive Atom keyboard activation.

Changelog

See CHANGELOG.md.