Skip to content
Thrum

Switch

A switch for one setting that applies the moment it changes: alerts on, live updates off. For a choice that waits for a Save button, or a list of options to tick, use a checkbox.

Install

npx shadcn@latest add https://thrumui.dev/r/switch.json

Adds components/ui/switch.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.

import { Switch } from "@/components/ui/switch";

Props

PropTypeDefaultDescription
checkedboolean–Whether it is on, when the parent holds the state. Use it with onCheckedChange.
defaultCheckedbooleanDefault falseWhether it starts on, when the switch holds its own state.
onCheckedChange(checked: boolean) => void–Called with the new state when it changes.
size"sm" | "md"Default "md"sm is 20 pixels tall and 36 wide, md is 24 tall and 44 wide. Both are a 24 pixel target.
disabledbooleanDefault falseDimmed, skipped by Tab, and cannot be changed.
namestring–The field name when the switch sits in a form. It submits like a checkbox.
soundbooleanDefault truePlay a rising pair of clicks when it turns on and a falling pair when it turns off, if the visitor has sound on.
classNamestring–Extra classes for the track, merged last.

Motion and sound

Motion

  • The thumb slides to the other side over 150 ms while the track and the thumb change colour.
  • A second press during the slide reverses it from where it is.
  • Reduced motion: the thumb jumps to the other side.

Sound

  • Two rising clicks when the switch turns on, two falling clicks when it turns off.

Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works

Accessibility

  • It has the switch role with aria-checked, and a hidden checkbox carries its value in a form.
  • Name it by wrapping it in a label, as the example does, or with an aria-label. Pressing the label's text also flips it.
  • On and off differ in the thumb's position as well as in colour.
  • The track's border keeps 3 to 1 contrast against the page in both themes, and the thumb keeps it against the track.
KeysAction
TabFocus the switch.
Space / EnterTurn it on or off.

Source

components/ui/switch.tsx
"use client";

import { Switch as SwitchPrimitive } from "@base-ui/react/switch";

import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";

export type SwitchProps = SwitchPrimitive.Root.Props & {
  /** `sm` is 20 pixels tall, `md` is 24. */
  size?: "sm" | "md";
  /** Play a short rising or falling pair of clicks when it changes, if the visitor has sound on. */
  sound?: boolean;
};

/**
 * A switch for a setting that applies at once: on or off, nothing to submit.
 * Name it with a `<label>` around it or an `aria-label`. For a choice that waits for a Save button, use a checkbox.
 */
export function Switch({ size = "md", sound = true, className, onCheckedChange, ...props }: SwitchProps) {
  return (
    <SwitchPrimitive.Root
      data-slot="switch" data-surface="control"
      onCheckedChange={(checked, details) => {
        if (sound) play(checked ? "toggleOn" : "toggleOff");
        onCheckedChange?.(checked, details);
      }}
      className={cn(
        // The pseudo-element keeps the small size a 24 pixel target.
        "relative inline-flex shrink-0 cursor-pointer items-center rounded-[calc(var(--radius)*9999)] border border-input bg-background p-px after:absolute after:-inset-0.5",
        "transition-[background-color,border-color] duration-150 ease-out motion-reduce:transition-none",
        "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring",
        "data-checked:border-primary data-checked:bg-primary data-disabled:cursor-not-allowed data-disabled:opacity-50",
        size === "sm" ? "h-5 w-9" : "h-6 w-11",
        className,
      )}
      {...props}
    >
      <SwitchPrimitive.Thumb
        className={cn(
          // On and off differ in position and in colour, so the state never rests on colour alone.
          "block rounded-[calc(var(--radius)*9999)] bg-muted-foreground transition-[translate,background-color] duration-150 ease-out motion-reduce:transition-none data-checked:bg-primary-foreground",
          size === "sm" ? "size-4 data-checked:translate-x-4" : "size-5 data-checked:translate-x-5",
        )}
      />
    </SwitchPrimitive.Root>
  );
}

Files and dependencies

Adds
components/ui/switch.tsx
Packages
@base-ui/react@1.9.0
Brings along
Sound engine

Changelog

  1. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor