# @salmonui/inputs/speech-to-text API reference

> 15 exported symbols of @salmonui/inputs/speech-to-text: 2 functions, 10 interfaces, 2 types and 1 constant.

Canonical: https://salmonui.dev/docs/reference/inputs/speech-to-text

```ts
import { … } from '@salmonui/inputs/speech-to-text';
```

## Functions

### SpeechToTextButton

Kind: function

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/SpeechToTextButton

```ts
function SpeechToTextButton(props: SpeechToTextButtonProps): ReactElement;
```

Toggle button that starts and stops speech recognition: `Button` with `aria-pressed` while listening. The name stays
constant; the state hint is the accessible description and the `title`.
Disabled (and described as unsupported) when the browser has no speech
recognition and no `recognizer` is given.

### useSpeechRecognition

Kind: function

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/useSpeechRecognition

```ts
function useSpeechRecognition(options?: UseSpeechRecognitionOptions): SpeechRecognitionApi;
```

Speech recognition over the
Web Speech API or a replaceable `recognizer`. Options are read when
events happen, so callbacks may be inline functions.

## Interfaces

### SpeechRecognitionApi

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/SpeechRecognitionApi

```ts
interface SpeechRecognitionApi {
    readonly state: SpeechRecognitionState;
    readonly transcript: string;
    readonly supported: boolean;
    readonly start: () => void;
    readonly stop: () => void;
    readonly abort: () => void;
}
```

Members (6):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `state` | `readonly state: SpeechRecognitionState;` |  | `'idle'`, `'listening'` or `'stopped'`. |
| `transcript` | `readonly transcript: string;` |  | Text of the current session: final results, then interim ones. |
| `supported` | `readonly supported: boolean;` |  | False on the server, during hydration and in browsers without the Web Speech API; true with a `recognizer`. |
| `start` | `readonly start: () => void;` |  | Starts a new session with an empty transcript. Without a recognizer it calls `onError({ code: 'unsupported' })`. |
| `stop` | `readonly stop: () => void;` |  | Stops listening; the last results still arrive, then the session ends. |
| `abort` | `readonly abort: () => void;` |  | Ends the session without further results. |

### SpeechRecognitionError

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/SpeechRecognitionError

```ts
interface SpeechRecognitionError {
    readonly code: SpeechRecognitionErrorCode;
    readonly message: string;
}
```

Members (2):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `code` | `readonly code: SpeechRecognitionErrorCode;` |  |  |
| `message` | `readonly message: string;` |  |  |

### SpeechRecognizer

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/SpeechRecognizer

```ts
interface SpeechRecognizer {
    lang: string;
    continuous: boolean;
    interimResults: boolean;
    start(): void;
    stop(): void;
    abort(): void;
    onstart: ((event: unknown) => void) | null;
    onresult: ((event: SpeechRecognizerResultEvent) => void) | null;
    onerror: ((event: {
        readonly error: string;
        readonly message?: string;
    }) => void) | null;
    onend: ((event: unknown) => void) | null;
}
```

The subset of the Web Speech `SpeechRecognition` interface the hook uses. A cloud recognizer implements it; one instance per session.

Members (10):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `lang` | `lang: string;` |  |  |
| `continuous` | `continuous: boolean;` |  |  |
| `interimResults` | `interimResults: boolean;` |  |  |
| `start` | `start(): void;` |  |  |
| `stop` | `stop(): void;` |  |  |
| `abort` | `abort(): void;` |  |  |
| `onstart` | `onstart: ((event: unknown) => void) \| null;` |  |  |
| `onresult` | `onresult: ((event: SpeechRecognizerResultEvent) => void) \| null;` |  |  |
| `onerror` | `onerror: ((event: {     readonly error: string;     readonly message?: string; }) => void) \| null;` |  |  |
| `onend` | `onend: ((event: unknown) => void) \| null;` |  |  |

### SpeechRecognizerAlternative

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/SpeechRecognizerAlternative

```ts
interface SpeechRecognizerAlternative {
    readonly transcript: string;
}
```

