Switch

An accessible on/off toggle switch built on react-aria-components' Switch, rendering a pill-shaped track with a sliding thumb and an optional label. It supports hover, focus-visible, selected, disabled, and forced-colors states.

Switch
LIVE · running in a sandboxed iframe
Installed with shadcn add · 1 workaround · theme from registry.json
  • installed base item https://ui-three-mu.vercel.app/r/default.json (registry preview settings)
See how it was built

Installation

pnpm dlx shadcn@latest add @preskok/switch

Requires @preskok in the registries of your components.json (shadcn adds official directory namespaces automatically).

Usage

usage.tsx
import { Switch } from "@/components/ui/switch"export function Example() {  return <Switch>Enable notifications</Switch>}
  • Toggling a boolean setting that takes effect immediately, such as notifications, dark mode, or a feature flag.
  • Forms where a checkbox-style boolean needs a more prominent on/off affordance.
  • Settings panels that need an accessible switch with label, description slot, and disabled state.
  • Any place where you need a react-aria-components Switch with the project's Tailwind styling and design tokens.

Examples

Basic labeled switch

A switch with a string child, which is automatically wrapped in SwitchLabel.

basic-labeled-switch.tsx
import { Switch } from "@/components/ui/switch"export function BasicSwitch() {  return <Switch defaultSelected>Airplane mode</Switch>}

Controlled switch

Binds the selected state to React state via isSelected and onChange.

controlled-switch.tsx
"use client"import { useState } from "react"import { Switch } from "@/components/ui/switch"export function ControlledSwitch() {  const [enabled, setEnabled] = useState(false)  return (    <Switch isSelected={enabled} onChange={setEnabled}>      Dark mode    </Switch>  )}

Render-prop children

Uses the function form of children to read the render state (isSelected, isHovered, isFocusVisible, isDisabled) and display a custom label.

render-prop-children.tsx
import { Switch } from "@/components/ui/switch"export function StatusSwitch() {  return (    <Switch>      {({ isSelected }) => (isSelected ? "Published" : "Draft")}    </Switch>  )}

Disabled switch

A non-interactive switch shown at 50% opacity.

disabled-switch.tsx
import { Switch } from "@/components/ui/switch"export function DisabledSwitch() {  return (    <Switch isDisabled isSelected>      Managed by admin    </Switch>  )}

API reference

PropTypeDefaultDescription
childrenReactNode | string | ((values: SwitchRenderProps) => ReactNode)—Label content. A string is wrapped in SwitchLabel; a function receives react-aria render props (isSelected, isHovered, isFocusVisible, isDisabled, etc.) and its return value is rendered as-is.
classNamestring—Additional classes merged onto the root switch element via cx(). Merged after the base styles.
...propsSwitchProps (from react-aria-components/Switch)—All other react-aria-components Switch props are forwarded, such as isSelected, defaultSelected, onChange, isDisabled, name, value, and aria-label. The style prop is merged with a defaultStyle that sets WebkitTapHighlightColor to transparent. data-slot is fixed to "control" and cannot be overridden by the spread because it is set after it.
SwitchLabel propsReact.ComponentProps<typeof Label> (elementType is fixed to "span")—Exported helper that renders the label text using Label from ./field with elementType="span". Accepts the same props as the field Label component.

Accessibility

  • Built on react-aria-components Switch, which renders a native-like role="switch" with aria-checked and keyboard support (Space and Enter toggle).
  • Always provide an accessible name: pass text children (wrapped in SwitchLabel) or an aria-label.
  • Focus is shown with a visible 2px ring (ring-ring/20) only on keyboard focus (isFocusVisible).
  • The thumb element is aria-hidden, so screen readers rely on the switch role and its label.
  • Disabled state is conveyed through react-aria's disabled attribute and a 50% opacity reduction; ensure the label explains why it is disabled when relevant.
  • Forced-colors mode (Windows High Contrast) receives a visible outline and uses the Highlight system color for the track.
  • Hover and focus states use distinct inset rings so they are not conveyed by color alone.

Docs written by anthropic:claude-haiku-5-5 from the registry source.

Use with Coding Agent

Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.

prompt.md
Use the "Switch" component (preskok/switch) from its shadcn registry.1. Install it with: npx shadcn@latest add @preskok/switch2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.4. Available props: children, className, ...props, SwitchLabel props.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { Switch } from "@/components/ui/switch"export function Example() {  return <Switch>Enable notifications</Switch>}```

Files & dependencies

  • registry/preskok/ui/preskok-ui/switch.tsx
dependenciesreact-aria-componentscn@^0.2.4
registryDependencieshttps://ui-three-mu.vercel.app/r/field.jsonhttps://ui-three-mu.vercel.app/r/primitive.json

Looks similar, elsewhere