Coder Social home page Coder Social logo

shahriyar-hosen / counter-up-trigger Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 67 KB

Counter Up Trigger Made in its own way combination with react-scroll-trigger and react-countup

TypeScript 70.70% CSS 13.36% JavaScript 15.94%
counter counterup nextjs nextjs13 react scrolltrigger tailwindcss tawilwind ts typescript

counter-up-trigger's Introduction

Counter Up Trigger

Interface

export interface ICounterCard {
  maxCount: number;
  label: string;
  title: string;
}

Counter Card Component

import { ICounterCard } from "@/Interface";
import { useEffect, useRef, useState } from "react";

export const CounterCard = ({ label, title, maxCount }: ICounterCard) => {
  const [count, setCount] = useState<number>(0);
  const [isVisible, setIsVisible] = useState<boolean>(false);
  const sectionRef = useRef<HTMLDivElement | null>(null);

// Update isVisible State. up and down trigger the counter
  useEffect(() => {
    const observer = new IntersectionObserver((entries) => {
      const [entry] = entries;
      setIsVisible(entry.isIntersecting);
    });

    if (sectionRef.current) {
      observer.observe(sectionRef.current);
    }

// Cleanup the observer when the component is unmounted
    return () => {
      if (sectionRef.current) {
        observer.unobserve(sectionRef.current);
      }
    };
  }, [sectionRef, setIsVisible]);

// Update counter and duration time duration 
  useEffect(() => {
    let intervalId: NodeJS.Timeout;

    if (isVisible) {
      intervalId = setInterval(() => {
        if (count < maxCount) {
          setCount((prevCount) => prevCount + 1);
        } else {
          clearInterval(intervalId);
        }
      }, 20); // Adjust the interval duration as needed
    }

  // Cleanup the interval when the component is unmounted or when visibility changes
    return () => {
      clearInterval(intervalId);
    };
  }, [isVisible, count, setCount]);

  return (
    <div
      ref={sectionRef}
      className="flex gap-5 md:gap-9 justify-center items-center lg:items-start flex-col text-center lg:text-start w-full"
    >
      <h1 className="text-4xl md:text-5xl font-extrabold">
        {count}
        <span className="text-primary">{label}</span>
      </h1>
      <p className="text-base md:text-xl font-bold">{title}</p>
    </div>
  );
};

Counter Main Component

import { CounterCard } from "@/components";

const CounterUpTrigger = () => (
  <main className="w-screen h-screen flex flex-col justify-center items-center">
    <div className="bg-green-500 py-12 px-8 md:py-20 md:px-16 w-fit lg:w-full max-w-6xl mx-auto rounded-[32px] grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-12 justify-items-center">
      <CounterCard maxCount={100} label="M" title="Client Satisfaction" />
      <CounterCard maxCount={24} label=" h" title="Expert Support Team" />
      <CounterCard maxCount={98} label=" k+" title="Sales Count" />
      <CounterCard maxCount={208} label=" +" title="Client Worldwide" />
    </div>
  </main>
);

export default CounterUpTrigger;

counter-up-trigger's People

Contributors

shahriyar-hosen avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.