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-typing-game-hook-v2

v1.3.7

Published

Easily create typing game functionality (10fastestfinger, monkeytype, keybr, etc) with this react hook that handles the typing logic

Downloads

17

Readme

GitHub Workflow Status

npm npm bundle size

React Typing Game Hook

Easily create typing games functionalty (10fastestfinger, monkeytype, keybr, etc) with this react hook that handles the typing logic

About

This hook takes care of the states and the little details that goes on when users type in a typing game/test/challenge! Your part is to just capture the inputs from the user!

Demos

Simple Demo

Typing on Input Demo

Typing on the text Demo

Getting Started

Importing

npm add react-typing-game-hook

Usage

Here's a simple example of it (live here)

import React from 'react';
// import it
import useTypingGame from 'react-typing-game-hook';

const TypingGameComponent = () => {
  // Call the hook
  const {
    states: { chars, charsState },
    actions: { insertTyping, resetTyping, deleteTyping },
  } = useTypingGame('Click on me and start typing away!');

  // Capture and display!
  return (
    <h1
      onKeyDown={e => {
        const key = e.key;
        if (key === 'Escape') {
          resetTyping();
        } else if (key === 'Backspace') {
          deleteTyping(false);
        } else if (key.length === 1) {
          insertTyping(key);
        }
        e.preventDefault();
      }}
      tabIndex={0}
    >
      {chars.split('').map((char, index) => {
        let state = charsState[index];
        let color = state === 0 ? 'black' : state === 1 ? 'green' : 'red';
        return (
          <span key={char + index} style={{ color }}>
            {char}
          </span>
        );
      })}
    </h1>
  );
};
export default TypingGameComponent;

Have a look at the demos on codesandbox above as well as the hooks details below to find out more!

Hook Details

Structure Breakdown

const {
  states: {
    startTime,
    endTime,
    chars,
    charsState,
    length,
    currIndex,
    currChar,
    correctChar,
    errorChar,
    phase,
  },
  actions: {
    resetTyping,
    endTyping,
    insertTyping,
    deleteTyping,
    setCurrIndex,
    getDuration,
  },
} = useTypingGame(text, {
  skipCurrentWordOnSpace: true,
  pauseOnError: false,
  countErrors: 'everytime',
});

Enumeration Breakdown

import { PhaseType, CharStateType } from 'react-typing-game-hook';

PhaseType {
  NotStarted = 0,
  Started = 1,
  Ended = 2,
}

CharStateType {
  Incomplete = 0,
  Correct = 1,
  Incorrect = 2,
}

Hook Parameters

| Name | Functionalty | Default value | | :------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-----------: | | skipCurrentWordOnSpace | When true, moves on to the next word when space is inputted. Otherwise, it moves on to the next character instead | true | | pauseOnError | When true, stays on the same character until it is correctly inputted. Otherwise, it moves on to the next character instead | false | | countErrors | When value is 'everytime', count errors anytime a mistake is made. When value is 'once', count errors only once for each mistake made at the position where the letter is typed. | 'everytime' |

Hook States

| Name | Functionalty | Data Type | Initial value | | :-------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :---------------: | :------------------------------------------------------------: | | startTime | Time in milliseconds (since the Unix Epoch) when the typing started. | number | Prior to when the typing starts, it is null | | endTime | Time in milliseconds(since the Unix Epoch) when the typing test ended. | number | Prior to when the typing ended, it is null | | chars | The inputted text to be used for typing. | string | - | | length | The lengh of the inputted text | number | Length of the inputted text chars | | charsState | Array of each character's state of the inputted text. Each item represents the corresponding chararacter of the text inputted. CharStateType.Incomplete - has yet to be determined to be correct or incorrect CharStateType.Correct - is determined to be correct CharStateType.Incorrect - is determined to be incorrect.Use of CharStateType over numeric literal is recommended | CharStateType[] | Array length of chars filled with CharStateType.Incomplete | | currIndex | Current character index of the text the user have typed till. | number | -1 | | currChar | Current character the user have typed till. | string | '' | | correctChar | Number of correct character the user had typed. | number | 0 | | errorChar | Number of incorrect character the user had typed. | number | 0 | | phase | Represent the current state of the typing test. PhaseType.NotStarted - typing haven't started PhaseType.Started - typing started PhaseType.Ended - typing ended Use of PhaseType over numeric literal is recommended | PhaseType | PhaseType.NotStarted |

Hook Methods

| Name | Functionalty | Signature | Return value | | :--------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :----------------------------------: | :--------------------------------------: | | insertTyping | Insert a character into the current typing sequence. It will start the typing if it hasn't been started as well as end the typing for you when the last character has been entered. | insertTyping(char:string) | - | | deleteTyping | It only works when the typing has been started. Allows for the current word in the typing sequence to be deleted when passed with a parameter of true, otherwise it deletes only a character from the current typing (default behavior). | deleteTyping(deleteWord?: boolean) | - | | resetTyping | Reset to its initial state. | resetTyping() | - | | endTyping | Ends the current typing sequence if it had started. States persist when ended and endTime is captured. | endTyping() | - | | setCurrIndex | Set the current index manually. Only works when phase is PhaseType.NotStarted (haven't started typing) or PhaseType.Started (during typing). | setCurrIndex(index:number) | true if successfull, otherwise false | | getDuration | Duration in milliseconds since the typing started. | getDuration() | 0 if the typing has yet to start. |