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
yarn add typed.js
- index.js file:
import React from 'react'import Image from 'next/image'import TypeWriter from '../components/TypeWriter'import { container, overlay } from '../../styles/type.module.css'const Type = () => {return (<div className={container}><TypeWriter /><Image src='/tw_2.jpg' layout='fill' objectFit='cover' alt='Taylor swift' /></div>)}export default Type
- TypeWriter.jsx file:
import React from 'react'import { typeContainer } from '../../styles/type.module.css'const Type = () => {return (<div className={typeContainer}><h1 style={{ display: 'inline' }} /></div>)}
- type.module.css file for styles
.container {height: 100vh;position: relative;}.typeContainer {position: absolute;background: #02020294;width: 100vw;z-index: 20;color: white;padding: 50px 0;bottom: 15%;padding-left: 2rem;}
- 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
import Typed from 'typed.js'
- We need to create two refs with the
useRef
hook. With ref, we can reference the dom element.
// TypeWriter.jsxconst Type = () => {// Create reference to store the DOM element containing the animationconst el = React.useRef(null)// Create reference to store the Typed instance itselfconst typed = React.useRef(null)return (<div className={typeContainer}><h1 ref={el} style={{ display: 'inline' }} /></div>)}
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
const Type = () => {// Create reference to store the DOM element containing the animationconst el = React.useRef(null)// Create reference to store the Typed instance itselfconst typed = React.useRef(null)React.useEffect(() => {const options = {// strings that will be rendered for typewriter effectstrings: ['Hello, My name is Taylor Swift.',"My new album Red (Taylor's Version) is coming on November 12, 2021.",'PRE-ORDER NOW',],typeSpeed: 50, // typeing speed will be 50msbackSpeed: 10, // typing backSpeed will be 10msloop: true,}// elRef refers to the <h1 /> rendered belowtyped.current = new Typed(el.current, options)return () => {// Make sure to destroy Typed instance during cleanup// to prevent memory leakstyped.current.destroy()}}, [])return (<div className={typeContainer}><h1 ref={el} style={{ display: 'inline' }} /></div>)}
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
Want to create your own blog? Well, I am creating a video series where you will learn about how to create a JAMstack blog with Nextjs and Chakra-UI.
Lessons
- Intro & Setup
- Build Homepage UI
- How our app will work
- MDX, MongoDB, Static Homepage
- Generate Static Blog Page
- Style Blog page with Chakra-UI and MDX-embed
- Build a real-time view counter
- Autocomplete search form with MongoDB Atlas Search Index
- Deploy application to Vercel
Demo
You can demo the website from here
Features
- Static Blog pages will make the website load faster.
- Blogs will have code blocks with syntax highlighting and many embed components like youtube videos, GitHub gist, Tweets, and so many other things.
- Autocomplete search feature for the blog posts.
- Real-time view counter and so on.
Please like and subscribe to Cules Coding. It motivates me to create more content like this.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
By the way, I am looking for a new opportunity in a company where I can provide great value with my skills. If you are a recruiter, looking for someone skilled in full-stack web development and passionate about revolutionizing the world, feel free to contact me. Also, I am open to talking about any freelance project. I am available on Upwork
Contacts
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram (personal): @thatanjan
- Twitter: @thatanjan
- Upwork: @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: