Toggle switch
Also called switch or toggle
An on/off control shaped like a small track with a sliding knob, used for settings that take effect immediately.
import { useState } from 'react';
import { Switch } from '@base-ui/react/switch';
const SETTINGS = [
{ id: 'mentions', label: 'Mentions', hint: 'When someone @mentions you' },
{ id: 'comments', label: 'Comments', hint: 'Replies on files you own' },
{ id: 'summary', label: 'Weekly summary', hint: 'Every Monday at 9 AM' }
];
type Props = { onChange: (id: string, on: boolean) => Promise<void> };
export function NotificationSettings({ onChange }: Props) {
const [enabled, setEnabled] = useState<Record<string, boolean>>({
mentions: true
});
function toggle(id: string, on: boolean) {
// Optimistic: flip right away, and flip back if saving fails.
setEnabled((current) => ({ ...current, [id]: on }));
onChange(id, on).catch(() =>
setEnabled((current) => ({ ...current, [id]: !on }))
);
}
return (
<ul className='w-96 divide-y rounded-xl border'>
{SETTINGS.map(({ id, label, hint }) => (
<li key={id}>
{/* The wrapping label makes the whole row toggle the switch. */}
<label className='flex cursor-pointer items-center justify-between gap-4 px-4 py-3'>
<span>
<span className='block text-sm font-medium'>{label}</span>
<span className='text-sm text-muted-foreground'>{hint}</span>
</span>
<Switch.Root
checked={enabled[id] ?? false}
onCheckedChange={(on) => toggle(id, on)}
className='flex h-6 w-11 shrink-0 rounded-full bg-muted-foreground/30 p-0.5 transition-colors duration-200 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring data-checked:bg-blue-600'
>
{/* A curve that overshoots slightly gives the knob a springy settle. */}
<Switch.Thumb className='size-5 rounded-full bg-white shadow-sm transition-transform duration-200 ease-[cubic-bezier(0.34,1.56,0.64,1)] data-checked:translate-x-5 motion-reduce:transition-none' />
</Switch.Root>
</label>
</li>
))}
</ul>
);
}Build the notification settings as a list of toggle switches: a 44×24px track with a 20px knob that slides with a spring (stiffness 500, damping 30) while the track fades from gray to blue over 200ms. Save each change right away with an optimistic update, make the whole row clickable, use role='switch' with aria-checked, and skip the slide when prefers-reduced-motion is set.
Paste into Layerwise or any AI coding tool.
What is Toggle switch?
A toggle switch mimics a physical switch: a pill-shaped track with a round knob that slides from one end to the other. The track changes color with it, usually gray for off and an accent color for on, so the state reads at a glance.
Use a switch for a setting that applies right away, like push notifications or dark mode. If the choice only takes effect after someone presses Save, a checkbox is clearer. Label the switch with the thing it controls, such as Weekly summary, and let the knob show whether it is on.
A small spring makes the knob feel physical: a quick slide of 150 to 250ms with only a hint of overshoot. Use role='switch' with aria-checked, or a native checkbox styled as a switch, make the whole row clickable, and rely on the knob's position as well as color to show the state.
Often confused with
- Checkbox
- A checkbox suits choices that apply when a form is submitted, or selecting several items. A switch applies a setting the moment you flip it.
Related terms
All termsCheckbox
A small square you tick to turn an option on or select an item, with a dash in a parent box when only some items are checked.
Spring animation
An animation driven by simulated spring physics in place of a fixed duration and curve, so motion can overshoot, settle, and react naturally.
Optimistic UI
A pattern where the interface shows the result of an action right away, before the server confirms it, and rolls it back if the request fails.
Micro-interaction
A small moment of feedback around a single action, like a heart that bursts when tapped or a copy icon that turns into a checkmark.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.