Skip to content

Components / Switch

Switch

Turn a setting on or off with a stable label that names the setting in either state.

Usage playground

Configure. Preview. Copy.

Configure this controlled switch, then copy its current state. Defaults and API.

Receive updates about your account.

Off

Label, description and label position are composition controls, not Switch props. Blank or whitespace-only labels use “Push notifications” as the accessible name. Blank descriptions are omitted. Click the visible label or use Tab, Space and Enter to try the switch.

React / TSX
"use client";

import { useId, useState } from "react";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

Installation

pnpm dlx [email protected] add biehlio/yet-another-design-system/switch#e7b31d0e0c9d8b24d53e97ca38b3b68d345e9f40

Documentation samples use basic Tailwind utilities for layout. Install each YADS component imported by the sample.

Run this revision-pinned GitHub command in a project configured for shadcn. It installs yads-switch, its scoped stylesheet, Radix Switch and clsx. Import { Switch } from @/components/ui/yads-switch (adjust the alias to your project). No separate theme install is needed.

Each example’s Copy sample includes a complete React module with a client boundary, instance-unique IDs and inline layout styles. No Tailwind utilities or site CSS are required. Label placement is layout composition, not a new component prop. Use checked with onCheckedChange for ongoing state ownership; defaultChecked only initializes an uncontrolled switch.

Storybook runs locally or as a separately hosted build; no hosted Storybook is configured. Source and setup links match the install revision. Follow Storybook setup above to run the reference locally.

State comparison

Compare initially off and on settings, enabled and disabled. Enabled switches can be changed with their labels, Space or Enter. Disabled switches keep their state and are skipped by Tab.

React / TSX
"use client";

import { useId } from "react";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

Label and description

A visible label names the setting and activates its switch. Associate supporting text with aria-describedby so it supplements, rather than replaces, the stable name.

Receive alerts about new sign-ins.

React / TSX
"use client";

import { useId } from "react";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

Visually unlabeled

When surrounding context makes a compact control understandable visually, still provide a meaningful accessible name. This switch uses aria-label="Mute notifications"; prefer a visible label when space permits.

React / TSX
"use client";

import { useId } from "react";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

Label placement

Place labels before or after the switch with flex layout. Each label targets only its own control. Label placement is composition, not a Switch prop; names stay the same in either state.

React / TSX
"use client";

import { useId } from "react";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

Controlled state

The parent owns checked and accepts onCheckedChange. Both the switch and the application button update the same state; the visible result stays synchronized. The setting name never changes.

Live updates: Off
React / TSX
"use client";

import { useId, useState } from "react";
import { Button } from "@/components/ui/yads-button";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

Uncontrolled initialization

defaultChecked initializes this switch to on; the switch then owns its state. Changing defaultChecked is not ongoing control of an already-mounted instance. Use checked and onCheckedChange when application state must drive the setting.

React / TSX
"use client";

import { useId } from "react";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

Settings groups

Native fieldset and legend name related settings. Stack controls vertically or use a wrapping horizontal row. Each switch has an independent name and state; this is layout composition, not a SwitchGroup API or a single shared value.

Delivery channels
Workspace preferences
React / TSX
"use client";

import { useId } from "react";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

Customization

Use className, the existing scoped color variables and data-state selectors to adjust this instance. The repeated class wins over the default light/dark selectors. This recipe changes track/thumb colors and the checked border only: it preserves dimensions, thumb travel, focus and reduced motion. These are existing CSS hooks, not a new size, thumb or theming API.

React / TSX
"use client";

import { useId } from "react";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

Native form submission

Submit to read real FormData locally, with no backend. Only enabled, checked switches with a name contribute an entry. The default value is on; a custom value replaces it. Unchecked, disabled and unnamed switches contribute nothing: there is no automatic false value or hidden fallback. Each setting has its own name.

Uses the default value when checked.

Uses the custom value weekly when checked.

Unchecked settings are omitted, not sent as false.

Disabled settings are omitted even when checked.

Without a name, this setting is omitted.

Submit to inspect the successful controls.

