Ambient Audio Toggle
A visitor-controlled background music toggle with an animated waveform, persistent playback, and accessible keyboard controls.
Installation#
pnpm dlx shadcn@latest add @davinciui/ambient-audio-toggleUsage#
API Reference#
| Prop | Type | Default |
|---|---|---|
srcAudio URL or uploaded file (MP3, WAV, OGG, or M4A). | string | — |
persistenceKeyStable identity for retaining the audio session across page remounts. | string | — |
loopRepeat the track after it ends. | boolean | true |
volumeVolume from 0 to 100 in 5-point steps. | number | 65 |
visualizerWave line or animated bars. | "wave" | "bars" | "wave" |
barCountNumber of bars, clamped from 4 to 80. | number | 24 |
activeColor / inactiveColorActive and paused waveform colors. | string | — |
playingColor / pausedColorPlaying and paused label colors. | string | — |
labelPlaying / labelPausedStatus text for each playback state. | string | — |
hideLabelHide visible status text while retaining the accessible button label. | boolean | false |
labelPositionPosition of the label around the waveform. | "left" | "right" | "above" | "below" | "right" |
waveformWidth / waveformHeightWaveform dimensions in pixels. | number | 112 / 36 |
lineWeight / lineCapWave line thickness and rounded or square ends. | number / string | 2 / round |
pausedAmountVisible waveform level while paused. | number | 0.2 |
animationSpeed / intensityVisualizer motion speed and amplitude. | number | 1.1 / 1 |
transitionPlay/pause transition duration in seconds. | number | 0.35 |
labelGapSpacing between label and waveform in pixels. | number | 10 |
className / styleAdditional button styling. | string / CSSProperties | — |
Accessibility#
Renders a native button with a dynamic aria-label, aria-pressed playback state, and a visible focus ring; Enter and Space work natively. The waveform is aria-hidden, so status is conveyed by label text unless hideLabel removes it.
Motion#
While playing, waveform dash-offset or bar scale animates; animationSpeed sets duration and intensity amplitude. transition sets color and height crossfades. An inline prefers-reduced-motion media query stops the keyframes but keeps property transitions.
About Ambient Audio Toggle#
A small, visitor-controlled sound layer for a calmer, more atmospheric site.
The player starts silent. Visitors activate playback with the waveform, and the same audio session continues through client-side page changes. Playback pauses when the tab is hidden and resumes when it becomes visible again.
Each audio source gets its own session. Set a stablepersistenceKeywhen a player needs a specific identity across page remounts.
Controls#
- Audio accepts MP3, WAV, OGG, and M4A. Playback begins only after a visitor activates the button.
- Wave or Bars changes the visualizer. Bars supports 4 to 80 columns.
- Volume and Loop set the track level and whether it repeats.
- Waveform controls size, line weight, caps, intensity, speed, and paused visibility.
- Label controls status text, position, colors, spacing, and visibility.
- Keyboard toggles with Enter or Space and shows a visible focus ring.
Best For#
Portfolio introductions, immersive product pages, audio essays, and ambient landing pages where visitors should choose when sound begins.
View source
Keep in mind
Need a custom component?
I build bespoke UI components & websites tailored to your brand.