One alternative of a result (Web Speech `SpeechRecognitionAlternative`).

Members (1):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `transcript` | `readonly transcript: string;` |  |  |

### SpeechRecognizerResult

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/SpeechRecognizerResult

```ts
interface SpeechRecognizerResult {
    readonly isFinal: boolean;
    readonly length: number;
    readonly [index: number]: SpeechRecognizerAlternative;
}
```

One result (Web Speech `SpeechRecognitionResult`): alternatives, best first.

Members (2):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `isFinal` | `readonly isFinal: boolean;` |  |  |
| `length` | `readonly length: number;` |  |  |

### SpeechRecognizerResultEvent

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/SpeechRecognizerResultEvent

```ts
interface SpeechRecognizerResultEvent {
    readonly results: {
        readonly length: number;
        readonly [index: number]: SpeechRecognizerResult;
    };
}
```

`result` event (Web Speech `SpeechRecognitionEvent`): every result of the session so far.

Members (1):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `results` | `readonly results: {     readonly length: number;     readonly [index: number]: SpeechRecognizerResult; };` |  |  |

### SpeechToTextButtonProps

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/SpeechToTextButtonProps

```ts
interface SpeechToTextButtonProps extends OwnButtonProps, UseSpeechRecognitionOptions {
    readonly children?: ButtonBaseProps['children'];
    readonly localeText?: SpeechToTextLocaleText | undefined;
}
```

