# SoundToggle

> Opt-in interface sound: a `sound` controller that synthesises eleven short cues (hover, press, toggle-on, toggle-off, open, close, select, success, error, tick, copy) with the Web Audio API, a SoundToggle switch that keeps the preference and volume, and a soundFeedback action that wires cues to any element — silent until the user switches it on, nothing on import, mount, navigation or scroll.

- Package: `fancy-ui-react` (React 18/19 + Tailwind CSS v4)
- Category: Actions
- Tags: sound, audio, feedback, web-audio, cues, toggle, accessibility, opt-in
- Types: `SoundToggleProps`, `SoundToggleSize`, `SoundToggleVariant`, `SoundController`, `SoundSnapshot`, `SoundFeedbackOptions`, `SoundFeedbackHandle`, `SoundCueResolver`, `SoundCueSpec`, `SoundControls`, `SoundEngine`, `SoundEngineOptions`, `SoundCue`, `SoundThemeName`, `SoundPlayOptions`, `SoundPreferences`, `SoundPreferencesV1`, `SoundStatus`, `SoundEngineState`, `SoundThemeDefinition`, `CueRecipe`, `SoundLayer`, `SoundOscillatorLayer`, `SoundNoiseLayer`, `SoundEnvelope`, `SoundFilter`, `SoundWave`
- Docs: https://fancy-ui.rama.app/docs/components/sound
- Source: https://github.com/RamaHerbin/fancy-ui/tree/main/react/src/sound

## Import

```tsx
import { SoundToggle } from 'fancy-ui-react';
```

Setup (once per project): `npm install fancy-ui-react`, then put these lines in your main CSS file (the stylesheet import goes right after `@import "tailwindcss";`):

```css
@import "tailwindcss";
@import "fancy-ui-react/tailwind.css";
```

and import the component styles once in the app entry: `import "fancy-ui-react/styles.css";`. Full guide: https://fancy-ui.rama.app/llms.txt

The names below are the ones to write in React; when one differs from the Svelte name, the Svelte name follows in parentheses. `class` becomes `className`, a callback takes the name the React port declares (`onclick` becomes `onClick`), snippets become `ReactNode` props (render functions when they take arguments) and the default snippet is `children`. Divergences: https://github.com/RamaHerbin/fancy-ui/tree/main/react#divergences-from-the-svelte-api

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `size` | `"sm" \| "md" \| "lg"` | `"md"` | Height of the control — md matches the other header-style triggers |
| `variant` | `"outline" \| "ghost"` | `"outline"` | Outline keeps a resting border; ghost shows one only on hover |
| `showLabel` | `boolean` | `false` | Renders the label and the On/Off word beside the icon; icon-only otherwise, with the label as the accessible name |
| `label` | `string` | `"Sound"` | Accessible name of the switch. Stays constant — the on/off state is announced through aria-checked |
| `labelOn` | `string` | `"On"` | Visible state word when showLabel is set |
| `labelOff` | `string` | `"Off"` | Visible state word when showLabel is set |
| `disabled` | `boolean` | `false` | Disables the control. A browser with no Web Audio also disables it, but only while sound is off, so a stored on preference can always be undone |
| `onEnabledChange` | `(enabled: boolean) => void` | — | Called after the preference flips, with the new value |
| `className` (Svelte `class`) | `string` | — | Additional CSS classes for the button |
| `ref` | `HTMLButtonElement \| null` | `null` | Bindable reference to the button |
| `sound.play(cue, options?)` | `(cue: SoundCue, options?: SoundPlayOptions) => void` | — | Plays one cue. A no-op while sound is off or unsupported, so call sites never branch; options carry volume, pitch and playbackRate |
| `sound.enable() / disable() / toggle()` | `() => void \| boolean` | — | Flip the preference. Enabling creates and resumes the audio context inside the calling user gesture |
| `sound.setVolume(v)` | `(v: number) => void` | — | Master volume, 0 to 1, persisted with the preference |
| `sound.enabled / volume / status` | `boolean / number / SoundStatus` | — | Reactive preference and engine state (supported, engine, storage, lastCue, lastError) for your own UI |
| `sound.subscribe(run)` | `(run: (prefs: SoundPreferences) => void) => () => void` | — | Store contract for non-rune consumers; sound.enabled is the idiomatic path |
| `useSoundFeedback` (Svelte `use:soundFeedback`) | `SoundFeedbackOptions` | — | Action mapping element events to cues — defaults to click → press; add pointerenter → hover to opt in. Rebinds on update and unwires on destroy |
| `sound (on Button, CopyButton, Checkbox, Switch, RadioGroup, Select, DropdownMenu)` | `boolean` | `false` | Per-instance opt-in: the component plays its own matching cue (press, copy/error, toggle-on/off, select, open/close) once the user has enabled sound |

## Example

No React example is published for this component yet: start from the import above and the props table. The full examples are Svelte-only, on https://fancy-ui.rama.app/docs/components/sound.md.
