react-text-to-speech
Version:
An easy-to-use React.js component that leverages the Web Speech API to convert text to speech.
76 lines (73 loc) • 2.92 kB
TypeScript
import { DetailedHTMLProps, HTMLAttributes, ReactNode, JSX } from 'react';
type DirectiveEvent = "change" | "pause" | null;
type HighlightMode = "word" | "sentence" | "line" | "paragraph";
type SpanProps = DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>;
type SpeakingWord = {
index: string;
charIndex: number;
length: number;
} | null;
type SpeechStatus = "started" | "paused" | "stopped" | "queued";
type SpeechSynthesisErrorHandler = (error: Error) => any;
type SpeechSynthesisEventHandler = () => any;
type SpeechSynthesisEventName = "word" | "sentence";
type SpeechSynthesisUtteranceProps = {
pitch?: number;
rate?: number;
volume?: number;
lang?: string;
voiceURI?: string | string[];
};
type UseSpeechOptions = SpeechSynthesisUtteranceProps & {
text: ReactNode;
autoPlay?: boolean;
preserveUtteranceQueue?: boolean;
highlightText?: boolean;
showOnlyHighlightedText?: boolean;
highlightMode?: HighlightMode;
highlightProps?: SpanProps;
enableDirectives?: boolean;
maxChunkSize?: number;
onError?: SpeechSynthesisErrorHandler;
onStart?: SpeechSynthesisEventHandler;
onResume?: SpeechSynthesisEventHandler;
onPause?: SpeechSynthesisEventHandler;
onStop?: SpeechSynthesisEventHandler;
onBoundary?: SpeechSynthesisEventHandler;
onQueueChange?: QueueChangeEventHandler;
};
type IconProps = DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>;
type Button = JSX.Element | string | null;
type Children = (childrenOptions: ChildrenOptions) => ReactNode;
type ChildrenOptions = {
speechStatus?: SpeechStatus;
isInQueue?: boolean;
start?: Function;
pause?: Function;
stop?: Function;
};
type DivProps = DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
type SpeechProps = UseSpeechOptions & {
id?: string;
startBtn?: Button;
pauseBtn?: Button;
stopBtn?: Button;
useStopOverPause?: boolean;
props?: DivProps;
children?: Children;
};
type QueueChangeEventHandler = (queue: SpeechUtterancesQueue) => any;
type SpeechQueueItem = {
text: string;
utterance: SpeechSynthesisUtterance;
setSpeechStatus: SpeechStatusUpdater;
};
type SpeechQueue = SpeechQueueItem[];
type SpeechStatusUpdater = (newStatus: SpeechStatus) => void;
type SpeechUtterancesQueue = Partial<SpeechSynthesisUtterance>[];
type Index = string | number;
type State = {
stopReason: "auto" | "change" | "manual";
};
type Words = string | Words[];
export type { Button, Children, ChildrenOptions, DirectiveEvent, DivProps, HighlightMode, IconProps, Index, QueueChangeEventHandler, SpanProps, SpeakingWord, SpeechProps, SpeechQueue, SpeechQueueItem, SpeechStatus, SpeechStatusUpdater, SpeechSynthesisErrorHandler, SpeechSynthesisEventHandler, SpeechSynthesisEventName, SpeechSynthesisUtteranceProps, SpeechUtterancesQueue, State, UseSpeechOptions, Words };