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-typewriter-animate

v1.2.3

Published

A React Typewriter that works on blocks of texts.

Downloads

23

Readme

React Typewriter Animate

banner

Useful Links

Note: Examples are written far below.

Table of content

Installation

npm install react-typewriter-animate

or

yarn add react-typewriter-animate

Usage in React


import Typewriter from 'react-typewriter-animate';
import "react-typewriter-animate/dist/Typewriter.css";

...

class Demo extends React.Components {
  render() {
    return (
      <div>
        <Typewriter
          dataToRotate={[
            [
              { type: "word", text: "Hello!" }
            ],
            [
              { type: "word", text: "I'm Anh Tu." }
            ]
          ]}
        />
      </div>
    )
  }
}

Options for Typewriter

type TypewriterProps = {
  dataToRotate: (WordBlock | ActionBlock)[][]; // Purpose: This is the array of all data (i.e banners) to type out. Each banner is a small array of WordBlock's and ActionBlock's

  cursor?: {
    char?: string; // Default: "│" | Purpose: Set cursor's appearance using string.
    cursorBlinkRate?: string; // Default: "900ms" | Purpose: Set blink speed of cursor.
    timeBeforeBlinkCursor?: number; // Default: 500ms | Purpose: When finish typing, set timeout before blinking cursor.
  };

  timeBeforeDelete?: number; // Default: 1000ms | Purpose: Wait time before deleting a banner after finish typing the whole banner.
  timeBeforeWriteNewRotateData?: number; // Default: 500ms | Purpose: Wait time before typing next banner.

  maxTypeSpeed?: number; // Default: 200ms | Purpose: Maximum typing speed.
  typeVariance?: number; // Default: 100ms | Purpose: Type speed range = maxTypespeed - typeVariance -> maxTypespeed. Type speed of each character can vary randomly within this range.

  maxDeleteSpeed?: number; // Default: 100ms | Purpose: Maximum deleting speed.
  deleteVariance?: number; // Default: 50ms | Purpose: Delete speed range = maxDeleteSpeed - deleteVariance -> maxDeleteSpeed. Delete speed of each character can vary randomly within this range.

  start?: boolean; // Default: true | Purpose: Determine whether Typewriter should start typing.
  loop?: boolean; // Default: false | Purpose: Determine whether Typewriter should loop these banners or stop after the last banner.

  containerClass?: string; // Purpose: Overwrite default styles of Typewriter-container.
  contentClass?: string; // Purpose: Overwrite default styles of Typewriter-content.
  cursorClass?: string; // Purpose: Overwrite default styles of Typewriter-cursor.
};

Note: You can access the class name keywords safely through TypewriterClassNames exported from the package.

enum TypewriterClassNames {
  Container = "Typewriter-container",
  Content = "Typewriter-content",
  Cursor = "Typewriter-cursor",
  Blink = "Typewriter-blink",
  Blink_Keyframe = "Typewriter-blink-keyframe"
}

Options for WordBlock

type WordBlock = {
  type: "word";
  text: string;

  spanClass?: string; // Purpose: Class name for current word block. Typewriter will wrap text inside a <span> for you.

  // Purpose: Override cursor style and appearance.
  cursor?: {
    char?: string; // Default "│"
    cursorClass?: string;
  };

  // Purpose: Override type speed and delete speed passed from main props.
  override?: {
    maxTypespeed?: number;
    typeVariance?: number;

    maxDeleteSpeed?: number;
    deleteVariance?: number;
  };
};

Options for ActionBlock

type DeleteBlock = {
  type: "action";
  action: "delete";
  amount: number;
  wait?: number; // Default wait time before delete is 1000ms
};

type FunctionBlock = {
  type: "action";
  action: "function";
  func: () => void;
  wait?: number; // Default wait time before doing function is 0ms
};

Examples

Basic Example

import Typewriter from "react-typewriter-animate";
import "react-typewriter-animate/dist/Typewriter.css";

class BasicExample extends React.Components {
  render() {
    return (
      <div className="root">
        <Typewriter
          dataToRotate={[
            // Banner 1
            [{ type: "word", text: "Hello!" }],
            // Banner 2
            [{ type: "word", text: "I'm Anh Tu." }]
          ]}
        />
      </div>
    );
  }
}

Example with override object in WordBlock

import Typewriter from "react-typewriter-animate";
import "react-typewriter-animate/dist/Typewriter.css";

