Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

💄 Animate landing stats with an odometer count-up on scroll (#2942)

Luke Vellafdd57c1b1a05
diff --git a/apps/landing/package.json b/apps/landing/package.json
index b73f881ee..9a86a7a1d 100644
--- a/apps/landing/package.json
+++ b/apps/landing/package.json
@@ -11,6 +11,7 @@
"i18n:scan": "i18next-cli extract"
},
"dependencies": {
+ "@number-flow/react": "^0.6.2",
"@prisma/adapter-pg": "^7.8.0",
"@prisma/client": "^7.8.0",
"@rallly/billing": "workspace:*",
diff --git a/apps/landing/src/components/home/animated-number.tsx b/apps/landing/src/components/home/animated-number.tsx
new file mode 100644
index 000000000..c81edee60
--- /dev/null
+++ b/apps/landing/src/components/home/animated-number.tsx
@@ -0,0 +1,132 @@
+"use client";
+
+import NumberFlow from "@number-flow/react";
+import * as React from "react";
+
+const DURATION_MS = 1200;
+
+// Locates the first localized integer (any Unicode numbering system, with
+// locale grouping separators) so the surrounding translation stays untouched.
+function parseLocalizedInteger(text: string, locale: string) {
+ const digitFor = new Map<string, number>();
+ const plainFormat = new Intl.NumberFormat(locale, { useGrouping: false });
+ for (let digit = 0; digit <= 9; digit++) {
+ digitFor.set(plainFormat.format(digit), digit);
+ }
+ const separators = new Intl.NumberFormat(locale)
+ .formatToParts(1234567)
+ .filter((part) => part.type === "group")
+ .map((part) => part.value)
+ .join("");
+ const digits = [...digitFor.keys()].join("");
+ const pattern = new RegExp(
+ `[${digits}](?:[${digits}${separators}.,\\s\\u00A0\\u202F]*[${digits}])?`,
+ "u",
+ );
+ const match = pattern.exec(text);
+ if (!match) {
+ return null;
+ }
+ const token = match[0];
+ let value = 0;
+ for (const char of token) {
+ const digit = digitFor.get(char);
+ if (digit !== undefined) {
+ value = value * 10 + digit;
+ }
+ }
+ return { value, token, start: match.index };
+}
+
+function AnimatedNumber({
+ value,
+ display,
+ locale,
+}: {
+ value: number;
+ display: string;
+ locale: string;
+}) {
+ const [shown, setShown] = React.useState(value);
+ const [animated, setAnimated] = React.useState(false);
+ const ref = React.useRef<HTMLSpanElement>(null);
+
+ React.useEffect(() => {
+ const el = ref.current;
+ if (!el) {
+ return;
+ }
+ if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
+ return;
+ }
+ let frame: number;
+ const observer = new IntersectionObserver(
+ ([entry]) => {
+ if (!entry.isIntersecting) {
+ return;
+ }
+ observer.disconnect();
+ // Two committed renders: drop to 0 without animation, then roll up.
+ setShown(0);
+ frame = requestAnimationFrame(() => {
+ frame = requestAnimationFrame(() => {
+ setAnimated(true);
+ setShown(value);
+ });
+ });
+ },
+ { threshold: 0.5 },
+ );
+ observer.observe(el);
+ return () => {
+ observer.disconnect();
+ cancelAnimationFrame(frame);
+ };
+ }, [value]);
+
+ return (
+ <span ref={ref} role="img" aria-label={display}>
+ <span aria-hidden="true">
+ <NumberFlow
+ value={shown}
+ locales={locale}
+ animated={animated}
+ spinTiming={{
+ duration: DURATION_MS,
+ easing: "cubic-bezier(0.16, 1, 0.3, 1)",
+ }}
+ />
+ </span>
+ </span>
+ );
+}
+
+export function AnimatedStat({
+ locale,
+ children,
+}: {
+ locale: string;
+ children?: React.ReactNode;
+}) {
+ const text = React.Children.toArray(children)
+ .filter((child): child is string => typeof child === "string")
+ .join("");
+ const parsed = parseLocalizedInteger(text, locale);
+ return (
+ <strong className="font-semibold text-gray-800">
+ {parsed ? (
+ <>
+ {text.slice(0, parsed.start)}
+ <AnimatedNumber
+ value={parsed.value}
+ display={parsed.token}
+ locale={locale}
+ />
+ {text.slice(parsed.start + parsed.token.length)}
+ </>
+ ) : (
+ children
+ )}
+ </strong>
+ );
+}
diff --git a/apps/landing/src/components/home/bonus.tsx b/apps/landing/src/components/home/bonus.tsx
index 01874cde3..78f529c5c 100644
--- a/apps/landing/src/components/home/bonus.tsx
+++ b/apps/landing/src/components/home/bonus.tsx
@@ -1,4 +1,5 @@
import { Trans } from "react-i18next/TransWithoutContext";
+import { AnimatedStat } from "@/components/home/animated-number";
import { getTranslation } from "@/i18n/server";
import { getMonthlyPollCount, getMonthlyVoterCount } from "@/lib/data";
@@ -18,7 +19,7 @@ export async function Bonus(props: { locale: string }) {
defaults="<b>{voterCount, plural, one {# person} other {# people}}</b> voted on <b>{pollCount, plural, one {# poll} other {# polls}}</b> in the last 30 days"
values={{ voterCount, pollCount }}
components={{
- b: <strong className="font-semibold text-gray-800" />,
+ b: <AnimatedStat locale={props.locale} />,
}}
/>
</p>
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index f2ea37af4..59dfbe633 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -62,6 +62,9 @@ importers:
apps/landing:
dependencies:
+ '@number-flow/react':
+ specifier: ^0.6.2
'@prisma/adapter-pg':
specifier: ^7.8.0
version: 7.8.0
@@ -614,7 +617,7 @@ importers:
version: link:../tsconfig
'@react-email/ui':
specifier: ^6.6.3
'@types/nodemailer':
specifier: ^6.4.14
version: 6.4.22
@@ -3413,6 +3416,12 @@ packages:
resolution: {integrity: sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==}
engines: {node: '>= 8'}
+ '@number-flow/[email protected]':
+ resolution: {integrity: sha512-WjZuV4aA+vhRCgCF+adGLgFVVAJ8vdvq6EchRT2FiBIgElTXtDOA3YgkcMr9UHpvpS9v4H70AB5wZv0D9jc3QA==}
+ peerDependencies:
+ react: ^18 || ^19
+ react-dom: ^18 || ^19
+
'@openapi-contrib/[email protected]':
resolution: {integrity: sha512-Gj6C0JwCr8arj0sYuslWXUBSP/KnUlEGnPW4qxlXvAl543oaNQgMgIgkQUA6vs5BCCvwTEiL8m/wdWzfl4UvSw==}
@@ -7661,6 +7670,9 @@ packages:
resolution: {integrity: sha512-2NxwbF/hZ0KpepYN0cNbo+FN6XoK7GaHlQhgx/hIZl6Va0bF45RQOOwhLIy8lQDbuCiadSLCBnH2CFYquit5bw==}
engines: {node: '>=8.0.0'}
+ resolution: {integrity: sha512-Epxrv+Nr/CaL4ZcFGPJIYLWFom+YeV1DqMLHJoEd9SYRxNbaFruBwfEX/kkHUJf55j2+TUbmDcmuilbP1TmXHA==}
+
resolution: {integrity: sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==}
engines: {node: '>=4'}
@@ -9526,6 +9538,9 @@ packages:
resolution: {integrity: sha512-lqjrjmaOoAnWfMmBPL+XNnynZh2+swxiX3WUE0s4yEHI6m+AwrK2UZOimIRl3X/4QctVqS8AiZjFqyOGrMXb/w==}
+ resolution: {integrity: sha512-MCnImG4Q5vPwhSXnov56nOuyyKn6LC+Qd7II1UiKc+ACRtug5iAtn0+CwXNxM38AC5lSowEY+oYEtZX2qMnUyw==}
+
resolution: {integrity: sha512-7wfH4sLbt4M0gCDzGE6vzQBo0bfTKjU7Sfpqy/7gs1qBfYz2vEJH6vXcBKpO3+6Yu1telwd0t9HpyOoLEQQbIQ==}
@@ -14802,6 +14817,13 @@ snapshots:
'@nodelib/fs.scandir': 2.1.5
fastq: 1.20.1
+ dependencies:
+ esm-env: 1.2.2
+ number-flow: 0.6.2
+ react: 19.2.6
+ react-dom: 19.2.6([email protected])
+
'@openapi-contrib/[email protected]':
dependencies:
fast-deep-equal: 3.1.3
@@ -16239,10 +16261,10 @@ snapshots:
dependencies:
react: 19.2.6
dependencies:
esbuild: 0.28.0
transitivePeerDependencies:
- '@babel/core'
- '@opentelemetry/api'
@@ -19828,6 +19850,8 @@ snapshots:
esrecurse: 4.3.0
estraverse: 4.3.0
+
@@ -22122,7 +22146,7 @@ snapshots:
react: 19.2.6
react-dom: 19.2.6([email protected])
dependencies:
'@next/env': 16.2.6
'@swc/helpers': 0.5.15
@@ -22227,6 +22251,10 @@ snapshots:
dependencies:
boolbase: 1.0.0
+ dependencies:
+ esm-env: 1.2.2
+