SPB Git

Repository Public

CountUp.tsx 2.8 KB 70 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : ORIX App Universe8 * File             : src/components/ui/CountUp.tsx9 * Module           : components10 * Repository       : https://git.spboucher.ai/orix-spboucher-ai11 * Project Website  : https://www.orixlab.dev12 * Created          : 2026-11-2513 * Last Modified    : 2026-11-2514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243"use client";44import { useEffect, useRef } from "react";4546/** A figure that counts up to its value the first time it is seen. The server renders the final value. */47export function CountUp({ value, className = "" }: { value: number; className?: string }) {48  const ref = useRef<HTMLSpanElement>(null);49  useEffect(() => {50    const el = ref.current;51    if (!el || value <= 0 || matchMedia("(prefers-reduced-motion: reduce)").matches) return;52    let raf = 0;53    const io = new IntersectionObserver(([e]) => {54      if (!e.isIntersecting) return;55      io.disconnect();56      const t0 = performance.now(), dur = 1100 + Math.min(900, value * 40);57      const tick = (now: number) => {58        const p = Math.min(1, (now - t0) / dur);59        el.textContent = String(Math.round(value * (1 - Math.pow(1 - p, 4))));60        if (p < 1) raf = requestAnimationFrame(tick);61      };62      el.textContent = "0";63      raf = requestAnimationFrame(tick);64    }, { threshold: 0.6 });65    io.observe(el);66    return () => { io.disconnect(); cancelAnimationFrame(raf); };67  }, [value]);68  return <span ref={ref} className={className}>{value}</span>;69}70
Files main
Menu

Theme