Typed.js is a library that types. Enter in any string, and watch it type at the speed you've set, backspace what it's typed, and begin a new sentence for however many strings you've set.
Installation
CDN
<script src="https://unpkg.com/typed.js@2.1.0/dist/typed.umd.js"></script>
For use directly in the browser via <script>
tag:
<span id="element"></span>
<script src="https://unpkg.com/typed.js@2.1.0/dist/typed.umd.js"></script>
<script>
var typed = new Typed('#element', {
strings: ['<i>First</i> sentence.', '& a second sentence.'],
typeSpeed: 50,
});
</script>
</body>
As an ESModule
For use with a build tool like Vite, and/or in a React application, install with NPM or Yarn.
NPM
npm install typed.js
Yarn
yarn add typed.js
General ESM Usage
import Typed from 'typed.js';
const typed = new Typed('#element', {
strings: ['<i>First</i> sentence.', '& a second sentence.'],
typeSpeed: 50,
});
ReactJS Usage
import React from 'react';
import Typed from 'typed.js';
function MyComponent() {
const el = React.useRef(null);
React.useEffect(() => {
const typed = new Typed(el.current, {
strings: ['<i>First</i> sentence.', '& a second sentence.'],
typeSpeed: 50,
});
return () => {
typed.destroy();
};
}, []);
return (
<div className="App">
<span ref={el} />
</div>
);
}
More complex hook-based function component: https://jsfiddle.net/mattboldt/60h9an7y/
Class component: https://jsfiddle.net/mattboldt/ovat9jmp/
Use with Vue.js
Check out the Vue.js component: https://github.com/Orlandster/vue-typed-js
Use it as WebComponent
Check out the WebComponent: https://github.com/Orlandster/wc-typed-js
Wonderful sites that have used (or are using) Typed.js
https://github.com/features/package-registry
https://slack.com/
https://envato.com/
https://gorails.com/
https://productmap.co/
https://www.typed.com/
https://apeiron.io
https://git.market/
https://commando.io/
http://testdouble.com/agency.html
https://www.capitalfactory.com/
http://www.maxcdn.com/
https://www.powerauth.com/
Strings from static HTML (SEO Friendly)
Rather than using the strings
array to insert strings, you can place an HTML div
on the page and read from it.
This allows bots and search engines, as well as users with JavaScript disabled, to see your text on the page.
<script>
var typed = new Typed('#typed', {
stringsElement: '#typed-strings'
});
</script>
<div id="typed-strings">
<p>Typed.js is a <strong>JavaScript</strong> library.</p>
<p>It <em>types</em> out sentences.</p>
</div>
<span id="typed"></span>
Type Pausing
You can pause in the middle of a string for a given amount of time by including an escape character.
var typed = new Typed('#element', {
strings: ['First ^1000 sentence.', 'Second sentence.'],
});
Smart Backspacing
In the following example, this would only backspace the words after "This is a"
var typed = new Typed('#element', {
strings: ['This is a JavaScript library', 'This is an ES6 module'],
smartBackspace: true,
});
Bulk Typing
The following example would emulate how a terminal acts when typing a command and seeing its result.
var typed = new Typed('#element', {
strings: ['git push --force ^1000\n `pushed to origin with option force`'],
});
CSS
CSS animations are built upon initialization in JavaScript. But, you can customize them at your will! These classes are:
.typed-cursor {
}
.typed-fade-out {
}
Customization
var typed = new Typed('#element', {
strings: [
'These are the default values...',
'You know what you should do?',
'Use your own!',
'Have a great day!',
],
stringsElement: null,
typeSpeed: 0,
startDelay: 0,
backSpeed: 0,
smartBackspace: true,
shuffle: false,
backDelay: 700,
fadeOut: false,
fadeOutClass: 'typed-fade-out',
fadeOutDelay: 500,
loop: false,
loopCount: Infinity,
showCursor: true,
cursorChar: '|',
autoInsertCss: true,
attr: null,
bindInputFocusEvents: false,
contentType: 'html',
onBegin: (self) => {},
onComplete: (self) => {},
preStringTyped: (arrayPos, self) => {},
onStringTyped: (arrayPos, self) => {},
onLastStringBackspaced: (self) => {},
onTypingPaused: (arrayPos, self) => {},
onTypingResumed: (arrayPos, self) => {},
onReset: (self) => {},
onStop: (arrayPos, self) => {},
onStart: (arrayPos, self) => {},
onDestroy: (self) => {},
});
Contributing
end
Thanks for checking this out. If you have any questions, I'll be on Twitter.
If you're using this, let me know! I'd love to see it.
It would also be great if you mentioned me or my website somewhere. www.mattboldt.com