UI

Switch

A labelled on/off switch for an immediate setting with press-stretch thumb physics, morphing thumb icons, and semantic active tones.

import { Switch } from '@uthereal/ui/switch'

Switch playground

Loading interactive preview…

Overview

Switch renders a native checkbox exposed with role="switch" inside the inline field anatomy: a borderless track and thumb beside its label, with an optional description and error below the label. Pressing with a pointer or Space stretches the thumb toward the opposite end while anchoring its outer edge; releasing glides the thumb across on a critically damped spring.

Your application owns the setting and applies changes reported by onCheckedChange. Switch supports optional single-side or paired thumb icons (with geometric morphs for curated pairs), five semantic active tones, three sizes, and start or end label alignment.

CapabilityBehavior
Thumb physicsPress-stretch widens the thumb 5px toward the far side; release glides without overshoot; click/Enter squashes 1.16×
Thumb iconsSet checkedIcon, uncheckedIcon, or both; iconMorph="auto" morphs curated pairs geometrically and rotates the rest
Tones & sizesFive active tones (neutral, accent, success, warning, danger) across sm (36×20), md (42×24), lg (50×28)
Field anatomylabelPosition="end" (default) places the label after the track; "start" aligns the switch to the far end of the row
Forms & stateControlled (checked) or uncontrolled (defaultChecked); name submits "on" with native HTML forms while checked

Use Switch for a setting that takes effect immediately, such as notifications or dark mode. Use Checkbox for choices confirmed on form submission or for selecting items in a list.

Usage

'use client'
import { useState } from 'react'
import { Switch } from '@uthereal/ui/switch'

export function NotificationSetting() {
  const [enabled, setEnabled] = useState(true)

  return (
    <Switch
      label="Email notifications"
      name="email_notifications"
      description="Receive a daily summary of workspace activity."
      labelPosition="start"
      uncheckedIcon="bell-off"
      checkedIcon="bell"
      checked={enabled}
      onCheckedChange={setEnabled}
    />
  )
}

Omit checked for an uncontrolled switch initialized from defaultChecked; onCheckedChange still fires on every toggle. When name is set, the hidden checkbox submits "on" with a surrounding form while checked unless the switch is disabled.

Examples

Thumb icons and semantic tones

Set checkedIcon, uncheckedIcon, or both to place a contrast-safe glyph inside the thumb (10px, 12px, or 14px across sm, md, and lg). In iconMorph="auto" (default), curated pairs—sun/moon, plus/check, play/pause, copy/check, bell-off/bell, heart/heart, and menu/x in either direction—run true geometric morphs, while single-side icons and all other pairs (such as x/check or eye-off/eye) use the rotate cross-morph. Set tone to accent, success, warning, or danger when the active fill carries semantic meaning.

Layout, sizes, and validation states

Use labelPosition="start" with description for settings rows where the switch sits at the inline end. Choose sm, md, or lg for track scale, disabled to dim the row and remove it from the tab order, required to mark mandatory toggles with aria-required, and error to draw a danger outline on the track and announce the message as an alert.

Send a Monday morning summary of workspace activity.
Backups are required on this plan.

API reference

These serializable arguments and defaults come directly from the component schema.

ArgumentType / defaultDescription
labelstringDefault: "Email notifications"Visible label and accessible name. Name the setting in sentence case, e.g. "Email notifications"; the switch shows whether it is on.
hideLabelbooleanDefault: falseHide the label visually when the context already names the field. It is still announced.
descriptionstring—Supporting text below the label, announced with the switch. Use it to say what the setting does.
errorstring—Validation message from your application. Setting it marks the field invalid; the message is linked to the field and announced politely.
requiredbooleanDefault: falseMarks the switch as required for assistive technology and shows an indicator, e.g. a setting that must be on to continue. Your application validates it.
disabledbooleanDefault: falseUnavailable and removed from the tab order, e.g. while a setting is locked by an administrator.
namestring—Native form field name, submitted with the value in a form.
defaultCheckedbooleanDefault: falseInitial on state. Later argument edits do not reset an existing instance; use checked and onCheckedChange to control it.
labelPositionstart | endDefault: "end"end puts the label after the switch. start puts it first and moves the switch to the end of the row, as in a settings list.
checkedIconIconName—Icon inside the thumb when on, e.g. "check", "moon", "bell", or "heart". Works alone as a single-side icon or paired with uncheckedIcon; curated pairs (sun/moon, plus/check, play/pause, copy/check, bell-off/bell, heart/heart, menu/x) morph geometrically in auto mode.
uncheckedIconIconName—Icon inside the thumb when off, e.g. "x", "sun", "bell-off", or "heart". Works alone as a single-side icon or paired with checkedIcon; icons enter, exit, or morph in sync with thumb travel.
iconMorphauto | swap | rotate | noneDefault: "auto"How the thumb icon transitions: auto uses a geometric morph for curated pairs and rotates the rest, swap crossfades, rotate always rotates, and none swaps immediately.
toneneutral | accent | success | warning | dangerDefault: "neutral"Color of the chosen fill: neutral is Arc’s ink control fill, accent follows the theme accent, and success, warning, and danger carry their status colors.
sizesm | md | lgDefault: "md"Track of 36×20, 42×24 (Arc), or 50×28 pixels in a row one control tall; the thumb icon is 10, 12, or 14 pixels.
recipestudio | softDefault: "studio"Visual treatment. Both recipes use the same interaction behavior.
themeSwitchTheme—Color mode, accent, and tokens, including trackWidth, trackHeight, and inset. See Theming.
motionnone | subtle | expressiveDefault: "subtle"Thumb and track animation preset. System reduced-motion preferences take precedence.
motionOptionsSwitchMotionOptions—Overrides for thumb travel (transition) and thumb icon swap (iconSwap): a tween with a duration (ms) or a spring. Defaults to Arc's critically damped glide and snappy icon spring.
dirltr | rtl—Reading direction; the thumb travels toward the inline end when on. Omit it to inherit the page direction.

