Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

feat(landing): animate footer star turns counter with NumberFlow (MUL-9739) (#11089)

Jiayuan Zhangd405319d18fd
diff --git a/apps/web/features/landing/site/footer-wordmark.test.tsx b/apps/web/features/landing/site/footer-wordmark.test.tsx
new file mode 100644
index 0000000000..63057ba8b4
--- /dev/null
+++ b/apps/web/features/landing/site/footer-wordmark.test.tsx
@@ -0,0 +1,189 @@
+import { act, fireEvent, render, waitFor } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { continuous } from "@multica/ui/components/ui/number-flow";
+import { FooterWordmark } from "./footer-wordmark";
+
+type IntersectionCb = IntersectionObserverCallback;
+
+const intersectionObservers: Array<{ cb: IntersectionCb; elements: Element[] }> = [];
+
+function stubBrowserAPIs(reducedMotion = false) {
+ intersectionObservers.length = 0;
+
+ vi.stubGlobal(
+ "matchMedia",
+ vi.fn().mockImplementation((query: string) => ({
+ matches: query === "(prefers-reduced-motion: reduce)" ? reducedMotion : false,
+ media: query,
+ addEventListener: vi.fn(),
+ removeEventListener: vi.fn(),
+ addListener: vi.fn(),
+ removeListener: vi.fn(),
+ dispatchEvent: vi.fn(),
+ })),
+ );
+
+ vi.stubGlobal(
+ "IntersectionObserver",
+ class MockIntersectionObserver {
+ private record: { cb: IntersectionCb; elements: Element[] };
+ constructor(cb: IntersectionCb) {
+ this.record = { cb, elements: [] };
+ intersectionObservers.push(this.record);
+ }
+ observe(el: Element) {
+ this.record.elements.push(el);
+ }
+ unobserve() {}
+ disconnect() {}
+ takeRecords() {
+ return [];
+ }
+ },
+ );
+
+ vi.stubGlobal(
+ "ResizeObserver",
+ class MockResizeObserver {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ },
+ );
+
+ vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue({
+ setTransform: vi.fn(),
+ clearRect: vi.fn(),
+ fillRect: vi.fn(),
+ translate: vi.fn(),
+ rotate: vi.fn(),
+ scale: vi.fn(),
+ fill: vi.fn(),
+ stroke: vi.fn(),
+ getImageData: vi.fn(() => ({ data: new Uint8ClampedArray(4) })),
+ fillStyle: "",
+ globalAlpha: 1,
+ lineWidth: 1,
+ } as unknown as CanvasRenderingContext2D);
+}
+
+function triggerAllIntersections() {
+ for (const obs of intersectionObservers) {
+ for (const target of obs.elements) {
+ obs.cb(
+ [{ isIntersecting: true, target } as IntersectionObserverEntry],
+ {} as IntersectionObserver,
+ );
+ }
+ }
+}
+
+describe("FooterWordmark", () => {
+ beforeEach(() => {
+ stubBrowserAPIs(false);
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+ });
+
+ it("keeps the counter unmounted until the server total arrives, then animates with NumberFlow", async () => {
+ let resolveFetch!: (value: Response) => void;
+ const fetchMock = vi.fn().mockImplementation(
+ () =>
+ new Promise<Response>((resolve) => {
+ resolveFetch = resolve;
+ }),
+ );
+ vi.stubGlobal("fetch", fetchMock);
+
+ const { container } = render(
+ <FooterWordmark
+ label="大家已经把这颗星吹转了 {count} 圈"
+ locale="zh-Hans"
+ />,
+ );
+
+ const counterParagraph = container.querySelector("p");
+ expect(counterParagraph).toHaveClass("opacity-0");
+ expect(container.querySelector("number-flow-react")).toBeNull();
+
+ act(() => {
+ triggerAllIntersections();
+ });
+
+ await waitFor(() => {
+ expect(fetchMock).toHaveBeenCalledWith("/api/site/star", { cache: "no-store" });
+ });
+
+ await act(async () => {
+ resolveFetch(
+ new Response(JSON.stringify({ turns: 113835 }), {
+ status: 200,
+ headers: { "Content-Type": "application/json" },
+ }),
+ );
+ });
+
+ const flow = container.querySelector("number-flow-react") as
+ | (HTMLElement & {
+ respectMotionPreference?: boolean;
+ plugins?: unknown[];
+ })
+ | null;
+
+ expect(counterParagraph).not.toHaveClass("opacity-0");
+ expect(counterParagraph).toHaveTextContent("大家已经把这颗星吹转了");
+ expect(counterParagraph).toHaveTextContent("圈");
+ expect(flow).not.toBeNull();
+ expect(flow).toHaveAttribute("aria-label", "113,835");
+ expect(flow?.respectMotionPreference).toBe(true);
+ expect(flow?.plugins).toEqual([continuous]);
+ });
+
+ it("updates the animated NumberFlow counter as turns accumulate", async () => {
+ stubBrowserAPIs(true);
+ const fetchMock = vi.fn().mockResolvedValue(
+ new Response(JSON.stringify({ turns: 113835 }), {
+ status: 200,
+ headers: { "Content-Type": "application/json" },
+ }),
+ );
+ vi.stubGlobal("fetch", fetchMock);
+
+ const { container } = render(
+ <FooterWordmark
+ label="Everyone has blown the star round {count} times"
+ locale="en"
+ />,
+ );
+
+ act(() => {
+ triggerAllIntersections();
+ });
+
+ await waitFor(() => {
+ expect(container.querySelector("number-flow-react")).toHaveAttribute(
+ "aria-label",
+ "113,835",
+ );
+ });
+
+ // Under reduced motion each click turns the star one arm (1/8 turn),
+ // so 8 clicks credit 1 full turn.
+ const canvas = container.querySelector("canvas")!;
+ act(() => {
+ for (let i = 0; i < 8; i++) {
+ fireEvent.pointerDown(canvas, { button: 0, clientX: 50, clientY: 50 });
+ }
+ });
+
+ await waitFor(() => {
+ expect(container.querySelector("number-flow-react")).toHaveAttribute(
+ "aria-label",
+ "113,836",
+ );
+ });
+ });
+});
diff --git a/apps/web/features/landing/site/footer-wordmark.tsx b/apps/web/features/landing/site/footer-wordmark.tsx
index bf7189437e..6eb75ec0db 100644
--- a/apps/web/features/landing/site/footer-wordmark.tsx
+++ b/apps/web/features/landing/site/footer-wordmark.tsx
@@ -1,6 +1,7 @@
"use client";
import { useEffect, useRef, useState } from "react";
+import { continuous, NumberFlow } from "@multica/ui/components/ui/number-flow";
import { WORDMARK, WORDMARK_STAR, WORDMARK_STROKE } from "@multica/ui/lib/multica-brand";
import { cn } from "@multica/ui/lib/utils";
import { Container } from "./primitives";
@@ -101,6 +102,8 @@ const MAX_REPORT = 30;
/** Spin (rad/s) a turn blown elsewhere adds here, capped so a busy moment still reads as a nudge. */
const REMOTE_PUSH = Math.PI;
const MAX_REMOTE_SPIN = 18;
+const STAR_COUNT_FORMAT = { maximumFractionDigits: 0 } as const;
+const STAR_COUNT_PLUGINS = [continuous];
/** 8×8 Bayer thresholds in (0, 1): a cell shows when its density exceeds its threshold. */
const BAYER = (() => {
@@ -668,6 +671,7 @@ export function FooterWordmark({ className, label, locale }: { className?: strin
}, []);
const [before, after = ""] = label.split("{count}");
+ const formattedTotal = total !== null ? new Intl.NumberFormat(locale, STAR_COUNT_FORMAT).format(total) : null;
return (
<div ref={box} className={cn("relative text-site-ink", className)}>
@@ -680,9 +684,21 @@ export function FooterWordmark({ className, label, locale }: { className?: strin
)}
aria-live="off"
>
- {before}
- <span className="tabular-nums text-site-ink">{new Intl.NumberFormat(locale).format(total ?? 0)}</span>
- {after}
+ {total !== null ? (
+ <>
+ {before}
+ <NumberFlow
+ value={total}
+ locales={locale}
+ format={STAR_COUNT_FORMAT}
+ plugins={STAR_COUNT_PLUGINS}
+ willChange
+ aria-label={formattedTotal ?? undefined}
+ className="text-site-ink"
+ />
+ {after}
+ </>
+ ) : null}
</p>
</div>
</Container>
diff --git a/packages/ui/components/ui/number-flow.tsx b/packages/ui/components/ui/number-flow.tsx
index e2094ae694..26f5eae6cb 100644
--- a/packages/ui/components/ui/number-flow.tsx
+++ b/packages/ui/components/ui/number-flow.tsx
@@ -1,6 +1,7 @@
"use client"
import NumberFlowPrimitive, {
+ continuous,
NumberFlowGroup,
type NumberFlowProps,
} from "@number-flow/react"
@@ -156,6 +157,7 @@ function CappedNumberFlow({
export {
CappedNumberFlow,
CompactNumberFlow,
+ continuous,
CurrencyNumberFlow,
NumberFlow,
NumberFlowGroup,