# Dictation (/docs/agent-ui/components/dictation)

Use dictation inside a [composer](./composer.mdx) to let people speak a prompt instead of typing it. It never picks where the audio goes: you pass a `source` that turns speech into text, like the browser's speech recognition or your own transcription endpoint, and Dictation shows the microphone's level and the words heard so far. Inside a composer, it adds the final text to the draft. Anywhere else, it hands the text to you.

<Example caption="Press the mic button beside Send to start. The waveform takes the prompt's place while the words arrive. Press Stop to add them to the prompt, or Cancel to drop them. This example plays back a recording, so it never opens your microphone.">
  <DictationExample />

  ```tsx tab="React"
  "use client";
  import type { DictationEvent, DictationSource } from "@kind-ui/agent-ui/core";
  import { Composer, ComposerInput, ComposerSubmit } from "@/components/ui/composer";
  import { Dictation } from "@/components/ui/dictation";

  const words = "Add a retry with backoff to the fetch client".split(" ");
  const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

  // Plays back a recording instead of opening the microphone, so the example runs without a speech service.
  const demoSource: DictationSource = {
    async *start(signal): AsyncGenerator<DictationEvent> {
      let heard = 0;
      for (let tick = 0; !signal.aborted; tick++) {
        const speaking = heard < words.length;
        yield { level: speaking ? 0.25 + 0.6 * Math.abs(Math.sin(tick * 1.7)) : 0.05 };
        if (speaking && tick % 5 === 4) {
          heard++;
          yield { text: words.slice(0, heard).join(" "), final: false };
        }
        await wait(80);
      }
      if (heard === 0 || signal.reason === "cancel") return;
      await wait(600);
      yield { text: words.slice(0, heard).join(" "), final: true };
    },
  };

  export default function DictationExample() {
    return (
      <div className="w-full max-w-md">
        <Composer aria-label="Message">
          <ComposerInput aria-label="Prompt" placeholder="Type or dictate a prompt" />
          <Dictation source={demoSource} />
          <ComposerSubmit />
        </Composer>
      </div>
    );
  }
  ```

  ```vue tab="Vue"
  <script setup lang="ts">
  import type { DictationEvent, DictationSource } from "@kind-ui/agent-ui/core";
  import { Composer, ComposerInput, ComposerSubmit } from "@/components/ui/vue/composer";
  import { Dictation } from "@/components/ui/vue/dictation";

  const words = "Add a retry with backoff to the fetch client".split(" ");
  const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

  // Plays back a recording instead of opening the microphone, so the example runs without a speech service.
  const demoSource: DictationSource = {
    async *start(signal): AsyncGenerator<DictationEvent> {
      let heard = 0;
      for (let tick = 0; !signal.aborted; tick++) {
        const speaking = heard < words.length;
        yield { level: speaking ? 0.25 + 0.6 * Math.abs(Math.sin(tick * 1.7)) : 0.05 };
        if (speaking && tick % 5 === 4) {
          heard++;
          yield { text: words.slice(0, heard).join(" "), final: false };
        }
        await wait(80);
      }
      if (heard === 0 || signal.reason === "cancel") return;
      await wait(600);
      yield { text: words.slice(0, heard).join(" "), final: true };
    },
  };
  </script>

  <template>
    <div class="w-full max-w-md">
      <Composer aria-label="Message">
        <ComposerInput aria-label="Prompt" placeholder="Type or dictate a prompt" />
        <Dictation :source="demoSource" />
        <ComposerSubmit />
      </Composer>
    </div>
  </template>
  ```

  ```svelte tab="Svelte"
  <script lang="ts">
    import type { DictationEvent, DictationSource } from "@kind-ui/agent-ui/core";
    import { Composer, ComposerInput, ComposerSubmit } from "@/components/ui/svelte/composer/index.js";
    import { Dictation } from "@/components/ui/svelte/dictation/index.js";

    const words = "Add a retry with backoff to the fetch client".split(" ");
    const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

    // Plays back a recording instead of opening the microphone, so the example runs without a speech service.
    const demoSource: DictationSource = {
      async *start(signal): AsyncGenerator<DictationEvent> {
        let heard = 0;
        for (let tick = 0; !signal.aborted; tick++) {
          const speaking = heard < words.length;
          yield { level: speaking ? 0.25 + 0.6 * Math.abs(Math.sin(tick * 1.7)) : 0.05 };
          if (speaking && tick % 5 === 4) {
            heard++;
            yield { text: words.slice(0, heard).join(" "), final: false };
          }
          await wait(80);
        }
        if (heard === 0 || signal.reason === "cancel") return;
        await wait(600);
        yield { text: words.slice(0, heard).join(" "), final: true };
      },
    };
  </script>

  <div class="w-full max-w-md">
    <Composer aria-label="Message">
      <ComposerInput aria-label="Prompt" placeholder="Type or dictate a prompt" />
      <Dictation source={demoSource} />
      <ComposerSubmit />
    </Composer>
  </div>
  ```

  ```tsx tab="Solid"
  import type { DictationEvent, DictationSource } from "@kind-ui/agent-ui/core";
  import { Composer, ComposerInput, ComposerSubmit } from "@/components/ui/solid/composer";
  import { Dictation } from "@/components/ui/solid/dictation";

  const words = "Add a retry with backoff to the fetch client".split(" ");
  const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

  // Plays back a recording instead of opening the microphone, so the example runs without a speech service.
  const demoSource: DictationSource = {
    async *start(signal): AsyncGenerator<DictationEvent> {
      let heard = 0;
      for (let tick = 0; !signal.aborted; tick++) {
        const speaking = heard < words.length;
        yield { level: speaking ? 0.25 + 0.6 * Math.abs(Math.sin(tick * 1.7)) : 0.05 };
        if (speaking && tick % 5 === 4) {
          heard++;
          yield { text: words.slice(0, heard).join(" "), final: false };
        }
        await wait(80);
      }
      if (heard === 0 || signal.reason === "cancel") return;
      await wait(600);
      yield { text: words.slice(0, heard).join(" "), final: true };
    },
  };

  export default function DictationExample() {
    return (
      <div class="w-full max-w-md">
        <Composer aria-label="Message">
          <ComposerInput aria-label="Prompt" placeholder="Type or dictate a prompt" />
          <Dictation source={demoSource} />
          <ComposerSubmit />
        </Composer>
      </div>
    );
  }
  ```
