Mirror to Gitea / mirror (push) Skipped
Use real public-domain photos throughout (Chicago skyline, CTA signage, Marina City, Union Station, guitars, synths, cassette, Game Boy and floppy stickers, daisies, clouds, flowers, a vintage postcard and stamp), credited on a new /credits page. Lean into Chicago: CTA 'L' line colors for sections and categories, a Chicago star logo, flag footer, a CTA platform work page with split-flap board and arrival rows, and a Greetings from Chicago postcard on contact. Add GSAP + Lenis scroll motion and React Bits components (split-flap, ticker, circular badge, count-up, sticker peel, scrambled text, halftone reveal, CRT terminal, staggered mobile menu). Rebuild pages mobile-first, drop the katakana, and lazy-load the 3D viewer.
126 lines
3.3 KiB
TypeScript
126 lines
3.3 KiB
TypeScript
// CountUp — from React Bits (https://github.com/DavidHDev/react-bits)
|
|
// Copyright (c) 2026 David Haz. MIT + Commons Clause. Adapted for yoyos.sh.
|
|
'use client';
|
|
|
|
import { useInView, useMotionValue, useReducedMotion, useSpring } from 'motion/react';
|
|
import { useCallback, useEffect, useRef } from 'react';
|
|
|
|
interface CountUpProps {
|
|
to: number;
|
|
from?: number;
|
|
direction?: 'up' | 'down';
|
|
delay?: number;
|
|
duration?: number;
|
|
className?: string;
|
|
startWhen?: boolean;
|
|
separator?: string;
|
|
onStart?: () => void;
|
|
onEnd?: () => void;
|
|
}
|
|
|
|
export default function CountUp({
|
|
to,
|
|
from = 0,
|
|
direction = 'up',
|
|
delay = 0,
|
|
duration = 2,
|
|
className = '',
|
|
startWhen = true,
|
|
separator = '',
|
|
onStart,
|
|
onEnd
|
|
}: CountUpProps) {
|
|
const ref = useRef<HTMLSpanElement>(null);
|
|
const motionValue = useMotionValue(direction === 'down' ? to : from);
|
|
|
|
const damping = 20 + 40 * (1 / duration);
|
|
const stiffness = 100 * (1 / duration);
|
|
|
|
const springValue = useSpring(motionValue, {
|
|
damping,
|
|
stiffness
|
|
});
|
|
|
|
const isInView = useInView(ref, { once: true, margin: '0px' });
|
|
const reduceMotion = useReducedMotion();
|
|
|
|
const getDecimalPlaces = (num: number): number => {
|
|
const str = num.toString();
|
|
if (str.includes('.')) {
|
|
const decimals = str.split('.')[1];
|
|
if (parseInt(decimals) !== 0) {
|
|
return decimals.length;
|
|
}
|
|
}
|
|
return 0;
|
|
};
|
|
|
|
const maxDecimals = Math.max(getDecimalPlaces(from), getDecimalPlaces(to));
|
|
|
|
const formatValue = useCallback(
|
|
(latest: number) => {
|
|
const hasDecimals = maxDecimals > 0;
|
|
|
|
const options: Intl.NumberFormatOptions = {
|
|
useGrouping: !!separator,
|
|
minimumFractionDigits: hasDecimals ? maxDecimals : 0,
|
|
maximumFractionDigits: hasDecimals ? maxDecimals : 0
|
|
};
|
|
|
|
const formattedNumber = Intl.NumberFormat('en-US', options).format(latest);
|
|
|
|
return separator ? formattedNumber.replace(/,/g, separator) : formattedNumber;
|
|
},
|
|
[maxDecimals, separator]
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (ref.current && !reduceMotion) {
|
|
ref.current.textContent = formatValue(direction === 'down' ? to : from);
|
|
}
|
|
}, [from, to, direction, formatValue, reduceMotion]);
|
|
|
|
useEffect(() => {
|
|
if (isInView && startWhen && !reduceMotion) {
|
|
if (typeof onStart === 'function') {
|
|
onStart();
|
|
}
|
|
|
|
const timeoutId = setTimeout(() => {
|
|
motionValue.set(direction === 'down' ? from : to);
|
|
}, delay * 1000);
|
|
|
|
const durationTimeoutId = setTimeout(
|
|
() => {
|
|
if (typeof onEnd === 'function') {
|
|
onEnd();
|
|
}
|
|
},
|
|
delay * 1000 + duration * 1000
|
|
);
|
|
|
|
return () => {
|
|
clearTimeout(timeoutId);
|
|
clearTimeout(durationTimeoutId);
|
|
};
|
|
}
|
|
}, [isInView, startWhen, reduceMotion, motionValue, direction, from, to, delay, onStart, onEnd, duration]);
|
|
|
|
useEffect(() => {
|
|
const unsubscribe = springValue.on('change', (latest: number) => {
|
|
if (ref.current) {
|
|
ref.current.textContent = formatValue(latest);
|
|
}
|
|
});
|
|
|
|
return () => unsubscribe();
|
|
}, [springValue, formatValue]);
|
|
|
|
// Render the final value on the server so it's readable before hydration and without JS.
|
|
return (
|
|
<span className={className} ref={ref} suppressHydrationWarning>
|
|
{formatValue(direction === 'down' ? from : to)}
|
|
</span>
|
|
);
|
|
}
|