Dictation
A mic button that types what people say. The same voice input Prompt Input and Quick Chat use.
Preview
This browser has no speech recognition, so the mic stays hidden. Try Chrome or Edge.
Setup
Install with the shadcn CLI
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
Run in your terminal:
$npm install clsx tailwind-merge framer-motion lucide-react - 2
Copy
lib/utils.tsfrom the Code tab. Skip if you already havecn(). - 3
Copy
styles/tokens.cssfrom the Code tab and import it in your global CSS after Tailwind:@import "./styles/tokens.css"; - 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
Import and render:
Exampleimport { 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:
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:
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:
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 exampleh-7 min-w-7 rounded-mdin a compact toolbar.
API
| useDictation(enabled, onText) | Type | What it is |
|---|---|---|
enabled | boolean | Turn voice input on or off. |
onText | (text) => void | Gets everything heard so far, each time more is heard. |
supported | boolean | Whether this browser can listen. |
listening | boolean | Whether it's listening right now. |
start / stop / toggle | () => void | Control it yourself. stop keeps the words. |
cancel | () => void | Stop 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