</Example>

## Installation [#installation]

```bash tab="React"
npx shadcn@latest add @kind-ui/dictation
```

```bash tab="Web Components"
npx shadcn@latest add @kind-ui/dictation-web
```

```bash tab="Vue"
npx shadcn@latest add @kind-ui/dictation-vue
```

```bash tab="Svelte"
npx shadcn@latest add @kind-ui/dictation-svelte
```

```bash tab="Solid"
npx shadcn@latest add @kind-ui/dictation-solid
```

## Composition [#composition]

```text
Dictation
├── DictationWaveform
│   └── DictationBar
├── DictationStatus
│   └── DictationStatusIcon
├── DictationStart
├── DictationCancel
└── DictationStop
```

`Dictation` is a group named by its `label`. `DictationStart` is the mic button. While listening, `DictationWaveform` draws one `DictationBar` per level, `DictationStatus` shows "Listening" or the words heard so far, and `DictationCancel` and `DictationStop` end the dictation. `DictationStatusIcon` is the mark beside the status: running while transcribing, an error after one. `Dictation` renders every part when you pass no children.

## Usage [#usage]

### Sources [#sources]

Pass a `source`, an object whose `start(signal)` returns an async iterable of events: `{ level }` for the waveform (0 to 1), `{ text, final }` for recognized text, and `{ error }` when it fails. It listens until `signal` aborts. Stop aborts it without a reason, and the source reports the text still to come and ends. Cancel, or the component unmounting, aborts it with the reason `"cancel"`: nothing more is read, so the source can drop what it heard. Without a `source` there's no mic button.

`createSpeechDictation` is a source over the browser's speech recognition, with levels from the microphone. Pass `lang` for the language spoken; it defaults to the page's `lang`. Chrome's speech recognition sends audio to a speech service, and browsers without speech recognition show "Not supported in this browser".

```tsx
import { createSpeechDictation } from "@kind-ui/agent-ui/core";

const speech = createSpeechDictation({ lang: "en-US" });

<Composer aria-label="Message">
  <ComposerInput aria-label="Prompt" />
  <Dictation source={speech} />
  <ComposerSubmit />
</Composer>;
```

To keep audio on your own servers, write a source. This one records with `MediaRecorder` and posts the recording to your endpoint once Stop is pressed, but not after Cancel:

```tsx
import type { DictationEvent, DictationSource } from "@kind-ui/agent-ui/core";

const recorded: DictationSource = {
  async *start(signal): AsyncGenerator<DictationEvent> {
    let stream: MediaStream;
    try {
      stream = await navigator.mediaDevices.getUserMedia({ audio: true });
    } catch {
      yield { error: "denied" };
      return;
    }
    const recorder = new MediaRecorder(stream);
    const chunks: Blob[] = [];
    recorder.ondataavailable = (event) => chunks.push(event.data);
    const stopped = new Promise((resolve) => (recorder.onstop = resolve));
    recorder.start();
    if (!signal.aborted) await new Promise((resolve) => signal.addEventListener("abort", resolve, { once: true }));
    recorder.stop();
    for (const track of stream.getTracks()) track.stop();
    await stopped;
    if (signal.reason === "cancel") return;
    const response = await fetch("/api/transcribe", {
      method: "POST",
      body: new Blob(chunks, { type: recorder.mimeType }),
    });
    if (!response.ok) {
      yield { error: "network" };
      return;
    }
    const { text } = await response.json();
    yield { text, final: true };
  },
};
```

It reports no levels, so its waveform stays flat; yield `{ level }` from Web Audio for one that moves.

### Inserting the Text [#inserting-the-text]

Place Dictation as a direct child of a `Composer`, beside `ComposerInput`, and it adds its final text to the end of the draft, after a space, with no code of your own. The composer reports the change to `onValueChange` with the reason `dictation` and the source of the press that stopped it, and a disabled composer takes nothing. The composer's api does the same through `appendText(text, source?)`.

