forked from streamich/react-use
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
82 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# `useSpeech` | ||
|
||
React UI hook that synthesizes human voice that speaks a given string. | ||
|
||
|
||
## Usage | ||
|
||
```jsx | ||
import {useSpeech} from 'react-use'; | ||
|
||
const Demo = () => { | ||
const state = useSpeech('Hello world!'); | ||
|
||
return ( | ||
<pre>{JSON.stringify(state, null, 2)}</pre> | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {storiesOf} from '@storybook/react'; | ||
import * as React from 'react'; | ||
import {useSpeech} from '..'; | ||
|
||
const Demo = () => { | ||
const state = useSpeech('Hello world!'); | ||
|
||
return ( | ||
<pre>{JSON.stringify(state, null, 2)}</pre> | ||
); | ||
}; | ||
|
||
storiesOf('useSpeech', module) | ||
.add('Example', () => | ||
<Demo/> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
import {useEffect} from './react'; | ||
|
||
const useMount = (mount) => { | ||
useEffect(() => { | ||
if (mount) mount(); | ||
}, []); | ||
}; | ||
const useMount = (mount) => useEffect(mount, []); | ||
|
||
export default useMount; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {useRef} from './react'; | ||
import useSetState from './useSetState'; | ||
import useMount from './useMount'; | ||
|
||
export interface SpeechState { | ||
isPlaying: boolean; | ||
volume: number; | ||
} | ||
|
||
export interface SpeechOptions { | ||
lang?: any; | ||
pitch?: number; | ||
rate?: number; | ||
voice?: any; | ||
volume?: number; | ||
} | ||
|
||
const useSpeech = (text: string, opts: SpeechOptions = {}): SpeechState => { | ||
const [state, setState] = useSetState<SpeechState>({ | ||
isPlaying: false, | ||
volume: opts.volume || 1, | ||
}); | ||
|
||
const uterranceRef = useRef<SpeechSynthesisUtterance | null>(null); | ||
|
||
useMount(() => { | ||
const utterance = new SpeechSynthesisUtterance(text); | ||
utterance.volume = opts.volume || 1; | ||
utterance.onstart = () => setState({isPlaying: true}); | ||
utterance.onresume = () => setState({isPlaying: true}); | ||
utterance.onend = () => setState({isPlaying: false}); | ||
utterance.onpause = () => setState({isPlaying: false}); | ||
uterranceRef.current = utterance; | ||
window.speechSynthesis.speak(uterranceRef.current); | ||
}); | ||
|
||
return state; | ||
}; | ||
|
||
export default useSpeech; |