# use-typewriter-effect

> typewriter hook for react components

Latest version **1.0.5** (published 2022-01-10) · ISC license · 0 weekly downloads

## Install

```sh
npm install use-typewriter-effect
pnpm add use-typewriter-effect
yarn add use-typewriter-effect
bun add use-typewriter-effect
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2022-01-10 |
| First published | 2022-01-07 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 19.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | reifocs |
| Keywords | react, typewriter, type, react-hooks |

## Links

- npm: https://www.npmjs.com/package/use-typewriter-effect
- Repository: https://github.com/reifocS/use-typewriter-effect
- Homepage: https://github.com/reifocS/use-typewriter-effect#readme
- Issues: https://github.com/reifocS/use-typewriter-effect/issues
- npm.io page: https://npm.io/package/use-typewriter-effect

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.5 (latest) — 2022-01-10
- 1.0.4 — 2022-01-07
- 1.0.2 — 2022-01-07
- 1.0.1 — 2022-01-07
- 1.0.0 — 2022-01-07

## README

# use-typewriter-effect

> Highly flexible React hook with declarative API for typewriting effect

## Install

```bash
npm install --save use-typewriter-effect
```
## Demo
https://vincent-escoffier.vercel.app/typewriter  

## Basic Usage

```tsx
import React from "react";
import useTypewriterEffect, {
  getTypewriter,
  useCursor,
} from "use-typewriter-effect";

function App() {
  const [state, dispatch, isTyping] = useTypewriterEffect();
  const cursor = useCursor(isTyping);

  React.useEffect(() => {
    getTypewriter(dispatch)
      .type("Hello world!")
      .pauseFor(1000)
      .deleteSome("6")
      .type("universe!")
      .pauseFor(1000)
      .deleteAll()
      .setLoop(true)
      .trigger();
      
      // React guaranties that dispatch reference is stable, so we can safely
      // declare it as a dependency
  }, [dispatch]);

  return (
    <main>
      <p>
        {state}
        <span style={{ visibility: cursor ? "visible" : "hidden" }}>|</span>
      </p>
    </main>
  );
}
```
## Example from the demo
```tsx
import React from "react";
import useTypewriterEffect, {
    getTypewriter,
    useCursor,
} from "use-typewriter-effect";

function App() {
    const [state, dispatch, isTyping] = useTypewriterEffect();
    const cursor = useCursor(isTyping);

    React.useEffect(() => {
        getTypewriter(dispatch)
            .type("Hello world!\n How are you")
            .pauseFor(1000)
            .deleteAll()
            .type("This is a typewriter effect")
            .pauseFor(1000)
            .deleteSome(6)
            .type("hook!")
            .pauseFor(1000)
            .deleteAll()
            .setLoop(true)
            .trigger()
    }, [dispatch]);

    const sentences = state.split("\n");
    const lastSentence = sentences.pop();
    return (
        <main>
            <input type="text" placeholder={lastSentence}/>

            <div>
                {sentences.map((sen, i) => (
                    <p key={i}><span style={{color:"cyan"}}>$</span> {sen}</p>
                ))}
                <p>
                    <span style={{color:"cyan"}}>$</span> {lastSentence}
                    <span style={{visibility: cursor ? "visible" : "hidden"}}>|</span>
                </p>
            </div>
        </main>
    );
}


```
## Typewriter API

In order to access the typewriter API, you must first call getTypewriter and pass it the typewriter hook dispatch.  
Rendering will start after the trigger method is called. This gives you full control over the typing sequence.
You can use it inside a useEffect or in event handlers.

### Options

| Name       | Type     | Default | Description                                    |
| ---------- | -------- | ------- | ---------------------------------------------- |
| type        | string   | null     | Write the given string. |
| pauseFor        | number   | null      | Pause the typing. |
| deleteSome  | number   | null    |  Delete some characters.        |
| deleteAll |    |     | Delete all typed characters so far.         |
| setLoop |  boolean  |     | If the given sequence of events should loop.           |
| setDelay |   number | [60:120]  | Typing speed (ms)        |
| trigger |    |     | Update typewriter state with the previously built sequence of events and start rendering        |


## License

MIT

---
_Source: https://npm.io/package/use-typewriter-effect · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
