{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "leaderboard",
  "title": "Leaderboard Block",
  "description": "A leaderboard with a medal podium for the top three, a ranked list with progress bars and up or down arrows against the previous period, period, metric and team switches that re-rank everyone with smooth motion, and your own row pinned at the bottom when you are further down.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/leaderboard/leaderboard.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, LayoutGroup, motion, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport interface LeaderboardPlayer {\n  id: string;\n  name: string;\n  team: string;\n  avatar?: string;\n  /** Score per period and metric, e.g. scores.week.points. */\n  scores: Record<string, Record<string, number>>;\n}\n\nexport interface LeaderboardMetric {\n  id: string;\n  label: string;\n  /** Unit after the number, e.g. \"pts\" or \"days\". */\n  unit?: string;\n}\n\nexport interface LeaderboardProps {\n  title?: string;\n  description?: string;\n  players?: LeaderboardPlayer[];\n  periods?: { id: string; label: string; previous?: string }[];\n  metrics?: LeaderboardMetric[];\n  /** Highlighted as \"You\" and pinned under the list when outside the shown rows. */\n  currentUserId?: string;\n  /** Rows shown under the podium. */\n  limit?: number;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { bg: \"#0A0A0A\", text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.6)\", faint: \"rgba(245,244,241,0.36)\", card: \"#111111\", border: \"rgba(255,255,255,0.09)\", chip: \"rgba(255,255,255,0.05)\", track: \"rgba(255,255,255,0.08)\", on: \"#F5F4F1\", onText: \"#0A0A0A\", up: \"#4ADE80\", down: \"#F87171\", me: \"rgba(124,140,255,0.14)\", meLine: \"#7C8CFF\", ring: \"rgba(245,244,241,0.6)\" },\n  light: { bg: \"#FAFAF8\", text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.6)\", faint: \"rgba(10,10,10,0.4)\", card: \"#FFFFFF\", border: \"rgba(10,10,10,0.09)\", chip: \"rgba(10,10,10,0.04)\", track: \"rgba(10,10,10,0.07)\", on: \"#0A0A0A\", onText: \"#FAFAF8\", up: \"#15803D\", down: \"#DC2626\", me: \"rgba(79,70,229,0.08)\", meLine: \"#4F46E5\", ring: \"rgba(10,10,10,0.5)\" },\n};\n\nconst MEDALS = [\"#F5C451\", \"#C9CED6\", \"#D19A66\"];\nconst DEFAULT_PERIODS = [\n  { id: \"week\", label: \"This week\", previous: \"lastWeek\" },\n  { id: \"month\", label: \"This month\", previous: \"lastMonth\" },\n  { id: \"all\", label: \"All time\" },\n];\nconst DEFAULT_METRICS: LeaderboardMetric[] = [\n  { id: \"points\", label: \"Points\", unit: \"pts\" },\n  { id: \"shipped\", label: \"Tasks shipped\" },\n  { id: \"streak\", label: \"Streak\", unit: \"days\" },\n];\n\nconst PEOPLE: [string, string, string][] = [\n  [\"Leyla Aksoy\", \"Product\", \"/demo/49.webp\"],\n  [\"Kenji Mori\", \"Engineering\", \"/demo/42.webp\"],\n  [\"Ines Duarte\", \"Design\", \"/demo/41.webp\"],\n  [\"Theo Mensah\", \"Engineering\", \"/demo/47.webp\"],\n  [\"Hanna Kowalski\", \"Product\", \"/demo/55.webp\"],\n  [\"Daniel Ortiz\", \"Engineering\", \"/demo/51.webp\"],\n  [\"Selin Ward\", \"Design\", \"/demo/50.webp\"],\n  [\"Marcus Hale\", \"Sales\", \"/demo/53.webp\"],\n  [\"Clara Weiss\", \"Sales\", \"/demo/44.webp\"],\n  [\"Tomas Lind\", \"Sales\", \"/demo/43.webp\"],\n  [\"Noor Rahman\", \"Engineering\", \"/demo/48.webp\"],\n  [\"Ava Lindqvist\", \"Product\", \"/demo/45.webp\"],\n  [\"Priya Nair\", \"Design\", \"/demo/40.webp\"],\n  [\"Sara Lindgren\", \"Engineering\", \"/demo/22.webp\"],\n  [\"Nadia Petrova\", \"Design\", \"\"],\n  [\"Mei Tanaka\", \"Sales\", \"/demo/112.webp\"],\n];\n\nfunction hash(s: string) {\n  let h = 2166136261;\n  for (let i = 0; i < s.length; i++) h = Math.imul(h ^ s.charCodeAt(i), 16777619);\n  return h >>> 0;\n}\nfunction seedPlayers(): LeaderboardPlayer[] {\n  return PEOPLE.map(([name, team, avatar], i) => {\n    const id = name.toLowerCase().replace(/\\s+/g, \"-\");\n    // The demo \"You\" sits lower down so the pinned row is visible.\n    const scale = id === \"nadia-petrova\" ? 0.45 : 1;\n    const v = (k: string, base: number, spread: number) => Math.round((base + (hash(`${id}-${k}`) % spread)) * scale);\n    const week = v(\"w\", 180, 640);\n    const lastWeek = v(\"lw\", 180, 640);\n    const month = week * 3 + v(\"m\", 300, 1400);\n    const lastMonth = v(\"lm\", 900, 2600);\n    const all = month * 4 + v(\"a\", 2000, 9000) + (PEOPLE.length - i) * 200;\n    const sh = (n: number) => Math.round(n / 38);\n    return {\n      id,\n      name,\n      team,\n      avatar: avatar || undefined,\n      scores: {\n        week: { points: week, shipped: sh(week), streak: v(\"sw\", 1, 7) },\n        lastWeek: { points: lastWeek, shipped: sh(lastWeek), streak: v(\"slw\", 1, 7) },\n        month: { points: month, shipped: sh(month), streak: v(\"sm\", 3, 26) },\n        lastMonth: { points: lastMonth, shipped: sh(lastMonth), streak: v(\"slm\", 3, 26) },\n        all: { points: all, shipped: sh(all), streak: v(\"sa\", 10, 90) },\n      },\n    };\n  });\n}\n\nfunction Avatar({ player, size, p }: { player: LeaderboardPlayer; size: number; p: (typeof PALETTES)[\"dark\"] }) {\n  if (player.avatar) {\n    // eslint-disable-next-line @next/next/no-img-element\n    return <img src={player.avatar} alt=\"\" width={size} height={size} className=\"shrink-0 rounded-full object-cover\" style={{ width: size, height: size }} />;\n  }\n  const initials = player.name.split(\" \").map((w) => w[0]).slice(0, 2).join(\"\");\n  return (\n    <span className=\"flex shrink-0 items-center justify-center rounded-full font-semibold\" style={{ width: size, height: size, background: p.chip, color: p.muted, fontSize: size * 0.36 }} aria-hidden=\"true\">\n      {initials}\n    </span>\n  );\n}\n\nfunction Delta({ d, p }: { d: number | null; p: (typeof PALETTES)[\"dark\"] }) {\n  if (d === null) return <span className=\"w-8\" />;\n  if (d === 0) return <span className=\"w-8 text-center text-[12px]\" style={{ color: p.faint }} aria-label=\"No change\">–</span>;\n  return (\n    <span className=\"w-8 text-center text-[12px] font-medium tabular-nums\" style={{ color: d > 0 ? p.up : p.down }} aria-label={d > 0 ? `Up ${d}` : `Down ${-d}`}>\n      {d > 0 ? \"▲\" : \"▼\"}\n      {Math.abs(d)}\n    </span>\n  );\n}\n\nexport function Leaderboard({\n  title = \"Leaderboard\",\n  description = \"Points come from shipped tasks, reviews and helping teammates. Updated every hour.\",\n  players,\n  periods = DEFAULT_PERIODS,\n  metrics = DEFAULT_METRICS,\n  currentUserId = \"nadia-petrova\",\n  limit = 7,\n  theme = \"dark\",\n  className,\n}: LeaderboardProps) {\n  const p = PALETTES[theme];\n  const reduce = !!useReducedMotion();\n  const uid = React.useId();\n  const all = React.useMemo(() => players ?? seedPlayers(), [players]);\n  const [period, setPeriod] = React.useState(periods[0]?.id ?? \"week\");\n  const [metric, setMetric] = React.useState(metrics[0]?.id ?? \"points\");\n  const [team, setTeam] = React.useState(\"All\");\n  const teams = [\"All\", ...Array.from(new Set(all.map((x) => x.team)))];\n  const m = metrics.find((x) => x.id === metric) ?? metrics[0];\n  const prev = periods.find((x) => x.id === period)?.previous;\n\n  const rank = (per: string) =>\n    all\n      .filter((x) => team === \"All\" || x.team === team)\n      .map((x) => ({ x, score: x.scores[per]?.[metric] ?? 0 }))\n      .sort((a, b) => b.score - a.score || a.x.name.localeCompare(b.x.name));\n  const ranked = rank(period);\n  const prevRank = prev ? rank(prev).map((r) => r.x.id) : null;\n  const delta = (id: string, now: number) => (prevRank ? prevRank.indexOf(id) + 1 - now : null);\n  const top = ranked[0]?.score || 1;\n  const podium = ranked.slice(0, 3);\n  const rows = ranked.slice(3, 3 + limit);\n  const meIdx = ranked.findIndex((r) => r.x.id === currentUserId);\n  const meOutside = meIdx >= 3 + limit;\n  const fmt = (n: number) => `${n.toLocaleString(\"en-US\")}${m?.unit ? ` ${m.unit}` : \"\"}`;\n  const ring = { [\"--tw-ring-color\" as string]: p.ring, [\"--tw-ring-offset-color\" as string]: p.card } as React.CSSProperties;\n  const focus = \"outline-none focus-visible:ring-2 focus-visible:ring-offset-2\";\n\n  const renderRow = (r: (typeof ranked)[number], i: number) => {\n    const me = r.x.id === currentUserId;\n    return (\n      <motion.li\n        key={r.x.id}\n        layout={!reduce}\n        layoutId={reduce ? undefined : `${uid}-${r.x.id}`}\n        initial={reduce ? false : { opacity: 0 }}\n        animate={{ opacity: 1 }}\n        exit={reduce ? undefined : { opacity: 0 }}\n        transition={{ type: \"spring\", stiffness: 300, damping: 32 }}\n        className=\"relative grid grid-cols-[28px_32px_1fr_auto] items-center gap-3 px-4 py-2.5 @[560px]:grid-cols-[32px_32px_1fr_160px_auto] @[560px]:px-5\"\n        style={{ background: me ? p.me : \"transparent\", boxShadow: me ? `inset 3px 0 0 ${p.meLine}` : undefined }}\n      >\n        <span className=\"text-center text-[14px] font-semibold tabular-nums\" style={{ color: p.muted }}>\n          {i + 1}\n        </span>\n        <Avatar player={r.x} size={32} p={p} />\n        <span className=\"min-w-0\">\n          <span className=\"flex items-center gap-2 truncate text-[14.5px] font-medium\">\n            {r.x.name}\n            {me && (\n              <span className=\"rounded-full px-1.5 py-px text-[10.5px] font-semibold\" style={{ background: p.meLine, color: \"#fff\" }}>\n                You\n              </span>\n            )}\n          </span>\n          <span className=\"block text-[12px]\" style={{ color: p.faint }}>\n            {r.x.team}\n          </span>\n        </span>\n        <span className=\"hidden h-1.5 overflow-hidden rounded-full @[560px]:block\" style={{ background: p.track }} aria-hidden=\"true\">\n          <motion.span className=\"block h-full rounded-full\" style={{ background: me ? p.meLine : p.on }} initial={false} animate={{ width: `${(r.score / top) * 100}%` }} transition={{ duration: reduce ? 0 : 0.5 }} />\n        </span>\n        <span className=\"flex items-center gap-2\">\n          <span className=\"w-[86px] text-right text-[14px] font-semibold tabular-nums\">{fmt(r.score)}</span>\n          <Delta d={delta(r.x.id, i + 1)} p={p} />\n        </span>\n      </motion.li>\n    );\n  };\n\n  return (\n    <section className={cn(\"@container w-full\", className)} style={{ background: p.bg, color: p.text }}>\n      <div className=\"mx-auto max-w-4xl px-4 py-12 @[640px]:px-6 @[640px]:py-16\">\n        <div className=\"overflow-hidden rounded-2xl border\" style={{ background: p.card, borderColor: p.border }}>\n          <div className=\"flex flex-col gap-4 border-b p-5 @[700px]:flex-row @[700px]:items-start @[700px]:justify-between\" style={{ borderColor: p.border }}>\n            <div>\n              <h2 className=\"m-0 text-[22px] font-semibold tracking-[-0.02em]\">{title}</h2>\n              {description && <p className=\"m-0 mt-1 max-w-[420px] text-[13.5px] leading-[1.5]\" style={{ color: p.muted }}>{description}</p>}\n            </div>\n            <div className=\"flex flex-wrap items-center gap-2\">\n              <div role=\"radiogroup\" aria-label=\"Period\" className=\"inline-flex rounded-lg p-0.5\" style={{ background: p.chip }}>\n                {periods.map((x) => (\n                  <button key={x.id} type=\"button\" role=\"radio\" aria-checked={period === x.id} onClick={() => setPeriod(x.id)} className={cn(\"relative h-8 cursor-pointer rounded-md border-0 bg-transparent px-3 text-[13px] font-medium whitespace-nowrap\", focus)} style={{ color: period === x.id ? p.onText : p.muted, ...ring }}>\n                    {period === x.id && <motion.span layoutId={`${uid}-period`} className=\"absolute inset-0 rounded-md\" style={{ background: p.on }} transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 420, damping: 34 }} />}\n                    <span className=\"relative\">{x.label}</span>\n                  </button>\n                ))}\n              </div>\n              {metrics.length > 1 && (\n                <>\n                  <label htmlFor={`${uid}-m`} className=\"sr-only\">\n                    Rank by\n                  </label>\n                  <select id={`${uid}-m`} value={metric} onChange={(e) => setMetric(e.target.value)} className={cn(\"h-9 cursor-pointer rounded-lg border px-2.5 text-[13px]\", focus)} style={{ background: p.card, borderColor: p.border, color: p.text, ...ring }}>\n                    {metrics.map((x) => (\n                      <option key={x.id} value={x.id}>\n                        {x.label}\n                      </option>\n                    ))}\n                  </select>\n                </>\n              )}\n            </div>\n          </div>\n\n          {teams.length > 2 && (\n            <div role=\"group\" aria-label=\"Team\" className=\"flex gap-1 overflow-x-auto border-b px-4 py-2.5 [scrollbar-width:none] @[560px]:px-5 [&::-webkit-scrollbar]:hidden\" style={{ borderColor: p.border }}>\n              {teams.map((t) => (\n                <button key={t} type=\"button\" aria-pressed={team === t} onClick={() => setTeam(t)} className={cn(\"h-8 shrink-0 cursor-pointer rounded-md border px-2.5 text-[13px]\", focus)} style={{ borderColor: team === t ? p.on : \"transparent\", background: team === t ? p.chip : \"transparent\", color: team === t ? p.text : p.muted, ...ring }}>\n                  {t}\n                </button>\n              ))}\n            </div>\n          )}\n\n          <LayoutGroup>\n            {/* Podium: 2nd, 1st, 3rd */}\n            <ol className=\"m-0 grid list-none grid-cols-3 items-end gap-2 px-4 pt-8 pb-5 @[560px]:gap-4 @[560px]:px-8\" aria-label=\"Top three\">\n              {[1, 0, 2].map((pos) => {\n                const r = podium[pos];\n                if (!r) return <li key={pos} />;\n                const me = r.x.id === currentUserId;\n                const h = [128, 96, 76][pos];\n                return (\n                  <motion.li key={r.x.id} layoutId={reduce ? undefined : `${uid}-${r.x.id}`} transition={{ type: \"spring\", stiffness: 300, damping: 32 }} className=\"flex flex-col items-center text-center\" style={{ order: pos === 0 ? 2 : pos === 1 ? 1 : 3 }}>\n                    <span className=\"relative\">\n                      <span className=\"block rounded-full p-[3px]\" style={{ background: MEDALS[pos] }}>\n                        <span className=\"block rounded-full p-[2px]\" style={{ background: p.card }}>\n                          <Avatar player={r.x} size={pos === 0 ? 64 : 52} p={p} />\n                        </span>\n                      </span>\n                      <span className=\"absolute -bottom-1.5 left-1/2 flex h-6 w-6 -translate-x-1/2 items-center justify-center rounded-full text-[12px] font-bold\" style={{ background: MEDALS[pos], color: \"#1A1A1A\" }}>\n                        {pos + 1}\n                      </span>\n                    </span>\n                    <span className=\"mt-3 block max-w-full truncate text-[13.5px] font-medium @[560px]:text-[14.5px]\">\n                      {r.x.name.split(\" \")[0]}\n                      {me ? \" (you)\" : \"\"}\n                    </span>\n                    <span className=\"block text-[12.5px] tabular-nums\" style={{ color: p.muted }}>\n                      {fmt(r.score)}\n                    </span>\n                    <motion.span className=\"mt-3 block w-full rounded-t-xl\" style={{ background: `linear-gradient(180deg, ${MEDALS[pos]}33, transparent)`, borderTop: `2px solid ${MEDALS[pos]}` }} initial={false} animate={{ height: h }} transition={{ type: \"spring\", stiffness: 160, damping: 22 }} aria-hidden=\"true\" />\n                  </motion.li>\n                );\n              })}\n            </ol>\n\n            <ol className=\"m-0 list-none border-t p-0\" style={{ borderColor: p.border }} start={4} aria-label=\"Rankings\">\n              <AnimatePresence initial={false} mode=\"popLayout\">\n                {rows.map((r, i) => renderRow(r, i + 3))}\n              </AnimatePresence>\n            </ol>\n          </LayoutGroup>\n\n          {meOutside && (\n            <div className=\"border-t\" style={{ borderColor: p.border }}>\n              <p className=\"m-0 px-5 pt-2 text-center text-[11px] tracking-[0.2em]\" style={{ color: p.faint }} aria-hidden=\"true\">\n                • • •\n              </p>\n              <ol className=\"m-0 list-none p-0\" start={meIdx + 1} aria-label=\"Your position\">\n                {renderRow(ranked[meIdx], meIdx)}\n              </ol>\n            </div>\n          )}\n          <p className=\"m-0 border-t px-5 py-3 text-[12.5px]\" style={{ borderColor: p.border, color: p.faint }} aria-live=\"polite\">\n            {ranked.length} people · ranked by {m?.label.toLowerCase()}\n            {meIdx >= 0 && ` · you are #${meIdx + 1}`}\n            {prev ? \" · arrows compare with the previous period\" : \"\"}\n          </p>\n        </div>\n      </div>\n    </section>\n  );\n}\n\nexport default Leaderboard;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}