avatarR-Dev

Summary

The provided content is a comprehensive tutorial on creating a responsive header component for a web application using React and the react-type-animation library.

Abstract

The tutorial guides developers through the process of building a dynamic and responsive header component in React. It covers the installation of the react-type-animation package, the implementation of the Header component with animated typing effects, and the integration of this component into a React application. The tutorial also includes custom CSS styling for visual enhancement and provides a live demo for users to interact with the final product. The guide emphasizes the importance of creating visually appealing and interactive user interfaces and encourages customization to meet individual design needs.

Opinions

  • The author suggests that the react-type-animation library is a valuable tool for adding dynamic text animations to web applications.
  • The tutorial is presented as a step-by-step guide, implying that it is suitable for developers of varying skill levels, from beginners to more advanced users.
  • The inclusion of a live demo and the recommendation of an AI service at the end of the article indicates the author's commitment to providing practical, hands-on learning experiences and resources.
  • The author's tone is encouraging and supportive, offering further assistance and inviting questions, which suggests a commitment to community engagement and educational support.
  • The use of custom CSS and SCSS files alongside React components demonstrates the author's preference for a modular and maintainable codebase with a focus on design and user experience.

Building a Responsive Header Component with React and react-type-animation

In this tutorial, we’ll create a responsive header component using React. The application includes the Header and App components, integration files, and custom styling. We'll also add the installation part for react-type-animation.

Getting Started

Before we start, let’s install the necessary package for react-type-animation.

# Using npm
npm install react-type-animation
# Using yarn
yarn add react-type-animation

Now, let’s proceed with the implementation.

header.jsx - Creating a Responsive Header Component

The header.jsx file contains the Header component, which includes a dynamic header with an introduction, a typed animation, and download CV and play video buttons.

import React from "react";
import { TypeAnimation } from "react-type-animation";

const Header = () => {
  return (
    <React.Fragment>
      <div className="container-fluid bg-light my-6 mt-0" id="home">
        <div className="container">
          <div className="row g-5 align-items-center">
            <div className="col-lg-6 py-6 pb-0 pt-lg-0">
              <h3 className="text-primary h2 mb-3 ">I&apos;m</h3>
              <h1 className="display-3 mb-3">Kate Winslet</h1>
              <h2 className="typed-text-output d-inline" />
              <TypeAnimation
                sequence={[
                  // Same substring at the start will only be typed once, initially

                  "Web Designer,",
                  1000,
                  "Web Developer,",
                  1000,
                  "Front End Developer,",
                  1000,
                  "Apps Designer,Apps Developer",
                  1000,
                  ,
                ]}
                className="h2"
                speed={50}
                repeat={Infinity}
              />
              <div className="d-flex align-items-center pt-5">
                <a href="" className="btn btn-primary py-3 px-4 me-5">
                  Download CV
                </a>
                <button
                  type="button"
                  className="btn-play"
                  data-bs-toggle="modal"
                  data-src="https://www.youtube.com/embed/DWRcNpR6Kdc"
                  data-bs-target="#videoModal"
                >
                  <span />
                </button>
                <h5 className="ms-4 mb-0 d-none d-sm-block">Play Video</h5>
              </div>
            </div>
            <div className="col-lg-6">
              <img
                className="img-fluid"
                src="https://i.imgur.com/XxANu5A.png"
                alt=""
              />
            </div>
          </div>
        </div>
      </div>
    </React.Fragment>
  );
};

export default Header;

app.jsx - Integrating the Header Component

The app.jsx file serves as the entry point, importing and rendering the Header component.

import './App.css';
import Header from './components/Header';

export default function App() {
  return (
    <>
      <Header />
    </>
  );
}

Styling with app.css

The app.css file contains custom styles to enhance the visual appeal of the application.

/********** Template CSS **********/
:root {
  --primary: #6244c5;
  --secondary: #ffc448;
  --light: #fafafb;
  --dark: #12141d;
}

.back-to-top {
  position: fixed;
  display: none;
  right: 45px;
  bottom: 45px;
  z-index: 99;
}

.my-6 {
  margin-top: 6rem;
  margin-bottom: 6rem;
}

.py-6 {
  padding-top: 6rem;
  padding-bottom: 6rem;
}

/*** Header ***/
#home {
  margin-bottom: 6rem;
  background: url('https://i.imgur.com/rXtr1Rc.png') left top no-repeat;
}

.typed-cursor {
  font-size: 30px;
  color: var(--dark);
}

.btn-play {
  position: relative;
  display: block;
  box-sizing: content-box;
  width: 16px;
  height: 26px;
  border-radius: 100%;
  border: none;
  outline: none !important;
  padding: 18px 20px 20px 28px;
  background: var(--primary);
}

.btn-play:before {
  content: '';
  position: absolute;
  z-index: 0;
  left: 50%;
  top: 50%;
  transform: translateX(-50%) translateY(-50%);
  display: block;
  width: 60px;
  height: 60px;
  background: var(--primary);
  border-radius: 100%;
  animation: pulse-border 1500ms ease-out infinite;
}

.btn-play:after {
  content: '';
  position: absolute;
  z-index: 1;
  left: 50%;
  top: 50%;
  transform: translateX(-50%) translateY(-50%);
  display: block;
  width: 60px;
  height: 60px;
  background: #ffffff;
  border-radius: 100%;
  transition: all 200ms;
}

.btn-play span {
  display: block;
  position: relative;
  z-index: 3;
  width: 0;
  height: 0;
  left: -1px;
  border-left: 16px solid var(--primary);
  border-top: 11px solid transparent;
  border-bottom: 11px solid transparent;
}

@keyframes pulse-border {
  0% {
    transform: translateX(-50%) translateY(-50%) translateZ(0) scale(1);
    opacity: 1;
  }

  100% {
    transform: translateX(-50%) translateY(-50%) translateZ(0) scale(2);
    opacity: 0;
  }
}

.modal-video .modal-dialog {
  position: relative;
  max-width: 800px;
  margin: 60px auto 0 auto;
}

.modal-video .modal-body {
  position: relative;
  padding: 0px;
}

.modal-video .close {
  position: absolute;
  width: 30px;
  height: 30px;
  right: 0px;
  top: -30px;
  z-index: 999;
  font-size: 30px;
  font-weight: normal;
  color: #ffffff;
  background: #000000;
  opacity: 1;
}

main.jsx - Rendering the App Component

The main.jsx file renders the App component into the root element of the HTML document.

import 'bootstrap/dist/css/bootstrap.css';
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.jsx';
import './custom.scss';
import './index.css';

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

Conclusion

By following this guide, you’ve created a responsive header component using React. Feel free to customize the code according to your specific design requirements. If you have any questions or need further assistance, please let me know!

Live Demo

Check out the Live Demo to experience the responsive header component in action.

React
React Type Animation
Dynamic Programming
Web Design
Bootstrap
Recommended from ReadMedium