{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "answer-pad",
  "type": "registry:ui",
  "title": "Answer Pad",
  "description": "The player's side of a live round: the question is on the big screen, and each phone shows big lettered tiles to pick from. The first pick is final and goes to the host; the pad then says the answer is in and waits. When the host reveals the round, the pad…",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "games",
    "game"
  ],
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json",
    "https://ds.interlace.tools/r/cn.json",
    "https://ds.interlace.tools/r/motion-token.json",
    "https://ds.interlace.tools/r/use-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/games/answer-pad.tsx",
      "target": "components/ui/games/answer-pad.tsx",
      "type": "registry:ui",
      "content": "'use client';\n\nimport * as React from 'react';\n\n// @interlace/answer-pad v1.0.1 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/answer-pad\n// What changed since: https://ds.interlace.tools/c/answer-pad#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — AnswerPad\n *\n * The player's side of a live round: the question is on the big screen, and\n * each phone shows big lettered tiles to pick from. The first pick is final\n * and goes to the host; the pad then says the answer is in and waits. When\n * the host reveals the round, the pad marks the right tile and says in words\n * how this player did. The pad never needs the right answer before the\n * reveal, so a competitive room can keep it off the phones.\n *\n * Letters (A, B, C…) tie a tile to the same choice on the big screen, so a\n * room can show only letters on phones (`display=\"keys\"`) and the words on\n * the shared screen. `choiceKey(i)` gives the host screen the same letters.\n *\n * ## Anatomy\n *\n *   AnswerPad                     (div — data-slot=\"answer-pad\", data-state)\n *     ├─ p?                       (data-slot=\"answer-pad-prompt\", when `prompt` is given)\n *     ├─ div role=\"group\"         (data-slot=\"answer-pad-choices\")\n *     │   └─ button × n           (data-slot=\"answer-pad-choice\", data-key, data-mark, aria-pressed)\n *     │       ├─ span             (data-slot=\"answer-pad-key\": A, B… or a check / cross)\n *     │       └─ span             (data-slot=\"answer-pad-label\", unless display=\"keys\")\n *     └─ p aria-live              (data-slot=\"answer-pad-status\": \"Answer in\", then the result)\n *\n * ## States\n *\n * `open`: tiles take a pick. `sent`: a pick is in, tiles are locked. `closed`:\n * time ran out with no pick. `revealed`: `correct` is known; the right tile is\n * marked, a wrong pick too, and the status gives the outcome and `points`.\n * The state follows from the props (`value`, `closed`, `correct`), so the pad\n * shows the same thing after a reload or a reconnect.\n *\n * ## Keys\n *\n * While open, the keys A to Z and 1 to 9 pick the matching tile from anywhere\n * inside the pad; each tile declares its keys in `aria-keyshortcuts`.\n *\n * ## Motion\n *\n * A tile presses in slightly when tapped, and the picked tile settles with a\n * small lift (transform only, 200ms). At the reveal the right tile lands\n * (a 1.04 scale, 420ms, eased out), the tiles nobody needs fade back, a wrong\n * pick gives one small sideways nudge, and on a right pick the points rise\n * from the tile (`data-slot=\"answer-pad-points\"`, aria-hidden: the status\n * says them). Everything is transform and opacity, run by the compositor.\n * Under reduced motion nothing moves: the marks, colours and words are the\n * same.\n *\n * ## MIN_VIEWPORT — 320\n *\n * | Rule | Concept                   | Where in this file                                     |\n * | ---- | ------------------------- | ------------------------------------------------------ |\n * | R4   | Extends native el         | `React.ComponentProps<'div'>`                          |\n * | R6   | data-slot per part        | answer-pad / -prompt / -choices / -choice / -key / -label / -status |\n * | R7   | cn + ...rest              | `cn(…, className)` + `{...props}`                      |\n * | R8   | Enums, not booleans       | `display`                                              |\n * | R9   | Controlled                | `value` / `onPick`                                     |\n * | R14  | Declares min viewport     | `data-min-viewport` + exported const                   |\n * | R19  | Tokens only               | success, destructive, primary, border, muted           |\n * | R26  | A11y                      | labelled group; aria-pressed; keys declared; outcome in words |\n */\n\nimport { Check, X } from 'lucide-react';\n\nimport { cn } from '@/lib/utils';\nimport { useReducedMotion } from '@/hooks/use-reduced-motion';\nimport { motionEase } from '@/lib/motion-token';\n\nexport const MIN_VIEWPORT = 320 as const;\n\nconst LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';\n\n/** The letter for the choice at `index` (0 → \"A\"), shared by the pad and the big screen. */\nexport function choiceKey(index: number): string {\n  return LETTERS[index] ?? String(index + 1);\n}\n\nexport type AnswerPadState = 'open' | 'sent' | 'closed' | 'revealed';\nexport type AnswerPadOutcome = 'correct' | 'wrong' | 'missed';\n\ninterface AnswerPadChoice {\n  id: string;\n  label: React.ReactNode;\n  /** Plain text for the result sentence, when `label` is not a string. */\n  text?: string;\n}\n\ninterface AnswerPadLabels {\n  /** Names the group of tiles. @default 'Your answer' */\n  group?: string;\n  /** While a pick waits for the reveal. @default 'Answer in. Watch the screen.' */\n  sent?: string;\n  /** Time ran out with no pick, before the reveal. @default \"Time's up.\" */\n  closed?: string;\n  /** The result sentence after the reveal. */\n  result?: (r: { outcome: AnswerPadOutcome; points: number | null; answer: string }) => string;\n  /** Read after the right tile. @default 'right answer' */\n  correct?: string;\n  /** Read after this player's tile. @default 'your answer' */\n  mine?: string;\n}\n\ntype AnswerPadProps = Omit<React.ComponentProps<'div'>, 'children' | 'onChange'> & {\n  choices: readonly AnswerPadChoice[];\n  /** The id this player picked, or null for none yet. */\n  value: string | null;\n  /** Called once, with the picked id, while the pad is open. */\n  onPick?: (id: string) => void;\n  /** The right choice's id, once the round is revealed. Leave null until then. */\n  correct?: string | null;\n  /** No more picks (time ran out or the host closed the round). */\n  closed?: boolean;\n  /** Points this player won this round, shown after the reveal. */\n  points?: number | null;\n  /** The question, when the pad shows it too. On a shared screen, leave it out. */\n  prompt?: React.ReactNode;\n  /** `full`: letter and words. `keys`: letters only, with the words on the big screen. @default 'full' */\n  display?: 'full' | 'keys';\n  locale?: string;\n  labels?: AnswerPadLabels;\n};\n\nconst result =\n  (locale?: string): NonNullable<AnswerPadLabels['result']> =>\n  ({ outcome, points, answer }) =>\n    outcome === 'correct'\n      ? points\n        ? `Right. +${points.toLocaleString(locale)} points.`\n        : 'Right.'\n      : outcome === 'missed'\n        ? `No answer this time. It was ${answer}.`\n        : `Not this time. It was ${answer}.`;\n\nconst textOf = (c: AnswerPadChoice) =>\n  c.text ?? (typeof c.label === 'string' || typeof c.label === 'number' ? String(c.label) : c.id);\n\nfunction AnswerPad({\n  choices,\n  value,\n  onPick,\n  correct = null,\n  closed = false,\n  points = null,\n  prompt,\n  display = 'full',\n  locale,\n  labels,\n  className,\n  onKeyDown,\n  ...props\n}: AnswerPadProps) {\n  const revealed = correct !== null && correct !== undefined;\n  const state: AnswerPadState = revealed ? 'revealed' : value !== null ? 'sent' : closed ? 'closed' : 'open';\n  const open = state === 'open';\n  const promptId = React.useId();\n  const reduced = useReducedMotion();\n  const tiles = React.useRef(new Map<string, HTMLButtonElement>());\n  const sent = React.useRef(false);\n  React.useEffect(() => {\n    if (value === null) sent.current = false;\n  }, [value]);\n\n  const pick = (id: string) => {\n    if (!open || sent.current) return;\n    sent.current = true;\n    onPick?.(id);\n  };\n\n  const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {\n    onKeyDown?.(e);\n    if (e.defaultPrevented || !open || e.altKey || e.ctrlKey || e.metaKey) return;\n    const k = e.key.toUpperCase();\n    let index = LETTERS.indexOf(k);\n    if (k.length !== 1) index = -1;\n    if (index < 0 && /^[1-9]$/.test(k)) index = Number(k) - 1;\n    const choice = index >= 0 ? choices[index] : undefined;\n    if (!choice) return;\n    e.preventDefault();\n    pick(choice.id);\n  };\n\n  const outcome: AnswerPadOutcome | null = revealed\n    ? value === null\n      ? 'missed'\n      : value === correct\n        ? 'correct'\n        : 'wrong'\n    : null;\n  // The reveal plays when `correct` arrives; a phone that reconnects into the reveal sees it too.\n  React.useEffect(() => {\n    if (!revealed || reduced) return;\n    const hit = correct ? tiles.current.get(correct) : undefined;\n    if (hit && typeof hit.animate === 'function')\n      hit.animate([{ transform: 'scale(1)' }, { transform: 'scale(1.04)', offset: 0.45 }, { transform: 'scale(1)' }], {\n        duration: 420,\n        easing: motionEase(hit, 'enter'),\n      });\n    const chip = hit?.querySelector<HTMLElement>('[data-slot=\"answer-pad-points\"]');\n    if (chip && typeof chip.animate === 'function')\n      chip.animate(\n        [\n          { transform: 'translateY(10px) scale(0.5)' },\n          { transform: 'translateY(-4px) scale(1.08)', offset: 0.6 },\n          { transform: 'translateY(0) scale(1)' },\n        ],\n        { duration: 560, delay: 160, easing: motionEase(chip, 'enter'), fill: 'backwards' },\n      );\n    const miss = value !== null && value !== correct ? tiles.current.get(value) : undefined;\n    if (miss && typeof miss.animate === 'function')\n      miss.animate(\n        [\n          { transform: 'translateX(0)' },\n          { transform: 'translateX(-4px)', offset: 0.3 },\n          { transform: 'translateX(3px)', offset: 0.65 },\n          { transform: 'translateX(0)' },\n        ],\n        { duration: 320, easing: 'ease-out' },\n      );\n  }, [revealed, correct, value, reduced]);\n\n  const right = choices.find((c) => c.id === correct);\n  const status =\n    state === 'revealed'\n      ? (labels?.result ?? result(locale))({\n          outcome: outcome!,\n          points,\n          answer: right ? textOf(right) : String(correct),\n        })\n      : state === 'sent'\n        ? (labels?.sent ?? 'Answer in. Watch the screen.')\n        : state === 'closed'\n          ? (labels?.closed ?? \"Time's up.\")\n          : '';\n\n  return (\n    <div\n      data-slot=\"answer-pad\"\n      data-state={state}\n      data-outcome={outcome ?? undefined}\n      data-display={display}\n      data-min-viewport={String(MIN_VIEWPORT)}\n      className={cn('@container grid gap-4', className)}\n      onKeyDown={handleKeyDown}\n      {...props}\n    >\n      {prompt ? (\n        <p id={promptId} data-slot=\"answer-pad-prompt\" className=\"font-heading text-lg font-semibold text-balance\">\n          {prompt}\n        </p>\n      ) : null}\n      <div\n        role=\"group\"\n        aria-labelledby={prompt ? promptId : undefined}\n        aria-label={prompt ? undefined : (labels?.group ?? 'Your answer')}\n        data-slot=\"answer-pad-choices\"\n        className={cn('grid gap-3', display === 'keys' ? 'grid-cols-2' : 'grid-cols-1 @md:grid-cols-2')}\n      >\n        {choices.map((c, i) => {\n          const key = choiceKey(i);\n          const mine = value === c.id;\n          const mark = revealed\n            ? c.id === correct\n              ? 'correct'\n              : mine\n                ? 'wrong'\n                : undefined\n            : mine\n              ? 'picked'\n              : undefined;\n          const shortcuts = i < 9 ? `${key} ${i + 1}` : key;\n          return (\n            <button\n              key={c.id}\n              ref={(el) => {\n                if (el) tiles.current.set(c.id, el);\n                else tiles.current.delete(c.id);\n              }}\n              type=\"button\"\n              data-slot=\"answer-pad-choice\"\n              data-key={key}\n              data-mark={mark}\n              aria-pressed={mine}\n              aria-keyshortcuts={shortcuts}\n              aria-disabled={!open || undefined}\n              onClick={() => pick(c.id)}\n              className={cn(\n                'relative flex items-center gap-3 rounded-lg border-2 px-4 py-3 text-start text-base font-medium',\n                display === 'keys' ? 'min-h-24 justify-center' : 'min-h-16',\n                'bg-card text-card-foreground border-border',\n                'focus-visible:outline-ring outline-offset-2 focus-visible:outline-2',\n                'motion-safe:transition-[transform,background-color,border-color,opacity] motion-safe:duration-(--duration-fast)',\n                open && 'hover:bg-accent hover:border-primary cursor-pointer motion-safe:active:scale-[0.97]',\n                !open && 'cursor-default',\n                mark === 'picked' && 'border-primary bg-primary/10 motion-safe:-translate-y-0.5',\n                mark === 'correct' && 'border-success bg-success/10',\n                mark === 'wrong' && 'border-destructive bg-destructive/10',\n                // At the reveal, the tiles that are neither right nor yours step back.\n                revealed && !mark && 'opacity-70',\n              )}\n            >\n              <span\n                aria-hidden\n                data-slot=\"answer-pad-key\"\n                className={cn(\n                  'grid shrink-0 place-items-center rounded-md font-semibold',\n                  display === 'keys' ? 'size-14 text-2xl' : 'size-9 text-base',\n                  mark === 'correct'\n                    ? 'bg-success text-success-foreground'\n                    : mark === 'wrong'\n                      ? 'bg-destructive text-destructive-foreground'\n                      : mark === 'picked'\n                        ? 'bg-primary text-primary-foreground'\n                        : 'bg-muted text-foreground',\n                )}\n              >\n                {mark === 'correct' ? (\n                  <Check className=\"size-5\" strokeWidth={3} />\n                ) : mark === 'wrong' ? (\n                  <X className=\"size-5\" strokeWidth={3} />\n                ) : (\n                  key\n                )}\n              </span>\n              {display === 'keys' ? (\n                <span className=\"sr-only\">\n                  {key}: {textOf(c)}\n                </span>\n              ) : (\n                <span data-slot=\"answer-pad-label\" className=\"min-w-0 flex-1\">\n                  <span className=\"sr-only\">{key}: </span>\n                  {c.label}\n                </span>\n              )}\n              {mark === 'correct' && mine && points ? (\n                <span\n                  aria-hidden\n                  data-slot=\"answer-pad-points\"\n                  className=\"bg-success text-success-foreground pointer-events-none absolute -top-3 end-3 rounded-full px-2.5 py-0.5 text-sm font-bold tabular-nums shadow-sm\"\n                >\n                  +{points.toLocaleString(locale)}\n                </span>\n              ) : null}\n              {mark === 'correct' || mark === 'wrong' ? (\n                <span className=\"sr-only\">\n                  , {mark === 'correct' ? (labels?.correct ?? 'right answer') : (labels?.mine ?? 'your answer')}\n                </span>\n              ) : null}\n            </button>\n          );\n        })}\n      </div>\n      <p\n        data-slot=\"answer-pad-status\"\n        aria-live=\"polite\"\n        className={cn('min-h-6 text-base font-semibold', outcome === 'correct' ? 'text-success' : 'text-foreground')}\n      >\n        {status}\n      </p>\n    </div>\n  );\n}\n\nexport { AnswerPad };\nexport type { AnswerPadProps, AnswerPadLabels, AnswerPadChoice };\n"
    }
  ],
  "meta": {
    "tier": "game",
    "client": true,
    "minViewport": 320,
    "loading": false,
    "version": "1.0.1",
    "since": "2.0.0"
  },
  "docs": "## @interlace/answer-pad\n\nInstalled to `components/ui/games/answer-pad.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/games/answer-pad';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/answer-pad\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
