fix(leaderboard): fix counter animation and number formatting
- AnimatedNumber: add ScrollTrigger so counters animate on scroll-into-view instead of firing immediately on mount (fixes table rows animating before the user scrolls to them) - AnimatedNumber: lock locale to 'en-US' for consistent comma formatting - LeaderboardPage: lock formatValue locale to 'en-US' - mockStore: update MOCK_SALES_HISTORY dates from Jan/Feb to March 2026 so the default "This Month" filter returns data; previous dates caused all agents to show $0 revenue, making the animation appear broken - mockStore: replace unrealistic amounts ($250K–$520K) with realistic residential roofing job values ($12K–$42K per deal)
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import React, { useEffect, useRef } from 'react';
|
||||
import { gsap } from 'gsap';
|
||||
import { ScrollTrigger } from 'gsap/ScrollTrigger';
|
||||
|
||||
gsap.registerPlugin(ScrollTrigger);
|
||||
|
||||
/**
|
||||
* AnimatedNumber - A reusable component that animates numbers from 0 to their target value using GSAP
|
||||
@@ -64,42 +67,35 @@ const AnimatedNumber = ({
|
||||
|
||||
const targetValue = numericValue * multiplier;
|
||||
|
||||
// Animate from 0 to target value
|
||||
// Animate from 0 to target value, triggered when element scrolls into view
|
||||
const obj = { val: 0 };
|
||||
|
||||
const formatValue = (v) => {
|
||||
let displayValue = v.toFixed(decimals);
|
||||
if (useLocaleString) {
|
||||
displayValue = parseFloat(displayValue).toLocaleString('en-US', {
|
||||
minimumFractionDigits: decimals,
|
||||
maximumFractionDigits: decimals
|
||||
});
|
||||
}
|
||||
return `${extractedPrefix}${displayValue}${extractedSuffix}`;
|
||||
};
|
||||
|
||||
animationRef.current = gsap.to(obj, {
|
||||
val: numericValue,
|
||||
duration: duration,
|
||||
delay: delay,
|
||||
ease: 'expo.out',
|
||||
scrollTrigger: {
|
||||
trigger: numberRef.current,
|
||||
start: 'top 95%',
|
||||
toggleActions: 'play none none none',
|
||||
},
|
||||
onUpdate: () => {
|
||||
if (numberRef.current) {
|
||||
let displayValue = obj.val.toFixed(decimals);
|
||||
|
||||
if (useLocaleString) {
|
||||
displayValue = parseFloat(displayValue).toLocaleString(undefined, {
|
||||
minimumFractionDigits: decimals,
|
||||
maximumFractionDigits: decimals
|
||||
});
|
||||
}
|
||||
|
||||
numberRef.current.textContent = `${extractedPrefix}${displayValue}${extractedSuffix}`;
|
||||
}
|
||||
if (numberRef.current) numberRef.current.textContent = formatValue(obj.val);
|
||||
},
|
||||
onComplete: () => {
|
||||
// Ensure final value is exact
|
||||
if (numberRef.current) {
|
||||
let displayValue = numericValue.toFixed(decimals);
|
||||
|
||||
if (useLocaleString) {
|
||||
displayValue = parseFloat(displayValue).toLocaleString(undefined, {
|
||||
minimumFractionDigits: decimals,
|
||||
maximumFractionDigits: decimals
|
||||
});
|
||||
}
|
||||
|
||||
numberRef.current.textContent = `${extractedPrefix}${displayValue}${extractedSuffix}`;
|
||||
}
|
||||
if (numberRef.current) numberRef.current.textContent = formatValue(numericValue);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user