React / TSX
"use client";

import { useId, useState } from "react";
import { Button } from "@/components/ui/yads-button";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

Required validation

required uses Radix’s associated checkbox for native browser validation, not just aria-required. Try submitting unchecked, then enable the setting and submit again. Disabling the setting excludes it from both validation and FormData.

The form’s onInvalid recipe replaces the invisible checkbox’s default tooltip/focus with a local error and focus on the visible, described switch. Preventing the invalid event’s default does not bypass validation or create a submit event. aria-invalid communicates the error; it does not validate. No new Switch validation prop is needed.

These recipes are browser-tested with @radix-ui/react-switch 1.4.1. The reset and external form recipes below describe their tested support and limitations.

Required while enabled. Disabled settings are excluded from validation and submission.

Submit to try native required validation.

React / TSX
"use client";

import { useId, useRef, useState } from "react";
import { Button } from "@/components/ui/yads-button";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

Uncontrolled reset

A native reset button restores the mounted switch’s initial defaultChecked state. Try changing both settings, resetting repeatedly, then submitting to inspect actual FormData. Unchecked still means omitted, not false. Changing defaultChecked later is not ongoing control.

These reset recipes declare a minimum of @radix-ui/react-switch 1.3.3; registry installation uses ^1.3.3. These recipes pass browser checks on 1.3.3 and 1.4.1, not every intervening or future version. They demonstrate normal, uncanceled resets only. In tested 1.4.1, canceling a reset can still change Switch and parent state: do not rely on preventDefault() to preserve them.

Reset restores this setting to on.

Reset restores this setting to off; unchecked settings are omitted.

Submit to inspect FormData.

React / TSX
"use client";

import { useId, useState } from "react";
import { Button } from "@/components/ui/yads-button";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

Controlled reset

Radix requests the mounted initial checked state on native reset. The parent must accept those requests through onCheckedChange, just as it accepts a toggle. Here checked and the visible parent output share that state. A fixed checked prop that ignores changes cannot synchronize reset. The form’s onReset only clears the old submission message; it does not set checked state.

Reset restores this setting to on.

Reset restores this setting to off; unchecked settings are omitted.

Parent state: on=true, off=false

Submit to inspect FormData.

React / TSX
"use client";

import { useId, useState } from "react";
import { Button } from "@/components/ui/yads-button";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

External form association

The switch is outside its form. Its form prop uses a generated ID to associate the native checkbox with exactly that mounted form for submission, required validation and reset. Copying this example twice creates independent owners.

Invalid events follow DOM ancestry, not form ownership. The shared container’s onInvalid handles the external checkbox’s error and focuses the visible switch; putting this handler only on the form misses it. This recipe assumes a stable form owner mounted with the switch. Missing owners, changing associations and canceled resets are not supported recipes here.

Outside the form, but required by its generated form ID. Reset restores off.

Submit to try the external required setting.

React / TSX
"use client";

import { useId, useRef, useState } from "react";
import { Button } from "@/components/ui/yads-button";
import { Switch } from "@/components/ui/yads-switch";
import { Label } from "@/components/ui/yads-label";

Accessibility

  • Name the setting, not its state. Prefer a visible label linked by htmlFor and id. Keep the name stable when toggled. For visually unlabeled usage, supply aria-label or aria-labelledby. Switch has no automatic accessible name.
  • Associate supporting text. Use aria-describedby for descriptions and errors with unique IDs. Label placement and descriptions are composition, not Switch props. Related settings can use a fieldset and legend; each switch still needs its own name.
  • Keep keyboard behavior and focus. The root is a button with role="switch" and aria-checked. Tab reaches enabled switches; Space and Enter toggle them. Disabled switches cannot activate and are skipped by Tab. Keep the existing focus ring, and explain unavailable settings in nearby text.
  • Use native validation with visible guidance. Radix supplies an associated checkbox for form participation. required blocks invalid submission, but browser default focus can land on that hidden checkbox. The required recipe handles onInvalid, shows an error and focuses the visible root via its ref without bypassing validation. aria-invalid communicates the error; it does not validate.
  • Respect external ownership. Invalid events follow DOM ancestry, not the associated form. The external form recipe handles them on a common container. Keep form owners mounted and IDs stable. Local results use status regions; no backend or persistence is included.

