# Rocker Switch with Click Sound

> A clean 3D OFF/ON rocker switch with a crisp mechanical click sound. Real checkbox underneath, keyboard-ready, any ON colour and size.

- Kind: Element
- Page: https://ownlyplans.com/bhaskar/rocker-switch-with-click-sound/
- Licence: MIT (do anything, keep the notice)

## Install

```bash
npx shadcn@latest add https://ownlyplans.com/r/bhaskar/rocker-switch-with-click-sound.json
```

## Code

`index.html`

```html
<!--
MIT License

Copyright (c) 2023 Richard Tsang (r7chardgh), original switch design on Uiverse.io
Copyright (c) 2026 OwnlyPlans, click sound, accessibility, options and React version

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<label class="rk-switch">
  <input class="rk-cb" type="checkbox" role="switch" aria-label="Power" name="power">
  <span class="rk-toggle">
    <span class="rk-left">off</span>
    <span class="rk-right">on</span>
  </span>
</label>
```

`styles.css`

```css
/*
 * MIT License
 *
 * Copyright (c) 2023 Richard Tsang (r7chardgh), original switch design on Uiverse.io
 * Copyright (c) 2026 OwnlyPlans, click sound, accessibility, options and React version
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
/* The switch - the box around the slider */
.rk-switch{--rk-size:17px;--rk-on:#487bdb;--rk-body:#373737;font-size:var(--rk-size);position:relative;display:inline-block;width:5em;height:2.5em;user-select:none}
/* Hide default HTML checkbox (it stays focusable for the keyboard) */
.rk-switch .rk-cb{opacity:0;width:0;height:0}
/* The slider */
.rk-toggle{position:absolute;cursor:pointer;width:100%;height:100%;background-color:var(--rk-body);border-radius:.1em;transition:.4s;text-transform:uppercase;font-weight:700;overflow:hidden;
 font-family:system-ui,-apple-system,"Segoe UI",sans-serif;
 box-shadow:-.3em 0 0 0 var(--rk-body),-.3em .3em 0 0 var(--rk-body),.3em 0 0 0 var(--rk-body),.3em .3em 0 0 var(--rk-body),0 .3em 0 0 var(--rk-body)}
.rk-toggle>.rk-left{position:absolute;display:flex;width:50%;height:88%;background-color:#f3f3f3;color:var(--rk-body);left:0;bottom:0;align-items:center;justify-content:center;
 transform-origin:right;transform:rotateX(10deg);transform-style:preserve-3d;transition:all 150ms}
.rk-left::before{position:absolute;content:"";width:100%;height:100%;background-color:#cecece;transform-origin:center left;transform:rotateY(90deg)}
.rk-left::after{position:absolute;content:"";width:100%;height:100%;background-color:#707070;transform-origin:center bottom;transform:rotateX(90deg)}
.rk-toggle>.rk-right{position:absolute;display:flex;width:50%;height:88%;background-color:#f3f3f3;color:#cecece;right:1px;bottom:0;align-items:center;justify-content:center;
 transform-origin:left;transform:rotateX(10deg) rotateY(-45deg);transform-style:preserve-3d;transition:all 150ms}
.rk-right::before{position:absolute;content:"";width:100%;height:100%;background-color:#cecece;transform-origin:center right;transform:rotateY(-90deg)}
.rk-right::after{position:absolute;content:"";width:100%;height:100%;background-color:#707070;transform-origin:center bottom;transform:rotateX(90deg)}
.rk-switch .rk-cb:checked+.rk-toggle>.rk-left{transform:rotateX(10deg) rotateY(45deg);color:#cecece}
.rk-switch .rk-cb:checked+.rk-toggle>.rk-right{transform:rotateX(10deg) rotateY(0deg);color:var(--rk-on)}
/* OwnlyPlans additions */
.rk-switch .rk-cb:focus-visible+.rk-toggle{outline:2px solid var(--rk-on);outline-offset:.45em}
.rk-switch .rk-cb:disabled+.rk-toggle{opacity:.5;cursor:not-allowed}
@media (prefers-reduced-motion:reduce){.rk-toggle>.rk-left,.rk-toggle>.rk-right{transition:none}}
```

`script.js`

