import { useRef } from 'react'; import useMount from './useMount'; import useSetState from './useSetState'; var voices = typeof window === 'object' && typeof window.speechSynthesis === 'object' ? window.speechSynthesis.getVoices() : []; var useSpeech = function (text, opts) { if (opts === void 0) { opts = {}; } var _a = useSetState({ isPlaying: false, lang: opts.lang || 'default', voice: opts.voice || voices[0], rate: opts.rate || 1, pitch: opts.pitch || 1, volume: opts.volume || 1, }), state = _a[0], setState = _a[1]; var uterranceRef = useRef(null); useMount(function () { var utterance = new SpeechSynthesisUtterance(text); opts.lang && (utterance.lang = opts.lang); opts.voice && (utterance.voice = opts.voice); utterance.rate = opts.rate || 1; utterance.pitch = opts.pitch || 1; utterance.volume = opts.volume || 1; utterance.onstart = function () { return setState({ isPlaying: true }); }; utterance.onresume = function () { return setState({ isPlaying: true }); }; utterance.onend = function () { return setState({ isPlaying: false }); }; utterance.onpause = function () { return setState({ isPlaying: false }); }; uterranceRef.current = utterance; window.speechSynthesis.speak(uterranceRef.current); }); return state; }; export default useSpeech;