From f2481e2489b2b97a7a23a42ed3c6e4b65136698a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Colla=C3=A7o?= Date: Tue, 1 Dec 2020 08:22:46 -0300 Subject: [PATCH] Add pause and resume features on useSpeechSynthesis. --- src/useSpeechSynthesis.jsx | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/useSpeechSynthesis.jsx b/src/useSpeechSynthesis.jsx index fbd5960..fe1b922 100644 --- a/src/useSpeechSynthesis.jsx +++ b/src/useSpeechSynthesis.jsx @@ -5,6 +5,7 @@ const useSpeechSynthesis = (props = {}) => { const [voices, setVoices] = useState([]); const [speaking, setSpeaking] = useState(false); const [supported, setSupported] = useState(false); + const [paused, setPaused] = useState(false); const processVoices = (voiceOptions) => { setVoices(voiceOptions); @@ -27,6 +28,7 @@ const useSpeechSynthesis = (props = {}) => { const handleEnd = () => { setSpeaking(false); + setPaused(false); onEnd(); }; @@ -56,14 +58,32 @@ const useSpeechSynthesis = (props = {}) => { const cancel = () => { if (!supported) return; setSpeaking(false); + setPaused(false); window.speechSynthesis.cancel(); }; + const pause = () => { + if (!supported) return; + setSpeaking(false); + setPaused(true); + window.speechSynthesis.pause(); + }; + + const resume = () => { + if (!supported) return; + setSpeaking(true); + setPaused(false); + window.speechSynthesis.resume(); + }; + return { supported, speak, speaking, cancel, + pause, + paused, + resume, voices, }; };