DEV Community

Cover image for Creating a Dynamic Typing Animation in React using Hooks and Bootstrap
ryad
ryad

Posted on

Creating a Dynamic Typing Animation in React using Hooks and Bootstrap

Image description
In the world of web development, creating engaging and interactive user interfaces is crucial. One way to captivate your visitors’ attention is by adding dynamic elements to your website. In this article, we’ll walk you through how to create a captivating dynamic typing animation using React hooks and the Bootstrap framework. We’ll break down the code step by step and guide you on where to copy and paste each section.

1. Setting Up the React App

Before we dive into the code, make sure you have a React project set up. If you haven’t already, you can create a new React app using the following command:

npx create-react-app dynamic-typing-animation
install some packages:

npm install react-bootstrap bootstrap react-bootstrap-icons react-on-screen
In your app.js file, you'll import necessary dependencies and the Banner component. This component will serve as the main content of your application. Copy and paste the following code into your app.js file:


// app.js
import logo from "./logo.svg";
import "./App.css";
import "bootstrap/dist/css/bootstrap.min.css";
import { Banner } from "./components/Banner";

function App() {
  return (
    <div className="App">
      <Banner />
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Next, you’ll implement the dynamic typing animation in the Banner component. This component uses hooks to manage state and timing for the animation.


// banner.js
import { useState, useEffect } from "react";
import { Container, Row, Col } from "react-bootstrap";
import { ArrowRightCircle } from "react-bootstrap-icons";
import TrackVisibility from "react-on-screen";

export const Banner = () => {
  const [loopNum, setLoopNum] = useState(0);
  const [isDeleting, setIsDeleting] = useState(false);
  const [text, setText] = useState("");
  const [delta, setDelta] = useState(300 - Math.random() * 100);
  const [index, setIndex] = useState(1);
  const toRotate = ["Web Developer", "Web Designer", "UI/UX Designer"];
  const period = 2000;

  useEffect(() => {
    let ticker = setInterval(() => {
      tick();
    }, delta);

    return () => {
      clearInterval(ticker);
    };
  }, [text]);

  const tick = () => {
    let i = loopNum % toRotate.length;
    let fullText = toRotate[i];
    let updatedText = isDeleting
      ? fullText.substring(0, text.length - 1)
      : fullText.substring(0, text.length + 1);

    setText(updatedText);

    if (isDeleting) {
      setDelta((prevDelta) => prevDelta / 2);
    }

    if (!isDeleting && updatedText === fullText) {
      setIsDeleting(true);
      setIndex((prevIndex) => prevIndex - 1);
      setDelta(period);
    } else if (isDeleting && updatedText === "") {
      setIsDeleting(false);
      setLoopNum(loopNum + 1);
      setIndex(1);
      setDelta(500);
    } else {
      setIndex((prevIndex) => prevIndex + 1);
    }
  };

  return (
    <section className="banner" id="home">
      <Container>
        <Row className="aligh-items-center">
          <Col xs={12} md={6} xl={7}>
            <TrackVisibility>
              {({ isVisible }) => (
                <div
                  className={
                    isVisible ? "animate__animated animate__fadeIn" : ""
                  }
                >
                  <span className="tagline">Welcome to my Portfolio</span>
                  <h1>
                    {`Hi! I'm Lorem`}{" "}
                    <span
                      className="txt-rotate"
                      dataPeriod="1000"
                      data-rotate='[ "Web dev", "Web master", "UI/UX programmer" ]'
                    >
                      <span className="wrap">{text}</span>
                    </span>
                  </h1>
                  <p>
                    Lorem Ipsum is simply dummy text of the printing and
                    typesetting industry. Lorem Ipsum has been the industry's
                    standard dummy text ever since the 1500s, when an unknown
                    printer took a galley of type and scrambled it to make a
                    type specimen book.
                  </p>
                  <button>
                    Let’s Connect <ArrowRightCircle size={25} />
                  </button>
                </div>
              )}
            </TrackVisibility>
          </Col>
          <Col xs={12} md={6} xl={5}>
            <TrackVisibility>
              {({ isVisible }) => (
                <div
                  className={
                    isVisible ? "animate__animated animate__zoomIn" : ""
                  }
                >
                  <img
                    src={"https://i.imgur.com/DC4LkuS.png"}
                    alt="Header Img"
                    style={{ width: 500 }}
                  />
                </div>
              )}
            </TrackVisibility>
          </Col>
        </Row>
      </Container>
    </section>
  );
};
Enter fullscreen mode Exit fullscreen mode

4. Copy and Paste App.css

Finally, add the necessary CSS to style your dynamic typing animation. Copy and paste the following CSS code into your App.css file:


/* App.css */
/************ Default Css ************/
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  scroll-padding-top: 75px;
}

body {
  font-weight: 400;
  overflow-x: hidden;
  position: relative;
  background-color: #121212 !important;
  color: #fff !important;
  font-family: "Centra", sans-serif !important;
}

button {
  border: 0;
  background-color: transparent;
}
Enter fullscreen mode Exit fullscreen mode

5. Styling Your Dynamic Typing Animation

/************ Banner Css ************/
.banner {
  margin-top: 0;
  padding: 260px 0 100px 0;
  background-image: url("https://i.imgur.com/J0zBNyV.png");
  background-position: top center;
  background-size: cover;
  background-repeat: no-repeat;
}
.banner .tagline {
  font-weight: 700;
  letter-spacing: 0.8px;
  padding: 8px 10px;
  background: linear-gradient(
    90.21deg,
    rgba(170, 54, 124, 0.5) -5.91%,
    rgba(74, 47, 189, 0.5) 111.58%
  );
  border: 1px solid rgba(255, 255, 255, 0.5);
  font-size: 20px;
  margin-bottom: 16px;
  display: inline-block;
}
.banner h1 {
  font-size: 65px;
  font-weight: 700;
  letter-spacing: 0.8px;
  line-height: 1;
  margin-bottom: 20px;
  display: block;
}
.banner p {
  color: #b8b8b8;
  font-size: 18px;
  letter-spacing: 0.8px;
  line-height: 1.5em;
  width: 96%;
}
.banner button {
  color: #fff;
  font-weight: 700;
  font-size: 20px;
  margin-top: 60px;
  letter-spacing: 0.8px;
  display: flex;
  align-items: center;
}
.banner button svg {
  font-size: 25px;
  margin-left: 10px;
  transition: 0.3s ease-in-out;
  line-height: 1;
}
.banner button:hover svg {
  margin-left: 25px;
}
.banner img {
  animation: updown 3s linear infinite;
}
@keyframes updown {
  0% {
    transform: translateY(-20px);
  }
  50% {
    transform: translateY(20px);
  }
  100% {
    transform: translateY(-20px);
  }
}
.txt-rotate > .wrap {
  border-right: 0.08em solid #666;
}
Enter fullscreen mode Exit fullscreen mode

6. Run Your React App

With all the code in place, you’re ready to see your dynamic typing animation in action. Open your terminal, navigate to your project directory, and run:

npm start
This will start your development server, and you can view the dynamic typing animation by opening your browser and visiting http://localhost:3000.

Congratulations!

You’ve successfully created a captivating dynamic typing animation using React hooks and the Bootstrap framework. This type of animation can make your website more engaging and visually appealing, providing a unique user experience. Feel free to customize the animation and styling to match your project’s design and branding.

Top comments (0)