API reference

Import { Switch } from @/components/ui/yads-switch. The wrapper accepts React.ComponentPropsWithoutRef<typeof SwitchPrimitives.Root> and forwards its ref to HTMLButtonElement. There is no separately exported SwitchProps type. The root renders Radix’s button with its own Thumb.

The wrapper owns children; arbitrary children and asChild composition are not supported recipes, even though inherited Radix types expose them. There are no sizes, variants, thumb slots/icons, render props or group APIs. Label position, descriptions and groups are ordinary React/HTML composition.

Defaults below are component defaults, not a new configuration API. On narrow screens, focus the table and scroll horizontally to read every column.

Switch props, types, defaults and behavior
PropTypeDefaultDescription
checkedbooleanNot set (uncontrolled)Current state when controlled. Pair with onCheckedChange and let the parent accept changes.
defaultCheckedbooleanfalseInitial uncontrolled state, not ongoing control of a mounted switch.
onCheckedChange(checked: boolean) => voidNot setReceives the next state on activation and the mounted initial state on normal native reset. Controlled parents must accept the request.
disabledbooleanfalsePrevents activation and sequential focus. Excluded from native validation and FormData.
requiredbooleanfalseAssociated native checkbox must be checked for valid submission while enabled. aria-required alone does not validate.
namestringNot setFormData key. Unnamed switches contribute no entry.
valuestring"on"Submitted value when named, checked and enabled. Unchecked switches are omitted, not submitted as false.
formstringNot set (ancestor form)ID of the owning form, including an external form. Use a generated, stable ID and a mounted owner.
idstringNot setConnect an external label with htmlFor. Generate instance-unique IDs, for example with useId.
aria-label / aria-labelledbystringNot setAccessible name when a visible associated label is unavailable. The component supplies no fallback name.
aria-describedbystringNot setSpace-separated IDs of supporting description and error text; does not replace the name.
classNamestringNot setAdds classes to the root. Existing data-state and data-disabled selectors support ordinary CSS recipes.
refReact.Ref<HTMLButtonElement>Not setForwarded to the Radix Root button, for example to focus the visible control after invalid submission.

Root attributes and styling

Native button attributes such as title, style, aria-*, data-* and event handlers are forwarded. Radix supplies type="button": toggling is not a submit action. Use separate submit/reset buttons. Preserve switch semantics rather than overriding role, aria-checked or type. The root exposes data-state="checked" or "unchecked" and data-disabled when disabled. See Customization for className, state selectors and existing color variables without changing geometry or thumb travel.

Forms, reset and tested limits

Only enabled, checked, named controls contribute to FormData. The default value is on; custom values replace it. There is no automatic false entry or hidden fallback. See Native form submission and Required validation.

Normal native reset restores the mounted initial state. In controlled usage, the parent must accept onCheckedChange requests; a fixed checked prop that ignores them cannot synchronize the UI, parent and submitted data. Uncontrolled reset restores initial defaultChecked. Changing defaultChecked later is not ongoing control.

Registry dependency: @radix-ui/react-switch@^1.3.3. The reset/external form recipes are browser-tested on the minimum 1.3.3 and installed 1.4.1, not every intervening or future release. Only normal, uncanceled resets with stable, mounted form owners are demonstrated. An upstream 1.4.1 limitation means canceling reset with preventDefault() can still change Switch and parent state. Do not rely on cancellation to preserve them. Missing/dynamically replaced owners and changing form associations are not supported recipes here.

Playground defaults

Reset restores unchecked, enabled, “Push notifications”, “Receive updates about your account.” and the label after the switch. The playground is controlled; copied sample initializes its own state to the current checked value. Label, description and label position are recipe controls, not public props. Blank or whitespace-only labels use the playground-only accessible fallback “Push notifications”; blank descriptions are omitted. The component itself supplies neither label nor description.