Speech Input
React speech to text button on the Web Speech API: live interim text, a mic ring that follows your voice, Escape to stop, no API key and no dependencies.
Installation
npx shadcn@latest add @minidev/speech-input
MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/speech-input.json
Usage
import { SpeechInput } from "@/components/ui/speech-input"
export function Example() {
return (
<SpeechInput />
)
}
API reference
| Prop | Type | Default |
|---|---|---|
| onTranscriptFires with recognised text. `final: false` is a live guess that will be replaced; `final: true` is settled. | (text: string, meta: { final: boolean }) => void | - |
| langBCP 47 language to recognise, e.g. "en-GB" or "hi-IN". Defaults to the browser language (`navigator.language`). | string | - |
| continuousKeep listening across pauses until stopped. When false, it stops after the first phrase. | boolean | true |
| interimSend interim guesses (`final: false`) while the user speaks. | boolean | true |
| onErrorFires with the Web Speech error code: "not-allowed", "audio-capture", "network", "no-speech"... | (error: string) => void | - |
| onUnsupportedFires once after mount when the browser has no speech recognition. | () => void | - |
| disabledDisables the button and stops listening if it is on. | boolean | - |
| size32px (sm) or 36px (md). | "sm""md" | "md" |
| aria-labelAccessible name of the button, also its tooltip. Defaults to "Dictate". | string | - |
| className | string | - |
When to use
Put it in the toolbar of PromptInput or AiComposer. Click to start, click again or press Escape to stop. onTranscript(text, { final }) fires with grey-preview text (final: false) while you speak and with settled text (final: true) when a phrase ends: append final text, replace the preview. It needs SpeechRecognition, which Chrome, Edge and Safari have and Firefox does not; there the button is disabled with a tooltip that says so. It calls no service of its own and needs no API key. To record an audio file instead of text use AudioRecorder.
Keyboard interactions
| Key | Action |
|---|---|
| EnterorSpace | On the button, starts listening, or stops when it is already listening. |
| Escape | Stops listening wherever focus is, for example while typing in the prompt. The listener is only attached while listening. |
Accessibility
- The button keeps one name,
aria-label("Dictate" by default), and exposes the listening state asaria-pressed. The icon swaps from a microphone to a stop square. - Without speech recognition the button uses
focusableWhenDisabled: it stays in the Tab order witharia-disabled="true", andaria-describedbypoints to hidden text that says voice input is not supported. - Errors appear as inline text with
role="alert", so screen readers read them as they appear. - The level ring is
aria-hidden. Under reduced motion it stays at a fixed size and the level stream is never opened.
Frequently asked questions
Which browsers support SpeechInput?
Browsers with SpeechRecognition or webkitSpeechRecognition: Chrome, Edge and Safari. Firefox has neither, so the button is disabled, its tooltip says "Voice input is not supported in this browser" and onUnsupported fires once. The component calls no service and needs no key, but Chrome recognises speech on a server, so it fails offline with the network error.
How do I show the words in grey while the user is still speaking?
onTranscript fires with final: false for a live guess that the next call replaces, and final: true when a phrase settles. Keep the guess in its own state: if (final) { setPrompt((p) => (p + " " + text).trim()); setPreview("") } else setPreview(text). Set interim={false} to get settled text only.
What happens when the user blocks the microphone?
The browser reports not-allowed, and SpeechInput shows "Microphone access is blocked. Allow it in the browser's site settings." next to the button and calls onError("not-allowed"). The message clears on the next start. audio-capture (no microphone) and no-speech get their own messages.
Does SpeechInput record or upload audio?
No, it only returns text. While listening it opens a second microphone stream with getUserMedia to read the level for the ring through an AnalyserNode, records nothing and stops it when listening ends. To save an audio file use AudioRecorder.
Dependencies
npm
Built from
Examples
Dictate into a prompt
Settled text is appended to the prompt; the live guess shows in grey under it until the phrase ends.
Notes with a set language
lang picks the recognition language. The preview sits inline after the settled text, in a lighter colour.
Sizes and states
sm is 32px for composer toolbars, md is 36px. In Firefox the button is disabled and its tooltip says why.