Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/elements/src/speech-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type SpeechInputMode = "speech-recognition" | "media-recorder" | "none";

export type SpeechInputProps = ComponentProps<typeof Button> & {
onTranscriptionChange?: (text: string) => void;
preferWebSpeechApi?: boolean;
/**
* Callback for when audio is recorded using MediaRecorder fallback.
* This is called in browsers that don't support the Web Speech API (Firefox, Safari).
Expand All @@ -80,12 +81,12 @@ export type SpeechInputProps = ComponentProps<typeof Button> & {
lang?: string;
};

const detectSpeechInputMode = (): SpeechInputMode => {
const detectSpeechInputMode = (preferWebSpeechApi: boolean): SpeechInputMode => {
if (typeof window === "undefined") {
return "none";
}

if ("SpeechRecognition" in window || "webkitSpeechRecognition" in window) {
if (preferWebSpeechApi && ("SpeechRecognition" in window || "webkitSpeechRecognition" in window)) {
return "speech-recognition";
}

Expand All @@ -100,6 +101,7 @@ export const SpeechInput = ({
className,
onTranscriptionChange,
onAudioRecorded,
preferWebSpeechApi = true,
lang = "en-US",
...props
}: SpeechInputProps) => {
Expand All @@ -115,8 +117,8 @@ export const SpeechInput = ({

// Detect mode on mount
useEffect(() => {
setMode(detectSpeechInputMode());
}, []);
setMode(detectSpeechInputMode(preferWebSpeechApi));
}, [preferWebSpeechApi]);

// Initialize Speech Recognition when mode is speech-recognition
useEffect(() => {
Expand Down