Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : UQO Éval — Le Grand Défi Immobilier8 * File : apps/web/src/components/CountUp.tsx9 * Module : components10 * Repository : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website : https://jeux.uqo-eval.app12 * 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: contact@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 */4243import { useEffect, useRef, useState } from "react";44import { prefersReducedMotion } from "../lib/store";4546/** Nombre animé : glisse de sa valeur précédente à la nouvelle. La valeur exacte reste lue par les lecteurs d'écran. */47export function CountUp({ value, format, duration = 900, delay = 0 }: { value: number; format: (v: number) => string; duration?: number; delay?: number }) {48 const [shown, setShown] = useState(value);49 const from = useRef(value);50 useEffect(() => {51 if (prefersReducedMotion() || from.current === value) {52 from.current = value;53 setShown(value);54 return;55 }56 const start = from.current;57 let frame = 0;58 const t0 = performance.now() + delay;59 const tick = (now: number): void => {60 const t = Math.min(1, Math.max(0, (now - t0) / duration));61 const eased = 1 - (1 - t) ** 4;62 setShown(start + (value - start) * eased);63 if (t < 1) frame = requestAnimationFrame(tick);64 else from.current = value;65 };66 frame = requestAnimationFrame(tick);67 return () => {68 cancelAnimationFrame(frame);69 from.current = value;70 };71 }, [value, duration, delay]);72 return (73 <span className="tabular">74 <span aria-hidden="true">{format(shown)}</span>75 <span className="sr-only">{format(value)}</span>76 </span>77 );78}79