class Example_Override_WordBlock extends React.Components {
  render() {
    return (
      <div className="root">
        <Typewriter
          dataToRotate={[
            // Banner 1
            [
              { type: "word", text: "I'm typing fast until here" },
              { type: "word", text: "...", override: { maxTypespeed: 600 } }
            ],
            // Banner 2
            [{ type: "word", text: "I'm Anh Tu." }]
          ]}
        />
      </div>
    );
  }
}

Example with override cursor in WordBlock

import Typewriter from "react-typewriter-animate";
import "react-typewriter-animate/dist/Typewriter.css";

class Example_Override_Cursor_WordBlock extends React.Components {
  render() {
    return (
      <div className="root">
        <Typewriter
          dataToRotate={[
            // Banner 1
            [
              { type: "word", text: "Changing cursor..." },
              {
                type: "word",
                text: "now!",
                cursor: { char: "😀", cursorClass: "css-class" }
              }
            ],
            // Banner 2
            [{ type: "word", text: "I'm Anh Tu." }]
          ]}
        />
      </div>
    );
  }
}

Example with styled WordBlock


import Typewriter from 'react-typewriter-animate';
import "react-typewriter-animate/dist/Typewriter.css";

class Example_Styled_WordBlock extends React.Components {
  render() {
    return (
      <div className="root">
        <Typewriter
          dataToRotate={[
            // Banner 1
            [
              { type: "word", text: "Hello", spanClass: "css-class-name" },
              { type: "word", text: " guys" }
            ],
            // Banner 2
            [
              { type: "word", text: "I'm Anh Tu." }
            ]
          ]}
        />
      </div>
    ):
  }
}

Example with overwrite style object: classes

import Typewriter from "react-typewriter-animate";
import "react-typewriter-animate/dist/Typewriter.css";
import "./overwrite.css";

/**
 *  Default styles:
 *
 *  .Typewriter-cursor {
 *     --cursor-blink-rate: 900ms;
 *
 *     color: inherit;
 *     transition: all 0.2s;
 *  }
 *
 *  .Typewriter-blink {
 *    animation: Typewriter-blink-keyframe var(--cursor-blink-rate) step-end infinite;
 *  },
 *
 *  @keyframes Typewriter-blink-keyframe {
 *    0% {
 *      opacity: 0;
 *    }
 *    50% {
 *      opacity: 1;
 *    }
 *  }
 */

/**
 *  ./overwrite.css file:
 *
 *  .typewriter-container-overwrite {
 *    color: "white";
 *    background-color: "blue";
 *  }
 */

class Example_Overwrite_Style extends React.Components {
  render() {
    return (
      <div className="root">
        <Typewriter
          containerClass="typewriter-container-overwrite"
          dataToRotate={[
            // Banner 1
            [
              { type: "word", text: "I'm typing fast until here" },
              { type: "word", text: "...", override: { maxTypespeed: 600 } }
            ],
            // Banner 2
            [{ type: "word", text: "I'm Anh Tu." }]
          ]}
        />
      </div>
    );
  }
}

Example with delete

import Typewriter from "react-typewriter-animate";
import "react-typewriter-animate/dist/Typewriter.css";

class Example_Delete extends React.Components {
  render() {
    return (
      <div className="root">
        <Typewriter
          dataToRotate={[
            // Banner 1
            [
              {
                type: "word",
                text: "What's up?",
                spanClass: "css-class-name"
              },
              { type: "action", action: "delete", amount: "up?".length },
              { type: "word", text: "going on, guys?" }
            ],
            // Banner 2
            [{ type: "word", text: "I'm Anh Tu." }]
          ]}
        />
      </div>
    );
  }
}

Example with combo: start flag and function

import Typewriter from "react-typewriter-animate";
import "react-typewriter-animate/dist/Typewriter.css";

class Example_Start_Function extends React.Components {
  state = {
    start: false
  };

  render() {
    const { start } = this.state;

    return (
      <div className="root">
        <h1 className="title">
          <Typewriter
            dataToRotate={[
              [
                { type: "word", text: "Here we go!" },
                {
                  type: "action",
                  action: "function",
                  func: () => {
                    console.log("Hello");
                    this.setState({
                      start: true
                    });
                  },
                  wait: 1000
                }
              ]
            ]}
          />
        </h1>

        <h1 className="description">
          <Typewriter
            start={start}
            dataToRotate={[
              [
                {
                  type: "word",
                  text: "Hello guys!"
                }
              ],
              [{ type: "word", text: "I'm Tu" }]
            ]}
          />
        </h1>
      </div>
    );
  }
}