```js
/*
 * MIT License
 *
 * Copyright (c) 2023 Richard Tsang (r7chardgh), original switch design on Uiverse.io
 * Copyright (c) 2026 OwnlyPlans, click sound, accessibility, options and React version
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
let rkAudio = null;
function rockerClick(on, volume = 0.6) {
  try {
    const AC = window.AudioContext || window.webkitAudioContext;
    if (!AC) return;
    rkAudio = rkAudio || new AC();
    const ac = rkAudio, t = ac.currentTime + 0.002, out = ac.createGain();
    out.gain.value = volume; out.connect(ac.destination);
    const len = Math.floor(ac.sampleRate * 0.03), buf = ac.createBuffer(1, len, ac.sampleRate), d = buf.getChannelData(0);
    for (let i = 0; i < len; i++) d[i] = (Math.random() * 2 - 1) * Math.pow(1 - i / len, 6);
    const snap = (at, freq, gain) => {
      const src = ac.createBufferSource(), bp = ac.createBiquadFilter(), g = ac.createGain();
      bp.type = 'bandpass'; bp.frequency.value = freq; bp.Q.value = 1.4; g.gain.value = gain;
      src.buffer = buf; src.connect(bp); bp.connect(g); g.connect(out); src.start(at);
    };
    snap(t, on ? 3600 : 2600, 0.9);
    snap(t + 0.011, on ? 5200 : 4200, 0.25);
    const body = ac.createOscillator(), bg = ac.createGain();
    body.type = 'sine'; body.frequency.setValueAtTime(on ? 210 : 150, t); body.frequency.exponentialRampToValueAtTime(on ? 120 : 85, t + 0.05);
    bg.gain.setValueAtTime(0.0001, t); bg.gain.exponentialRampToValueAtTime(0.7, t + 0.002); bg.gain.exponentialRampToValueAtTime(0.0001, t + 0.07);
    body.connect(bg); bg.connect(out); body.start(t); body.stop(t + 0.08);
  } catch (e) { /* no audio available: stay silent */ }
}
/* Every .rk-switch on the page clicks when it changes. */
document.querySelectorAll('.rk-switch').forEach((sw) => {
  const cb = sw.querySelector('.rk-cb');
  cb.addEventListener('change', () => {
    if (sw.dataset.sound !== 'off') rockerClick(cb.checked, parseFloat(sw.dataset.volume || '0.6'));
    sw.dispatchEvent(new CustomEvent('rk:change', { bubbles: true, detail: { checked: cb.checked, name: cb.name } }));
  });
});
```

`rocker-switch-with-click-sound.tsx`

