// implement a voice component by speech synthesis to read the text with the voice of the user's choice import * as React from "react"; import { useState } from "react"; import { IconButton } from "./button"; import VoiceIcon from "../icons/voice.svg"; const voices = window.speechSynthesis.getVoices(); const speak = ({ text, voice, }: { text: string; voice?: SpeechSynthesisVoice; }) => { if (!text) return; const utterance = new SpeechSynthesisUtterance(text); utterance.voice = voice || voices[0]; window.speechSynthesis.speak(utterance); }; export function Voice(props: { text: string }) { const [voice, setVoice] = useState(null); return props.text ? (
{ if (voice) { speak({ text: props.text, voice }); } }} icon={} title="Read" />
) : ( <> ); }