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
PartTokenWhy
Buttonghost (text-fg-muted, hover:bg-fg/5) or secondary (surface, border, shadow-raised); size-11 sm:size-9, or size-8A 44 px target on touch screens.
Listeningtext-danger (aria-pressed:), a ring-2 ring-danger that pings outward, motion-safeRed is the recording convention; the ring stays, still, under reduced motion, so the state is never motion alone.
Statustext-meta text-fg-muted; text-danger in error; empty:sr-onlyKept 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. lang from the prop or <html lang>; continuous; interimResults from interim.
  • Listening: data-ds-state="listening" on the root, aria-pressed true, the name stopLabel, the status "Listening…".
  • Results: each final result is inserted into for with setRangeText at the caret (replacing a selection), with a space either side where the neighbouring text has none, then a bubbling input event (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. result is 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; timeout ms with nothing heard.
  • Error: data-ds-state="error", the status says words.errors[code] (not-allowed, service-not-allowed, no-speech, audio-capture, network, language-not-supported, else "Dictation stopped."), error dispatched.
  • Events from the root: start, end, result, error.

Accessibility

  • Role and name: a button with aria-pressed; named label at rest and stopLabel while 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.

Was this page helpful?