[NPM Repository](https://npmjs.org/typewriter-effect)
[JSFiddle Example](https://jsfiddle.net/hzfxp2L9/)
[Emoji Example](https://codesandbox.io/s/typewriter-effect-emojis-pgz6e)
## CDN
You can use the CDN version of this plugin for fast and easy setup.
```html
```
If unpkg is down, you can use jsDelivr instead.
```html
```
## Upgrading to v3
Version 3 drops support for Internet Explorer 11 and other outdated browsers, and changes how the React component handles `options` changes. See [CHANGELOG.md](CHANGELOG.md) for all breaking changes.
## Installation
You can install Typewriterjs with just one command and you're good to go
```shell
# with npm
npm i typewriter-effect
# with yarn
yarn add typewriter-effect
```
## Browser support
Supported browsers follow [Baseline Widely available](https://web.dev/baseline), which covers browser versions released in the last 30 months, including Chromium based browsers such as Samsung Internet and Opera. This is set using the browserslist query `baseline widely available with downstream` in `package.json`.
## Building
You will need to bundle the javascript before it can be used, this can be done using `npm run build:dev` for development or `npm run build:prod` for production.
Building and running the tests requires Node.js 24 or newer. If you use [nvm](https://github.com/nvm-sh/nvm), run `nvm use` to switch to the version in `.nvmrc`.
This only applies to working on this project and the command line tool. Websites and apps that install `typewriter-effect` can use any Node.js version.
## Core
This is the base version of the typewriter effect. It will need a the bundled version of the javascript to work correctly.
See examples in the 'examples' folder.
```js
import Typewriter from 'typewriter-effect/dist/core';
new Typewriter('#typewriter', {
strings: ['Hello', 'World'],
autoStart: true,
});
```
## Options
| Name | Type | Default value | Description |
| --- | --- | --- | --- |
| strings | String or Array | null | Strings to type out when using ``autoStart`` option |
| cursor | String | Pipe character | String value to use as the cursor. |
| delay | 'natural', Number or ``{ min, max }`` | 'natural' | The delay between each key when typing. Use ``{ min: 50, max: 100 }`` to pick a random delay between two values. |
| deleteSpeed | 'natural', Number or ``{ min, max }`` | 'natural' | The delay between deleting each character. Use ``{ min: 20, max: 40 }`` to pick a random delay between two values. |
| loop | Boolean | false | Whether to keep looping or not. |
| autoStart | Boolean | false | Whether to autostart typing strings or not. You are required to provide ``strings`` option. |
| pauseFor | Number | 1500 | The pause duration after a string is typed when using autostart mode |
| devMode | Boolean | false | Whether or not to display console logs. |
| skipAddStyles | Boolean | false | Skip adding default typewriter css styles. |
| wrapperClassName | String | 'Typewriter__wrapper' | Class name for the wrapper element. |
| cursorClassName | String | 'Typewriter__cursor' | Class name for the cursor element. The blinking animation uses the ``Typewriter__cursor`` class, so include it to keep the blinking, for example ``'Typewriter__cursor my-cursor'``. |
| stringSplitter | Function | null | String splitter function, can be used to [split emoji's](https://codesandbox.io/s/typewriter-effect-emojis-pgz6e) |
| onCreateTextNode | Function | null | Callback function to replace the internal method which creates a text node for the character before adding it to the DOM. If you return null, then it will not add anything to the DOM and so it is up to you to handle it. |
| onRemoveNode | Function | null | Callback function when a node is about to be removed. First param will be an object `{ node: HTMLNode, character: string }`. The `character` is only set when a text node is removed. |
| maxCatchUpDelta | Number | null | Max milliseconds of elapsed time to catch up on in a single frame. When set, several events can run per frame so typing keeps its pace after the tab was inactive. Disabled by default (one event per frame). |
| parseHTML | Boolean | true | Whether strings that look like HTML should be added as HTML elements. Set to `false` to always type strings exactly as written, for example when the text contains `<` or `>` characters or comes from users. |
| initialText | String | null | Text that is shown straight away when the typewriter is created. It can be deleted like typed text, and it is not typed again when looping. |
| keepLastString | Boolean | false | Keep the last string of the ``strings`` option on the page instead of deleting it. Only used when ``loop`` is false. |
| deleteOnLoop | Boolean | true | Whether to delete everything when the loop starts again. Set to ``false`` to keep adding text on every loop. |
| cursorBlink | Boolean | true | Whether the cursor blinks. |
| useGraphemeSplitter | Boolean | false | Split strings into graphemes, so emojis like 👨👩👧 and letters with accents or marks are typed and deleted as one character. Ignored when ``stringSplitter`` is set. |
| mergeTextNodes | Boolean | false | Add typed characters to a single text node instead of one text node for each character. Fixes text shaping in Safari for scripts such as Arabic or Thai, and makes long text faster. Ignored when ``onCreateTextNode`` is set. |
| pasteAsCharacters | Boolean | false | Let ``deleteChars`` delete pasted text one character at a time, instead of deleting the whole pasted string at once. Ignored when ``onCreateTextNode`` is set. |
| reserveSpace | Boolean | false | Reserve the space that the typed text will need, so the page does not move while typing. The space is worked out from the queued events each time ``start`` is called, and is as big as the largest text that will be shown. |
| onlyDelayVisibleChanges | Boolean | false | Only wait for ``delay`` or ``deleteSpeed`` when a character is typed, pasted or deleted. Other events such as ``callFunction``, ``pauseFor``, ``changeDelay`` and empty HTML tags run straight away, and ``pasteString`` with HTML is pasted in one step. |
## Methods
All methods can be chained together.
| Name | Params | Description |
| --- | --- | --- |
| start | - | Start the typewriter effect. |
| stop | - | Stop the typewriter effect. |
| pause | - | Pause the typewriter effect. Call ``start`` to continue. |
| skipToEnd | - | Run every event in the queue straight away, without any delay or pause. Useful for a skip button. When looping, call ``stop`` after it to keep the final text on the page. |
| clearQueue | - | Remove every event that has not run yet. Call ``start`` again after adding new events. |
| moveCursor | ``amount`` Number of characters, negative to move left | Move the cursor by an amount of characters. Typed, pasted and deleted text then happens at the cursor. |
| moveCursorToStart | - | Move the cursor before all of the text. |
| moveCursorToEnd | - | Move the cursor after all of the text. |
| pauseFor | ``ms`` Time to pause for in milliseconds | Pause for milliseconds |
| typeString | ``string`` String to type out, it can contain HTML tags | Type out a string using the typewriter effect. |
| pasteString | ``string`` String to paste out, it can contain HTML tags | Paste out a string. |
| deleteAll | ``speed`` Speed to delete all visibles nodes, can be number or 'natural'. Defaults to 'natural' | Delete everything that is visible inside of the typewriter wrapper element. |
| deleteAllWithDeleteSpeed | - | Delete everything that is visible inside of the typewriter wrapper element using the current ``deleteSpeed`` option. |
| clear | - | Remove everything that is visible inside of the typewriter wrapper element straight away, without a delete animation. |
| deleteChars | ``amount`` Number of characters | Delete and amount of characters, starting at the end of the visible string. |
| callFunction | ``cb`` Callback, ``thisArg`` this Object to bind to the callback function | Call a callback function. The first parameter to the callback ``elements`` which contains all DOM nodes used in the typewriter effect. |
| changeDeleteSpeed | ``speed`` Number or 'natural' | The speed at which to delete the characters, lower number is faster. |
| changeDelay | ``delay`` Number or 'natural' | Change the delay when typing out each character |
| changeCursor | ``cursor`` String to use as the cursor | Change the cursor. Use an empty string to hide the cursor. |
## HTML strings
``typeString`` and ``pasteString`` add strings that contain HTML tags as real HTML elements.
- Void elements such as ```` tag. Use ``component='span'`` or move it out of the ``
`` tag. ### How do I make it accessible? The cursor is hidden from screen readers. Screen readers may read the text while it is being typed, so you can hide the typewriter with ``aria-hidden="true"`` and add the full text in a visually hidden element. To respect reduced motion settings, you can stop the blinking and skip the typing. ```js const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; const typewriter = new Typewriter('#app', { cursorBlink: !reduceMotion }) .typeString('Hello World!') .start(); if(reduceMotion) { typewriter.skipToEnd(); } ``` ### How do I use it with RequireJS? The CDN file registers a module named ``Typewriter``. ```js require.config({ paths: { Typewriter: 'https://unpkg.com/typewriter-effect@3/dist/core' }, }); require(['Typewriter'], function(Typewriter) { new Typewriter('#app').typeString('Hello').start(); }); ``` ### Translation tools change the text while typing Add ``translate="no"`` to the container element, or pass ``translate='no'`` to the React component.