Gathering detailed insights and metrics for react-dictate-button
Gathering detailed insights and metrics for react-dictate-button
Gathering detailed insights and metrics for react-dictate-button
Gathering detailed insights and metrics for react-dictate-button
npm install react-dictate-button
[1.2.2] - 2020-02-27
Published on 28 Feb 2020
[1.2.1] - 2019-12-04
Published on 04 Dec 2019
[1.2.0] - 2019-12-03
Published on 03 Dec 2019
[1.1.3] - 2018-07-19
Published on 19 Jul 2018
[1.1.2] - 2018-06-29
Published on 29 Jun 2018
[1.1.1] - 2018-06-29
Published on 29 Jun 2018
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
25 Stars
68 Commits
1 Forks
2 Watching
6 Branches
2 Contributors
Updated on 02 Aug 2024
JavaScript (91.14%)
HTML (7.38%)
CSS (1.28%)
Shell (0.21%)
Cumulative downloads
Total Downloads
Last day
-3.1%
1,484
Compared to previous day
Last week
-5.8%
7,919
Compared to previous week
Last month
14.7%
34,296
Compared to previous month
Last year
33.4%
333,792
Compared to previous year
A button to start speech recognition using Web Speech API, with an easy to understand event lifecycle.
react@>=16.8.0
and core-js@3
SpeechGrammarList
is only constructed when grammar
props is presentspeechRecognition
prop is not present, capability detection is now done through window.mediaDevices.getUserMedia
Try out this component at github.io/compulim/react-dictate-button.
Reasons why we need to build our own component, instead of using existing packages on NPM:
First, install our production version by npm install react-dictate-button
. Or our development version by npm install react-dictate-button@master
.
1import DictateButton from 'react-dictate-button'; 2 3export default () => ( 4 <DictateButton 5 className="my-dictate-button" 6 grammar="#JSGF V1.0; grammar districts; public <district> = Tuen Mun | Yuen Long;" 7 lang="en-US" 8 onDictate={this.handleDictate} 9 onProgress={this.handleProgress} 10 > 11 Start/stop 12 </DictateButton> 13);
Name | Type | Default | Description |
---|---|---|---|
className | string | undefined | Class name to apply to the button |
disabled | boolean | false | true to abort ongoing recognition and disable the button, otherwise, false |
extra | { [key: string]: any } | {} | Additional properties to set to SpeechRecognition before start , useful when bringing your own SpeechRecognition |
grammar | string | undefined | Grammar list in JSGF format |
lang | string | undefined | Language to recognize, for example, 'en-US' or navigator.language |
speechGrammarList | any | window.SpeechGrammarList (or vendor-prefixed) | Bring your own SpeechGrammarList |
speechRecognition | any | window.SpeechRecognition (or vendor-prefixed) | Bring your own SpeechRecognition |
Note: change of
extra
,grammar
,lang
,speechGrammarList
, andspeechRecognition
will not take effect until next speech recognition is started.
Name | Signature | Description |
---|---|---|
onClick | (event: MouseEvent) => void | Emit when the user click on the button, preventDefault will stop recognition from starting |
onDictate |
({ result: { confidence: number, transcript: number }, type: 'dictate' }) => void | Emit when recognition is completed |
onError | (event: SpeechRecognitionErrorEvent) => void | Emit when error has occurred or recognition is interrupted, see below |
onProgress |
({ abortable: boolean, results: [{ confidence: number, transcript: number }], type: 'progress' }) => void | Emit for interim results, the array contains every segments of recognized text |
onRawEvent | (event: SpeechRecognitionEvent) => void |
Emit for handling raw events from
SpeechRecognition
|
Although previous versions exported a React Context, it is recommended to use the hooks interface.
Name | Signature | Description |
---|---|---|
useAbortable | [boolean] | If ongoing speech recognition can be aborted, true , otherwise, false |
useReadyState | [number] | Returns the current state of recognition, refer to this section |
useSupported | [boolean] | If speech recognition is supported, true , otherwise, false |
To determines whether speech recognition is supported in the browser:
speechRecognition
prop is undefined
window.navigator.mediaDevices
and window.navigator.mediaDevices.getUserMedia
are falsy, it is not supported
window.SpeechRecognition
and vendor-prefixed are falsy, it is not supportednot-allowed
error code, it is not supportedEven the browser is on an insecure HTTP connection,
window.SpeechRecognition
(or vendor-prefixed) will continue to be truthy. Instead,mediaDevices.getUserMedia
is used for capability detection.
One of the design aspect is to make sure events are easy to understand and deterministic. First rule of thumb is to make sure onProgress
will lead to either onDictate
or onError
. Here are some samples of event firing sequence (tested on Chrome 67):
onProgress({})
(just started, therefore, no results
)onProgress({ results: [] })
onDictate({ result: ... })
onProgress({})
onDictate({})
(nothing is recognized, therefore, no result
)onProgress({})
onError({ error: 'no-speech' })
onProgress({})
onProgress({ results: [] })
props.disabled
to false
, abort recognitiononError({ error: 'aborted' })
onError({ error: 'not-allowed' })
Instead of passing child elements, you can pass a function to render different content based on ready state. This is called function as a child.
Ready state | Description |
---|---|
0 | Not started |
1 | Starting recognition engine, recognition is not ready until it turn to 2 |
2 | Recognizing |
3 | Stopping |
For example,
1<DictateButton> 2 {({ readyState }) => 3 readyState === 0 ? 'Start' : readyState === 1 ? 'Starting...' : readyState === 2 ? 'Listening...' : 'Stopping...' 4 } 5</DictateButton>
You can build your own component by copying our layout code, without messing around the logic code behind the scene. For details, please refer to DictateButton.js
, DictateCheckbox.js
, and DictationTextbox.js
.
In addition to <button>
, we also ship <input type="checkbox">
out of the box. The checkbox version is better suited for toggle button scenario and web accessibility. You can use the following code for the checkbox version.
1import { DictateCheckbox } from 'react-dictate-button'; 2 3export default () => ( 4 <DictateCheckbox 5 className="my-dictate-checkbox" 6 grammar="#JSGF V1.0; grammar districts; public <district> = Redmond | Bellevue;" 7 lang="en-US" 8 onDictate={this.handleDictate} 9 onProgress={this.handleProgress} 10 > 11 Start/stop 12 </DictateCheckbox> 13);
We also provide a "textbox with dictate button" version. But instead of shipping a full-fledged control, we make it a minimally-styled control so you can start copying the code and customize it in your own project. The sample code can be found at DictationTextbox.js.
onstart
, onaudiostart
, onsoundstart
, onspeechstart
onresult
may not fire in some cases, onnomatch
is not fired in ChromeonProgress
, then either onDictate
or onError
onError
Please feel free to file suggestions.
readyState
is 1 or 3 (transitioning), the underlying speech engine cannot be started/stopped until the state transition is complete
Composer.js
, how about
SpeechRecognition
into another object with simpler event model and readyState
Composer.js
to bridge the new SimpleSpeechRecognition
model and React ContextSimpleSpeechRecognition
so people not on React can still benefit from the simpler event modelLike us? Star us.
Want to make it better? File us an issue.
Don't like something you see? Submit a pull request.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/17 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
92 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More