Members (18):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `children` | `readonly children?: ButtonBaseProps['children'];` | optional | Visible label; without children the button is icon-only, named by `localeText.label`. |
| `localeText` | `readonly localeText?: SpeechToTextLocaleText \| undefined;` | optional |  |
| `variant` | `variant?: ButtonVariant;` | optional, inherited | Visual weight. Default `'solid'` (or the group's). |
| `tone` | `tone?: ButtonTone;` | optional, inherited | Color role. Default `'neutral'`; `'primary'` for a `floating` button (or the group's). |
| `size` | `size?: ButtonSize;` | optional, inherited | Metric set. Default `'md'` (or the group's). |
| `iconStart` | `iconStart?: ReactNode;` | optional, inherited | Decorative icon before the label (inline-start; follows `dir`). |
| `iconEnd` | `iconEnd?: ReactNode;` | optional, inherited | Decorative icon after the label (inline-end; follows `dir`). |
| `floating` | `floating?: boolean \| ButtonFloatingPosition;` | optional, inherited | Floating action button: circular, elevated, positioned by CSS. |
| `floatingScope` | `floatingScope?: "parent" \| "viewport";` | optional, inherited | `'viewport'` (default, `position: fixed`) or `'parent'` (`position: absolute` in the nearest positioned ancestor). |
| `ref` | `ref?: Ref<HTMLButtonElement>;` | optional, inherited | The `<button>` (or the element `render` returns). |
| `lang` | `lang?: string;` | optional, inherited | BCP 47 recognition language. Default: `document.documentElement.lang`, read when a session starts. |
| `interimResults` | `interimResults?: boolean;` | optional, inherited | Deliver interim (non-final) results. Default `true`. |
| `continuous` | `continuous?: boolean;` | optional, inherited | Keep listening after a pause until `stop()`. Default `true`. |
| `recognizer` | `recognizer?: (() => SpeechRecognizer);` | optional, inherited | Replaces the browser recognizer, e.g. a cloud service. Called once per session. |
| `onTranscriptChange` | `onTranscriptChange?: ((transcript: string, details: SpeechTranscriptDetails) => void);` | optional, inherited | The session transcript changed. |
| `onStart` | `onStart?: (() => void);` | optional, inherited | The recognizer started capturing audio. |
| `onStop` | `onStop?: (() => void);` | optional, inherited | The session ended (`stop()`, `abort()`, an error or the recognizer). |
| `onError` | `onError?: ((error: SpeechRecognitionError) => void);` | optional, inherited | A recognizer error, or `'unsupported'` when `start()` has no recognizer. |

### SpeechToTextLocaleText

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/SpeechToTextLocaleText

```ts
interface SpeechToTextLocaleText {
    readonly label?: string;
    readonly start?: string;
    readonly stop?: string;
    readonly unsupported?: string;
}
```

User-visible strings of `SpeechToTextButton`.

Members (4):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `label` | `readonly label?: string;` | optional | Accessible name of an icon-only button. Default `"Speech to text"`. |
| `start` | `readonly start?: string;` | optional | Hint while idle or stopped. Default `"Start listening"`. |
| `stop` | `readonly stop?: string;` | optional | Hint while listening. Default `"Stop listening"`. |
| `unsupported` | `readonly unsupported?: string;` | optional | Hint when the browser has no speech recognition. |

### SpeechTranscriptDetails

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/SpeechTranscriptDetails

```ts
interface SpeechTranscriptDetails {
    readonly final: boolean;
}
```

Second argument of `onTranscriptChange`: `final` is true when every result of the session is final.

Members (1):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `final` | `readonly final: boolean;` |  |  |

### UseSpeechRecognitionOptions

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/UseSpeechRecognitionOptions

```ts
interface UseSpeechRecognitionOptions {
    readonly lang?: string | undefined;
    readonly interimResults?: boolean | undefined;
    readonly continuous?: boolean | undefined;
    readonly recognizer?: (() => SpeechRecognizer) | undefined;
    readonly onTranscriptChange?: ((transcript: string, details: SpeechTranscriptDetails) => void) | undefined;
    readonly onStart?: (() => void) | undefined;
    readonly onStop?: (() => void) | undefined;
    readonly onError?: ((error: SpeechRecognitionError) => void) | undefined;
}
```

Members (8):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `lang` | `readonly lang?: string \| undefined;` | optional | BCP 47 recognition language. Default: `document.documentElement.lang`, read when a session starts. |
| `interimResults` | `readonly interimResults?: boolean \| undefined;` | optional | Deliver interim (non-final) results. Default `true`. |
| `continuous` | `readonly continuous?: boolean \| undefined;` | optional | Keep listening after a pause until `stop()`. Default `true`. |
| `recognizer` | `readonly recognizer?: (() => SpeechRecognizer) \| undefined;` | optional | Replaces the browser recognizer, e.g. a cloud service. Called once per session. |
| `onTranscriptChange` | `readonly onTranscriptChange?: ((transcript: string, details: SpeechTranscriptDetails) => void) \| undefined;` | optional | The session transcript changed. |
| `onStart` | `readonly onStart?: (() => void) \| undefined;` | optional | The recognizer started capturing audio. |
| `onStop` | `readonly onStop?: (() => void) \| undefined;` | optional | The session ended (`stop()`, `abort()`, an error or the recognizer). |
| `onError` | `readonly onError?: ((error: SpeechRecognitionError) => void) \| undefined;` | optional | A recognizer error, or `'unsupported'` when `start()` has no recognizer. |

## Types

### SpeechRecognitionErrorCode

Kind: type

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/SpeechRecognitionErrorCode

```ts
type SpeechRecognitionErrorCode = 'no-speech' | 'aborted' | 'audio-capture' | 'network' | 'not-allowed' | 'service-not-allowed' | 'bad-grammar' | 'language-not-supported' | 'unsupported' | (string & {});
```

A Web Speech `SpeechRecognitionErrorEvent.error` code, or `'unsupported'`.

### SpeechRecognitionState

Kind: type

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/SpeechRecognitionState

```ts
type SpeechRecognitionState = 'idle' | 'listening' | 'stopped';
```

`'idle'` before the first session, `'listening'` while a session runs, `'stopped'` after it ends.

## Constants

### SPEECH_TO_TEXT_DEFAULT_LOCALE_TEXT

Kind: const

Page: https://salmonui.dev/docs/reference/inputs/speech-to-text/SPEECH_TO_TEXT_DEFAULT_LOCALE_TEXT

```ts
const SPEECH_TO_TEXT_DEFAULT_LOCALE_TEXT: Required<SpeechToTextLocaleText>;
```
