Dictation

A mic button that types what people say. The same voice input Prompt Input and Quick Chat use.

Preview

Setup

Install with the shadcn CLI

$npx shadcn@latest add https://kinetikui.singhgourav.site/r/dictation.json

One command adds every file, the npm packages and any Kinetik components it builds on. Needs a components.json; the CLI offers to create one if your project has none.

Or install manually

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge framer-motion lucide-react
  2. 2

    Copy lib/utils.ts from the Code tab. Skip if you already have cn().

  3. 3

    Copy styles/tokens.css from the Code tab and import it in your global CSS after Tailwind: @import "./styles/tokens.css";

  4. 4

    Create a components/ui/dictation/ folder and copy every file from the Code tab into it. Each file does one job, so you can read or change a piece without the rest.

  5. 5

    Import and render:

    Example
    import { DictationButton, useDictation } from "@/components/ui/dictation"
    
    const dictation = useDictation(true, setText)
    
    <DictationButton
      supported={dictation.supported}
      listening={dictation.listening}
      onToggle={dictation.toggle}
    />

Usage

useDictation listens, and DictationButton is the mic. Every time the browser hears more, you get the whole sentence so far, so you can put it straight into your text box:

Example
import { DictationButton, useDictation } from "@/components/ui/dictation"

const [text, setText] = useState("")
const dictation = useDictation(true, setText)

<textarea value={text} onChange={(e) => setText(e.target.value)} />
<DictationButton
  supported={dictation.supported}
  listening={dictation.listening}
  onToggle={dictation.toggle}
/>

Press the mic to start. It turns your accent color, pulses and shows a little waveform while it listens. Press it again to stop and keep the words.

Keep what was already typed

The example above replaces the text. To add the spoken words after what's already there, remember the text when the mic starts:

Example
const base = useRef("")
const dictation = useDictation(true, (spoken) => setText(base.current + spoken))

<DictationButton
  supported={dictation.supported}
  listening={dictation.listening}
  onToggle={() => {
    base.current = text ? `${text.trimEnd()} ` : ""
    dictation.toggle()
  }}
/>

Add a cancel button

Pass onCancel and a ✕ appears next to the mic while it listens. Use dictation.cancel() in it (it stops without letting a late result sneak back in) and put the old text back:

Example
const before = useRef("")

<DictationButton
  supported={dictation.supported}
  listening={dictation.listening}
  onToggle={() => {
    if (!dictation.listening) before.current = text
    dictation.toggle()
  }}
  onCancel={() => {
    dictation.cancel()
    setText(before.current)
  }}
/>

Good to know

  • It uses the speech recognition built into the browser, so there's no API key or setup.
  • It works in Chrome, Edge and Safari. Where it isn't available (Firefox, for now) the button simply doesn't show.
  • The browser asks for microphone permission the first time.
  • Change the button's size with className, for example h-7 min-w-7 rounded-md in a compact toolbar.

API

useDictation(enabled, onText)TypeWhat it is
enabledbooleanTurn voice input on or off.
onText(text) => voidGets everything heard so far, each time more is heard.
supportedbooleanWhether this browser can listen.
listeningbooleanWhether it's listening right now.
start / stop / toggle() => voidControl it yourself. stop keeps the words.
cancel() => voidStop and ignore anything still on its way.

Accessibility

  • The button says "Start voice input" or "Stop voice input" and is marked as pressed while listening
  • The ✕ is labeled "Cancel voice input"
  • With reduced motion, the pulsing ring is turned off