svelte-attach

Sound

API docs
import { sound, Sound, isSupported } from "@joknoll/svelte-attach/sound";

Play on click

<button {@attach sound(click)}>Click</button>

On and off sounds

<input
  type="checkbox"
  role="switch"
  {@attach sound((input) => (input.checked ? switchOn : switchOff), {
    on: "change",
  })}
/>

Hover to play, move away to stop

<button
  {@attach sound(cheering, {
    on: "mouseenter",
    stopOn: "mouseleave",
    volume: 0.2,
  })}
>Hover</button>

Play from code

const cheer = new Sound(cheering, { volume: 0.2 });

<button onclick={() => cheer.play()}>Play</button>
<button onclick={() => cheer.stop()}>Stop</button>