npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-speech-kit

v3.0.1

Published

React hooks for in-browser Speech Recognition and Speech Synthesis.

Downloads

10,751

Readme

react-speech-kit 🎤    CircleCI codecov

React hooks for in-browser Speech Recognition and Speech Synthesis. Demo here

Table of Contents

Install

yarn add react-speech-kit

Examples and Demo

A full example can be found here. The source code is in the examples directory.

useSpeechSynthesis

A react hook for the browser's SpeechSynthesis API. It exposes the options and controls to the underlying SpeechSynthesis in the browser.

Usage

import React, { useState } from 'react';
import { useSpeechSynthesis } from 'react-speech-kit';

function Example() {
  const [value, setValue] = useState('');
  const { speak } = useSpeechSynthesis();

  return (
    <div>
      <textarea
        value={value}
        onChange={(event) => setValue(event.target.value)}
      />
      <button onClick={() => speak({ text: value })}>Speak</button>
    </div>
  );
}

Args

onEnd

function() optional

Called when SpeechSynthesis finishes reading the text or is cancelled. It is called with no argumnents. Very useful for triggering a state change after something has been read.

Returns

useSpeechSynthesis returns an object which contains the following:

speak

function({ text: string, voice: SpeechSynthesisVoice })

Call to make the browser read some text. You can change the voice by passing an available SpeechSynthesisVoice (from the voices array). Note that some browsers require a direct user action initiate SpeechSynthesis. To make sure it works, it is recommended that you call speak for the first time in a click event handler.

cancel

function()

Call to make SpeechSynthesis stop reading.

speaking

boolean

True when SpeechSynthesis is actively speaking.

supported

boolean

Will be true if the browser supports SpeechSynthesis. Keep this in mind and use this as a guard before rendering any control that allow a user to call speak.

voices

[SpeechSynthesisVoice]

An array of available voices which can be passed to the speak function. An example SpeechSynthesisVoice voice has the following properties.

{
  default: true
  lang: "en-AU"
  localService: true
  name: "Karen"
  voiceURI: "Karen"
}

In some browsers voices load asynchronously. In these cases, the array will be empty until they are available.

useSpeechRecognition

A react hook for the browser's SpeechRecognition API.

Usage

import React, { useState } from 'react';
import { useSpeechRecognition } from 'react-speech-kit';

function Example() {
  const [value, setValue] = useState('');
  const { listen, listening, stop } = useSpeechRecognition({
    onResult: (result) => {
      setValue(result);
    },
  });

  return (
    <div>
      <textarea
        value={value}
        onChange={(event) => setValue(event.target.value)}
      />
      <button onMouseDown={listen} onMouseUp={stop}>
        🎤
      </button>
      {listening && <div>Go ahead I'm listening</div>}
    </div>
  );
}

Args

onEnd

function()

Called when SpeechRecognition stops listening.

onResult

function(string)

Called when SpeechRecognition has a result. It is called with a string containing a transcript of the recognized speech.

Returns

useSpeechRecognition returns an object which contains the following:

listen

function({ interimResults: boolean, lang: string })

Call to make the browser start listening for input. Every time it processes a result, it will forward a transcript to the provided onResult function. You can modify behavior by passing the following arguments:

  • lang
    string
    The language the SpeechRecognition will try to interpret the input in. Use the languageCode from this list of languages that Chrome supports (here) e.g: "en-AU". If not specified, this defaults to the HTML lang attribute value, or the user agent's language setting if that isn't set either.

  • interimResults
    boolean (default: true)
    SpeechRecognition can provide realtime results as it's trying to figure out the best match for the input. Set to false if you only want the final result.

stop

function()

Call to make SpeechRecognition stop listening. This will call the provided onEnd function as well.

listening

boolean

True when SpeechRecognition is actively listening.

supported

boolean

Will be true if the browser supports SpeechRecognition. Keep this in mind and use this as a guard before rendering any control that allow a user to call listen.