Platform audio player that wraps native <audio> with custom controls in shadow DOM. Progressive enhancement — native audio works without JS.
Overview
A platform-native audio player that wraps <audio> in light DOM and renders custom controls in shadow DOM. The native player is always the fallback — if JS is unavailable or the component fails, <audio controls> works normally.
Pair with <audio-visualizer> for canvas-based frequency or waveform visualization.
Single Track
1234567
<audio-player> <audio controls> <source src="track.ogg" type="audio/ogg"> <source src="track.mp3" type="audio/mpeg"> <p><a href="track.mp3" download>Download track</a></p> </audio></audio-player>
Playlist Mode
Include a <details> with <ol class="track-list"> inside the component. Clicking a track updates the audio source and plays it. When a track ends it is marked data-audio-played, keeps data-audio-active until the next track starts, and playback advances in list order, stopping after the last track (or wrapping with loop).
1234567891011121314151617181920212223
<audio-player> <audio controls> <source src="tracks/01-opening.mp3" type="audio/mpeg"> <p><a href="tracks/01-opening.mp3" download>Download track 1</a></p> </audio> <details> <summary>Track Listing</summary> <ol class="track-list"> <li data-audio-active> <a href="tracks/01-opening.mp3">01. Opening Theme</a> <span class="track-meta"><time datetime="PT2M14S">2:14</time></span> </li> <li> <a href="tracks/02-main.mp3">02. Main Theme</a> <span class="track-meta"><time datetime="PT3M45S">3:45</time></span> </li> <li data-audio-favorite> <a href="tracks/03-ending.mp3">03. Ending Theme</a> <span class="track-meta"><time datetime="PT4M12S">4:12</time></span> </li> </ol> </details></audio-player>
Shuffle and Loop
With shuffle, the next track is picked at random from the tracks not yet played in the current cycle, so a track never plays twice in a row. Without loop playback stops once every track has played; with loop a new shuffled cycle starts. Clicking a track counts it as played for the cycle, and toggling shuffle starts a fresh cycle.
12345678910111213
<audio-player shuffle loop> <audio controls> <source src="tracks/01.mp3" type="audio/mpeg"> </audio> <details open> <summary>Shuffle Playlist</summary> <ol class="track-list"> <li data-audio-active><a href="tracks/01.mp3">Track 1</a></li> <li><a href="tracks/02.mp3">Track 2</a></li> <li><a href="tracks/03.mp3">Track 3</a></li> </ol> </details></audio-player>
Attributes
| Attribute | Type | Description |
autoplay | Boolean | Start playing on load (subject to browser autoplay policy) |
loop | Boolean | Loop the track (single-track mode sets the native audio.loop) or wrap the playlist back to the first track; with shuffle, start a new shuffled cycle |
shuffle | Boolean | Advance to a random unplayed track (once per cycle, never twice in a row) |
Reflected state
Set by the component — read them, style with them, but don't author them.
| Attribute | Values | Description |
state | playing | paused | ended | Current playback state |
muted | Boolean | Mirrors the inner audio.muted |
data-state | error | The audio (or every <source>) failed to load. The player shows a visible message, announced politely to screen readers. Cleared when a source loads |
The native controls attribute belongs on the inner <audio>: it is the no-JS fallback. The component removes it while upgraded and restores it if the player is removed from the page.
Properties
| Property | Type | Description |
src | String | Get or set the audio source. Setting it loads the new source, refreshes the title, moves data-audio-active to a matching track link (if any) and fires audio-player:src-changed |
currentTime | Number | Playback position in seconds |
CSS Custom Properties
| Property | Default | Description |
--audio-player-accent | var(--color-primary) | Play button, slider thumbs, and progress fill color |
--audio-player-bg | var(--color-surface) | Player background and slider thumb border color |
--audio-player-radius | var(--radius-m) | Player border radius |
--audio-player-text | var(--color-text) | Track title and controls text color |
--audio-player-border | var(--color-border) | Player border and slider track color |
--audio-player-shadow | none | Player box shadow (opt-in) |
--audio-player-padding | var(--size-xs) var(--size-s) | Controls area padding |
123456789
audio-player { --audio-player-accent: oklch(60% 0.2 30); --audio-player-bg: oklch(15% 0.02 260); --audio-player-text: oklch(95% 0 0); --audio-player-border: oklch(30% 0.02 260); --audio-player-shadow: var(--shadow-m); --audio-player-radius: var(--radius-l); --audio-player-padding: var(--size-s) var(--size-m);}
Dark Mode
The player adapts automatically to light and dark mode via design tokens. All colors resolve through CSS custom properties that respond to theme changes. Set data-mode="dark" on the player or any ancestor (such as the <section> it sits in), or let the system preference apply.
12345
<audio-player data-mode="dark"> <audio controls> <source src="track.mp3" type="audio/mpeg"> </audio></audio-player>
Elevated Card Style
Opt in to a shadow with --audio-player-shadow for a raised card look. Set the properties in your stylesheet on a class:
1234
.player-elevated { --audio-player-shadow: var(--shadow-m); --audio-player-padding: var(--size-s) var(--size-m);}
12345
<audio-player class="player-elevated"> <audio controls> <source src="track.mp3" type="audio/mpeg"> </audio></audio-player>
Shadow Parts
| Part | Description |
player | Outer player container |
controls | Controls row |
play-button | Play/pause button |
timeline | Seek range input |
volume | Volume range input |
track-info | Track title and time display |
error | Load-error message (hidden unless data-state="error") |
Events
| Event | Detail |
audio-player:play | { currentTime, src } |
audio-player:pause | { currentTime } |
audio-player:ended | { src } |
audio-player:track-change | { src, title } |
audio-player:src-changed | { src, source: 'property' } |
123456789
const player = document.querySelector('audio-player'); player.addEventListener('audio-player:play', (e) => { console.log('Playing:', e.detail.src);}); player.addEventListener('audio-player:track-change', (e) => { console.log('Now playing:', e.detail.title);});
Keyboard Shortcuts
| Key | Action |
| Space | Play / Pause |
| Left Arrow | Seek back 10 seconds |
| Right Arrow | Seek forward 10 seconds |
| M | Toggle mute |
Shortcuts apply when focus is anywhere inside the player. The player itself is not a tab stop: it is exposed as a group named “Audio player” with aria-keyshortcuts, and Tab moves straight to its controls. A key that the focused control uses itself is left to that control: Space on the play or mute button or the playlist <summary> activates only that control, and the arrow keys on the seek or volume slider adjust only that slider. Every other shortcut still works from those controls, so M mutes from the seek slider and the arrow keys seek from the play button. Keys typed in a text field, <select> or <textarea> placed inside the player are never treated as shortcuts.
Progressive Enhancement
The <audio> element lives in light DOM. Before JS loads, native controls render. After the component upgrades, native controls are hidden and custom chrome takes over. If the component is removed from the DOM, native controls are restored.
12345678
<audio-player> <audio controls> <source src="track.mp3" type="audio/mpeg"> <p><a href="track.mp3" download>Download track</a></p> </audio></audio-player>
Track List Data Attributes
<li> elements in .track-list use data attributes for state:
| Attribute | Set by | Meaning |
data-audio-active | Component + author | Currently loaded/playing track |
data-audio-played | Component | Track has been played this session |
data-audio-favorite | Author | Editorially marked as a highlight |
Not using the component? Put a native <audio controls> and the same track list in a container with the data-audio-playlist attribute. The playlist utility gives it the same click-to-play and advance-on-ended behavior (without shuffle or list looping). See the native audio page.
Accessibility
- All controls have
aria-label attributes
- The host has
role="group" and the label “Audio player” (author-supplied role, aria-label or aria-labelledby are kept)
- The seek slider announces the position as text, e.g. “1:05 of 3:20” (
aria-valuetext), updated during playback and seeking
- The mute button keeps the stable label “Mute” and reports its state with
aria-pressed
- Load failures show a visible message in a
role="status" region, so screen readers announce it politely
- Keyboard navigable: every control is a native focusable element, the host adds no extra tab stop, and the shortcuts work from any control without overriding that control's own keys
- Respects
prefers-reduced-motion
- Falls back to fully accessible native
<audio> without JS
Companion: audio-visualizer
Pair with <audio-visualizer> for canvas-based frequency or waveform display. The visualizer is a separate component — it connects to any <audio> via the for attribute.