Typewriter effect with React and Typed.js
Typewriter effect is an animation effect where a text appears like someone is typing.
In this blog, you are going to create a typewriter effect with React and Typed.js.
Demo:
Starter code.
- Create a React project(I am using NextJS) and install typed.js
1yarn add typed.js
- index.js file:
1import React from 'react'2import Image from 'next/image'34import TypeWriter from '../components/TypeWriter'5import { container, overlay } from '../../styles/type.module.css'67const Type = () => {8 return (9 <div className={container}>10 <TypeWriter />11 <Image src='/tw_2.jpg' layout='fill' objectFit='cover' alt='Taylor swift' />12 </div>13 )14}1516export default Type
- TypeWriter.jsx file:
1import React from 'react'23import { typeContainer } from '../../styles/type.module.css'45const Type = () => {6 return (7 <div className={typeContainer}>8 <h1 style={{ display: 'inline' }} />9 </div>10 )11}
- type.module.css file for styles
1.container {2 height: 100vh;3 position: relative;4}56.typeContainer {7 position: absolute;8 background: #02020294;9 width: 100vw;10 z-index: 20;11 color: white;12 padding: 50px 0;13 bottom: 15%;14 padding-left: 2rem;15}
- Result
We don't have anything special. We have just a container with the position of relative
. The container is taking full width and height of the screen.
A nextjs Image component is used to display the image as if it is a background image.
Behind the scenes, the image is positioned absolute
and it is taking the whole width and height of its container to make it look like a background image.
On the Typewriter component, we have a container with className of .typeContainer
. It is also positioned as absolute
. Inside we have an h1
tag. This will be used to create a typewriter effect.
Let's do the actual task.
- import typed.js
1import Typed from 'typed.js'
- We need to create two refs with the
useRef
hook. With ref, we can reference the dom element.
1// TypeWriter.jsx2const Type = () => {3 // Create reference to store the DOM element containing the animation4 const el = React.useRef(null)5 // Create reference to store the Typed instance itself6 const typed = React.useRef(null)78 return (9 <div className={typeContainer}>10 <h1 ref={el} style={{ display: 'inline' }} />11 </div>12 )13}
We have attached the el
ref to the h1 using the ref
prop.
- We need to use the useEffect hook which will always run after the component gets mounted
1const Type = () => {2 // Create reference to store the DOM element containing the animation3 const el = React.useRef(null)4 // Create reference to store the Typed instance itself5 const typed = React.useRef(null)67 React.useEffect(() => {8 const options = {9 // strings that will be rendered for typewriter effect10 strings: [11 'Hello, My name is Taylor Swift.',12 "My new album Red (Taylor's Version) is coming on November 12, 2021.",13 'PRE-ORDER NOW',14 ],15 typeSpeed: 50, // typeing speed will be 50ms16 backSpeed: 10, // typing backSpeed will be 10ms17 loop: true,18 }1920 // elRef refers to the <h1 /> rendered below21 typed.current = new Typed(el.current, options)2223 return () => {24 // Make sure to destroy Typed instance during cleanup25 // to prevent memory leaks26 typed.current.destroy()27 }28 }, [])2930 return (31 <div className={typeContainer}>32 <h1 ref={el} style={{ display: 'inline' }} />33 </div>34 )35}
We have created a new instance of the Typed class passing the h1
dom reference and options.
To see more available options visit the docs
Our Final Result:
That's it for this blog.
Shameless Plug
I have made an Xbox landing page clone with React and Styled components. I hope you will enjoy it. Please consider like this video and subscribe to my channel.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
Contacts
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram : @thatanjan
- Twitter: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: