Skip to content

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

✨ Keep the stats badges ticking up while the page is open (#3038)

Luke Vellad5208f3f30ff
diff --git a/apps/landing/src/app/[locale]/(main)/(seo)/best-doodle-alternative/page.tsx b/apps/landing/src/app/[locale]/(main)/(seo)/best-doodle-alternative/page.tsx
index aa77851c6..1471d292b 100644
--- a/apps/landing/src/app/[locale]/(main)/(seo)/best-doodle-alternative/page.tsx
+++ b/apps/landing/src/app/[locale]/(main)/(seo)/best-doodle-alternative/page.tsx
@@ -55,8 +55,8 @@ export default async function Page(props: {
defaults="<0>{voterCount, plural, one {# person} other {# people}}</0> voted on <1>{pollCount, plural, one {# poll} other {# polls}}</1> in the last 30 days"
values={{ voterCount, pollCount }}
components={[
- <PeopleBadge key="people" locale={locale} />,
- <PollsBadge key="polls" locale={locale} />,
+ <PeopleBadge key="people" locale={locale} live />,
+ <PollsBadge key="polls" locale={locale} live />,
]}
/>
</Stats>
diff --git a/apps/landing/src/app/[locale]/(main)/(seo)/free-scheduling-poll/page.tsx b/apps/landing/src/app/[locale]/(main)/(seo)/free-scheduling-poll/page.tsx
index 57b0c6e32..941241751 100644
--- a/apps/landing/src/app/[locale]/(main)/(seo)/free-scheduling-poll/page.tsx
+++ b/apps/landing/src/app/[locale]/(main)/(seo)/free-scheduling-poll/page.tsx
@@ -55,8 +55,8 @@ export default async function Page(props: {
defaults="<0>{voterCount, plural, one {# person} other {# people}}</0> voted on <1>{pollCount, plural, one {# poll} other {# polls}}</1> in the last 30 days"
values={{ voterCount, pollCount }}
components={[
- <PeopleBadge key="people" locale={locale} />,
- <PollsBadge key="polls" locale={locale} />,
+ <PeopleBadge key="people" locale={locale} live />,
+ <PollsBadge key="polls" locale={locale} live />,
]}
/>
</Stats>
diff --git a/apps/landing/src/app/[locale]/(main)/(seo)/when2meet-alternative/page.tsx b/apps/landing/src/app/[locale]/(main)/(seo)/when2meet-alternative/page.tsx
index ebea883f7..4e8681f1d 100644
--- a/apps/landing/src/app/[locale]/(main)/(seo)/when2meet-alternative/page.tsx
+++ b/apps/landing/src/app/[locale]/(main)/(seo)/when2meet-alternative/page.tsx
@@ -55,8 +55,8 @@ export default async function Page(props: {
defaults="<0>{voterCount, plural, one {# person} other {# people}}</0> voted on <1>{pollCount, plural, one {# poll} other {# polls}}</1> in the last 30 days"
values={{ voterCount, pollCount }}
components={[
- <PeopleBadge key="people" locale={locale} />,
- <PollsBadge key="polls" locale={locale} />,
+ <PeopleBadge key="people" locale={locale} live />,
+ <PollsBadge key="polls" locale={locale} live />,
]}
/>
</Stats>
diff --git a/apps/landing/src/app/[locale]/(main)/page.tsx b/apps/landing/src/app/[locale]/(main)/page.tsx
index e00894ecc..b2666a7b3 100644
--- a/apps/landing/src/app/[locale]/(main)/page.tsx
+++ b/apps/landing/src/app/[locale]/(main)/page.tsx
@@ -78,8 +78,8 @@ export default async function Page(props: {
defaults="<0>{voterCount, plural, one {# person} other {# people}}</0> voted on <1>{pollCount, plural, one {# poll} other {# polls}}</1> in the last 30 days"
values={{ voterCount, pollCount }}
components={[
- <PeopleBadge key="people" locale={locale} />,
- <PollsBadge key="polls" locale={locale} />,
+ <PeopleBadge key="people" locale={locale} live />,
+ <PollsBadge key="polls" locale={locale} live />,
]}
/>
</Stats>
diff --git a/apps/landing/src/app/[locale]/(main)/pricing/page.tsx b/apps/landing/src/app/[locale]/(main)/pricing/page.tsx
index 198454068..733353f4a 100644
--- a/apps/landing/src/app/[locale]/(main)/pricing/page.tsx
+++ b/apps/landing/src/app/[locale]/(main)/pricing/page.tsx
@@ -432,8 +432,8 @@ export default async function Page(props: {
defaults="<0>{voterCount, plural, one {# person} other {# people}}</0> voted on <1>{pollCount, plural, one {# poll} other {# polls}}</1> in the last 30 days"
values={{ voterCount, pollCount }}
components={[
- <PeopleBadge key="people" locale={locale} />,
- <PollsBadge key="polls" locale={locale} />,
+ <PeopleBadge key="people" locale={locale} live />,
+ <PollsBadge key="polls" locale={locale} live />,
]}
/>
</Stats>
diff --git a/apps/landing/src/components/home/animated-number.tsx b/apps/landing/src/components/home/animated-number.tsx
index 23592eb1b..e1929f3ca 100644
--- a/apps/landing/src/components/home/animated-number.tsx
+++ b/apps/landing/src/components/home/animated-number.tsx
@@ -39,28 +39,84 @@ function parseLocalizedInteger(text: string, locale: string) {
return { value, token, start: match.index };
}
+const THIRTY_DAYS_MS = 30 * 24 * 60 * 60 * 1000;
+// Below this the ticker would fire faster than it reads as a discrete event.
+const MIN_TICK_MS = 2000;
+
+/**
+ * Ticks the badge up at the average rate the 30-day count was accumulated,
+ * so the number visibly moves while the page is open.
+ *
+ * Deliberately anchored to page load rather than to when the snapshot was
+ * taken. The 30-day figure is a rolling window and so roughly stationary —
+ * votes age out of the back as fast as they arrive at the front — meaning
+ * projecting it forward from a cached timestamp would overstate a real
+ * published statistic without bound (a week-old cache entry would add ~23%).
+ * Starting from the measured value keeps first paint accurate.
+ */
+function useLiveCount({
+ baseValue,
+ enabled,
+}: {
+ baseValue: number;
+ enabled: boolean;
+}) {
+ const tickMs = baseValue > 0 ? THIRTY_DAYS_MS / baseValue : 0;
+ const [value, setValue] = React.useState(baseValue);
+
+ React.useEffect(() => {
+ setValue(baseValue);
+ if (!enabled || tickMs <= 0) {
+ return;
+ }
+ const interval = Math.max(tickMs, MIN_TICK_MS);
+ const startedAt = Date.now();
+ // Derived from the wall clock rather than incremented, so a throttled or
+ // suspended timer catches up in one step instead of drifting behind.
+ const id = window.setInterval(() => {
+ const elapsed = Math.max(0, Date.now() - startedAt);
+ setValue(baseValue + Math.floor(elapsed / interval));
+ }, interval);
+ return () => window.clearInterval(id);
+ }, [baseValue, tickMs, enabled]);
+
+ return value;
+}
+
function AnimatedNumber({
value,
display,
locale,
+ live: liveEnabled = false,
}: {
value: number;
display: string;
locale: string;
+ live?: boolean;
}) {
+ const [rolledUp, setRolledUp] = React.useState(false);
const [shown, setShown] = React.useState(value);
const [animated, setAnimated] = React.useState(false);
const ref = React.useRef<HTMLSpanElement>(null);
+ const live = useLiveCount({
+ baseValue: value,
+ // Ticking starts only once the roll-up has landed, so the two animations
+ // never fight over the same digits.
+ enabled: liveEnabled && rolledUp,
+ });
+
React.useEffect(() => {
const el = ref.current;
if (!el) {
return;
}
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
+ setRolledUp(true);
return;
}
let frame: number;
+ let settle: number;
const observer = new IntersectionObserver(
([entry]) => {
if (!entry.isIntersecting) {
@@ -73,6 +129,7 @@ function AnimatedNumber({
frame = requestAnimationFrame(() => {
setAnimated(true);
setShown(value);
+ settle = window.setTimeout(() => setRolledUp(true), DURATION_MS);
});
});
},
@@ -82,22 +139,35 @@ function AnimatedNumber({
return () => {
observer.disconnect();
cancelAnimationFrame(frame);
+ window.clearTimeout(settle);
};
}, [value]);
+ // Once ticking, the live value drives the display; before that the roll-up
+ // owns it.
+ const target = liveEnabled && rolledUp ? live : shown;
+ // The sizer keeps the translation's own token unless ticking has pushed the
+ // number past it, so a badge that never ticks measures exactly as before and
+ // a ticking one never reflows the sentence as digits are added.
+ const sizerText =
+ live > value ? new Intl.NumberFormat(locale).format(live) : display;
+
return (
// tabular-nums keeps the sizer and NumberFlow (which renders digits at a
// uniform width) measuring identically.
<span
ref={ref}
role="img"
+ // Stays on the value the sentence was written around: the ticking is a
+ // liveness flourish, and a label that mutates every minute would be
+ // churn for assistive tech without conveying anything new.
aria-label={display}
className="relative inline-block tabular-nums"
>
{/* Invisible copy of the final number holds its width for the whole
animation so the surrounding text doesn't shift as digits roll in */}
<span className="invisible" aria-hidden="true">
- {display}
+ {sizerText}
</span>
{/* NumberFlow's box is taller than the text (mask padding) but
symmetric around the glyphs, so centering it on the sizer — also
@@ -107,7 +177,7 @@ function AnimatedNumber({
aria-hidden="true"
>
<NumberFlow
- value={shown}
+ value={target}
locales={locale}
animated={animated}
// Layout snaps instantly so digits spin straight vertically in
@@ -126,10 +196,12 @@ function AnimatedNumber({
function AnimatedStat({
locale,
icon,
+ live,
children,
}: {
locale: string;
icon?: React.ReactNode;
+ live?: boolean;
children?: React.ReactNode;
}) {
const text = React.Children.toArray(children)
@@ -154,6 +226,7 @@ function AnimatedStat({
value={parsed.value}
display={parsed.token}
locale={locale}
+ live={live}
/>
{text.slice(parsed.start + parsed.token.length)}
</>
@@ -167,13 +240,15 @@ function AnimatedStat({
export function PeopleBadge({
locale,
+ live,
children,
}: {
locale: string;
+ live?: boolean;
children?: React.ReactNode;
}) {
return (
- <AnimatedStat locale={locale} icon={<UsersIcon />}>
+ <AnimatedStat locale={locale} icon={<UsersIcon />} live={live}>
{children}
</AnimatedStat>
);
@@ -181,13 +256,15 @@ export function PeopleBadge({
export function PollsBadge({
locale,
+ live,
children,
}: {
locale: string;
+ live?: boolean;
children?: React.ReactNode;
}) {
return (
- <AnimatedStat locale={locale} icon={<BarChart2Icon />}>
+ <AnimatedStat locale={locale} icon={<BarChart2Icon />} live={live}>
{children}
</AnimatedStat>
);