React integration

PropPurpose
checked, onCheckedChangeControlled on state. onCheckedChange fires on every toggle in both controlled and uncontrolled mode.
renderThumbIcon(checked)Replaces the built-in thumb icon slot with custom decorative markup for the current boolean state.
refRef to the native <input type="checkbox" role="switch">, e.g. for programmatic focus.
idInput id; the label, description, and error ids derive from it.
presetApplication defaults (size, labelPosition, tone, theme), overridden by instance props.
tokensCSS custom properties (--ui-*) applied after the typed theme on the field root.
className, styleApplied to the root field element, which carries the resolved --ui-* tokens.
Native attributesForwarded to the hidden <input> (onFocus, onBlur, onChange, autoFocus, aria-*).

Keyboard & accessibility

KeyInteraction
TabMoves focus to or from the switch.
SpaceToggles the switch; holding stretches the thumb as a press until release.
EnterToggles the switch immediately without submitting a surrounding form.

The switch is a visually hidden native <input type="checkbox" role="switch"> wired through Zag's switch machine, so assistive technology announces it as a switch with its on/off state. The visible <label> also names the input through aria-labelledby (preserving the accessible name when hideLabel is true), while description and error link via aria-describedby. Setting error adds aria-invalid and announces changes in an alert region; required sets aria-required without triggering browser validation bubbles. Clicking the track or label toggles the control across the full row hit height (44px at md).

Keyboard focus draws a single focus ring around the whole row; pointer focus draws none. In dir="rtl", the track mirrors so the thumb travels and stretches from inline-start to inline-end, while the icon slot counter-mirrors and restores direction: rtl so non-directional icons stay upright and directional icons (arrow-right, send, etc.) mirror once. Under forced-colors: active, the track uses ButtonText / Highlight, the thumb uses ButtonText (ButtonFace icon) when off, HighlightText (Highlight icon) when on, and GrayText (Canvas icon) when disabled.

Motion and theming

  • Thumb travel and press-stretch: holding a pointer or Space widens the thumb 5px toward the opposite side on spring.snappy while anchoring its outer edge; releasing glides it on Arc's critically damped spring (motionOptions.transition). Toggling without a press plays a brief 1.16× horizontal squash (340ms).
  • Thumb icons and iconMorph: auto dispatches curated pairs to IconMorph primitives and rotates other pairs or single-side icons (±30°, 0.7 scale, 1px blur) using motionOptions.iconSwap. rotate always rotates, swap crossfades opacity, and none swaps immediately.
  • Track and thumb colors: hovering an unchecked track darkens it over 160ms; checked track opacity, thumb fill, and thumb icon color crossfade over 240ms.
  • Reduced motion and motion="none": prefers-reduced-motion: reduce removes travel, press-stretch, squash, geometric morphs, rotation, scale, blur, and CSS track transitions while keeping a 120ms opacity crossfade on thumb icon changes. motion="none" makes every state and icon update immediate (0ms).

theme accepts the core, control, and selectionControl token groups, borderStrong, success, warning, and toneForeground, plus own track geometry tokens (trackWidth, trackHeight, inset). Thumb icon contrast uses --ui-switch-icon-off (textSecondary in light mode, surface in dark mode) and --ui-switch-icon-on (the active track color) so icons stay readable in both light and dark modes across all tones. An optional UtherealProvider supplies application-wide defaults.

Composition

Switch is a leaf component. Its on state (boolean) is host state at /fields/instance-<id>/checked, initialized from defaultChecked. Stored state survives configuration edits to labels, icons, tones, or appearance. See Building with agents.

{
  "type": "Switch",
  "props": {
    "label": "Dark mode",
    "description": "Use dark colors across the app.",
    "labelPosition": "start",
    "uncheckedIcon": "sun",
    "checkedIcon": "moon"
  },
  "children": []
}

On this page