TypewriterJS

A simple yet powerful native javascript plugin for a cool typewriter effect...

README

TypewriterJS v2

CircleCI

undefined


CDN


You can use the CDN version of this plugin for fast and easy setup.

  1. ```html
  2. <script src="https://unpkg.com/typewriter-effect@latest/dist/core.js"></script>
  3. ```

Installation

You can install Typewriterjs with just one command and you're good to go
  1. ```shell

  2. # with npm
  3. npm i typewriter-effect

  4. # with yarn
  5. yarn add typewriter-effect

  6. ```

Core


This include the core typewriter library, which can be used directly through the API.

See examples in the 'examples' folder.

  1. ```js
  2. import Typewriter from 'typewriter-effect/dist/core';

  3. new Typewriter('#typewriter', {
  4.   strings: ['Hello', 'World'],
  5.   autoStart: true,
  6. });
  7. ```

Options


NameTypeDefaultDescription
------------
stringsStringnullStrings
cursorStringPipeString
delay'natural''natural'The
deleteSpeed'natural''natural'The
loopBooleanfalseWhether
autoStartBooleanfalseWhether
pauseForNumber1500The
devModeBooleanfalseWhether
skipAddStylesBooleanSkip
wrapperClassNameString'Typewriter__wrapper'Class
cursorClassNameString'Typewriter__cursor'Class
stringSplitterFunctionString
onCreateTextNodeFunctionnullCallback
onRemoveNodeFunctionnullCallback

Methods


All methods can be chained together.

NameParamsDescription
---------
start-Start
stop-Stop
pauseFor``ms``Pause
typeString``string``Type
pasteString``string``Paste
deleteAll``speed``Delete
deleteChars``amount``Delete
callFunction``cb``Call
changeDeleteSpeed``speed``The
changeDelay``delay``Change

Examples


Basic example


  1. ```js
  2. var app = document.getElementById('app');

  3. var typewriter = new Typewriter(app, {
  4.   loop: true,
  5.   delay: 75,
  6. });

  7. typewriter
  8.   .pauseFor(2500)
  9.   .typeString('A simple yet powerful native javascript')
  10.   .pauseFor(300)
  11.   .deleteChars(10)
  12.   .typeString('<strong>JS</strong> plugin for a cool typewriter effect and ')
  13.   .typeString('<strong>only <span style="color: #27ae60;">5kb</span> Gzipped!</strong>')
  14.   .pauseFor(1000)
  15.   .start();
  16. ```

Custom text node creator using callback


  1. ```js
  2. var app = document.getElementById('app');

  3. var customNodeCreator = function(character) {
  4.   return document.createTextNode(character);
  5. }

  6. var typewriter = new Typewriter(app, {
  7.   loop: true,
  8.   delay: 75,
  9.   onCreateTextNode: customNodeCreator,
  10. });

  11. typewriter
  12.   .typeString('A simple yet powerful native javascript')
  13.   .pauseFor(300)
  14.   .start();
  15. ```

Custom handling text insert using input placeholder


  1. ```js
  2. var input = document.getElementById('input')

  3. var customNodeCreator = function(character) {
  4.   // Add character to input placeholder
  5.   input.placeholder = input.placeholder + character;

  6.   // Return null to skip internal adding of dom node
  7.   return null;
  8. }

  9. var onRemoveNode = function({ character }) {
  10.   if(input.placeholder) {
  11.     // Remove last character from input placeholder
  12.     input.placeholder = input.placeholder.slice(0, -1)
  13.   }
  14. }

  15. var typewriter = new Typewriter(null, {
  16.   loop: true,
  17.   delay: 75,
  18.   onCreateTextNode: customNodeCreator,
  19.   onRemoveNode: onRemoveNode,
  20. });

  21. typewriter
  22.   .typeString('A simple yet powerful native javascript')
  23.   .pauseFor(300)
  24.   .start();
  25. ```

React


This includes a React component which can be used within your project. You can pass in a onInit function which will be called with the instance of the typewriter so you can use the typewriter core API.

  1. ```jsx
  2. import Typewriter from 'typewriter-effect';

  3. <Typewriter
  4.   onInit={(typewriter) => {
  5.     typewriter.typeString('Hello World!')
  6.       .callFunction(() => {
  7.         console.log('String typed out!');
  8.       })
  9.       .pauseFor(2500)
  10.       .deleteAll()
  11.       .callFunction(() => {
  12.         console.log('All strings were deleted');
  13.       })
  14.       .start();
  15.   }}
  16. />
  17. ```

Alternatively you can also pass in options to use auto play and looping for example:

  1. ```jsx
  2. import Typewriter from 'typewriter-effect';

  3. <Typewriter
  4.   options={{
  5.     strings: ['Hello', 'World'],
  6.     autoStart: true,
  7.     loop: true,
  8.   }}
  9. />
  10. ```