```tsx
/*
 * MIT License
 *
 * Copyright (c) 2023 Richard Tsang (r7chardgh), original switch design on Uiverse.io
 * Copyright (c) 2026 OwnlyPlans, click sound, accessibility, options and React version
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
"use client";
import { useState, type CSSProperties } from "react";

const css = `/* The switch - the box around the slider */
.rk-switch{--rk-size:17px;--rk-on:#487bdb;--rk-body:#373737;font-size:var(--rk-size);position:relative;display:inline-block;width:5em;height:2.5em;user-select:none}
/* Hide default HTML checkbox (it stays focusable for the keyboard) */
.rk-switch .rk-cb{opacity:0;width:0;height:0}
/* The slider */
.rk-toggle{position:absolute;cursor:pointer;width:100%;height:100%;background-color:var(--rk-body);border-radius:.1em;transition:.4s;text-transform:uppercase;font-weight:700;overflow:hidden;
 font-family:system-ui,-apple-system,"Segoe UI",sans-serif;
 box-shadow:-.3em 0 0 0 var(--rk-body),-.3em .3em 0 0 var(--rk-body),.3em 0 0 0 var(--rk-body),.3em .3em 0 0 var(--rk-body),0 .3em 0 0 var(--rk-body)}
.rk-toggle>.rk-left{position:absolute;display:flex;width:50%;height:88%;background-color:#f3f3f3;color:var(--rk-body);left:0;bottom:0;align-items:center;justify-content:center;
 transform-origin:right;transform:rotateX(10deg);transform-style:preserve-3d;transition:all 150ms}
.rk-left::before{position:absolute;content:"";width:100%;height:100%;background-color:#cecece;transform-origin:center left;transform:rotateY(90deg)}
.rk-left::after{position:absolute;content:"";width:100%;height:100%;background-color:#707070;transform-origin:center bottom;transform:rotateX(90deg)}
.rk-toggle>.rk-right{position:absolute;display:flex;width:50%;height:88%;background-color:#f3f3f3;color:#cecece;right:1px;bottom:0;align-items:center;justify-content:center;
 transform-origin:left;transform:rotateX(10deg) rotateY(-45deg);transform-style:preserve-3d;transition:all 150ms}
.rk-right::before{position:absolute;content:"";width:100%;height:100%;background-color:#cecece;transform-origin:center right;transform:rotateY(-90deg)}
.rk-right::after{position:absolute;content:"";width:100%;height:100%;background-color:#707070;transform-origin:center bottom;transform:rotateX(90deg)}
.rk-switch .rk-cb:checked+.rk-toggle>.rk-left{transform:rotateX(10deg) rotateY(45deg);color:#cecece}
.rk-switch .rk-cb:checked+.rk-toggle>.rk-right{transform:rotateX(10deg) rotateY(0deg);color:var(--rk-on)}
/* OwnlyPlans additions */
.rk-switch .rk-cb:focus-visible+.rk-toggle{outline:2px solid var(--rk-on);outline-offset:.45em}
.rk-switch .rk-cb:disabled+.rk-toggle{opacity:.5;cursor:not-allowed}
@media (prefers-reduced-motion:reduce){.rk-toggle>.rk-left,.rk-toggle>.rk-right{transition:none}}`;

let rkAudio: AudioContext | null = null;
/** Plays the rocker click. Call it from a user action (browsers need a gesture). */
export function rockerClick(on: boolean, volume = 0.6) {
  try {
    const AC = window.AudioContext || (window as unknown as { webkitAudioContext?: typeof AudioContext }).webkitAudioContext;
    if (!AC) return;
    rkAudio = rkAudio || new AC();
    const ac = rkAudio, t = ac.currentTime + 0.002, out = ac.createGain();
    out.gain.value = volume; out.connect(ac.destination);
    const len = Math.floor(ac.sampleRate * 0.03), buf = ac.createBuffer(1, len, ac.sampleRate), d = buf.getChannelData(0);
    for (let i = 0; i < len; i++) d[i] = (Math.random() * 2 - 1) * Math.pow(1 - i / len, 6);
    const snap = (at: number, freq: number, gain: number) => {
      const src = ac.createBufferSource(), bp = ac.createBiquadFilter(), g = ac.createGain();
      bp.type = 'bandpass'; bp.frequency.value = freq; bp.Q.value = 1.4; g.gain.value = gain;
      src.buffer = buf; src.connect(bp); bp.connect(g); g.connect(out); src.start(at);
    };
    snap(t, on ? 3600 : 2600, 0.9);
    snap(t + 0.011, on ? 5200 : 4200, 0.25);
    const body = ac.createOscillator(), bg = ac.createGain();
    body.type = 'sine'; body.frequency.setValueAtTime(on ? 210 : 150, t); body.frequency.exponentialRampToValueAtTime(on ? 120 : 85, t + 0.05);
    bg.gain.setValueAtTime(0.0001, t); bg.gain.exponentialRampToValueAtTime(0.7, t + 0.002); bg.gain.exponentialRampToValueAtTime(0.0001, t + 0.07);
    body.connect(bg); bg.connect(out); body.start(t); body.stop(t + 0.08);
  } catch (e) { /* no audio available: stay silent */ }
}

export function RockerSwitch({
  checked,
  defaultChecked = false,
  onChange,
  offLabel = "off",
  onLabel = "on",
  size = 17,
  onColor = "#487bdb",
  sound = true,
  volume = 0.6,
  name,
  disabled,
  "aria-label": ariaLabel = "Switch",
}: {
  checked?: boolean;
  defaultChecked?: boolean;
  onChange?: (checked: boolean) => void;
  offLabel?: string;
  onLabel?: string;
  /** Font size in px; the whole switch scales from it (default 17). */
  size?: number;
  onColor?: string;
  sound?: boolean;
  /** 0 to 1 */
  volume?: number;
  name?: string;
  disabled?: boolean;
  "aria-label"?: string;
}) {
  const [inner, setInner] = useState(defaultChecked);
  const on = checked ?? inner;
  return (
    <label className="rk-switch" style={{ "--rk-size": `${size}px`, "--rk-on": onColor } as CSSProperties}>
      <style>{css}</style>
      <input
        className="rk-cb"
        type="checkbox"
        role="switch"
        aria-label={ariaLabel}
        name={name}
        disabled={disabled}
        checked={on}
        onChange={(e) => {
          const next = e.target.checked;
          if (checked === undefined) setInner(next);
          if (sound) rockerClick(next, volume);
          onChange?.(next);
        }}
      />
      <span className="rk-toggle">
        <span className="rk-left">{offLabel}</span>
        <span className="rk-right">{onLabel}</span>
      </span>
    </label>
  );
}

/* Usage:
   <RockerSwitch aria-label="Power" onChange={setPower} />
   <RockerSwitch size={24} onColor="#16a34a" offLabel="no" onLabel="yes" volume={0.4} name="newsletter" />
*/
```

## Licence notice

Keep this notice with the code (the licence requires it).

```text
MIT License

Copyright (c) 2023 Richard Tsang (r7chardgh), original switch design on Uiverse.io
Copyright (c) 2026 OwnlyPlans, click sound, accessibility, options and React version

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
```

## About

A minimal on/off rocker with depth: the paddle tips over in 3D, the active side lights up, and it makes a crisp mechanical click (a bright snap, a short woody thock and a tiny spring tick, slightly higher when it turns on). The sound is made in code with the Web Audio API, so there are no audio files to load.

Underneath it is a real checkbox with role="switch", so it works in forms (name and value), with Space and Tab, and with screen readers, and it shows a focus ring for keyboard users. Change the ON colour with --rk-on, the size with --rk-size (the whole switch scales from it), the labels in the HTML, and the volume, or turn the sound off with data-sound="off" on the label. Class names are prefixed (rk-) so they never clash with your own styles.

Based on the open-source switch by Richard Tsang (r7chardgh) on Uiverse.io, customised by OwnlyPlans with the sound, accessibility, options and a React version.

MIT License

Copyright (c) 2023 Richard Tsang (r7chardgh), original switch design on Uiverse.io
Copyright (c) 2026 OwnlyPlans, click sound, accessibility, options and React version

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

---
Also available as JSON: https://ownlyplans.com/bhaskar/rocker-switch-with-click-sound/?format=json
MCP server: https://ownlyplans.com/mcp
