🔢 Count Up
The CountUp component isn’t just about numbers—it’s about bringing data to life! 🎢 Watch as digits smoothly count up from start to finish, making stats, milestones, and achievements more engaging. Built for React with Framer Motion, this animated number counter adds excitement to dashboards, analytics, and landing pages. And when it hits the goal? BOOM! 🎉 A confetti explosion makes every milestone feel like a celebration!
Preview
Follow below steps 👇🏻
Install dependencies
1npm i framer-motion react-confetti
Component
Create a file count-up.tsx in your components folder and paste this code
1import { useState, useEffect } from 'react';2import { motion, useMotionValue, useTransform, animate, useAnimationControls } from 'framer-motion';3import Confetti from 'react-confetti';45const CountUp = ({6target, // ? The final number to reach7start = 0, // ? The number to start from8duration = 1, // ? The duration of the animation9confettiDuration = 10, // ? The duration of the confetti animation10className = '' // ? Additional class names11}: {12target: number;13start?: number;14duration?: number;15confettiDuration?: number;16className?: string;17}) => {18const [showConfetti, setShowConfetti] = useState(false);19const count = useMotionValue(start);20const roundedCount = useTransform(count, (latest) => Math.floor(latest));21const numberControls = useAnimationControls();2223useEffect(() => {24const controls = animate(count, target, {25duration,26ease: 'easeOut'27});2829controls.then(() => {30setShowConfetti(true);31numberControls.start('end');32setTimeout(() => setShowConfetti(false), confettiDuration * 1000);33});3435return () => controls.stop();36}, [target, count]);3738return (39<div className="relative flex items-center justify-center p-4">40{showConfetti && (41<div className="absolute h-full w-full">42<Confetti width={350} height={350} className="rounded-full h-full w-full" />43</div>44)}4546<motion.span47variants={{48end: {49scale: [1, 1.1, 1],50transition: {51duration: 0.4,52ease: 'easeOut'53}54}55}}56animate={numberControls}57className={`text-white text-center z-10 ${className}`}58>59{roundedCount}60</motion.span>61</div>62);63};6465export default CountUp;
Usage
1<CountUp2duration={1.5}3start={500}4target={777}5confettiDuration={50}6className="text-[100px] font-bold"7/>
⭐️ Got a question or feedback?
Feel free to reach out!