diff --git a/src/Components/Article/MainContainer.jsx b/src/Components/Article/MainContainer.jsx index 6dd10c83..10180f2d 100644 --- a/src/Components/Article/MainContainer.jsx +++ b/src/Components/Article/MainContainer.jsx @@ -9,6 +9,7 @@ import {NamePlate, WarppedDate, GrayText, DisplayItem, BlogTitle, BlogSubTitle, import {ArticleHr} from '../../Layout/Hr/styledHr'; import {AlertP} from '../../Layout/Paragraph/styledParagraph'; import {AlertLink} from '../../Layout/ATag/styledATag'; +import TextToSpeech from './TextToSpeech'; import marked from 'marked'; import Newsletter from '../Subscribe/Newsletter'; import RelatedPosts from './RelatedPosts'; @@ -47,7 +48,7 @@ const MainContainer = (props) => {  ·  {readTime} min read    ·  -   Listen + diff --git a/src/Components/Article/TextToSpeech.jsx b/src/Components/Article/TextToSpeech.jsx new file mode 100644 index 00000000..999a9d3c --- /dev/null +++ b/src/Components/Article/TextToSpeech.jsx @@ -0,0 +1,77 @@ +import React, { useState, useEffect } from "react"; +import { DisplayItem } from '../../Layout/Text/styledText'; +import { AiFillPlayCircle, AiFillPauseCircle, AiFillStop } from 'react-icons/ai'; + +const TextToSpeech = ({ text }) => { + const [isPaused, setIsPaused] = useState(false); + const [utterance, setUtterance] = useState(null); + + useEffect(() => { + const synth = window.speechSynthesis; + const u = new SpeechSynthesisUtterance(text); + + setUtterance(u); + setIsPaused(true) + + return () => { + synth.cancel(); + }; +}, [text]); + +const handlePlay = () => { + const synth = window.speechSynthesis; + const voices = synth.getVoices(); + + utterance.voice = voices[15]; + utterance.rate = 0.95; + + if (isPaused) { + synth.resume(); + } + + synth.speak(utterance); + + setIsPaused(false); + }; + + const handlePause = () => { + const synth = window.speechSynthesis; + + synth.pause(); + + setIsPaused(true); + }; + + const handleStop = () => { + const synth = window.speechSynthesis; + + synth.cancel(); + + setIsPaused(true); + }; + + return ( +
+ { + isPaused ? + + +   {isPaused ? "Listen" : "Resume"} + + : + + +   Pause + + + } +    + + +   Stop + +
+ ); +}; + +export default TextToSpeech; \ No newline at end of file diff --git a/src/Layout/Text/styledText.jsx b/src/Layout/Text/styledText.jsx index 10d73d57..0517e3be 100644 --- a/src/Layout/Text/styledText.jsx +++ b/src/Layout/Text/styledText.jsx @@ -54,6 +54,9 @@ export const DisplayItem= styled.div` ${props => props.Green && css` color: green; `} + ${props => props.Red && css` + color: red; + `} `; export const BlogTitle= styled.h2`