Skip to content
Thrum

Toggle

A button that stays pressed: pin a chart, mark a favourite, turn bold on. It reports its state with aria-pressed. For a setting that has a label beside it use the switch; for a set of toggles that share one value use the toggle group.

Install

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

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

import { Toggle } from "@/components/ui/toggle";

Props

PropTypeDefaultDescription
pressedboolean–Whether it is pressed, when the parent holds the state. Use it with onPressedChange.
defaultPressedbooleanDefault falseWhether it starts pressed, when the toggle holds its own state.
onPressedChange(pressed: boolean) => void–Called with the new state when it changes.
variant"default" | "outline"Default "default"default has no frame until it is pressed. outline always shows its border.
size"sm" | "md" | "lg"Default "md"sm is 28 pixels tall, md 32, lg 40. With only an icon the toggle is a square of that size.
disabledbooleanDefault falseDimmed, skipped by Tab, and cannot be changed.
valuestring–Its name inside a toggle group. Not needed for a toggle that stands alone.
soundbooleanDefault truePlay a rising pair of clicks when it is pressed and a falling pair when it is released, if the visitor has sound on.
classNamestring–Extra classes, merged last.
...propsComponentProps<"button">–Every other button attribute and event handler is passed through.

Motion and sound

Motion

  • Background, border and text colour change over 150 ms when the state changes and on hover.
  • A press scales the toggle to 97% and back over 150 ms. A disabled toggle does not move.
  • Reduced motion: colours and scale change at once.

Sound

  • Two rising clicks when the toggle is pressed, two falling clicks when it is released.

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

Accessibility

  • It is a native button with aria-pressed, the WAI-ARIA toggle button pattern. A screen reader reads its name and whether it is pressed.
  • Its name is its text. A toggle with only an icon needs an aria-label, and the name stays the same in both states.
  • Pressed is a filled box in the primary colour with the primary text colour, against an empty box: the difference does not rest on a tint.
  • The smallest size is 28 pixels tall and at least 28 wide, above the 24 pixel minimum for pointer targets.
KeysAction
TabFocus the toggle. A disabled one is skipped.
Enter / SpacePress or release it.

Source

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

import { Toggle as TogglePrimitive } from "@base-ui/react/toggle";

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

export type ToggleVariant = "default" | "outline";
export type ToggleSize = "sm" | "md" | "lg";

export type ToggleProps = TogglePrimitive.Props & {
  /** `default` has no frame until it is pressed. `outline` always shows its border. */
  variant?: ToggleVariant;
  /** `sm` is 28 pixels tall, `md` 32, `lg` 40. A toggle with only an icon is a square of that size. */
  size?: ToggleSize;
  /** Play a rising pair of clicks when it is pressed and a falling pair when it is released, if the visitor has sound on. */
  sound?: boolean;
};

const BASE =
  "relative inline-flex shrink-0 cursor-pointer items-center justify-center rounded-md border font-medium whitespace-nowrap text-foreground select-none " +
  "transition-[background-color,border-color,color,scale] duration-150 ease-out motion-reduce:transition-none " +
  "hover:bg-accent hover:text-accent-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring active:scale-[0.97] " +
  // Pressed is the primary fill with its own text colour: a filled box against an empty one, not a tint of the same box.
  "data-pressed:border-primary data-pressed:bg-primary data-pressed:text-primary-foreground data-pressed:hover:bg-primary/85 " +
  "data-disabled:cursor-not-allowed data-disabled:opacity-50 data-disabled:active:scale-100";

const VARIANT: Record<ToggleVariant, string> = {
  default: "border-transparent",
  outline: "border-input bg-background",
};

const SIZE: Record<ToggleSize, string> = {
  // The icon's negative margin lets a toggle with only an icon come out square at the same padding a label gets.
  sm: "h-7 min-w-7 gap-1.5 px-2 text-xs [&_svg]:-mx-0.5 [&_svg]:size-3.5",
  md: "h-8 min-w-8 gap-2 px-2 text-sm [&_svg]:-mx-0.5 [&_svg]:size-4",
  lg: "h-10 min-w-10 gap-2.5 px-3 text-sm [&_svg]:-mx-1 [&_svg]:size-5",
};

/**
 * A button that stays pressed: bold, pinned, muted. It carries `aria-pressed`, so a screen reader says which state it is in.
 * Name it with its text, or with an `aria-label` when it holds only an icon. For a setting with a label beside it, use a switch.
 */
export function Toggle({ variant = "default", size = "md", sound = true, className, onPressedChange, ...props }: ToggleProps) {
  return (
    <TogglePrimitive
      data-slot="toggle" data-surface="control"
      onPressedChange={(pressed, details) => {
        if (sound) play(pressed ? "toggleOn" : "toggleOff");
        onPressedChange?.(pressed, details);
      }}
      className={cn(BASE, VARIANT[variant], SIZE[size], className)}
      {...props}
    />
  );
}

Files and dependencies

Adds
components/ui/toggle.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