{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "leaderboard",
  "type": "registry:ui",
  "title": "Leaderboard",
  "description": "A group's board for a game: rank, name and score, ties sharing a rank, the viewing player's row marked (and kept even when it falls below the top rows), and the whole board held back until enough people have played that no one's score stands alone. Ranking…",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "games",
    "game"
  ],
  "dependencies": [],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json",
    "https://ds.interlace.tools/r/cn.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/games/leaderboard.tsx",
      "target": "components/ui/games/leaderboard.tsx",
      "type": "registry:ui",
      "content": "import * as React from 'react';\n\n// @interlace/leaderboard v1.0.0 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/leaderboard\n// What changed since: https://ds.interlace.tools/c/leaderboard#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — Leaderboard\n *\n * A group's board for a game: rank, name and score, ties sharing a rank,\n * the viewing player's row marked (and kept even when it falls below the\n * top rows), and the whole board held back until enough people have played\n * that no one's score stands alone. Ranking lives in `leaderboard-model.ts`\n * and is exported, so the server that stores scores ranks them the same way.\n *\n * ## Anatomy\n *\n *   Leaderboard                    (div — data-slot=\"leaderboard\", data-state=visible|hidden)\n *     ├─ table                     (data-slot=\"leaderboard-table\", captioned)\n *     │   └─ tr × n                (data-slot=\"leaderboard-row\", data-current, data-rank)\n *     │       ├─ td                (rank: \"2\" or \"=2\" when tied)\n *     │       ├─ th scope=\"row\"    (name, with a \"You\" tag on the viewer's row)\n *     │       └─ td                (score, formatted)\n *     └─ p                         (data-slot=\"leaderboard-hidden\": \"Shows once 5 people have played. 3 so far.\")\n *\n * A real table, so a screen reader moves by row and column. Rank, name and\n * score are text; the viewer's row is marked by a word and weight, not only\n * a tint.\n *\n * ## Server first\n *\n * No hooks: it renders on the server.\n *\n * ## MIN_VIEWPORT — 320\n *\n * Names truncate before the score column moves.\n *\n * | Rule | Concept                   | Where in this file                         |\n * | ---- | ------------------------- | ------------------------------------------ |\n * | R4   | Extends native el         | `React.ComponentProps<'div'>`              |\n * | R6   | data-slot per part        | leaderboard / -table / -row / -hidden       |\n * | R7   | cn + ...rest              | `cn(…, className)` + `{...props}`          |\n * | R14  | Declares min viewport     | `data-min-viewport` + exported const       |\n * | R19  | Tokens only               | muted, primary, border                     |\n * | R26  | A11y                      | captioned table; row headers; \"You\" in words |\n */\n\nimport { cn } from '@/lib/utils';\nimport { buildBoard, type LeaderboardEntry, type RankOptions, type RankedEntry } from '@/components/ui/games/leaderboard-model';\n\nexport const MIN_VIEWPORT = 320 as const;\n\ninterface LeaderboardLabels {\n  rank?: string;\n  name?: string;\n  score?: string;\n  /** @default 'You' */\n  you?: string;\n  /** @default (min, players) => `The board shows once ${min} people have played. ${players} so far.` */\n  hidden?: (min: number, players: number) => string;\n}\n\ntype LeaderboardProps = Omit<React.ComponentProps<'div'>, 'children'> &\n  RankOptions & {\n    entries: readonly LeaderboardEntry[];\n    /** Names the table. */\n    caption: React.ReactNode;\n    /** Show the caption, or keep it for assistive tech only. @default false */\n    showCaption?: boolean;\n    /** `Intl.NumberFormat` options for scores. */\n    format?: Intl.NumberFormatOptions;\n    locale?: string;\n    labels?: LeaderboardLabels;\n  };\n\nfunction Row({\n  entry,\n  current,\n  score,\n  you,\n}: {\n  entry: RankedEntry;\n  current: boolean;\n  score: string;\n  you: string;\n}) {\n  return (\n    <tr\n      data-slot=\"leaderboard-row\"\n      data-rank={entry.rank}\n      data-current={current ? '' : undefined}\n      className={cn('border-border border-t first:border-t-0', current && 'bg-primary/8')}\n    >\n      <td className={cn('py-2.5 ps-3 pe-2 tabular-nums', entry.rank <= 3 ? 'font-semibold' : 'text-muted-foreground')}>\n        {entry.tied ? `=${entry.rank}` : entry.rank}\n      </td>\n      <th scope=\"row\" className=\"max-w-0 py-2.5 pe-2 text-start font-normal\">\n        <span className=\"flex min-w-0 items-center gap-2\">\n          <span className={cn('truncate', current && 'font-semibold')}>{entry.name}</span>\n          {current ? (\n            <span className=\"border-primary text-primary shrink-0 rounded-full border px-2 text-xs leading-5 font-semibold\">\n              {you}\n            </span>\n          ) : null}\n        </span>\n      </th>\n      <td className={cn('py-2.5 ps-2 pe-3 text-end tabular-nums', current && 'font-semibold')}>{score}</td>\n    </tr>\n  );\n}\n\nfunction Leaderboard({\n  entries,\n  caption,\n  showCaption = false,\n  order,\n  minPlayers,\n  limit,\n  currentId,\n  format,\n  locale,\n  labels,\n  className,\n  ...props\n}: LeaderboardProps) {\n  const board = buildBoard(entries, { order, minPlayers, limit, currentId });\n  const nf = new Intl.NumberFormat(locale, format);\n  const you = labels?.you ?? 'You';\n\n  if (!board.visible) {\n    const min = Math.max(1, minPlayers ?? 1);\n    return (\n      <div\n        data-slot=\"leaderboard\"\n        data-state=\"hidden\"\n        data-min-viewport={String(MIN_VIEWPORT)}\n        className={cn('border-border text-muted-foreground rounded-xl border border-dashed p-5 text-sm', className)}\n        {...props}\n      >\n        {showCaption ? <p className=\"text-foreground mb-1 font-semibold\">{caption}</p> : null}\n        <p data-slot=\"leaderboard-hidden\">\n          {(labels?.hidden ?? ((m: number, p: number) => `The board shows once ${m} people have played. ${p} so far.`))(\n            min,\n            board.players,\n          )}\n        </p>\n      </div>\n    );\n  }\n\n  return (\n    <div\n      data-slot=\"leaderboard\"\n      data-state=\"visible\"\n      data-min-viewport={String(MIN_VIEWPORT)}\n      className={cn('border-border bg-card text-card-foreground overflow-hidden rounded-xl border', className)}\n      {...props}\n    >\n      <table data-slot=\"leaderboard-table\" className=\"w-full table-fixed border-collapse text-sm\">\n        <caption\n          className={cn(showCaption ? 'border-border border-b px-3 py-2.5 text-start font-semibold' : 'sr-only')}\n        >\n          {caption}\n        </caption>\n        <colgroup>\n          <col className=\"w-14\" />\n          <col />\n          <col className=\"w-24\" />\n        </colgroup>\n        <thead className=\"sr-only\">\n          <tr>\n            <th scope=\"col\">{labels?.rank ?? 'Rank'}</th>\n            <th scope=\"col\">{labels?.name ?? 'Name'}</th>\n            <th scope=\"col\">{labels?.score ?? 'Score'}</th>\n          </tr>\n        </thead>\n        <tbody>\n          {board.top.map((e) => (\n            <Row key={e.id} entry={e} current={e.id === currentId} score={nf.format(e.score)} you={you} />\n          ))}\n        </tbody>\n        {board.current ? (\n          <tbody className=\"border-border border-t border-dashed\">\n            <Row entry={board.current} current score={nf.format(board.current.score)} you={you} />\n          </tbody>\n        ) : null}\n      </table>\n    </div>\n  );\n}\nLeaderboard.displayName = 'Leaderboard';\n\nexport { Leaderboard };\nexport type { LeaderboardProps, LeaderboardLabels };\n"
    },
    {
      "path": "registry/interlace-ui/games/leaderboard-model.ts",
      "target": "components/ui/games/leaderboard-model.ts",
      "type": "registry:ui",
      "content": "export interface LeaderboardEntry {\n  /** Stable id: a player or a group. */\n  id: string;\n  /** What the board shows. */\n  name: string;\n  score: number;\n  /** Breaks ties on score: lower first (time taken, say). Equal or missing values share a rank. */\n  tiebreak?: number;\n}\n\nexport interface RankedEntry extends LeaderboardEntry {\n  /** 1-based; ties share a rank and the next rank skips (1, 2, 2, 4). */\n  rank: number;\n  /** True when another entry has the same rank. */\n  tied: boolean;\n}\n\nexport interface RankOptions {\n  /** `desc` when a higher score wins. @default 'desc' */\n  order?: 'desc' | 'asc';\n  /** Below this many entries the board is hidden. @default 1 */\n  minPlayers?: number;\n  /** Rows to show from the top. @default all */\n  limit?: number;\n  /** The viewing player's id: their row is kept even outside `limit`. */\n  currentId?: string;\n}\n\nexport interface RankedBoard {\n  /** False while fewer than `minPlayers` have played. */\n  visible: boolean;\n  /** Entries counted. */\n  players: number;\n  /** The top rows, within `limit`. Empty while hidden. */\n  top: RankedEntry[];\n  /** The viewing player's row when it falls outside `top`. */\n  current?: RankedEntry;\n}\n\n/** Rank every entry. */\nexport function rankEntries(entries: readonly LeaderboardEntry[], order: 'desc' | 'asc' = 'desc'): RankedEntry[] {\n  const sign = order === 'desc' ? -1 : 1;\n  // An id counts once: a repeated message must not add a row, and the latest report wins.\n  const sorted = [...new Map(entries.map((e) => [e.id, e])).values()]\n    .sort(\n      (a, b) =>\n        sign * (a.score - b.score) || (a.tiebreak ?? Infinity) - (b.tiebreak ?? Infinity) || a.name.localeCompare(b.name),\n    );\n  const same = (a: LeaderboardEntry, b: LeaderboardEntry) => a.score === b.score && (a.tiebreak ?? null) === (b.tiebreak ?? null);\n  const ranked: RankedEntry[] = [];\n  sorted.forEach((entry, i) => {\n    const prev = sorted[i - 1];\n    const rank = prev && same(prev, entry) ? (ranked[i - 1] as RankedEntry).rank : i + 1;\n    ranked.push({ ...entry, rank, tied: false });\n  });\n  for (let i = 0; i < ranked.length; i++) {\n    const r = ranked[i] as RankedEntry;\n    r.tied = ranked[i - 1]?.rank === r.rank || ranked[i + 1]?.rank === r.rank;\n  }\n  return ranked;\n}\n\n/** The board as a page shows it: hidden below the minimum, top rows, and the viewer's own row. */\nexport function buildBoard(entries: readonly LeaderboardEntry[], options: RankOptions = {}): RankedBoard {\n  const { order = 'desc', minPlayers = 1, limit, currentId } = options;\n  const ranked = rankEntries(entries, order);\n  const players = ranked.length;\n  if (players < Math.max(1, minPlayers)) return { visible: false, players, top: [] };\n  const top = limit === undefined ? ranked : ranked.slice(0, limit);\n  const mine = currentId === undefined ? undefined : ranked.find((e) => e.id === currentId);\n  const current = mine && !top.includes(mine) ? mine : undefined;\n  return { visible: true, players, top, current };\n}\n\n/**\n * @interlace/ui — games/leaderboard-model\n *\n * Ranking for a group's board: ties share a rank (1, 2, 2, 4), the board\n * stays hidden until enough people have played, and the player's own row is\n * found even when it falls outside the top rows. Pure, so the server that\n * stores scores ranks them the same way.\n *\n * ## Why a minimum\n *\n * A board of two people tells each of them exactly what the other scored.\n * `minPlayers` keeps a small group's scores private until there are enough\n * of them to stand in a crowd.\n */\n"
    }
  ],
  "meta": {
    "tier": "game",
    "client": false,
    "minViewport": 320,
    "loading": false,
    "version": "1.0.0",
    "since": "2.0.0"
  },
  "docs": "## @interlace/leaderboard\n\nInstalled to `components/ui/games/leaderboard.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/games/leaderboard';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/leaderboard\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
