--- title: Switch description: Switches toggle a single setting on or off with immediate effect. category: Forms --- import { meta as SwitchMeta, Default, On } from '@/content/stories/switch'; import Canvas from '@/components/component-docs/canvas'; import Demo from '@/components/component-docs/demo'; export const config = { dependencies: { components: [Canvas, Demo], scripts: [ '../../components/component-docs/demo.script.tsx', '../../components/component-docs/canvas.script.tsx', '../../components/component-docs/controls.script.tsx', ], }, }; # Switch

Switches toggle a single setting on or off with immediate effect, such as notifications, dark mode, or feature flags.

## Try it ## Usage Bind `checked` for controlled state. Place inside `RuiField` with a `RuiLabel` that describes what the switch controls when composing forms. ```tsx import { RuiSwitch } from '@ecopages/radiant-ui/switch'; Email notifications ``` ## Custom markup `` coordinates any light-DOM tree that matches its query contract. The `RuiSwitch` helper stamps these targets; it is not required. ```tsx import '@ecopages/radiant-ui/switch'; ``` BEM classes are presentation-only. The host syncs `checked`, `disabled`, and `name` on `[data-ref="input"]`. ## Switch vs checkbox Use switches for settings that take effect immediately. Use checkboxes for options collected on form submit. ## Theming Switch chrome maps to **semantic surface roles**, never Tailwind palette steps. Override `--rui-switch-*` on `rui-switch`. | Part / state | Default | Override | | --- | --- | --- | | Track size | `2.5rem` × `1.5rem` | `--rui-switch-width`, `--rui-switch-height` | | Thumb | `1rem` | `--rui-switch-thumb-size` | | Track off / hover | `--on-background` at 20% / 30% | `--rui-switch-track`, `--rui-switch-track-hover` | | Track on / hover | `--primary` | `--rui-switch-track-on`, `--rui-switch-track-on-hover` | | Thumb off / on | `--on-background` / `--on-primary` | `--rui-switch-thumb`, `--rui-switch-thumb-on` | | Focus | `focus-ring` | theme `--focus-ring` | | Disabled | `opacity-disabled` | theme `--opacity-disabled` | ```css rui-switch { --rui-switch-width: 3rem; --rui-switch-track-on: var(--success); } ``` Label text uses `on-background`; track uses the `rounded-pill` radius token. Remap theme roles for global mood. ## Accessibility - Switches expose `role="switch"` with `aria-checked`. - Labels must describe the setting, not the control (for example, "Email notifications", not "Toggle"). - Space toggles the switch when focused. ## API `RuiSwitch` renders a `