GithubHelp home page GithubHelp logo

jason89521 / react-typist-component Goto Github PK

View Code? Open in Web Editor NEW
26.0 3.0 4.0 2.48 MB

A react component to mimic typewriter effect

License: MIT License

CSS 0.28% TypeScript 94.70% HTML 1.61% JavaScript 3.41%
react typewriter react-component typist react-components reactjs

react-typist-component's Introduction

React Typist Component

Create typewriter effect by setting up a component's children directly.

Install

npm install react-typist-component
# or
yarn add react-typist-component

Example

import Typist from 'react-typist-component';

const MyComponent = () => {
  return (
    <Typist typingDelay={100} cursor={<span className='cursor'>|</span>}>
      This is a typo
      <br />
      <Typist.Backspace count={5} />
      <Typist.Delay ms={1500} />
      react component
      <Typist.Paste>
        <div>
          use
          <div>deeper div</div>
        </div>
      </Typist.Paste>
    </Typist>
  );
};

API reference

Typist

export type Delay = number | (() => number);
export type Splitter = (str: string) => string[];
export type TypistProps = {
  children: React.ReactNode;
  typingDelay?: Delay;
  backspaceDelay?: Delay;
  loop?: boolean;
  pause?: boolean;
  startDelay?: number;
  finishDelay?: number;
  onTypingDone?: () => void;
  splitter?: Splitter;
  cursor?: string | React.ReactElement;
  disabled?: boolean;
  restartKey?: any;
  hideCursorWhenDone?: boolean;
};

children

The contents that will be rendered with typewriter effect. It accepts nested elements, so you can easily style your contents.

Note that Typist treats the element whose children is null or undefined as a single token. For example:

const Foo = () => {
  return <div>Foo</div>;
};

// The text "Foo" will be displayed after "123" immediately instead of displayed seperately
const App = () => {
  return (
    <Typist>
      123
      <Foo />
    </Typist>
  );
};

typingDelay

Default: 75

The delay after typing a token. If you pass in a function, Typist will call the function after typing a token and use the return value as the delay.

backspaceDelay

Default: typingDelay

The delay after backspacing a token. If you pass in a function, Typist will call the function after backspacing a token and use the return value as the delay.

loop

Default: false

Typist will automatically restart the typing animation if this value is true.

pause

Default: false

Set to true if you want to pause the typing animation.

startDelay

Default: 0

Typist will wait for this delay before starting the typing animation.

finishDelay

Default: 0

Typist will wait for this delay after finishing the typing animation.

onTypingDone

This function will be called when the typing animation finishes. It will be called before waiting for the timeout with finishDelay.

splitter

Default: (str: string) => str.split('')

Typist will use this function to get tokens from strings. It may be useful when you want to split your string in different way. For example, you can use grapheme-splitter to split string if your string contains emoji.

import GraphemeSplitter from 'grapheme-splitter';

const splitter = (str: string) => {
  return new GraphemeSplitter().splitGraphemes(str);
};

const App = () => {
  return (
    <Typist splitter={splitter}>
      ๐Ÿ˜Ž๐Ÿ—‘๐Ÿฅตโš ๐Ÿ˜€๐Ÿ‘โœŒ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ“๐Ÿ’ก๐Ÿš€๐ŸŽ‚๐Ÿ˜“๐ŸŽˆ๐Ÿ’•๐Ÿ˜˜
      <Typist.Backspace count={16} />
    </Typist>
  );
};

cursor

Will be inserted after the last typed token.

disabled

Default: false

If this value is true, the result will be displayed immediately without typing animation. It can be useful when you want to display the final result if a user has visited the typing animation.

restartKey

Typist will restart the typing animation whenever restartKey changes.

hideCursorWhenDone

Hide the cursor when the typing animation is done.

Typist.Backspace

type Props = {
  count: number;
};

count

The number of tokens that will be backspaced.

Typist.Delay

type Props = {
  ms: number;
};

ms

The duration of the delay in milliseconds.

Typist.Paste

type Props = {
  children: React.ReactNode;
};

children

Children inside this component will be pasted without typewriter effect. Do not wrap another Paste inside this component, otherwise Typist will produce weird behavior.

react-typist-component's People

Contributors

dependabot[bot] avatar jason89521 avatar ziayanj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-typist-component's Issues

[Bug] Implicitly has an 'any' type

Describe the bug
Incorrect default export

To Reproduce
Steps to reproduce the behavior:

  1. import Typist from "react-typist-component"
  2. See error

Error
There are types at 'c:/.../node_modules/react-typist-component/dist/types/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'react-typist-component' library may need to update its package.json or typings.ts(7016)

Additional context
https://arethetypeswrong.github.io/?p=react-typist-component%401.0.5

Cant Resolve Module (v1.0.0)

Great approach thanks, but am getting

`Module not found: Can't resolve 'react-typist-component'

7 | import Typist from 'react-typist-component';`

with v1 of this module (v0.5.0 works as expected)

[Bug] Hide Cursor When Done

Could you please implement a hideCursor property to hide the cursor when done like it is possible to do with react-typist?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.