aiellie

Search

Find a component, template or page

GitHub
Components

Dictate Button

A microphone button that types what you say into a field, using the browser's own speech recognition, and grows into a live waveform while it listens. It renders nothing where the browser has none.

fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/dictate-button"

shadcn needs the @aiellie registry in components.json first. npx aiellie add dictate-button adds it on its first run, then installs the same way.

Dictate button turns speech into text in a field you already have. Press it and talk: what is said is added to the field's value as it is said, with a live Waveform in the button while it listens. It uses the browser's own speech recognition, so there is no service to set up. In a browser without one, like Firefox, it records instead and hands the audio to transcribe. Without transcribe there, it renders nothing.

Getting started

  1. Put it beside the field

    Share the field's value and setter with it. It writes value plus what has been said so far, so anything typed before stays.

    components/message-field.tsx
    "use client"
    
    import * as React from "react"
    
    import { DictateButton } from "@/components/aiellie/dictate-button"
    import { Input } from "@/components/ui/input"
    
    export function MessageField() {
      const [value, setValue] = React.useState("")
    
      return (
        <div className="flex items-center gap-2">
          <Input
            value={value}
            onChange={(event) => setValue(event.target.value)}
            aria-label="Message"
          />
          <DictateButton value={value} onValueChange={setValue} />
        </div>
      )
    }
  2. Say when it can't listen

    onError gets the recognizer's error code. not-allowed means the microphone is blocked; the others are rarer. Stopping and silence aren't errors and never reach it.

    <DictateButton
      value={value}
      onValueChange={setValue}
      onError={(code) => toast.add({ title: "Dictation stopped", description: code })}
    />

Anatomy

<button data-slot="dictate-button" data-listening> {/* a TooltipIconButton */}
  <svg /> {/* a mic while idle */}
  {/* while listening: */}
  <div data-slot="waveform" />
  <svg /> {/* stop */}
</button>

Idle, it is a 28px round icon button. While listening it widens into a pill in the live color, with a 16-bar waveform and a stop glyph, and its tooltip names the next press. The waveform asks for the microphone itself, since the recognizer keeps its audio to itself; without that permission it still shows, resting.

If the value changes to something it didn't write, because someone typed or the message was sent, it stops listening, so the draft is theirs again. The language comes from lang, then the page's lang attribute, then the browser's.

Examples

In a message box, with errors

Try it with the microphone blocked to see the error come through.

Where there's no speech recognition

Pass transcribe to keep the button in browsers without a recognizer. It records the whole take with MediaRecorder, and when it's stopped, sends the audio to your function and adds the text it returns. A live dot stands in for the waveform while it waits. A failed transcribe reaches onError as transcribe-failed.

<DictateButton
  value={value}
  onValueChange={setValue}
  transcribe={async (audio) => {
    const body = new FormData()
    body.append("audio", audio)
    const response = await fetch("/api/transcribe", { method: "POST", body })
    return (await response.json()).text
  }}
/>

Another language

Set lang to a BCP 47 tag to listen for that language.

<DictateButton lang="fr-FR" value={value} onValueChange={setValue} />

API reference

DictateButton

PropTypeDefaultDescription
valuestringrequiredThe field's current text. What is said is added after it.
onValueChange(value: string) => voidrequiredCalled with value plus what has been said so far, as it is said.
langstringthe page's languageThe language to listen for.
onError(error: string) => voidCalled with the recognizer's error code, like not-allowed.
transcribe(audio: Blob) => Promise<string>Turns recorded audio into text, for browsers without speech recognition.
tooltipstring"Dictate"The label while idle.
stopTooltipstring"Stop dictating"The label while listening.
side"top" | "bottom" | "left" | "right""top"Where the tooltip opens.
classNamestringMerged onto the button.

All other TooltipIconButton props are forwarded, except onClick, which it sets itself.