{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "flip-card",
  "type": "registry:ui",
  "title": "Flip Card",
  "description": "A card with two faces: a front that asks (a claim, a question) and a back that answers (with any children, such as source links). Pressing the card turns it over in 3D. It makes the reader commit to a guess before the answer arrives, which is what turns a…",
  "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"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/patterns/flip-card.tsx",
      "target": "components/ui/patterns/flip-card.tsx",
      "type": "registry:ui",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\n// @interlace/flip-card v1.2.0 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/flip-card\n// What changed since: https://ds.interlace.tools/c/flip-card#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — FlipCard\n *\n * A card with two faces: a front that asks (a claim, a question) and a back\n * that answers (with any children, such as source links). Pressing the card\n * turns it over in 3D. It makes the reader commit to a guess before the\n * answer arrives, which is what turns a fact into a moment.\n *\n * ## Anatomy\n *\n *   FlipCard                         (div — data-slot=\"flip-card\", data-flipped)\n *     └─ div                         (data-slot=\"flip-card-inner\" — the turning plate, a 1×1 grid)\n *         ├─ div                     (data-slot=\"flip-card-front\")\n *         │   ├─ {front}\n *         │   └─ button              (aria-expanded, aria-controls → back; stretched over the face)\n *         └─ div#back                (data-slot=\"flip-card-back\")\n *             ├─ {back}\n *             └─ button              (turns it back)\n *\n * ## Equal height\n *\n * Both faces sit in the same grid cell, so the card is as tall as its taller\n * face and never jumps when it turns. In a grid of cards, give the grid\n * `items-stretch` (the default) and every card in a row shares a height.\n *\n * ## Both faces stay in the DOM\n *\n * Nothing is mounted on demand. The face that is turned away is `inert`, so\n * its links are not tabbed to while hidden, and focus moves to the other\n * face's button when the card turns. Without JavaScript (`noscript:`), the\n * card does not turn at all: both faces show, one above the other, and the\n * buttons are hidden.\n *\n * ## Reduced motion\n *\n * Under `prefers-reduced-motion` there is no rotation: the faces cross-fade\n * in place.\n *\n * ## MIN_VIEWPORT — 320\n *\n * The faces are fluid; text wraps.\n *\n * | Rule | Concept                 | Where in this file                                         |\n * | ---- | ----------------------- | ---------------------------------------------------------- |\n * | R4   | Extends native el       | `React.ComponentProps<'div'>`                              |\n * | R6   | data-slot per part      | flip-card / -inner / -front / -back / -toggle              |\n * | R7   | cn + ...rest            | `cn(…, className)` + `{...props}`                          |\n * | R9   | Controlled + uncontrolled | `flipped` / `defaultFlipped` / `onFlippedChange`          |\n * | R14  | Declares min viewport   | `data-min-viewport` + exported const                       |\n * | R18  | Tailwind only           | 3D with `perspective-*`, `transform-3d`, `backface-hidden` |\n * | R19  | Tokens only             | `bg-card`, `border-border`, `text-primary`                 |\n * | R26  | A11y                    | a disclosure: `aria-expanded` + `aria-controls`, inert hidden face |\n */\n\nimport { cn } from \"@/lib/utils\";\n\nexport const MIN_VIEWPORT = 320 as const;\n\ntype FlipCardProps = Omit<React.ComponentProps<\"div\">, \"children\"> & {\n  /** The side that asks: a claim, a question. */\n  front: React.ReactNode;\n  /** The side that answers. Links and other controls are fine here. */\n  back: React.ReactNode;\n  /** Label of the button on the front. @default 'Show answer' */\n  revealLabel?: string;\n  /** Label of the button on the back. @default 'Show question' */\n  returnLabel?: string;\n  /** Controlled state. */\n  flipped?: boolean;\n  /** Uncontrolled initial state. @default false */\n  defaultFlipped?: boolean;\n  onFlippedChange?: (flipped: boolean) => void;\n};\n\nconst FACE =\n  \"bg-card text-card-foreground border-border relative col-start-1 row-start-1 flex flex-col rounded-xl border p-6 backface-hidden\";\n\nconst TOGGLE =\n  \"text-primary focus-visible:outline-ring mt-auto inline-flex min-h-11 items-center gap-1.5 self-start pt-4 text-sm font-semibold underline-offset-4 hover:underline focus-visible:outline-2 focus-visible:outline-offset-2 noscript:hidden\";\n\nfunction FlipCard({\n  front,\n  back,\n  revealLabel = \"Show answer\",\n  returnLabel = \"Show question\",\n  flipped: flippedProp,\n  defaultFlipped = false,\n  onFlippedChange,\n  className,\n  ...props\n}: FlipCardProps) {\n  const [uncontrolled, setUncontrolled] = React.useState(defaultFlipped);\n  const flipped = flippedProp ?? uncontrolled;\n  // Until hydration nothing is inert, so a page without JavaScript reads both faces.\n  const [hydrated, setHydrated] = React.useState(false);\n  React.useEffect(() => setHydrated(true), []);\n  const backId = React.useId();\n  const frontButton = React.useRef<HTMLButtonElement>(null);\n  const backButton = React.useRef<HTMLButtonElement>(null);\n  // The side a press asked for, kept only for the render that press causes.\n  // A controlled parent that refuses the turn and later changes `flipped` for\n  // its own reasons must not pull focus, so the request expires once the\n  // press has been handled.\n  const pendingFocus = React.useRef<boolean | null>(null);\n\n  const turn = (next: boolean) => {\n    pendingFocus.current = next;\n    if (flippedProp === undefined) setUncontrolled(next);\n    onFlippedChange?.(next);\n    setTimeout(() => {\n      pendingFocus.current = null;\n    }, 0);\n  };\n\n  // After a turn, focus follows to the face now showing.\n  React.useEffect(() => {\n    const asked = pendingFocus.current;\n    pendingFocus.current = null;\n    if (asked !== flipped) return;\n    (flipped ? backButton : frontButton).current?.focus({\n      preventScroll: true,\n    });\n  }, [flipped]);\n\n  return (\n    <div\n      data-slot=\"flip-card\"\n      data-min-viewport={String(MIN_VIEWPORT)}\n      data-flipped={flipped ? \"\" : undefined}\n      // h-full: in a grid row the card fills its cell, so cards with\n      // different amounts of text line up. Outside a sized parent it is auto.\n      className={cn(\n        \"group/flip h-full min-w-0 perspective-[1600px]\",\n        className,\n      )}\n      {...props}\n    >\n      <div\n        data-slot=\"flip-card-inner\"\n        className={cn(\n          \"relative grid h-full transform-3d\",\n          \"motion-safe:transition-transform motion-safe:duration-(--duration-slow) motion-safe:ease-move\",\n          \"motion-safe:group-data-flipped/flip:rotate-y-180\",\n          \"noscript:gap-3 noscript:transform-none\",\n        )}\n      >\n        <div\n          data-slot=\"flip-card-front\"\n          inert={hydrated && flipped ? true : undefined}\n          className={cn(\n            FACE,\n            \"motion-reduce:transition-opacity motion-reduce:duration-(--duration-medium)\",\n            \"motion-reduce:group-data-flipped/flip:opacity-0\",\n          )}\n        >\n          {front}\n          <button\n            ref={frontButton}\n            type=\"button\"\n            data-slot=\"flip-card-toggle\"\n            aria-expanded={flipped}\n            aria-controls={backId}\n            onClick={() => turn(true)}\n            // Stretched over the face, so a tap anywhere on the front turns it.\n            className={cn(\n              TOGGLE,\n              \"after:absolute after:inset-0 after:rounded-xl after:content-['']\",\n            )}\n          >\n            {revealLabel}\n            <span aria-hidden>↻</span>\n          </button>\n        </div>\n        <div\n          id={backId}\n          data-slot=\"flip-card-back\"\n          inert={hydrated && !flipped ? true : undefined}\n          className={cn(\n            FACE,\n            \"motion-safe:rotate-y-180\",\n            \"motion-reduce:opacity-0 motion-reduce:transition-opacity motion-reduce:duration-(--duration-medium)\",\n            \"motion-reduce:group-data-flipped/flip:opacity-100\",\n            \"noscript:row-start-2 noscript:rotate-none noscript:opacity-100\",\n          )}\n        >\n          {back}\n          <button\n            ref={backButton}\n            type=\"button\"\n            data-slot=\"flip-card-toggle\"\n            aria-expanded={flipped}\n            aria-controls={backId}\n            onClick={() => turn(false)}\n            className={TOGGLE}\n          >\n            <span aria-hidden>↺</span>\n            {returnLabel}\n          </button>\n        </div>\n      </div>\n    </div>\n  );\n}\nFlipCard.displayName = \"FlipCard\";\n\nexport { FlipCard };\nexport type { FlipCardProps };\n"
    }
  ],
  "meta": {
    "tier": "pattern",
    "client": true,
    "minViewport": 320,
    "loading": false,
    "version": "1.2.0",
    "since": "2.0.0"
  },
  "docs": "## @interlace/flip-card\n\nInstalled to `components/ui/patterns/flip-card.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/patterns/flip-card';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/flip-card\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
