Skip to content
Thrum

Sound toggle

The visitor's switch for the library's interface sounds. Every sound in the library stays silent until someone turns it on, and this is the control that does it. Put one in a header or a settings page; the choice is saved in the browser and every sound toggle on the page shows the same state.

Install

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

Adds components/ui/sound-toggle.tsx. Nothing else in your project changes.

import { SoundToggle } from "@/components/ui/sound-toggle";

Automatically installs icons volume and volume-off from the icon set.

Props

PropTypeDefaultDescription
variant"default" | "outline"Default "default"The toggle variant. default has no frame until sound is on; outline always shows its border.
size"sm" | "md" | "lg"Default "md"sm is 28 pixels tall, md 32, lg 40. With no children it is a square of that size.
childrenReactNode–A label beside the icon. Without it the toggle is named Sound.
aria-labelstringDefault "Sound"The name of a toggle with no children. Translate it here.
onPressedChange(on: boolean) => void–Called after the visitor turns sound on or off.
disabledbooleanDefault falseDimmed, skipped by Tab, and cannot be changed.
classNamestring–Extra classes, merged last.
...propsComponentProps<"button">–Every other button attribute is passed through. There is no pressed prop: the state is the visitor's saved choice.

Motion and sound

Motion

  • Background, border and icon colour change over 150 ms when the state changes. The icon swaps its waves for a cross at once.
  • Reduced motion: the colours change at once.

Sound

  • Two rising clicks when sound is turned on, so the visitor hears that it works. Turning it off is silent.

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

Accessibility

  • It is a toggle button: a native button with aria-pressed, true while sound is on. Its name stays Sound in both states.
  • On and off differ in shape as well as colour: the speaker shows waves when sound is on and a cross when it is off.
  • Sound starts off for every visitor. Browsers allow audio only after the page has been used, and the press that turns sound on is that use.
  • The choice is stored in localStorage under thrum:sound. Where storage is blocked it holds for the visit.
  • Sound is never the only feedback in the library, so a visitor who leaves it off misses nothing.
KeysAction
TabFocus the toggle.
Enter / SpaceTurn sound on or off.

Source

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

import { useSound } from "@/lib/thrum-sound";
import { Toggle, type ToggleProps } from "@/components/ui/toggle";
import { VolumeIcon } from "@/components/icons/volume";
import { VolumeOffIcon } from "@/components/icons/volume-off";

export type SoundToggleProps = Omit<ToggleProps, "pressed" | "defaultPressed" | "sound" | "value">;

/**
 * The visitor's switch for the library's interface sounds. Sound is off until someone turns it on here;
 * the choice is saved in the browser and shared by every component and every sound toggle on the page.
 * With no children it is a square icon button named "Sound". Children become a label beside the icon.
 */
export function SoundToggle({ children, onPressedChange, "aria-label": ariaLabel, ...props }: SoundToggleProps) {
  // False on the server and in the first client render, then the saved choice.
  const { enabled, setEnabled } = useSound();
  return (
    <Toggle
      data-slot="sound-toggle"
      pressed={enabled}
      // Turning sound on plays the "on" clicks itself, so the toggle adds none of its own.
      sound={false}
      onPressedChange={(pressed, details) => {
        setEnabled(pressed);
        onPressedChange?.(pressed, details);
      }}
      aria-label={ariaLabel ?? (children ? undefined : "Sound")}
      {...props}
    >
      {/* Waves when it is on, struck through when it is off: the state does not rest on the fill colour. */}
      {enabled ? <VolumeIcon size={16} /> : <VolumeOffIcon size={16} />}
      {children}
    </Toggle>
  );
}

Files and dependencies

Adds
components/ui/sound-toggle.tsx
Packages
None
Brings along
Sound engineToggle
Icons
volumevolume-off

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