Dictation button
A microphone toggle that types what is said into a field, on the browser's
Web Speech API (SpeechRecognition, or webkitSpeechRecognition).
dsDictation drives it.
Form control. It takes input in a form, so it is drawn on the Every input type block, beside every other control, as it starts and with an error where it has one.
Anatomy
(🎤) Listening… ← button (toggle) + status (role=status)
((🎤)) thanks for ← listening: ring pulsing, interim words
| Part | Token | Why |
|---|---|---|
| Button | ghost (text-fg-muted, hover:bg-fg/5) or secondary (surface, border, shadow-raised); size-11 sm:size-9, or size-8 | A 44 px target on touch screens. |
| Listening | text-danger (aria-pressed:), a ring-2 ring-danger that pings outward, motion-safe | Red is the recording convention; the ring stays, still, under reduced motion, so the state is never motion alone. |
| Status | text-meta text-fg-muted; text-danger in error; empty:sr-only | Kept in the tree when empty, so the live region exists before it speaks. |
Support
The root renders hidden. The script shows it only where a recognition
constructor exists; elsewhere (Firefox, no script) there is no button at all
rather than one that does nothing. Chrome and Edge send the audio to a
server and need a network; Safari may recognise on the device. The docs page
carries the privacy note.
Behaviour
- Start: only a press.
langfrom the prop or<html lang>;continuous;interimResultsfrominterim. - Listening:
data-ds-state="listening"on the root,aria-pressedtrue, the namestopLabel, the status "Listening…". - Results: each final result is inserted into
forwithsetRangeTextat the caret (replacing a selection), with a space either side where the neighbouring text has none, then a bubblinginputevent (x-model, wire:model). A field never focused has its caret at 0; there the words go at the end. Read-only and disabled fields are left alone.resultis dispatched with{ text, final }for interim and final text. - Stop: a second press; Escape (handled on the document and stopped
there, so a surrounding dialog stays open); the page hidden; the window
blurred;
timeoutms with nothing heard. - Error:
data-ds-state="error", the status sayswords.errors[code](not-allowed, service-not-allowed, no-speech, audio-capture, network, language-not-supported, else "Dictation stopped."),errordispatched. - Events from the root:
start,end,result,error.
Accessibility
- Role and name: a
buttonwitharia-pressed; namedlabelat rest andstopLabelwhile listening. Both change together: the state for those who hear pressed, the action for those who hear only the name. - Status:
role=status, polite. - Keyboard: Enter / Space toggle; Escape stops.
- Focus: stays on the button; the text goes into the field without moving focus to it.
Do not
- Render a button where recognition is missing. The root is hidden until the script proves it.
- Insert interim text into the field. It changes as it is heard; only final text is typed.
- Let Escape through while listening. It is the stop, not the dialog's close.
From the package repo: edit this page on GitHub.