{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "guess-reveal",
  "type": "registry:ui",
  "title": "Guess Reveal",
  "description": "A one-question quiz: a question, two to four answers as buttons, and an explanation that opens once the reader picks. The pick is marked right or wrong with a short movement, the right answer is always shown, and the consumer hears about it through…",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "marketing",
    "pattern"
  ],
  "dependencies": [],
  "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/patterns/guess-reveal.tsx",
      "target": "components/ui/patterns/guess-reveal.tsx",
      "type": "registry:ui",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\n// @interlace/guess-reveal v1.2.2 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/guess-reveal\n// What changed since: https://ds.interlace.tools/c/guess-reveal#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — GuessReveal\n *\n * A one-question quiz: a question, two to four answers as buttons, and an\n * explanation that opens once the reader picks. The pick is marked right or\n * wrong with a short movement, the right answer is always shown, and the\n * consumer hears about it through `onAnswer` to record it if it wants to.\n * It turns a fact into a guess first, which is what makes it stick.\n *\n * ## Anatomy\n *\n *   GuessReveal                      (div — data-slot=\"guess-reveal\", data-state=open|answered)\n *     ├─ p                           (data-slot=\"guess-reveal-question\", id ← group label)\n *     ├─ div role=\"group\"            (data-slot=\"guess-reveal-options\")\n *     │   └─ button × 2–4            (data-slot=\"guess-reveal-option\", data-result=right|wrong|none)\n *     ├─ p aria-live=\"polite\"        (data-slot=\"guess-reveal-status\": \"Right.\" / \"Not quite.\")\n *     ├─ div                         (data-slot=\"guess-reveal-explanation\" — opens on answer)\n *     └─ details                     (data-slot=\"guess-reveal-fallback\" — only without JavaScript)\n *\n * ## After the pick\n *\n * Every option stays focusable but `aria-disabled`, so focus does not fall\n * to the page when the buttons stop taking answers. The pick is marked with\n * a word and an icon, never colour alone. The status line is a polite live\n * region, so a screen reader hears the result without moving.\n *\n * ## Without JavaScript\n *\n * A `<details>` under the options (shown only under `noscript:`) holds the\n * right answer and the explanation, so the fact is never locked behind a\n * script.\n *\n * ## Reduced motion\n *\n * The right pick lifts and a wrong one shakes, through the Web Animations\n * API. Under `prefers-reduced-motion` neither moves and the explanation\n * opens without a transition.\n *\n * ## MIN_VIEWPORT — 320\n *\n * Options stack below 30rem and sit in a row above it.\n *\n * | Rule | Concept                   | Where in this file                                       |\n * | ---- | ------------------------- | -------------------------------------------------------- |\n * | R4   | Extends native el         | `React.ComponentProps<'div'>`                            |\n * | R6   | data-slot per part        | guess-reveal / -question / -options / -option / -status / -explanation / -fallback |\n * | R7   | cn + ...rest              | `cn(…, className)` + `{...props}`                        |\n * | R9   | Controlled + uncontrolled | `answer` / `defaultAnswer` / `onAnswer`                  |\n * | R14  | Declares min viewport     | `data-min-viewport` + exported const                     |\n * | R19  | Tokens only               | `success`, `destructive`, `border`, `card`               |\n * | R26  | A11y                      | group labelled by the question; live status; aria-disabled after the pick |\n */\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\ninterface GuessRevealOption {\n  /** Stable id, passed back through `onAnswer`. */\n  id: string;\n  label: React.ReactNode;\n  /** Exactly one option should be correct. */\n  correct?: boolean;\n}\n\ntype GuessRevealProps = Omit<React.ComponentProps<\"div\">, \"children\"> & {\n  question: React.ReactNode;\n  /** Two to four answers. */\n  options: GuessRevealOption[];\n  /** Shown once the reader picks: why the right answer is right, with its source. */\n  explanation?: React.ReactNode;\n  /** Controlled: the id of the picked option, or null for none yet. */\n  answer?: string | null;\n  /** Uncontrolled initial pick. */\n  defaultAnswer?: string | null;\n  /** Called once, with the picked option and whether it was right. */\n  onAnswer?: (id: string, correct: boolean) => void;\n  /** Status words. @default { right: 'Right.', wrong: 'Not quite.' } */\n  labels?: { right?: string; wrong?: string; reveal?: string };\n};\n\nconst OPTION =\n  \"border-border bg-card text-card-foreground focus-visible:outline-ring relative flex min-h-11 w-full items-center justify-between gap-3 rounded-lg border px-4 py-3 text-start text-sm font-medium transition-colors focus-visible:outline-2 focus-visible:outline-offset-2\";\n\nfunction GuessReveal({\n  question,\n  options,\n  explanation,\n  answer: answerProp,\n  defaultAnswer = null,\n  onAnswer,\n  labels,\n  className,\n  ...props\n}: GuessRevealProps) {\n  const [uncontrolled, setUncontrolled] = React.useState<string | null>(\n    defaultAnswer,\n  );\n  const answer = answerProp === undefined ? uncontrolled : answerProp;\n  const reduced = useReducedMotion();\n  const questionId = React.useId();\n  const explanationId = React.useId();\n  const refs = React.useRef(new Map<string, HTMLButtonElement>());\n  const animateOn = React.useRef<string | null>(null);\n\n  const right = options.find((o) => o.correct);\n  const picked = options.find((o) => o.id === answer);\n  const answered = picked !== undefined;\n  const pickedRight = picked?.correct === true;\n  const words = {\n    right: \"Right.\",\n    wrong: \"Not quite.\",\n    reveal: \"Show the answer\",\n    ...labels,\n  };\n\n  // Closes the door on the first pick at once. `answered` only flips when the\n  // answer renders, and a controlled parent that sets it after an await\n  // would otherwise let a second click through and call onAnswer twice.\n  const pickedRef = React.useRef(answered);\n  React.useEffect(() => {\n    // A parent that clears the answer (a retry) opens the door again.\n    if (!answered) pickedRef.current = false;\n  }, [answered]);\n\n  const pick = (option: GuessRevealOption) => {\n    if (answered || pickedRef.current) return;\n    pickedRef.current = true;\n    animateOn.current = option.id;\n    if (answerProp === undefined) setUncontrolled(option.id);\n    onAnswer?.(option.id, option.correct === true);\n  };\n\n  // The pick moves once: a lift when right, a shake when wrong.\n  React.useEffect(() => {\n    const id = animateOn.current;\n    if (!id || id !== answer) return;\n    animateOn.current = null;\n    const el = refs.current.get(id);\n    if (!el || reduced || typeof el.animate !== \"function\") return;\n    const keyframes = pickedRight\n      ? [\n          { transform: \"scale(1)\" },\n          { transform: \"scale(1.04)\" },\n          { transform: \"scale(1)\" },\n        ]\n      : [\n          { transform: \"translateX(0)\" },\n          { transform: \"translateX(-6px)\" },\n          { transform: \"translateX(6px)\" },\n          { transform: \"translateX(-4px)\" },\n          { transform: \"translateX(0)\" },\n        ];\n    el.animate(keyframes, {\n      duration: pickedRight ? 320 : 360,\n      easing: motionEase(el, 'enter'),\n    });\n  }, [answer, pickedRight, reduced]);\n\n  return (\n    <div\n      data-slot=\"guess-reveal\"\n      data-min-viewport={String(MIN_VIEWPORT)}\n      data-state={answered ? \"answered\" : \"open\"}\n      className={cn(\"grid gap-4\", className)}\n      {...props}\n    >\n      <p\n        id={questionId}\n        data-slot=\"guess-reveal-question\"\n        className=\"font-heading text-lg font-semibold text-balance\"\n      >\n        {question}\n      </p>\n      <div\n        role=\"group\"\n        aria-labelledby={questionId}\n        data-slot=\"guess-reveal-options\"\n        className=\"grid grid-cols-[repeat(auto-fit,minmax(min(100%,max(13rem,calc(50%_-_0.25rem))),1fr))] gap-2\"\n      >\n        {options.map((option) => {\n          const result = !answered\n            ? \"none\"\n            : option.correct\n              ? \"right\"\n              : option.id === answer\n                ? \"wrong\"\n                : \"none\";\n          return (\n            <button\n              key={option.id}\n              ref={(el) => {\n                if (el) refs.current.set(option.id, el);\n                else refs.current.delete(option.id);\n              }}\n              type=\"button\"\n              data-slot=\"guess-reveal-option\"\n              data-result={result}\n              data-picked={option.id === answer ? \"\" : undefined}\n              aria-disabled={answered || undefined}\n              aria-describedby={\n                answered && result !== \"none\" && explanation\n                  ? explanationId\n                  : undefined\n              }\n              onClick={() => pick(option)}\n              className={cn(\n                OPTION,\n                !answered && \"hover:bg-muted cursor-pointer\",\n                answered && \"cursor-default\",\n                answered && result === \"none\" && \"text-muted-foreground\",\n                result === \"right\" &&\n                  \"border-success bg-[color-mix(in_oklab,var(--success)_12%,var(--card))] text-foreground\",\n                result === \"wrong\" &&\n                  \"border-destructive bg-[color-mix(in_oklab,var(--destructive)_10%,var(--card))] text-foreground\",\n              )}\n            >\n              <span>{option.label}</span>\n              {result === \"right\" ? (\n                <span className=\"text-success inline-flex items-center gap-1 text-xs font-semibold\">\n                  <span aria-hidden>✓</span>\n                  {option.id === answer ? \"Your answer\" : \"The answer\"}\n                </span>\n              ) : result === \"wrong\" ? (\n                <span className=\"text-destructive inline-flex items-center gap-1 text-xs font-semibold\">\n                  <span aria-hidden>✗</span>\n                  Your answer\n                </span>\n              ) : null}\n            </button>\n          );\n        })}\n      </div>\n      <p\n        data-slot=\"guess-reveal-status\"\n        aria-live=\"polite\"\n        className=\"text-sm font-semibold empty:hidden\"\n      >\n        {answered ? (pickedRight ? words.right : words.wrong) : \"\"}\n      </p>\n      {explanation ? (\n        <div\n          data-slot=\"guess-reveal-explanation\"\n          className={cn(\n            \"grid\",\n            \"motion-safe:transition-[grid-template-rows,opacity] motion-safe:duration-(--duration-medium) motion-safe:ease-enter\",\n            answered\n              ? \"grid-rows-[1fr] opacity-100\"\n              : \"grid-rows-[0fr] opacity-0\",\n          )}\n          // Closed, its links are out of the tab order and the accessibility tree.\n          inert={answered ? undefined : true}\n        >\n          <div\n            id={explanationId}\n            className=\"text-muted-foreground -m-1 min-h-0 overflow-hidden p-1 text-sm leading-relaxed\"\n          >\n            {explanation}\n          </div>\n        </div>\n      ) : null}\n      <details\n        data-slot=\"guess-reveal-fallback\"\n        className=\"hidden text-sm noscript:block\"\n      >\n        <summary className=\"text-primary cursor-pointer font-semibold\">\n          {words.reveal}\n        </summary>\n        <div className=\"mt-2 grid gap-2\">\n          {right ? <p className=\"font-semibold\">{right.label}</p> : null}\n          {explanation ? (\n            <div className=\"text-muted-foreground\">{explanation}</div>\n          ) : null}\n        </div>\n      </details>\n    </div>\n  );\n}\nGuessReveal.displayName = \"GuessReveal\";\n\nexport { GuessReveal };\nexport type { GuessRevealProps, GuessRevealOption };\n"
    }
  ],
  "meta": {
    "tier": "pattern",
    "client": true,
    "minViewport": 320,
    "loading": false,
    "version": "1.2.2",
    "since": "2.0.0"
  },
  "docs": "## @interlace/guess-reveal\n\nInstalled to `components/ui/patterns/guess-reveal.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/patterns/guess-reveal';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/guess-reveal\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
