Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

feat(frontend): add a rolling number adapted from Bencho's Rolling counter

spacesdrive54de19ef9727
diff --git a/frontend/src/components/RollingNumber.tsx b/frontend/src/components/RollingNumber.tsx
new file mode 100644
index 0000000..f384e44
--- /dev/null
+++ b/frontend/src/components/RollingNumber.tsx
@@ -0,0 +1,38 @@
+import type { CSSProperties } from 'react';
+import { rollingChars } from '@/lib/rollingNumber';
+import { cn } from '@/lib/utils';
+
+export interface RollingNumberProps {
+ // the number as it should read, already formatted ("12.4K", "1,204")
+ text: string;
+ className?: string;
+}
+
+const DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
+
+// adapted from Bencho's Rolling counter (MIT, see THIRD_PARTY_NOTICES.md): every digit is a 0 to 9 strip behind a
+// one-line window, so a changed value rolls each column to its new digit like an odometer; screen readers get the text
+export default function RollingNumber({ text, className }: RollingNumberProps) {
+ const chars = rollingChars(text);
+ return (
+ <span className={cn('roll tabular-nums', className)}>
+ <span className="sr-only">{text}</span>
+ <span className="roll-row" aria-hidden="true">
+ {chars.map((item, index) =>
+ item.digit === null ? (
+ <span key={item.key} className="roll-sign">{item.char}</span>
+ ) : (
+ <span key={item.key} className="roll-window">
+ <span
+ className="roll-strip"
+ style={{ '--roll-digit': item.digit, '--roll-order': chars.length - 1 - index } as CSSProperties}
+ >
+ {DIGITS.map(digit => <span key={digit}>{digit}</span>)}
+ </span>
+ </span>
+ ),
+ )}
+ </span>
+ </span>
+ );
+}
diff --git a/frontend/src/lib/rollingNumber.ts b/frontend/src/lib/rollingNumber.ts
new file mode 100644
index 0000000..2d016de
--- /dev/null
+++ b/frontend/src/lib/rollingNumber.ts
@@ -0,0 +1,17 @@
+// the characters of a formatted number, each marked as a rolling digit or a fixed sign (comma, point, unit letter)
+
+export interface RollingChar {
+ // counted from the right, so the ones column keeps its key when the number grows a digit on the left
+ key: string;
+ char: string;
+ digit: number | null;
+}
+
+export function rollingChars(text: string): RollingChar[] {
+ const chars = [...text];
+ return chars.map((char, index) => {
+ const fromRight = chars.length - 1 - index;
+ const digit = /^[0-9]$/.test(char) ? Number(char) : null;
+ return { key: `${digit === null ? 's' : 'd'}${fromRight}`, char, digit };
+ });
+}
diff --git a/frontend/tests/lib/rollingNumber.test.ts b/frontend/tests/lib/rollingNumber.test.ts
new file mode 100644
index 0000000..6676fa8
--- /dev/null
+++ b/frontend/tests/lib/rollingNumber.test.ts
@@ -0,0 +1,15 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { rollingChars } from '../../src/lib/rollingNumber.ts';
+
+test('digits roll and signs stay fixed', () => {
+ assert.deepEqual(rollingChars('12.4K').map(item => item.digit), [1, 2, null, 4, null]);
+});
+
+test('columns are keyed from the right so the ones column keeps its key when the number grows', () => {
+ const before = rollingChars('999');
+ const after = rollingChars('1,000');
+ assert.equal(before[before.length - 1].key, after[after.length - 1].key);
+ assert.equal(after[0].key, 'd4');
+ assert.equal(after[1].key, 's3');
+});