0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…
Text-to-speech reader with word-level highlighting. Uses the Web Speech API for synthesis and the CSS Custom Highlight API for word tracking.
Overview
The <text-reader> component attaches a text-to-speech control bar to any element. It uses the Web Speech API for synthesis and the CSS Custom Highlight API for real-time word tracking. Progressive enhancement — the component hides itself if the browser lacks speechSynthesis.
Basic usage
123456789
<text-readerfor="my-article"speed="1"></text-reader><articleid="my-article"><p>The web platform has evolved dramatically over the past decade. Modern browsers now support features that previously required complex JavaScript libraries or plugins.</p><p>Progressive enhancement remains the most resilient approach to building for the web.</p></article>
The reading language comes from the nearest langattribute on the target (falling back to <html lang>). It is set on the utterance, and voices for that language are listed and selected first, so a French article in an English page is read with a French voice.
Localised reader for French content
1234567
<text-readerfor="article-fr"data-label-play="Lire"data-label-pause="Pause"data-label-stop="Arrêter"data-label-reader="Lecteur d'article"data-label-voice="Voix"data-label-speed="Vitesse"></text-reader><articleid="article-fr"lang="fr"><p>Le Web est fondé sur l'amélioration progressive.</p></article>
What Gets Read
Elements matching selectors (default p,li) inside the target, in document order.
Matches inside <pre>, <code> or an aria-hidden="true" / hidden subtree are skipped entirely.
Inside a matched paragraph, text in aria-hidden="true", hidden, <script>, <style> and <template> descendants is skipped, so decorative icons are not spoken.
The text is re-extracted every time playback starts, so content added after load is included.
Literal < and > are replaced with spaces before speaking; speech engines otherwise treat them as SSML and stop silently.
Observed Attributes
These attributes are reactive — changing them at runtime triggers an update:
Restarts speech from the current word at the new rate
voice
Restarts speech from the current word with the new voice
for
Stops current speech, re-resolves target element
JavaScript API
Public methods and properties
1234567891011121314
const reader = document.querySelector('text-reader');reader.play(); // Begin or resume playbackreader.pause(); // Pause mid-sentencereader.resume(); // Resume from pausereader.stop(); // Cancel and reset// Read-only propertiesreader.speaking; // booleanreader.paused; // booleanreader.progress; // 0–1 (charIndex / fullText.length)// Get available voicesconst voices = await reader.voices;
Method / Property
Type
Description
play()
method
Begin playback (or resume if paused)
pause()
method
Pause mid-sentence
resume()
method
Resume from paused state
stop()
method
Cancel speech and reset position
voices
Promise<SpeechSynthesisVoice[]>
Available TTS voices
speaking
boolean
Whether speech is currently active
paused
boolean
Whether speech is paused
progress
number (0–1)
Current position as fraction of total text
Events
Event listeners
12345678910111213
const reader = document.querySelector('text-reader');reader.addEventListener('text-reader:play', e => {console.log('Playing at speed:', e.detail.speed);});reader.addEventListener('text-reader:word', e => {console.log('Word:', e.detail.word);});reader.addEventListener('text-reader:end', e => {console.log('Finished in', e.detail.duration, 'ms');});
Any element with slot="icon-play", slot="icon-pause", or slot="icon-stop" will be moved into the corresponding button.
Icon Variant
Use variant="icon" for a compact speaker icon that opens a floating mini-toolbar on click. Ideal for article toolbars and <page-tools> integration.
Icon variant in an article toolbar
123456789
<navdata-layout="cluster"data-layout-gap="s"aria-label="Article tools"><share-wcvariant="icon"></share-wc><print-page></print-page><text-readerfor="my-article"variant="icon"></text-reader></nav><articleid="my-article"><p>Content to read aloud…</p></article>
The floating toolbar provides play/pause, stop, and speed controls. It appears above the trigger icon and auto-dismisses when reading ends. Click outside the toolbar to hide it without stopping playback; click the trigger again to re-show it.
The toolbar follows the trigger in the DOM, so Tab from the trigger moves straight into it. It is shown in the top layer (a manual popover), so transformed or contained ancestors can't clip it. With focus on the trigger or inside the toolbar, Escape hides it and moves focus to the trigger. Escape pressed elsewhere on the page is left alone; a click outside also hides the toolbar. Hiding the toolbar does not stop speech: use the Stop button, or press the trigger to bring the toolbar back.
Customize the trigger icon with slot="icon-trigger":
Controls are wrapped in a role="group" with aria-label="Article reader"
Each button has an aria-label (customizable via data-label-* attributes)
All controls are keyboard-focusable and operable. When Play and Pause swap, focus moves to the button that replaces the one you pressed, and to Play when Stop becomes disabled.
Auto-scroll to the spoken word is instant (no smooth scrolling) under prefers-reduced-motion: reduce
Transport buttons keep a visible border in forced-colours mode
Only one reader speaks at a time. Starting one cancels the other, which resets and fires text-reader:stop; a reader that isn't speaking never cancels another's speech
Voice select and speed slider have accessible labels
Highlighting uses system colors (Mark / MarkText) by default, respecting user contrast preferences
Speech is cancelled on page unload to prevent orphaned audio