`onText` is called first, once when a dictation ends, with its final texts joined, and never for one that was canceled, failed or heard nothing. The reason is `stop-press` from Stop, `imperative` from the api, like an agent's `stop`, or `source-end` when the source ended on its own. Call `details.cancel()` to keep the text out of the draft and handle it yourself.

Outside a composer, Dictation inserts nothing, so pass `onText` and put the text where it belongs:

```tsx
<Dictation source={source} onText={(text) => setNote((note) => `${note} ${text}`.trim())} />
```

### Phases [#phases]

The mic button starts listening. While listening, the waveform follows the levels, the status shows the words heard so far, and Cancel and Stop take the button's place. Stop moves to transcribing, which shows "Transcribing", with the waveform holding its last levels, until the source reports its last text. Then Dictation goes back to the mic button and inserts the text. Cancel goes straight back and inserts nothing.

Inside a composer, the mic button is a round icon just before Send. While listening and transcribing, Dictation takes over the composer: the waveform fills the place of the prompt, Cancel takes the place of the controls at the start of the actions row, the status sits in the middle of the row, and Stop sits beside Send. The composer keeps its size, so nothing around it moves.

When a source fails, the mic button comes back with the reason beside it, like "Microphone blocked", "No speech heard" or "Speech service unreachable", and the reason is announced. The words heard while listening aren't announced.

### Keyboard [#keyboard]

The mic button, Cancel and Stop are buttons, so Enter or Space presses them. When the mic button starts a dictation, focus moves to Stop, which stays (unavailable) through transcribing, and back to the mic button when it ends.

### Size and Motion [#size-and-motion]

On its own, Dictation keeps one size in every phase, so nothing beside it moves: `--kind-dictation-width` (16rem by default) and `--kind-dictation-height` (2rem). Set either on the component. Inside a composer, it takes the composer's size instead. The waveform draws one bar per level, the newest at the end, so the bars flow toward the start. `bars` sets how many levels it keeps, from 1 to 256 (128 by default), and bars that don't fit are cut at the start. `motion` sets how the waveform moves: `"auto"`, the default, follows `prefers-reduced-motion`; under reduced motion and `"none"` the waveform holds still.

## API Reference [#api-reference]

### DictationOptions

| Prop            | Type                                                                                 | Description                                                                                                                                                                                    |
| --------------- | ------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `source?`       | `DictationSource \| undefined`                                                       | Turns speech into text. Without one there's no mic button: Dictation never picks a source itself, so the host decides where audio goes. `createSpeechDictation` is the browser's.              |
| `onText?`       | `((text: string, details: ChangeDetails<DictationTextReason>) => void) \| undefined` | Called once when a dictation ends, with its final text. Never for a canceled or failed one, or without text. Inside a Composer the text then goes into the draft, unless you cancel `details`. |
| `label?`        | `string \| undefined`                                                                | Names the mic button and the component. Default: `"Dictate"`                                                                                                                                   |
| `bars?`         | `number \| undefined`                                                                | How many levels the waveform keeps, a whole number from 1 to 256: one bar each, the newest at the end. Bars that don't fit are cut at the start. Default: `128`                                |
| `agentConfirm?` | `boolean \| undefined`                                                               | Whether an agent needs a person to start a dictation, which opens the microphone. Default: `true`                                                                                              |
| `motion?`       | `"auto" \| "none" \| "reduced"`                                                      | `"auto"` follows `prefers-reduced-motion`. Under reduced motion and `"none"` the waveform holds still. Default: `"auto"`                                                                       |

Vue, Svelte and Solid take the same props. Controlled props and callbacks are spelled as each framework expects:

| React    | Vue     | Svelte   | Solid    |
| -------- | ------- | -------- | -------- |
| `onText` | `@text` | `onText` | `onText` |

### Agent [#agent]

An agent connected with `createAgent` finds Dictation by its `label`, "Dictate" by default, and reads its `phase`. `start` is offered with a `source` while idle or after an error, `stop` while listening, and `cancel` while listening or transcribing. Starting opens the microphone, so while `agentConfirm` is on, the default, `start` waits for a person to confirm: an agent never opens the microphone without one. `stop` and `cancel` don't ask.

### DictationAgentActions

| Prop     | Type                           | Description                                                                                                                                 |
| -------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `start`  | `(() => boolean) \| undefined` | Starts listening, and returns whether it did. Offered only with a `source`, while idle or after an error.                                   |
| `stop`   | `(() => boolean) \| undefined` | Stops listening, and returns whether it did; the final text goes to `onText` once the source has reported it. Offered only while listening. |
| `cancel` | `(() => boolean) \| undefined` | Stops, inserts nothing, and returns whether it did. Offered while listening or transcribing.                                                |

<Caption>
  Runs in React, Vue, Svelte, Solid and Web Components. On `<kind-dictation>`, set `source` as a property and `label`, `bars`, `agent-confirm` and `motion` as attributes, and listen for `dictationtext`, with the text as `event.detail.value`. Inside a `<kind-composer>`, the text goes into the draft. The element fills in its parts when it has no children.
</Caption>
