kipluo
React8 min read5 steps

How to Add Sound Effects to React UI

UI sound works when it is quiet, short and tied to something the user just did. This guide builds a tiny synthesized click with an OscillatorNode, explains the browser autoplay rule, adds a persistent mute switch, and shows the same API in @kipluo/core.

You needReact 18+Basic Web Audio API conceptslocalStorage
Loading demo
Click Copy to hear the confirmation sound. Nothing plays until you interact with the page.Component page

1. When sound helps and when it doesn't

Sound is useful for confirming actions that are easy to miss visually: a copy to clipboard, a sent message, a completed upload, a drag that snapped into place. It is noise when it plays on hover, on page load, or on every keystroke.

Treat sound as optional. Every interface that plays sound needs a visible way to turn it off, and the interface must make sense with sound off.

  • Play only in response to a user action or a result they are waiting for.
  • Keep sounds under about 250 ms and well below media volume.
  • Never carry information only in sound; pair it with a visual change and, where useful, an aria-live message.

2. The autoplay rule

Browsers block audio until the user has interacted with the page (a click, tap or key press). An AudioContext created before that starts in the suspended state. Create it lazily inside an event handler, or call resume() from one, and any sound you play afterwards will work.

3. Synthesize a click without audio files

An OscillatorNode generates a tone and a GainNode shapes its volume. A pitch that drops quickly while the volume decays exponentially sounds like a soft click or pop. There is nothing to download, and pitch can be varied per call so repeated clicks don't sound identical.

uiSound.ts
let ctx: AudioContext | null = null;
const STORAGE_KEY = "ui-sound";

export function isSoundOn() {
  try {
    return localStorage.getItem(STORAGE_KEY) !== "off";
  } catch {
    return true;
  }
}

export function setSoundOn(on: boolean) {
  try {
    localStorage.setItem(STORAGE_KEY, on ? "on" : "off");
  } catch {}
}

export function playClick(pitch = 1) {
  if (!isSoundOn()) return;
  ctx ??= new AudioContext();
  if (ctx.state === "suspended") void ctx.resume();

  const now = ctx.currentTime;
  const osc = ctx.createOscillator();
  const gain = ctx.createGain();

  osc.type = "sine";
  osc.frequency.setValueAtTime(720 * pitch, now);
  osc.frequency.exponentialRampToValueAtTime(430 * pitch, now + 0.08);

  gain.gain.setValueAtTime(0.0001, now);
  gain.gain.exponentialRampToValueAtTime(0.15, now + 0.005); // quiet peak
  gain.gain.exponentialRampToValueAtTime(0.0001, now + 0.13);

  osc.connect(gain).connect(ctx.destination);
  osc.start(now);
  osc.stop(now + 0.15);
}

4. Wire it into React with a mute switch

Call the sound from the event handler that performs the action, after the action succeeds. The mute switch below stores the choice in localStorage so it survives reloads.

CopyButton.tsx
import { useState } from "react";
import { isSoundOn, playClick, setSoundOn } from "./uiSound";

export function CopyButton({ text }: { text: string }) {
  const [copied, setCopied] = useState(false);

  async function copy() {
    await navigator.clipboard.writeText(text);
    playClick(1.2);
    setCopied(true);
    setTimeout(() => setCopied(false), 1500);
  }

  return (
    <button type="button" onClick={copy}>
      {copied ? "Copied" : "Copy"}
      <span aria-live="polite" className="sr-only">{copied ? "Copied to clipboard" : ""}</span>
    </button>
  );
}

export function SoundSwitch() {
  const [on, setOn] = useState(isSoundOn);
  return (
    <button
      type="button"
      aria-pressed={on}
      onClick={() => {
        setSoundOn(!on);
        setOn(!on);
      }}
    >
      Sound {on ? "on" : "off"}
    </button>
  );
}

5. The same approach in @kipluo/core

Kipluo components use a larger version of this idea from the free @kipluo/core package: around 17 synthesized sounds (pop, tick, swish, chime and others), placed in stereo where the element is on screen, throttled so bursts don't stack up, and silent while the tab is hidden.

You can use the engine on your own elements, set a global switch, or silence a single component with sound={false}. The free Copy Feedback component is a ready-made example.

KipluoSound.tsx
// npx kipluo add copy-feedback   (also installs @kipluo/core)
import { play, useSoundEnabled } from "@kipluo/core";
import { CopyFeedback } from "@/components/kipluo/copy-feedback";

export function SendButton({ onSend }: { onSend: () => void }) {
  return (
    <button
      type="button"
      onClick={(e) => {
        onSend();
        play("pop", { at: e.currentTarget }); // panned toward the button's position
      }}
    >
      Send
    </button>
  );
}

export function SoundToggle() {
  const [on, setOn] = useSoundEnabled(); // saved in localStorage
  return <button onClick={() => setOn(!on)}>{on ? "Sound on" : "Sound off"}</button>;
}

export function InstallCommand() {
  return <CopyFeedback text="npx kipluo add copy-feedback" />;
}

Accessibility checklist

Tick each one off as you check your build. Your ticks stay in this browser.

0 of 4 checked

Build it yourself or install it

Build it yourself when

You need one confirmation click in one place. The 30-line synth above is enough.

Install Kipluo when

You want a consistent set of quiet, stereo-placed sounds across many components, with throttling and a global switch already handled.

Keep building