The page's own controls over a native audio or video element.
On this page
A player puts controls of the page's own over a native <audio> or <video>. The media stays the element that plays: it keeps its semantics, its place in the browser's media controls and its controls attribute, which is the fallback. The playerbehavior takes that attribute away as it attaches, and from then on mirrors the media into the row: the play button is pressed while it plays, the seek track is the slider's own range input with what has loaded drawn under what has played, and the times are small tabular figures that keep their width.
player-video makes the player the frame of a video. The picture fills a dark field with a surface's corner and the same row stands over its lower edge, on a shade that rises from the edge. The row is drawn in the dark scheme in either theme, as a screen is dark. A video adds three controls, each shown only where it can work: captions when the video has a captions or subtitles track, picture in picture where the browser has it, full screen where the browser allows it. A click on the picture plays and pauses it, and a double click fills the screen.
While the video plays and the pointer has been still for two and a half seconds, or has left the frame, the row fades out of the picture and the pointer with it. Any movement brings it back. It never fades while a control has keyboard focus or the speed menu is open, and never for a reader who asks for reduced motion. On a touch screen, a tap on a picture whose controls have faded only brings them back.
Play it and keep the pointer still: the row fades. Turn the captions on with the button or C.
Set --player-ratio to the video's shape so the frame holds it before the video loads. The row answers the room the player is given: narrower than 26rem the volume slider leaves it and the mute button stays, and narrower than 24rem picture in picture and the time so far leave too, so a phone keeps every control with the 44px target a finger needs.
Until the behavior attaches, the media keeps its controls attribute and the stylesheet hides the player's row, so the recording plays with the browser's own controls. This is the same markup as the first example, with no script.
player takes the root, the element with the player class, and returns the function that detaches it and gives the media its controls back.
app.js
import{ player }from'@master/ui/behaviors/player.js'const detach =player(document.querySelector('.player'))
The media is the one source of truth. The behavior listens to it and mirrors what it says into attributes the stylesheet reads, and every control asks the media to change rather than changing itself, so a recording played from elsewhere (the system's media keys, another script) is drawn the same.
Toggles. The play button is aria-pressed="true" while the media plays, the mute button while it is muted, the captions button while the first captions or subtitles track is showing, the picture-in-picture and full screen buttons while the video is in either. A toggle keeps one name, Play or Mute, and its aria-pressed says the state. With a player-off and a player-on face it shows the one for its state; with one face it sinks into a well while it is on.
Time. The seek track's max is the duration and its value the current time, with an aria-valuetext in words. The elapsed time and the duration are written into their parts, with hours once the recording is an hour long. While the reader drags the track, the media does not move the thumb under the finger.
Loaded. What has loaded around the current time is --player-buffered on the root, from 0 to 1, drawn on the track under what has played.
Volume and speed. The volume slider holds the volume, and keeps it, a step quieter, while the media is muted; it is hidden on a device whose volume only its own buttons set. The speed button opens the menu its popovertarget names; each menu-input radio in it is a rate, and the behavior writes the rate on the button and checks its radio.
Captions. Turned off, a track is hidden, not disabled, so a transcript that reads the same track keeps its cues.
Full screen is the player's, not the video's, so the row stays over the picture. On an iPhone, which lets only a video fill the screen, it is the system's own player.
The value. The function it returns has a value, the current time in seconds; set it to seek.
Option
Default
step
5
Seconds the arrow keys seek by
jump
10
Seconds J and L seek by
volumeStep
0.1
How much ArrowUp and ArrowDown change the volume
idle
2500
Milliseconds of a still pointer before a playing video's controls fade
valueText
English
The seek track's aria-valuetext, from the time and the duration, in the page's language
The keys work wherever focus is in the player, and after a click on a video's picture. A key a control needs stays the control's: Space presses a focused button, the arrows move the volume slider, and the speed menu has the keys of a menu.
Keys
Action
Space, K
Play or pause. Space on a button presses that button instead
ArrowLeft, ArrowRight
Seek back or forward 5 seconds; on the seek track ArrowDown and ArrowUp do the same. The two change places in a right-to-left player. On the volume slider, the arrows set the volume as the native input does
J, L
Seek back or forward 10 seconds
ArrowUp, ArrowDown
Turn the volume up or down a tenth, away from the seek track and the volume slider
M
Mute or unmute
C
Turn the captions on or off, when the video has a captions or subtitles track
F
Enter or leave full screen, for a video where the browser allows it
Home, End
Go to the start or the end. On the volume slider they set the least and the most volume
Give the root role="group" and an aria-label that names the recording, so the controls are heard as the controls of that recording.
Name every button for what it does, in the page's language, and keep the name when the state changes: the state is aria-pressed.
A video with speech needs captions: add a <track kind="captions">. A recording without a picture needs a transcript.
Never start a recording with sound by itself. The player plays when the reader asks.
The buttons are 32px at every pointer; on touch screens a target reaches 44px above and below each. The times keep their width as the digits change, so the track does not shift while it plays.
The player component renders the media and its row from src, video, poster and tracks, attaches the behavior, and passes the current time and whether it plays in and out: time and playing with onTimeChange and onPlayingChange in React, bindable props in Svelte, v-model:time and v-model:playing in Vue. A time set from outside seeks; playing plays and pauses. Copy it into the project with npx master-ui add player; React, Svelte and Vue says how the components are built and used.
ui/player.tsx
'use client'import{ useEffect, useId, useRef, useState,type ComponentProps }from'react'import{ clock, player }from'@master/ui/behaviors/player.js'import{ useBehavior }from'./use-behavior'exportinterfacePlayerTrack{src:string/** Captions carry the sounds as well as the words; subtitles only the words. */kind?:'captions'|'subtitles'srcLang:stringlabel:stringdefault?:boolean}exportinterfacePlayerPropsextendsOmit<ComponentProps<'div'>,'children'|'ref'>{/** The recording. */src:string/** A video: the frame, with the controls over its lower edge. Without it, an audio player. */video?:boolean/** The picture a video shows before it plays. */poster?:string/** Text tracks of a video; the first captions or subtitles track is what the captions button turns on. */tracks?:PlayerTrack[]/** What the recording is: the name of the player. */label:string/** The current time in seconds, when this component is told it. Setting it seeks. */time?:numberdefaultTime?:number/** Called as the time moves, while it plays and when the reader seeks. */onTimeChange?:(time:number)=>void/** Whether it plays, when this component is told it. */playing?:boolean/** Called when it starts or stops playing, by the reader or by the browser. */onPlayingChange?:(playing:boolean)=>void/** The speeds of the speed menu. */rates?:number[]/** The names of the controls, in the page's language. */labels?:Partial<typeof names>/** What the seek track says for a time, in the page's language: its `aria-valuetext`. */valueText?:(time:number, duration:number)=>string/** What the speed button says for a rate. */rateText?:(rate:number)=>string}/** The behavior finds its media as it attaches. Audio and video are two elements, so it attaches again when `video` changes. */constseen=(root:HTMLElement, options?:{video?:boolean,valueText?:(time:number, duration:number)=>string,rateText?:(rate:number)=>string})=>player(root,{...(options?.valueText&&{valueText: options.valueText}),...(options?.rateText&&{rateText: options.rateText})})const names ={play:'Play',seek:'Seek',mute:'Mute',volume:'Volume',speed:'Playback speed',captions:'Captions',pip:'Picture in picture',fullscreen:'Full screen'}/** A recording with the page's own controls over the native element, which keeps its controls until the behavior attaches. */exportfunctionPlayer({ src, video, poster, tracks =[], label, time, defaultTime =0, onTimeChange, playing, onPlayingChange, rates =[0.5,0.75,1,1.25,1.5,2], labels, valueText, rateText, className,...root }:PlayerProps){const name ={...names,...labels }const speeds =`${useId()}-speeds`const[first]=useState(time ?? defaultTime)const media =useRef<HTMLVideoElement&HTMLAudioElement>(null)const ref =useBehavior(seen,{ video, valueText, rateText })const live =useRef({ onTimeChange, onPlayingChange }) live.current={ onTimeChange, onPlayingChange }/** What the owner and this component last agreed on: what was said, either way, is not said again. */const said =useRef({time: time ?? defaultTime, playing })// What the media does comes out: its time and whether it plays. What the owner set is not said back.useEffect(()=>{const element = media.currentif(!element)returnconst controller =newAbortController()consttell=()=>{const now = element.currentTimeif(now === said.current.time)return said.current.time= now live.current.onTimeChange?.(now)}consttold=()=>{const now =!element.pausedif(now === said.current.playing)return said.current.playing= now live.current.onPlayingChange?.(now)}for(const type of['timeupdate','seeked']) element.addEventListener(type, tell,{signal: controller.signal})for(const type of['play','pause','ended']) element.addEventListener(type, told,{signal: controller.signal})if(first && element.currentTime!== first) element.currentTime= firstreturn()=> controller.abort()// The media is another element once `video` changes: its events are heard on the one that is there.},[first, video])// What the owner sets goes in: a time a quarter second or more from the media's seeks, and playing plays.useEffect(()=>{const element = media.currentif(time ===undefined)return said.current.time= timeif(element && Math.abs(element.currentTime- time)>=0.25) element.currentTime= time},[time])useEffect(()=>{const element = media.currentif(playing ===undefined)return said.current.playing= playingif(!element || playing ===!element.paused)returnif(playing) element.play()?.catch?.(()=>{})else element.pause()},[playing])const Media = video ?'video':'audio'const rate = rates.includes(1)?1: rates[0]return(<divclassName={['player', video &&'player-video', className ].filter(Boolean).join(' ')}role="group"aria-label={label}ref={ref}{...root}><MediaclassName="player-media"src={src}poster={video ? poster :undefined}controlspreload="metadata"playsInline={video ||undefined}ref={media}>{video && tracks.map(track =><trackkey={track.src}src={track.src}kind={track.kind??'captions'}srcLang={track.srcLang}label={track.label}default={track.default}/>)}</Media><divclassName="player-controls"><buttontype="button"className="player-button player-play"aria-label={name.play}aria-pressed="false"><svgclassName="icon player-off"viewBox="0 0 16 16"fill="none"stroke="currentColor"strokeWidth="1"strokeLinecap="round"strokeLinejoin="round"aria-hidden="true"><pathd="M6.1664 3.8652L11.8319 7.1338A1 1 0 0 1 11.8319 8.8662L6.1664 12.1348A1 1 0 0 1 4.6667 11.2686L4.6667 4.7314A1 1 0 0 1 6.1664 3.8652Z"/></svg><svgclassName="icon player-on"viewBox="0 0 16 16"fill="none"stroke="currentColor"strokeWidth="1"strokeLinecap="round"strokeLinejoin="round"aria-hidden="true"><rectx="4"y="3"width="2.6667"height="10"rx="0.8"/><rectx="9.3333"y="3"width="2.6667"height="10"rx="0.8"/></svg></button><spanclassName="player-elapsed">{clock(first)}</span><inputtype="range"className="slider player-seek"min={0}max={0}step="any"defaultValue={first}aria-label={name.seek}/><spanclassName="player-duration">{clock(0)}</span><buttontype="button"className="player-button player-mute"aria-label={name.mute}aria-pressed="false"><svgclassName="icon player-off"viewBox="0 0 16 16"fill="none"stroke="currentColor"strokeWidth="1"strokeLinecap="round"strokeLinejoin="round"aria-hidden="true"><pathd="M2.6 6L4.4365 6A0.6 0.6 0 0 0 4.8378 5.846L6.9986 3.9013A0.6 0.6 0 0 1 8 4.3472L8 11.6528A0.6 0.6 0 0 1 6.9986 12.0987L4.8378 10.154A0.6 0.6 0 0 0 4.4365 10L2.6 10A0.6 0.6 0 0 1 2 9.4L2 6.6A0.6 0.6 0 0 1 2.6 6Z"/><pathd="M10.3333 6C11.3333 7 11.3333 9 10.3333 10"/><pathd="M12.3333 3.6667C14.6667 6 14.6667 10 12.3333 12.3333"/></svg><svgclassName="icon player-on"viewBox="0 0 16 16"fill="none"stroke="currentColor"strokeWidth="1"strokeLinecap="round"strokeLinejoin="round"aria-hidden="true"><pathd="M2.6 6L4.4365 6A0.6 0.6 0 0 0 4.8378 5.846L6.9986 3.9013A0.6 0.6 0 0 1 8 4.3472L8 11.6528A0.6 0.6 0 0 1 6.9986 12.0987L4.8378 10.154A0.6 0.6 0 0 0 4.4365 10L2.6 10A0.6 0.6 0 0 1 2 9.4L2 6.6A0.6 0.6 0 0 1 2.6 6Z"/><pathd="M11 6.3333L14.3333 9.6667"/><pathd="M14.3333 6.3333L11 9.6667"/></svg></button><inputtype="range"className="slider player-volume"min={0}max={1}step={0.05}defaultValue={1}aria-label={name.volume}/><buttontype="button"className="player-button player-speed"popoverTarget={speeds}aria-label={name.speed}>{`${rate}×`}</button><divid={speeds}popover="auto"className="menu menu-top menu-end">{rates.map(each =>(<labelclassName="menu-item"key={each}><inputclassName="menu-input"type="radio"name={speeds}value={each}defaultChecked={each === rate}/>{`${each}×`}</label>))}</div>{video && tracks.length>0&&(<buttontype="button"className="player-button player-captions"aria-label={name.captions}aria-pressed="false"><svgclassName="icon"viewBox="0 0 16 16"fill="none"stroke="currentColor"strokeWidth="1"strokeLinecap="round"strokeLinejoin="round"aria-hidden="true"><rectx="2"y="3"width="12"height="10"rx="1"/><pathd="M4.6667 6.6667L6 6.6667"/><pathd="M8.6667 6.6667L11.3333 6.6667"/><pathd="M4.6667 9.6667L7.3333 9.6667"/><pathd="M10 9.6667L11.3333 9.6667"/></svg></button>)}{video &&(<buttontype="button"className="player-button player-pip"aria-label={name.pip}aria-pressed="false"><svgclassName="icon"viewBox="0 0 16 16"fill="none"stroke="currentColor"strokeWidth="1"strokeLinecap="round"strokeLinejoin="round"aria-hidden="true"><pathd="M5.5 12L2.3333 12A1 1 0 0 1 1.3333 11L1.3333 3.6667A1 1 0 0 1 2.3333 2.6667L13 2.6667A1 1 0 0 1 14 3.6667L14 6.1667"/><rectx="8"y="8.6667"width="6.6667"height="4.6667"rx="1"/></svg></button>)}{video &&(<buttontype="button"className="player-button player-fullscreen"aria-label={name.fullscreen}aria-pressed="false"><svgclassName="icon player-off"viewBox="0 0 16 16"fill="none"stroke="currentColor"strokeWidth="1"strokeLinecap="round"strokeLinejoin="round"aria-hidden="true"><pathd="M2.6667 6L2.6667 3.6667A1 1 0 0 1 3.6667 2.6667L6 2.6667"/><pathd="M10 2.6667L12.3333 2.6667A1 1 0 0 1 13.3333 3.6667L13.3333 6"/><pathd="M13.3333 10L13.3333 12.3333A1 1 0 0 1 12.3333 13.3333L10 13.3333"/><pathd="M6 13.3333L3.6667 13.3333A1 1 0 0 1 2.6667 12.3333L2.6667 10"/></svg><svgclassName="icon player-on"viewBox="0 0 16 16"fill="none"stroke="currentColor"strokeWidth="1"strokeLinecap="round"strokeLinejoin="round"aria-hidden="true"><pathd="M2.6667 6L5 6A1 1 0 0 0 6 5L6 2.6667"/><pathd="M10 2.6667L10 5A1 1 0 0 0 11 6L13.3333 6"/><pathd="M13.3333 10L11 10A1 1 0 0 0 10 11L10 13.3333"/><pathd="M6 13.3333L6 11A1 1 0 0 0 5 10L2.6667 10"/></svg></button>)}</div></div>)}
ui/Player.svelte
<scriptlang="ts"module>importtype{ HTMLAttributes }from'svelte/elements'exportinterfacePlayerTrack{src:string/** Captions carry the sounds as well as the words; subtitles only the words. */kind?:'captions'|'subtitles'srclang:stringlabel:stringdefault?:boolean}exportinterfacePlayerLabels{play:stringseek:stringmute:stringvolume:stringspeed:stringcaptions:stringpip:stringfullscreen:string}exportinterfacePlayerPropsextendsOmit<HTMLAttributes<HTMLDivElement>,'children'>{/** The recording. */src:string/** A video: the frame, with the controls over its lower edge. Without it, an audio player. */video?:boolean/** The picture a video shows before it plays. */poster?:string/** Text tracks of a video; the first captions or subtitles track is what the captions button turns on. */tracks?:PlayerTrack[]/** What the recording is: the name of the player. */label:string/** The current time in seconds. Bind it, or leave it out: it starts at 0. Setting it seeks. */time?:numberonTimeChange?:(time:number)=>void/** Whether it plays. Bind it to play and pause from outside. */playing?:booleanonPlayingChange?:(playing:boolean)=>void/** The speeds of the speed menu. */rates?:number[]/** The names of the controls, in the page's language. */labels?:Partial<PlayerLabels>/** What the seek track says for a time, in the page's language: its `aria-valuetext`. */valueText?:(time:number, duration:number)=>string/** What the speed button says for a rate. */rateText?:(rate:number)=>string}</script><scriptlang="ts">import{ untrack }from'svelte'import{ clock, player }from'@master/ui/behaviors/player.js'import{ behavior }from'./behavior.svelte.js'let{ src, video, poster, tracks =[], label, time =$bindable(), onTimeChange, playing =$bindable(), onPlayingChange, rates =[0.5,0.75,1,1.25,1.5,2], labels, valueText, rateText,class: className,...root }:PlayerProps=$props()const name =$derived({play:'Play',seek:'Seek',mute:'Mute',volume:'Volume',speed:'Playback speed',captions:'Captions',pip:'Picture in picture',fullscreen:'Full screen',...labels })const id =$props.id()const speeds =`${id}-speeds`const first =untrack(()=> time)??0const rate =$derived(rates.includes(1)?1: rates[0])let media =$state<HTMLMediaElement>()// The behavior finds its media as it attaches. Audio and video are two elements, so it attaches again when `video` changes.constseen=(element:HTMLElement, options?:{video?:boolean,valueText?:(time:number, duration:number)=>string,rateText?:(rate:number)=>string})=>player(element,{...(options?.valueText&&{valueText: options.valueText}),...(options?.rateText&&{rateText: options.rateText})})const attach =behavior(seen,()=>({ video, valueText, rateText }))// What the owner sets goes in: a time a quarter second or more from the media's seeks, and playing plays.$effect(()=>{const at = timeif(media && at !==undefined&& Math.abs(media.currentTime- at)>=0.25) media.currentTime= at})$effect(()=>{const play = playingif(!media || play ===undefined|| play ===!media.paused)returnif(play) media.play()?.catch?.(()=>{})else media.pause()})// What the media does comes out: its time and whether it plays. What the owner set is not said back.consttell=()=>{const now = media!.currentTimeif(now === time)return time = nowonTimeChange?.(now)}consttold=()=>{const now =!media!.pausedif(now === playing)return playing = nowonPlayingChange?.(now)}</script>{#snippetcontrols()}<divclass="player-controls"><buttontype="button"class="player-button player-play"aria-label={name.play}aria-pressed="false"><svgclass="icon player-off"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><pathd="M6.1664 3.8652L11.8319 7.1338A1 1 0 0 1 11.8319 8.8662L6.1664 12.1348A1 1 0 0 1 4.6667 11.2686L4.6667 4.7314A1 1 0 0 1 6.1664 3.8652Z"/></svg><svgclass="icon player-on"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><rectx="4"y="3"width="2.6667"height="10"rx="0.8"/><rectx="9.3333"y="3"width="2.6667"height="10"rx="0.8"/></svg></button><spanclass="player-elapsed">{clock(first)}</span><inputtype="range"class="slider player-seek"min="0"max="0"step="any"value={first}aria-label={name.seek}/><spanclass="player-duration">{clock(0)}</span><buttontype="button"class="player-button player-mute"aria-label={name.mute}aria-pressed="false"><svgclass="icon player-off"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><pathd="M2.6 6L4.4365 6A0.6 0.6 0 0 0 4.8378 5.846L6.9986 3.9013A0.6 0.6 0 0 1 8 4.3472L8 11.6528A0.6 0.6 0 0 1 6.9986 12.0987L4.8378 10.154A0.6 0.6 0 0 0 4.4365 10L2.6 10A0.6 0.6 0 0 1 2 9.4L2 6.6A0.6 0.6 0 0 1 2.6 6Z"/><pathd="M10.3333 6C11.3333 7 11.3333 9 10.3333 10"/><pathd="M12.3333 3.6667C14.6667 6 14.6667 10 12.3333 12.3333"/></svg><svgclass="icon player-on"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><pathd="M2.6 6L4.4365 6A0.6 0.6 0 0 0 4.8378 5.846L6.9986 3.9013A0.6 0.6 0 0 1 8 4.3472L8 11.6528A0.6 0.6 0 0 1 6.9986 12.0987L4.8378 10.154A0.6 0.6 0 0 0 4.4365 10L2.6 10A0.6 0.6 0 0 1 2 9.4L2 6.6A0.6 0.6 0 0 1 2.6 6Z"/><pathd="M11 6.3333L14.3333 9.6667"/><pathd="M14.3333 6.3333L11 9.6667"/></svg></button><inputtype="range"class="slider player-volume"min="0"max="1"step="0.05"value="1"aria-label={name.volume}/><buttontype="button"class="player-button player-speed"popovertarget={speeds}aria-label={name.speed}>{rate}×</button><divid={speeds}popover="auto"class="menu menu-top menu-end">{#each rates as each (each)}<labelclass="menu-item"><inputclass="menu-input"type="radio"name={speeds}value={each}checked={each === rate}/>{each}×</label>{/each}</div>{#if video && tracks.length}<buttontype="button"class="player-button player-captions"aria-label={name.captions}aria-pressed="false"><svgclass="icon"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><rectx="2"y="3"width="12"height="10"rx="1"/><pathd="M4.6667 6.6667L6 6.6667"/><pathd="M8.6667 6.6667L11.3333 6.6667"/><pathd="M4.6667 9.6667L7.3333 9.6667"/><pathd="M10 9.6667L11.3333 9.6667"/></svg></button>{/if}{#if video}<buttontype="button"class="player-button player-pip"aria-label={name.pip}aria-pressed="false"><svgclass="icon"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><pathd="M5.5 12L2.3333 12A1 1 0 0 1 1.3333 11L1.3333 3.6667A1 1 0 0 1 2.3333 2.6667L13 2.6667A1 1 0 0 1 14 3.6667L14 6.1667"/><rectx="8"y="8.6667"width="6.6667"height="4.6667"rx="1"/></svg></button><buttontype="button"class="player-button player-fullscreen"aria-label={name.fullscreen}aria-pressed="false"><svgclass="icon player-off"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><pathd="M2.6667 6L2.6667 3.6667A1 1 0 0 1 3.6667 2.6667L6 2.6667"/><pathd="M10 2.6667L12.3333 2.6667A1 1 0 0 1 13.3333 3.6667L13.3333 6"/><pathd="M13.3333 10L13.3333 12.3333A1 1 0 0 1 12.3333 13.3333L10 13.3333"/><pathd="M6 13.3333L3.6667 13.3333A1 1 0 0 1 2.6667 12.3333L2.6667 10"/></svg><svgclass="icon player-on"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><pathd="M2.6667 6L5 6A1 1 0 0 0 6 5L6 2.6667"/><pathd="M10 2.6667L10 5A1 1 0 0 0 11 6L13.3333 6"/><pathd="M13.3333 10L11 10A1 1 0 0 0 10 11L10 13.3333"/><pathd="M6 13.3333L6 11A1 1 0 0 0 5 10L2.6667 10"/></svg></button>{/if}</div>{/snippet}<!--@component A recording with the page's own controls over the native element, which keeps its controls until the behavior attaches.--><divclass={['player', video &&'player-video', className]}role="group"aria-label={label}{@attach attach}{...root}>{#if video}<!-- The captions are the page's to give, in `tracks`; a video without speech needs none. --><!-- svelte-ignore a11y_media_has_caption --><videoclass="player-media"{src}{poster}controlspreload="metadata"playsinlinebind:this={media}ontimeupdate={tell}onseeked={tell}onplay={told}onpause={told}onended={told}>{#each tracks as track (track.src)}<tracksrc={track.src}kind={track.kind??'captions'}srclang={track.srclang}label={track.label}default={track.default}/>{/each}</video>{:else}<audioclass="player-media"{src}controlspreload="metadata"bind:this={media}ontimeupdate={tell}onseeked={tell}onplay={told}onpause={told}onended={told}></audio>{/if}{@rendercontrols()}</div>
ui/Player.vue
<scriptsetuplang="ts">import{ computed, onMounted, useId, useTemplateRef, watch }from'vue'import{ clock, player }from'@master/ui/behaviors/player.js'import{ useBehavior }from'./use-behavior'defineOptions({inheritAttrs:false})exportinterfacePlayerTrack{src:string/** Captions carry the sounds as well as the words; subtitles only the words. */kind?:'captions'|'subtitles'srclang:stringlabel:stringdefault?:boolean}const{ video, tracks =[], rates =[0.5,0.75,1,1.25,1.5,2], labels, valueText, rateText }=defineProps<{/** The recording. */src:string/** A video: the frame, with the controls over its lower edge. Without it, an audio player. */video?:boolean/** The picture a video shows before it plays. */poster?:string/** Text tracks of a video; the first captions or subtitles track is what the captions button turns on. */tracks?:PlayerTrack[]/** What the recording is: the name of the player. */label:string/** The speeds of the speed menu. */rates?:number[]/** The names of the controls, in the page's language. */labels?:Partial<Record<'play'|'seek'|'mute'|'volume'|'speed'|'captions'|'pip'|'fullscreen',string>>/** What the seek track says for a time, in the page's language: its `aria-valuetext`. */valueText?:(time:number, duration:number)=>string/** What the speed button says for a rate. */rateText?:(rate:number)=>string}>()/** The current time in seconds: `v-model:time`. It moves as the media plays; setting it seeks. */const time =defineModel<number>('time')/** Whether it plays: `v-model:playing`. */const playing =defineModel<boolean>('playing')/* A recording with the page's own controls over the native element, which keeps its controls until the behavior attaches. */const name =computed(()=>({play:'Play',seek:'Seek',mute:'Mute',volume:'Volume',speed:'Playback speed',captions:'Captions',pip:'Picture in picture',fullscreen:'Full screen',...labels }))const speeds =`${useId()}-speeds`const first = time.value??0const rate =computed(()=>(rates.includes(1)?1: rates[0]))const root =useTemplateRef<HTMLDivElement>('root')const media =useTemplateRef<HTMLMediaElement>('media')// The behavior finds its media as it attaches. Audio and video are two elements, so it attaches again when `video` changes.constseen=(element:HTMLElement, options?:{video?:boolean,valueText?:(time:number, duration:number)=>string,rateText?:(rate:number)=>string})=>player(element,{...(options?.valueText&&{valueText: options.valueText}),...(options?.rateText&&{rateText: options.rateText})})useBehavior(root, seen,()=>({ video, valueText, rateText }))// What the owner sets goes in: a time a quarter second or more from the media's seeks, and playing plays./** What the owner and this component last agreed on: what was said, either way, is not said again. */const said:{time?:number,playing?:boolean}={time: time.value,playing: playing.value}constseek=()=>{if(time.value===undefined)return said.time= time.valueif(media.value&& Math.abs(media.value.currentTime- time.value)>=0.25) media.value.currentTime= time.value}constplay=()=>{const element = media.valueif(playing.value===undefined)return said.playing= playing.valueif(!element || playing.value===!element.paused)returnif(playing.value) element.play()?.catch?.(()=>{})else element.pause()}onMounted(()=>{seek();play()})watch(time, seek,{flush:'post'})watch(playing, play,{flush:'post'})// What the media does comes out: its time and whether it plays. What the owner set is not said back.consttell=()=>{if(!media.value|| media.value.currentTime=== said.time)return said.time= media.value.currentTime time.value= said.time}consttold=()=>{if(!media.value||!media.value.paused=== said.playing)return said.playing=!media.value.paused playing.value= said.playing}</script><template><divclass="player":class="{'player-video': video }"role="group":aria-label="label"ref="root"v-bind="$attrs"><videov-if="video"class="player-media"ref="media":src="src":poster="poster"controlspreload="metadata"playsinline@timeupdate="tell"@seeked="tell"@play="told"@pause="told"@ended="told"><trackv-for="track in tracks":key="track.src":src="track.src":kind="track.kind??'captions'":srclang="track.srclang":label="track.label":default="track.default"></video><audiov-elseclass="player-media"ref="media":src="src"controlspreload="metadata"@timeupdate="tell"@seeked="tell"@play="told"@pause="told"@ended="told" /><divclass="player-controls"><buttontype="button"class="player-button player-play":aria-label="name.play"aria-pressed="false"><svgclass="icon player-off"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><pathd="M6.1664 3.8652L11.8319 7.1338A1 1 0 0 1 11.8319 8.8662L6.1664 12.1348A1 1 0 0 1 4.6667 11.2686L4.6667 4.7314A1 1 0 0 1 6.1664 3.8652Z"/></svg><svgclass="icon player-on"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><rectx="4"y="3"width="2.6667"height="10"rx="0.8"/><rectx="9.3333"y="3"width="2.6667"height="10"rx="0.8"/></svg></button><spanclass="player-elapsed">{{clock(first)}}</span><inputtype="range"class="slider player-seek"min="0"max="0"step="any":value.attr="first":aria-label="name.seek"><spanclass="player-duration">{{clock(0)}}</span><buttontype="button"class="player-button player-mute":aria-label="name.mute"aria-pressed="false"><svgclass="icon player-off"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><pathd="M2.6 6L4.4365 6A0.6 0.6 0 0 0 4.8378 5.846L6.9986 3.9013A0.6 0.6 0 0 1 8 4.3472L8 11.6528A0.6 0.6 0 0 1 6.9986 12.0987L4.8378 10.154A0.6 0.6 0 0 0 4.4365 10L2.6 10A0.6 0.6 0 0 1 2 9.4L2 6.6A0.6 0.6 0 0 1 2.6 6Z"/><pathd="M10.3333 6C11.3333 7 11.3333 9 10.3333 10"/><pathd="M12.3333 3.6667C14.6667 6 14.6667 10 12.3333 12.3333"/></svg><svgclass="icon player-on"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><pathd="M2.6 6L4.4365 6A0.6 0.6 0 0 0 4.8378 5.846L6.9986 3.9013A0.6 0.6 0 0 1 8 4.3472L8 11.6528A0.6 0.6 0 0 1 6.9986 12.0987L4.8378 10.154A0.6 0.6 0 0 0 4.4365 10L2.6 10A0.6 0.6 0 0 1 2 9.4L2 6.6A0.6 0.6 0 0 1 2.6 6Z"/><pathd="M11 6.3333L14.3333 9.6667"/><pathd="M14.3333 6.3333L11 9.6667"/></svg></button><inputtype="range"class="slider player-volume"min="0"max="1"step="0.05"value="1":aria-label="name.volume"><buttontype="button"class="player-button player-speed":popovertarget="speeds":aria-label="name.speed">{{ rate }}×</button><div:id="speeds"popover="auto"class="menu menu-top menu-end"><labelv-for="each in rates":key="each"class="menu-item"><inputclass="menu-input"type="radio":name="speeds":value="each":checked="each === rate">{{ each }}×</label></div><buttonv-if="video && tracks.length"type="button"class="player-button player-captions":aria-label="name.captions"aria-pressed="false"><svgclass="icon"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><rectx="2"y="3"width="12"height="10"rx="1"/><pathd="M4.6667 6.6667L6 6.6667"/><pathd="M8.6667 6.6667L11.3333 6.6667"/><pathd="M4.6667 9.6667L7.3333 9.6667"/><pathd="M10 9.6667L11.3333 9.6667"/></svg></button><templatev-if="video"><buttontype="button"class="player-button player-pip":aria-label="name.pip"aria-pressed="false"><svgclass="icon"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><pathd="M5.5 12L2.3333 12A1 1 0 0 1 1.3333 11L1.3333 3.6667A1 1 0 0 1 2.3333 2.6667L13 2.6667A1 1 0 0 1 14 3.6667L14 6.1667"/><rectx="8"y="8.6667"width="6.6667"height="4.6667"rx="1"/></svg></button><buttontype="button"class="player-button player-fullscreen":aria-label="name.fullscreen"aria-pressed="false"><svgclass="icon player-off"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><pathd="M2.6667 6L2.6667 3.6667A1 1 0 0 1 3.6667 2.6667L6 2.6667"/><pathd="M10 2.6667L12.3333 2.6667A1 1 0 0 1 13.3333 3.6667L13.3333 6"/><pathd="M13.3333 10L13.3333 12.3333A1 1 0 0 1 12.3333 13.3333L10 13.3333"/><pathd="M6 13.3333L3.6667 13.3333A1 1 0 0 1 2.6667 12.3333L2.6667 10"/></svg><svgclass="icon player-on"viewBox="0 0 16 16"fill="none"stroke="currentColor"stroke-width="1"stroke-linecap="round"stroke-linejoin="round"aria-hidden="true"><pathd="M2.6667 6L5 6A1 1 0 0 0 6 5L6 2.6667"/><pathd="M10 2.6667L10 5A1 1 0 0 0 11 6L13.3333 6"/><pathd="M13.3333 10L11 10A1 1 0 0 0 10 11L10 13.3333"/><pathd="M6 13.3333L6 11A1 1 0 0 0 5 10L2.6667 10"/></svg></button></template></div></div></template>
The player of an audio recording: a raised sheet holding the row of controls, 6px in. Give it role="group" and an aria-label that names the recording
player-video
The player of a video: the frame itself, a dark field with a surface's corner, with the controls over its lower edge in the dark scheme. They fade while the video plays and the pointer is stillApply with player
player-media
The native audio or video element, with its controls attribute: the fallback the behavior removes as it attaches
player-controls
The row of buttons, times and the seek track. Hidden while the media has its controls attribute
player-button
A quiet 32px control with one icon, with a target 44px tall on touch screens. A toggle has aria-pressed and a fixed name: with player-off and player-on faces it shows the one for its state; with one face it sinks into a well while it is on
player-play
Play and pause, its icon a size up: pressed while the media plays. Name it PlayApply with player-button
player-mute
Sound and silence: pressed while the media is muted. Name it MuteApply with player-button
player-speed
The speed, in figures, that opens a menu of speeds through popovertarget: a menu of label items, each around a radio menu-input whose value is a playback rateApply with player-button
player-captions
Captions on or off: pressed while the first captions or subtitles track is showing. Hidden when the video has noneApply with player-button
player-pip
Picture in picture: pressed while the video plays in its own window. Hidden where the browser has none, and in a player narrower than 24remApply with player-button
player-fullscreen
Full screen: pressed while the player fills the screen. Hidden where the browser does not allow itApply with player-button
player-off
The face of a toggle while it is off: the icon for play, sound, into full screen
player-on
The face of a toggle while it is on: the icon for pause, silence, out of full screen
player-elapsed
How much has played, in small tabular figures, left out of a player narrower than 24rem. The behavior writes it
player-duration
How long the recording is, in small tabular figures. The behavior writes it once the media knows
player-seek
The seek track: a slider as long as the row allows, with what has loaded drawn under what has played. The behavior sets its max, its value and an aria-valuetext in words
player-volume
The volume: a short slider from 0 to 1 beside the mute button, left out where the player is narrower than 26rem and hidden where the device does not let a page set it
The seek track and the volume are slider inputs; the speed menu is a menu of label items with menu-input radios.
The player's corner: a small control's corner plus the inset, so the 32px buttons in its corners are concentric with it, and a surface's corner for a videoSet on player
/* * Player: controls of the page's own over a native <audio> or <video>, which keeps its semantics, its keys in * the browser's menus and its fallback. * The media keeps the controls attribute in the markup. While it has it the browser's controls are what is seen * and the player's are hidden, so a page without the script still plays; the behavior removes the attribute as it * attaches and puts it back as it detaches. * An audio player is one raised sheet: a row of quiet 32px buttons, the times in small tabular figures and a seek * track that is the slider's own native range input, 6px inside a corner of the small control's radius plus that * inset, so a 32px button in a corner of the row is concentric with it. * A video player is the frame itself: the picture covers a dark field with a surface's corner, and the same row * stands over its lower edge on a shade that rises from the edge. That row is drawn in the dark scheme in both * themes, as a screen is dark, so every token it reads resolves to its obsidian value. * State is drawn from attributes. A toggle has aria-pressed: one with two faces (play and pause, sound and * silence, into and out of full screen) shows the face for its state, one with a single face (captions, picture in * picture) sinks into a well while it is on. The behavior mirrors the media into them: the play button is pressed * while the media plays, the mute button while it is muted. What has loaded is --player-buffered, from 0 to 1, * drawn on the seek track under what has played. * Over a video the row fades while the media plays and the pointer has not moved for a while (--player-idle, set * by the behavior). It never fades while anything in the player has keyboard focus or its speed menu is open, and * never for a reader who asks for reduced motion. * The row answers the room the player is given: narrower than 26rem the volume slider steps out and the mute * button stays; narrower than 24rem picture in picture and the time so far step out too. * Without the behavior the media keeps the browser's controls. Without container queries the row keeps every * control at every width. */@layer components.ui {.player{--player-inset:6px;/* The inset is added after the corner scale, so it is passed divided by it. */--player-radius:calc(var(--radius-control-sm)+var(--player-inset)/var(--corner-scale));--player-buffered:0;--player-idle:0;--surface-color:var(--color-surface-raised);position:relative;display:grid;min-width:0;margin:0;padding:var(--player-inset);border-radius:calc(var(--player-radius)*var(--corner-scale));corner-shape:var(--corner-shape);@apply--surface-sheet;color:var(--color-text-body);box-shadow:var(--shadow-raised);container-type: inline-size;/* * The frame of a video. The picture fills it and the controls stand over its lower edge. A corner of a * surface, the dark field around the picture, and an inward hairline so a pale frame does not bleed out. */&.player-video{--player-radius:var(--radius-surface);--surface-color:var(--color-surface-media);padding:0;overflow:hidden;background:var(--color-surface-media);box-shadow:var(--shadow-raised);/* The inward line every picture gets, drawn over the picture and the controls alike. */outline:1pxsolidvar(--color-line-image);outline-offset:-1px;.player-media{grid-area:1/1;width:100%;height:auto;aspect-ratio:var(--player-ratio,auto);object-fit:contain;}.player-controls{grid-area:1/1;align-self:end;/* 8px from the frame's edges, so a corner button's corner is concentric with the frame's 16. */padding:2.5rem.5rem.5rem;background:linear-gradient(totop,var(--color-on-image-scrim),transparent);/* A screen is dark: the controls over it read every token as obsidian, in both themes. */color-scheme:dark;color:var(--color-on-image);transition: opacity var(--duration-base)var(--easing-glide);}/* The times read on any picture: white, with a shade under the figures. */.player-elapsed,.player-duration{color:var(--color-on-image);text-shadow:01px2pxvar(--color-on-image-shade);}}/* Full screen: the frame becomes the screen, square and with no edge. */&:fullscreen{grid-template-rows:minmax(0,1fr);border-radius:0;box-shadow:none;outline:0;.player-media{height:100%;}}/* While the media keeps the browser's controls, they are the controls: the script has not attached. */&:has(.player-media[controls]).player-controls{display:none;}@media(forced-colors:active){outline:1pxsolid CanvasText;outline-offset:-1px;}}/* * While the video plays, the pointer is still and nothing in the player has keyboard focus or an open menu, * the controls fade out of the picture, and the pointer with them. Any movement brings them back at once. */@media(prefers-reduced-motion:no-preference){.player-video:has(.player-play[aria-pressed="true"]):not(:has(:focus-visible,.menu:popover-open)).player-controls{opacity:calc(1-var(--player-idle));}@container style(--player-idle:1){.player-video:has(.player-play[aria-pressed="true"]):not(:has(:focus-visible,.menu:popover-open)).player-media{cursor:none;}}}/* * Captions the browser draws over the picture rise above the controls while they show, and settle to the * edge as they fade. Chromium and WebKit name the box the cues stand in; elsewhere they stay at the edge. */.player-video.player-media::-webkit-media-text-track-container{translate:0calc(-1*var(--player-cue-lift,2.75rem));transition: translate var(--duration-base)var(--easing-glide);}@media(prefers-reduced-motion:no-preference){.player-video:has(.player-play[aria-pressed="true"]):not(:has(:focus-visible,.menu:popover-open)).player-media{--player-cue-lift:calc(2.75rem*(1-var(--player-idle)));}}/* The native element. Audio without controls draws nothing; a video is the picture. */.player-media{display:block;width:100%;max-width:100%;margin:0;border:0;outline:0;}/* The row of controls: buttons, the times and the seek track between them. */.player-controls{display:flex;align-items:center;gap:.25rem;min-width:0;margin:0;}/* * A control of the player: a quiet 32px square with one icon, or the speed as a few figures. It answers the * pointer as a quiet button does, rising onto a sheet; it is never a hue. */.player-button{--icon-size:1rem;position:relative;display:inline-flex;flex:none;align-items:center;justify-content:center;min-width:2rem;height:2rem;margin:0;padding:0;border:0;border-radius:calc(var(--radius-control-sm)*var(--corner-scale));corner-shape:var(--corner-shape);background:transparent;color:inherit;font:var(--font-weight-control).75rem/1var(--font-family-sans);font-feature-settings:inherit;font-variant-numeric:tabular-nums;appearance:none;cursor:pointer;user-select:none;-webkit-user-select:none;-webkit-tap-highlight-color:transparent;touch-action:manipulation;transition-property: box-shadow,color;transition-duration:var(--duration-quick);transition-timing-function:var(--easing-glide);&[hidden]{display:none;}@media(hover:hover){&:hover:not(:disabled){@apply--surface-sheet;color:var(--color-text-strong);box-shadow:var(--shadow-raised);}}/* Held, and a toggle with one face that is on: sunk into a well. */&:active:not(:disabled),&[aria-pressed="true"]:not(:has(.player-on)),&:has(+.menu:popover-open){background:var(--color-surface-inset);color:var(--color-text-strong);box-shadow:var(--shadow-pressed);transition-duration:var(--duration-press);}&:focus-visible{@apply--focus-ring;}&:disabled{color:var(--color-text-disabled);cursor:not-allowed;}/* Controls stand 4px apart, so on touch a target reaches 44px above and below and keeps its width. */@media(pointer:coarse){&::after{@apply--touch-target-block;}}/* * The well of a toggle that is on, or of the speed while its menu is open, is gone with its shadow: a firmer * outline in the highlight keeps it, and gives way to the focus ring, as a quiet button's does. */@media(forced-colors:active){&:is([aria-pressed="true"]:not(:has(.player-on)),:has(+.menu:popover-open)):not(:focus-visible){outline:2pxsolid Highlight;outline-offset:-2px;}&:focus-visible{outline-color: Highlight;}&:disabled{color: GrayText;}}}/* The speed: a few figures, a little wider than an icon. */.player-speed{padding-inline:.5rem;}/* Play is the control a reader looks for first: its icon is a size up. */.player-play{--icon-size:1.125rem;}/* Mute stands close to its volume slider, so the two read as one control. */.player-mute{margin-inline-end:-.125rem;}/* Captions, picture in picture and full screen belong to a picture: an audio player leaves them out. */.player-captions,.player-pip,.player-fullscreen{.player:not(.player-video)&{display:none;}}/* * In a player narrower than 24rem, picture in picture and the time so far step out of the row, the length * stays, and the controls close up. */@container(width<24rem){.player-pip,.player-elapsed{display:none;}.player-controls{gap:0;}}/* Muted, the volume slider keeps the level it will return to, a step quieter. */.player-controls:has(.player-mute[aria-pressed="true"]).player-volume{opacity:.45;}/* * The two faces of a toggle: the one for off (play, sound, into full screen) and the one for on (pause, * silence, out of full screen). Only the face for the button's aria-pressed is shown. */.player-on{display:none;}.player-button[aria-pressed="true"]{.player-on{display:inline-block;}.player-off{display:none;}}/* How much has played, and how long it is: small tabular figures, as wide whatever the digits. */.player-elapsed,.player-duration{flex:none;min-width:2.25rem;margin:0;color:var(--color-text-muted);font-size:.75rem;line-height:1;font-variant-numeric:tabular-nums;text-align:center;white-space:nowrap;}/* * The seek track: the slider's own range input, as long as the row allows. Under what has played, what has * loaded is a quiet share of the same ink, as far as --player-buffered. */.player-seek{/* What has loaded and not played: a quiet share of the fill's own ink, read where the slider sets that ink. */--player-buffer-color:color-mix(in oklab,var(--slider-color,var(--color-surface-inverse))24%,transparent);flex:110;width:auto;min-width:3rem;margin-inline:.25rem;&::-webkit-slider-runnable-track{background:linear-gradient(90deg,var(--slider-color)var(--slider-fill),transparent0),linear-gradient(90deg,var(--player-buffer-color)calc(var(--player-buffered)*100%),transparent0)var(--color-surface-inset);}&::-moz-range-track{background:linear-gradient(90deg,var(--player-buffer-color)calc(var(--player-buffered)*100%),transparent0)var(--color-surface-inset);}&:dir(rtl)::-webkit-slider-runnable-track{background:linear-gradient(270deg,var(--slider-color)var(--slider-fill),transparent0),linear-gradient(270deg,var(--player-buffer-color)calc(var(--player-buffered)*100%),transparent0)var(--color-surface-inset);}&:dir(rtl)::-moz-range-track{background:linear-gradient(270deg,var(--player-buffer-color)calc(var(--player-buffered)*100%),transparent0)var(--color-surface-inset);}@media(forced-colors:active){&::-webkit-slider-runnable-track{background:linear-gradient(90deg, Highlight var(--slider-fill),transparent0) Canvas;}&:dir(rtl)::-webkit-slider-runnable-track{background:linear-gradient(270deg, Highlight var(--slider-fill),transparent0) Canvas;}&::-moz-range-track,&:dir(rtl)::-moz-range-track{background: Canvas;}}}/* The volume: a short slider beside the mute button. Where the player is narrow, the mute button is enough. */.player-volume{flex:none;width:4.5rem;margin-inline:.25rem.5rem;&[hidden]{display:none;}@container(width<26rem){display:none;}}}