Theme switcher
A light and dark toggle with a sun and moon swap, ready for a view transition.
import { ThemeSwitcher } from '@uthereal/ui/theme-switcher'Theme switcher playground
Overview
ThemeSwitcher renders a toggle button that swaps a sun and moon icon and reports the next appearance. It does not apply the theme to the page. On press, onValueChange receives the next appearance, the transition name, and the pressed button element so the host can start a view transition from the button position.
Use ThemeSwitcher for a standalone light and dark toggle in a top bar or page corner. When theme selection includes a system option inside an account menu, use a user menu instead.
Usage
'use client'
import {
ThemeSwitcher,
type ThemeSwitcherValue,
} from '@uthereal/ui/theme-switcher'
export function AppearanceToggle({
theme,
setTheme,
}: {
theme: ThemeSwitcherValue
setTheme: (next: ThemeSwitcherValue) => void
}) {
return (
<ThemeSwitcher
value={theme}
onValueChange={(next, _transition, trigger) => {
if (!document.startViewTransition) {
setTheme(next)
return
}
const { left, top, width, height } = trigger.getBoundingClientRect()
document.documentElement.style.setProperty(
'--x',
`${left + width / 2}px`,
)
document.documentElement.style.setProperty(
'--y',
`${top + height / 2}px`,
)
document.startViewTransition(() => setTheme(next))
}}
/>
)
}Pass value to control the switch from your theme state, or omit it to start from defaultValue. onValueChange runs in both modes, and onAction reports any configured action name after it.
Examples
Transitions
transition names the page transition for your handler and pairs it with a matching icon nudge when active in dark mode.
| Transition | Icon nudge (dark) | Intended page transition |
|---|---|---|
reveal | scale(1.08) | Circular reveal from the button |
eclipse | 2px toward inline start | Sweeping eclipse across the viewport |
split | 1px toward inline end | Horizontal split from center seam |
rise | 2px upward | Quiet vertical lift |
Icon only
iconOnly hides the visible "Switch theme" text and renders a square button sized to the control height. The accessible name stays intact for compact headers and mobile toolbars.
API reference
These serializable arguments and defaults come directly from the component schema.
| Argument | Type / default | Description |
|---|---|---|
defaultValue | light | darkDefault: "light" | Initial appearance. Later argument edits do not reset an existing instance; use value and onValueChange to control it. |
transition | reveal | eclipse | split | riseDefault: "reveal" | Names the page transition the host should run (reveal, eclipse, split, rise) and sets the matching icon nudge. |
iconOnly | booleanDefault: false | Hide the "Switch theme" label and render a square icon button; the accessible name is kept. |
label | string— | Accessible name. Defaults to "Switch to dark mode" or "Switch to light mode", following the current value. |
variant | solid | soft | outline | ghostDefault: "outline" | Fill treatment, as Button. Outline is Arc's secondary switch; ghost fits a toolbar. |
size | xs | sm | md | lgDefault: "md" | Height: 32, 36, 44, or 50 pixels, matching the Button of the same size. |
action | string— | Name of an action registered by your application. Sent to onAction when the switch is activated. |
disabled | booleanDefault: false | Unavailable and removed from the tab order. |
recipe | studio | softDefault: "studio" | Visual treatment. Both recipes use the same interaction behavior. |
theme | ThemeSwitcherTheme— | Color mode, accent, and semantic tokens. See Theming for all keys and defaults. |
motion | none | subtle | expressiveDefault: "subtle" | Press and icon swap animation preset. System reduced-motion preferences take precedence. |
motionOptions | ThemeSwitcherMotionOptions— | Press scale (omitted, it follows the width), and label swap duration (ms) and travel (px). |
dir | ltr | rtl— | Reading direction. Omit it to inherit the page direction. |
React integration
| Prop | Purpose |
|---|---|
value | Controlled appearance ('light' or 'dark'). Omit to run uncontrolled from defaultValue. |
onValueChange(next, transition, trigger) | Called on press with the next appearance, transition name, and <button> element. |
onAction(action) | Called with the configured action name on activation, after onValueChange. |
onClick(event) | Native click handler; runs before onValueChange, never while disabled. |
ref | The rendered <button> element. |
preset | Application defaults, overridden by explicit instance arguments. |
tokens | CSS variables applied after the typed theme. |
className, style | Added to the rendered element. |
| Native attributes | id, aria-*, title, form, name, and other <button> attributes. |
Keyboard & accessibility
| Key | Interaction |
|---|---|
Tab | Focus the switch; a keyboard focus ring appears. |
Enter, Space | Toggle the appearance. |
aria-pressed is true while the value is 'dark'. The accessible name defaults to "Switch to dark mode" or "Switch to light mode" based on the current value, and label overrides it. The sun and moon icons carry aria-hidden="true". Horizontal nudges (eclipse and split) mirror automatically in right-to-left layouts. Under forced-colors: active, the outline border uses ButtonText and the focus outline remains visible.
Motion and theming
The sun and moon swap with a shared ±30° rotation, 0.7 scale, and 2px blur on Arc's snappy spring, while the icon container nudges on Arc's 580ms CSS spring. A two-frame post-mount gate suppresses motion during initial hydration so a stored dark preference settles in place without spinning. Under prefers-reduced-motion: reduce, the icon swaps with a 120ms opacity crossfade and the nudge is removed; skip the host view transition as well. motion="none" makes the swap immediate and removes all nudge and press effects.
theme accepts a color mode, an accent, and semantic tokens from the core and control groups plus borderStrong, toneForeground, tintStrength, and shadowResting. An application-wide UtherealProvider supplies defaults.
Composition
In an agent composition, ThemeSwitcher is a leaf. Its value persists in host state and survives argument edits. When action is set, it must match a host-registered action name; the handler receives { componentId }. See Building with agents.
{
"type": "ThemeSwitcher",
"props": {
"transition": "reveal",
"iconOnly": true,
"action": "toggle_dark_mode"
},
"children": []
}