Dictation button

A microphone button beside a field: press it, speak, and the words are typed in at the caret. Press it again to stop.

Dictation button

Needs Alpine and the package's script, and a browser with speech recognition. Where there is none the button is not shown at all, so there is never a control that does nothing. See getting started.

Use it

<x-ds::input as="textarea" id="message" name="message" label="Message" />
<x-ds::dictation-button for="message" lang="en-GB" />

Props

PropTypeDefaultWhat it does
forstring—The id of the input or textarea it types into. Without it, it only dispatches result.
langstringthe page's langThe language spoken, as a BCP 47 tag.
continuousboolfalseKeep listening through pauses until pressed again.
interimboolfalseShow the words in the status line as they are heard.
timeoutint8000Stop after this many milliseconds with nothing heard; 0 to leave it to the browser.
labelstringDictateThe button's name at rest.
stopLabelstringStop dictationIts name while listening.
sizesm mdmd
variantghost secondaryghost

Words go in at the caret, replacing a selection and spaced from the words around them; in a field nobody has been in yet, at the end. An input event follows, so x-model and wire:model hear it.

Events, from the button's wrapper: start, end, result with { text, final }, and error with { error }.

Where it works

The Web Speech API's recognition is in Chrome, Edge and Safari (as webkitSpeechRecognition), not in Firefox. In Chrome and Edge it needs a network connection. Each browser asks for the microphone the first time.

Privacy. In Chrome and Edge the audio is sent to the browser maker's speech service to be recognised; Safari may do it on the device. Say so where people dictate something sensitive, and don't offer dictation on a field that must not leave the device.

Accessibility

  • A toggle button: aria-pressed, and its name changes to "Stop dictation" while it listens, so both the state and what a press does are said.
  • A status region says "Listening…", the words as they are heard with interim, and what went wrong ("Microphone permission was denied.").
  • While listening a ring pulses round the button; under reduced motion it stays, still.
  • Escape stops it, and goes no further, so a dialog around it stays open. It stops by itself when the page is hidden or the window loses focus.

Don't

  • Don't make it the only way to fill the field. It is a shortcut beside the keyboard.
  • Don't start it for the reader. Nothing starts it but a press.
  • Don't use it for passwords or codes.

More in specs/dictation-button.md.


From the package repo: edit this page on GitHub.

Was this page helpful?