Realistic Button Click Sounds in JavaScript – Button Sounds

Category: Javascript | November 26, 2026
Authorjoshwcomeau
Last UpdateNovember 26, 2026
LicenseMIT
Tags
Views0 views
Realistic Button Click Sounds in JavaScript – Button Sounds

Button Sounds is a JavaScript library that adds realistic click and toggle sound effects to web controls.

It includes multi-sampled recordings of mechanical keyboards, mice, game controllers, and switches.

Each press or release plays a random sample of the selected sound.

Features

  • Adjustable volume and randomized pitch.
  • Optional lo-fi bitcrusher effect.
  • Automatic audio preloading when a sound is attached to a control.
  • Manual playback for custom events and application states.
  • Stereo MP3 (192 kbps) and WAV (48 kHz) downloads.

How To Use It

Installation

Install Button Sounds with NPM:

npm install button-sounds

Basic Usage

Preview the recordings in the Sound Playground. Import SOUND_NAMES to get the preset names in your installed version.

Set the MP3 directory with setPath(), then attach a sound to a button with wireUp().

For npm projects, import the functions from button-sounds.

<button id="save" type="button">Save settings</button>
<script type="module">
  import { setPath, wireUp } from 'https://cdn.jsdelivr.net/npm/button-sounds/+esm';
  setPath('https://cdn.jsdelivr.net/npm/button-sounds/assets/');
  const button = document.querySelector('#save');
  wireUp(button, 'hi75-soft', {
    volume: 0.3
  });
</script>

All Configuration Options

The wireUp() function accepts these options:

  • volume (number, default: 0.5): Playback volume from 0 (silent) to 1 (maximum). Use 0.25 to 0.5 for subtle UI feedback.
  • pitchVariation (number or [number, number], default: 0): Randomizes pitch through playback speed. For example, 0.05 selects a rate from 0.95 to 1.05, and [0.8, 1.2] sets the minimum and maximum rates.
  • lofi (boolean, default: false): Applies a bitcrusher effect.
  • lofiOptions (object): Bitcrusher settings for use with lofi: true.
    • bits (number, default: 4): Bit depth from 1 to 16 (rounded to an integer). Lower values produce more distortion.
    • downsample (number, default: 0.25): Fraction of the original sample rate to retain (0 to 1). At 1, the sample rate is unchanged; 0 produces silence.

The press() and release() functions accept volume, lofi, lofiOptions, and two additional options:

  • sampleIndex (integer, default: random): Zero-based index of the recorded sample to play. Out-of-range integer indices wrap around the available samples.
  • playbackRate (number, default: 1): Playback speed for a single call. For example, 0.8 slows the sound and lowers its pitch.

Play Sounds Based on Control State

Play a different switch sound when a notification setting is enabled or disabled. Include a sound preference to let your visitors silence the feedback.

<label>
  <input id="notifications" type="checkbox">
  Notifications
</label>
<label>
  <input id="sound-effects" type="checkbox" checked>
  Sound effects
</label>
<script type="module">
  import { setPath, load, press } from 'https://cdn.jsdelivr.net/npm/button-sounds/+esm';
  setPath('https://cdn.jsdelivr.net/npm/button-sounds/assets/');
  const notifications = document.querySelector('#notifications');
  const soundEffects = document.querySelector('#sound-effects');
  const onSound = 'guitar-toggle-on';
  const offSound = 'guitar-toggle-off';
  load(onSound);
  load(offSound);
  notifications.addEventListener('change', () => {
    if (!soundEffects.checked) return;
    press(notifications.checked ? onSound : offSound, {
      volume: 0.3
    });
  });
</script>

Public API

Named exportDescription
wireUp(element, name, options?)Preloads the sound and binds playback to pointerdown on the HTML element and the next pointerup on window. Accepts optional wiring settings. Returns a cleanup function that removes the binding and any pending release listeners.
load(name)Starts preloading the MP3 and returns void immediately. Reuses cached loads for the same sound and directory.
press(name, options?)Plays a press sample with optional playback settings. Returns void.
release(name, options?)Plays a release sample with optional playback settings. Returns void.
setPath(path)Sets the shared MP3 directory for subsequent loads and playback. Returns void.
resolveSoundUrl(name, baseUrl?)Builds the MP3 URL from its name and an optional base URL. Defaults to DEFAULT_BASE_URL.

Additional exports:

  • SOUND_NAMES: Array of sound names recognized by the API.
  • SPRITE_DATA: Audio sprite maps indexed by sound name, with sample keys such as down1 and up1 and values of [startMs, durationMs].
  • DEFAULT_NAME: Preset name 'uhk-soft' for use as a default selection.
  • DEFAULT_BASE_URL: Built-in jsDelivr audio directory (@0.0.0/assets/ in v1.0.0).
  • DEFAULT_LOFI_OPTIONS: Default bitcrusher settings (bits: 4, downsample: 0.25).

Self-Host the Audio Files

Copy the packaged MP3 files into your site’s public directory:

npx button-sounds copy ./public/sounds/buttons

If your server serves public at its root, set the new audio directory before loading any sounds.

import { setPath } from 'button-sounds';
setPath('/sounds/buttons/');

React Integration

The optional React bindings require React and React DOM. Import the hooks from button-sounds/react.

import React, { useRef } from 'react';
import { setPath } from 'button-sounds';
import { useButtonSounds } from 'button-sounds/react';
setPath('https://cdn.jsdelivr.net/npm/button-sounds/assets/');
export default function SaveButton() {
  const ref = useRef(null);
  useButtonSounds(ref, 'uhk-soft', { volume: 0.3 });
  return <button ref={ref} type="button">Save</button>;
}
  • useButtonSounds(ref, name, options?): Accepts an HTML element ref, a sound name, and WireUpOptions. Preloads the sound, updates the binding when the name or options change, and cleans up on unmount. Returns void.
  • useImperativeButtonSounds(name, options?): Preloads the sound on mount and when its name changes. Returns { press, release }. Pass TriggerOptions to the hook for defaults and to either function to override them for one call. Both functions also accept React pointer events.

Use the Audio Files Directly

Download the MP3 files or WAV recordings for your own audio engine or design tools.

Each selectable sound is an audio sprite with four to eight pairs of press and release samples. The first pair starts at 1,000 ms (press) and 1,200 ms (release). Each subsequent pair starts one second later.

Alternatives

You Might Be Interested In:


Leave a Reply