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.jsonAdds 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
| Prop | Type | Default | Description |
|---|---|---|---|
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. |
children | ReactNode | – | A label beside the icon. Without it the toggle is named Sound. |
aria-label | string | Default "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. |
disabled | boolean | Default false | Dimmed, skipped by Tab, and cannot be changed. |
className | string | – | Extra classes, merged last. |
...props | ComponentProps<"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.
| Keys | Action |
|---|---|
| Tab | Focus the toggle. |
| Enter / Space | Turn sound on or off. |
Source
"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.0.0 · 2026-12-03First release.