Leaderboard
1 variant
A compact leaderboard card with a podium for the top three, period tabs, live re-ranking, rank movement, and a pinned row for the viewer.
Live ranking
leaderboard.tsxPlinths grow up for the top three, then the list cascades in. Switching period hands podium places to new people and glides the list into its new order while scores roll, and your row glides up from its pin when you climb onto the board.
Leaderboard
Points from merged reviews
- Jonas Hale388Down 1
- Mira Okafor412Up 1
- Amara Diallo371No change
- 4
Theo LindqvistUp 2340
- 5
Ines VargaDown 1326
- 6
Sam OrtegaYouDown 1301
- 7
Kai MoriyamaNo change297
TSXcomponents/previews/premium/leaderboard-preview.tsx
"use client";
import { useInView, useReducedMotion } from "motion/react";
import { useEffect, useRef, useState } from "react";
import {
DEFAULT_LEADERBOARD_PERIODS,
DEFAULT_LEADERBOARD_USER_ID,
Leaderboard,
type LeaderboardPeriod,
rankEntries,
} from "@/components/premium/leaderboard";
const TICK_MS = 2800;
const VISIBLE_COUNT = 7;
const randomInt = (min: number, max: number) =>
min + Math.floor(Math.random() * (max - min + 1));
/**
* Demo feed: bumps a couple of scores in the viewed period. Most ticks set
* up an overtake near the top, and now and then the viewer climbs a place.
*/
function bumpScores(period: LeaderboardPeriod): LeaderboardPeriod {
const ranked = rankEntries(period.entries);
if (ranked.length < 2) return period;
const scale = ranked[0].score;
const bumps = new Map<string, number>();
const you = ranked.find((e) => e.id === DEFAULT_LEADERBOARD_USER_ID);
const youCanClimb =
you && you.rank > 1 && you.rank <= VISIBLE_COUNT + 3 && Math.random() < 0.3;
const index = youCanClimb
? you.rank - 1
: randomInt(1, Math.min(ranked.length - 1, VISIBLE_COUNT + 1));
const chaser = ranked[index];
const gap = ranked[index - 1].score - chaser.score;
bumps.set(
chaser.id,
Math.random() < 0.6
? gap + randomInt(1, Math.max(2, Math.round(scale * 0.03)))
: randomInt(1, Math.max(2, Math.round(scale * 0.02))),
);
const other = ranked[randomInt(0, ranked.length - 1)];
if (!bumps.has(other.id)) {
bumps.set(other.id, randomInt(1, Math.max(2, Math.round(scale * 0.015))));
}
return {
...period,
entries: period.entries.map((entry) =>
bumps.has(entry.id)
? { ...entry, score: entry.score + (bumps.get(entry.id) ?? 0) }
: entry,
),
};
}
export function LeaderboardPreview() {
const reduce = useReducedMotion();
const ref = useRef<HTMLDivElement>(null);
const inView = useInView(ref, { amount: 0.3 });
const [periods, setPeriods] = useState(DEFAULT_LEADERBOARD_PERIODS);
const [periodId, setPeriodId] = useState(DEFAULT_LEADERBOARD_PERIODS[0].id);
const [listActive, setListActive] = useState(false);
const live = inView && !reduce && !listActive;
useEffect(() => {
if (!live) return;
const id = window.setInterval(() => {
setPeriods((current) =>
current.map((p) => (p.id === periodId ? bumpScores(p) : p)),
);
}, TICK_MS);
return () => window.clearInterval(id);
}, [live, periodId]);
return (
<div ref={ref} className="w-full px-4 py-10">
<Leaderboard
periods={periods}
periodId={periodId}
onPeriodChange={setPeriodId}
visibleCount={VISIBLE_COUNT}
onListActiveChange={setListActive}
/>
</div>
);
}
Source code is locked
Get access to install premium components and view the full source.
Open dashboardInstall
$ bunx --bun shadcn add @beui-pro/leaderboard