{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"flip-card","type":"registry:ui","title":"FlipCard","description":"Card that flips in 3D to reveal its back, on hover (plus focus and tap) or as a click toggle; it lifts and lands with a slight overshoot, keeps turning the way the pointer travels, and its faces catch a sweeping sheen and shade edge-on","author":"fancy-ui <https://fancy-ui.rama.app>","categories":["cards"],"registryDependencies":["https://fancy-ui.rama.app/r/internals.json","https://fancy-ui.rama.app/r/internals-dom.json","https://fancy-ui.rama.app/r/internals-motion.json","https://fancy-ui.rama.app/r/utils.json"],"files":[{"path":"react/src/components/flip-card/FlipCard.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport type {\n\tCSSProperties,\n\tFocusEvent,\n\tKeyboardEvent,\n\tMouseEvent,\n\tPointerEvent,\n\tReactNode,\n} from \"react\";\nimport { cn } from \"../../utils\";\nimport { useReducedMotion } from \"../../internals/motion/media-query\";\nimport { useInertAttribute } from \"../../internals/dom/use-inert-attribute\";\nimport { useLiveRef } from \"../../internals/dom/use-live-ref\";\nimport \"./flip-card.css\";\n\nexport type FlipCardTrigger = \"hover\" | \"click\";\n\nexport interface FlipCardProps {\n\t/** Axis of rotation */\n\trotate?: \"x\" | \"y\";\n\t/**\n\t * What flips the card. `hover` (default) flips on pointer hover and on\n\t * keyboard focus; `click` makes the card a toggle button (click, tap,\n\t * Enter or Space).\n\t */\n\ttrigger?: FlipCardTrigger;\n\t/**\n\t * Whether the back is showing. Seeds the card on mount; changing it later\n\t * turns the card the rest of the way. Pair with `onFlip` to keep it in sync\n\t * (the React counterpart of the Svelte source's `bind:flipped`).\n\t */\n\tflipped?: boolean;\n\t/** Called with the new state after every flip, however it happened. */\n\tonFlip?: (flipped: boolean) => void;\n\t/** Length of one flip in milliseconds */\n\tduration?: number;\n\t/** Light the faces as they turn: a sheen sweeping across and shading edge-on */\n\tglare?: boolean;\n\t/** Accessible name for the card (announced in click mode) */\n\tlabel?: string;\n\t/** Additional CSS classes on the card */\n\tclassName?: string;\n\t/** Front face content */\n\tchildren?: ReactNode;\n\t/** Back face content */\n\tback?: ReactNode;\n}\n\n/** Whether an accumulated angle (a whole number of half turns) shows the back. */\nexport function showsBack(angle: number): boolean {\n\treturn Math.abs(Math.round(angle / 180)) % 2 === 1;\n}\n\n/**\n * Which way to turn, from where the pointer crosses the card's edge: the\n * card turns the way the pointer travels, so it seems pushed by it.\n * `entering` is true on enter, false on leave.\n */\nexport function flipDirection(\n\trect: { left: number; top: number; width: number; height: number },\n\tpoint: { x: number; y: number },\n\taxis: \"x\" | \"y\",\n\tentering: boolean\n): 1 | -1 {\n\tconst along =\n\t\taxis === \"y\" ? point.x - rect.left - rect.width / 2 : point.y - rect.top - rect.height / 2;\n\t// Entering from the start side, or leaving through the end side, is\n\t// travelling toward the end: turn positive.\n\tconst towardEnd = entering ? along < 0 : along >= 0;\n\tconst sign = towardEnd ? 1 : -1;\n\t// rotateX turns the top away for positive angles: flip the sign so a\n\t// pointer moving down tips the card down.\n\treturn (axis === \"y\" ? sign : -sign) as 1 | -1;\n}\n\nconst INTERACTIVE =\n\t\"a, button, input, select, textarea, label, [role='button'], [contenteditable='true']\";\n\nexport function FlipCard({\n\trotate = \"y\",\n\ttrigger = \"hover\",\n\tflipped = false,\n\tonFlip,\n\tduration = 700,\n\tglare = true,\n\tlabel,\n\tclassName = \"\",\n\tchildren,\n\tback,\n}: FlipCardProps) {\n\tconst reduced = useReducedMotion();\n\n\t/** Accumulated angle: every flip adds a half turn, so it never rewinds. */\n\tconst [angle, setAngle] = useState(() => (flipped ? 180 : 0));\n\t// Mirrors `angle` synchronously, so two flips inside one render cycle\n\t// each read the angle the previous one left.\n\tconst angleRef = useRef(angle);\n\tconst showingBack = showsBack(angle);\n\n\tconst rootRef = useRef<HTMLDivElement | null>(null);\n\tconst liftRef = useRef<HTMLDivElement | null>(null);\n\tconst shadowRef = useRef<HTMLDivElement | null>(null);\n\n\tconst frontInertRef = useInertAttribute<HTMLDivElement>(showingBack);\n\tconst backInertRef = useInertAttribute<HTMLDivElement>(!showingBack);\n\n\tconst live = useLiveRef({ onFlip, duration, reduced });\n\n\tfunction setAngleTo(next: number) {\n\t\tangleRef.current = next;\n\t\tsetAngle(next);\n\t}\n\n\t/** The card rises through the middle of the flip and settles; its shadow spreads, then gathers. */\n\tfunction lift() {\n\t\tconst { reduced, duration } = live.current;\n\t\tif (reduced) return;\n\t\tconst opts = { duration, easing: \"cubic-bezier(0.33, 1, 0.68, 1)\" };\n\t\tliftRef.current?.animate?.(\n\t\t\t[\n\t\t\t\t{ transform: \"translateZ(0) scale(1)\" },\n\t\t\t\t{ transform: \"translateZ(40px) scale(1.05)\", offset: 0.45 },\n\t\t\t\t{ transform: \"translateZ(0) scale(1)\" },\n\t\t\t],\n\t\t\topts\n\t\t);\n\t\tshadowRef.current?.animate?.(\n\t\t\t[\n\t\t\t\t{ opacity: 0.35, transform: \"translateY(10px) scale(0.92)\", filter: \"blur(14px)\" },\n\t\t\t\t{\n\t\t\t\t\topacity: 0.18,\n\t\t\t\t\ttransform: \"translateY(26px) scale(0.85)\",\n\t\t\t\t\tfilter: \"blur(26px)\",\n\t\t\t\t\toffset: 0.45,\n\t\t\t\t},\n\t\t\t\t{ opacity: 0.35, transform: \"translateY(10px) scale(0.92)\", filter: \"blur(14px)\" },\n\t\t\t],\n\t\t\topts\n\t\t);\n\t}\n\n\tfunction turn(direction: 1 | -1) {\n\t\tsetAngleTo(angleRef.current + 180 * direction);\n\t\tconst next = showsBack(angleRef.current);\n\t\tlive.current.onFlip?.(next);\n\t\tlift();\n\t}\n\n\t// A `flipped` changed from outside turns the card the rest of the way.\n\t// Keyed on the prop alone: an internal flip never re-runs it, and a\n\t// consumer echoing the new state back through `onFlip` finds the card\n\t// already there.\n\tconst liveLift = useLiveRef(lift);\n\tuseEffect(() => {\n\t\tif (flipped !== showsBack(angleRef.current)) {\n\t\t\tsetAngleTo(angleRef.current + 180);\n\t\t\tlive.current.onFlip?.(flipped);\n\t\t\tliveLift.current();\n\t\t}\n\t}, [flipped, live, liveLift]);\n\n\t// ---- hover mode --------------------------------------------------------\n\n\tfunction pointerCross(event: PointerEvent<HTMLDivElement>, entering: boolean) {\n\t\tconst root = rootRef.current;\n\t\tif (trigger !== \"hover\" || event.pointerType === \"touch\" || !root) return;\n\t\tconst wantBack = entering;\n\t\tif (showsBack(angleRef.current) === wantBack) return;\n\t\tconst dir = flipDirection(\n\t\t\troot.getBoundingClientRect(),\n\t\t\t{ x: event.clientX, y: event.clientY },\n\t\t\trotate,\n\t\t\tentering\n\t\t);\n\t\tturn(dir);\n\t}\n\n\t// Keyboard users reach the back by focusing the card, in hover mode too.\n\tfunction handleFocus(event: FocusEvent<HTMLDivElement>) {\n\t\tif (trigger !== \"hover\" || showsBack(angleRef.current)) return;\n\t\tif (event.target === rootRef.current) turn(1);\n\t}\n\tfunction handleBlur(event: FocusEvent<HTMLDivElement>) {\n\t\t// React's onBlur bubbles (focusout); the Svelte source's listener does\n\t\t// not, so only the card's own blur counts here.\n\t\tif (event.target !== rootRef.current) return;\n\t\tif (trigger !== \"hover\" || !showsBack(angleRef.current)) return;\n\t\tconst root = rootRef.current;\n\t\tif (root && event.relatedTarget instanceof Node && root.contains(event.relatedTarget)) return;\n\t\tturn(1);\n\t}\n\n\t// ---- click mode / touch --------------------------------------------------\n\n\t/** A click on a link or button inside a face is that control's, not the card's. */\n\tfunction isInnerControl(target: EventTarget | null): boolean {\n\t\tconst root = rootRef.current;\n\t\tif (!(target instanceof Element) || !root) return false;\n\t\tconst control = target.closest(INTERACTIVE);\n\t\treturn control !== null && control !== root && root.contains(control);\n\t}\n\n\tfunction handleClick(event: MouseEvent<HTMLDivElement>) {\n\t\t// A tap flips a hover card too: touch has no hover to reveal the back.\n\t\tconst touchOnHover =\n\t\t\ttrigger === \"hover\" && (event.nativeEvent as globalThis.PointerEvent).pointerType === \"touch\";\n\t\tif (trigger !== \"click\" && !touchOnHover) return;\n\t\tif (isInnerControl(event.target)) return;\n\t\tturn(1);\n\t}\n\n\tfunction handleKeyDown(event: KeyboardEvent<HTMLDivElement>) {\n\t\tif (trigger !== \"click\" || event.target !== rootRef.current) return;\n\t\tif (event.key === \"Enter\" || event.key === \" \") {\n\t\t\tevent.preventDefault();\n\t\t\tturn(1);\n\t\t}\n\t}\n\n\tconst style = {\n\t\t\"--fc-duration\": `${duration}ms`,\n\t\t\"--fc-target\": `${angle}deg`,\n\t} as CSSProperties;\n\n\treturn (\n\t\t<div\n\t\t\tref={rootRef}\n\t\t\tclassName={cn(\"group ft-flip-card relative h-72 w-56 [perspective:1000px]\", className)}\n\t\t\tdata-axis={rotate}\n\t\t\tdata-flipped={showingBack ? \"\" : undefined}\n\t\t\tdata-trigger={trigger}\n\t\t\tstyle={style}\n\t\t\trole={trigger === \"click\" ? \"button\" : \"group\"}\n\t\t\ttabIndex={0}\n\t\t\taria-pressed={trigger === \"click\" ? showingBack : undefined}\n\t\t\taria-label={label}\n\t\t\taria-roledescription=\"flip card\"\n\t\t\tonPointerEnter={(e) => pointerCross(e, true)}\n\t\t\tonPointerLeave={(e) => pointerCross(e, false)}\n\t\t\tonFocus={handleFocus}\n\t\t\tonBlur={handleBlur}\n\t\t\tonClick={handleClick}\n\t\t\tonKeyDown={handleKeyDown}\n\t\t>\n\t\t\t<div ref={shadowRef} className=\"ft-flip-card__shadow\" aria-hidden=\"true\"></div>\n\n\t\t\t<div ref={liftRef} className=\"ft-flip-card__lift size-full [transform-style:preserve-3d]\">\n\t\t\t\t<div className=\"ft-flip-card__inner relative size-full rounded-2xl [transform-style:preserve-3d]\">\n\t\t\t\t\t{/* Front */}\n\t\t\t\t\t<div\n\t\t\t\t\t\tref={frontInertRef}\n\t\t\t\t\t\tclassName=\"ft-flip-card__face ft-flip-card__front bg-card text-card-foreground absolute size-full overflow-hidden rounded-2xl border [backface-visibility:hidden]\"\n\t\t\t\t\t\taria-hidden={showingBack ? \"true\" : undefined}\n\t\t\t\t\t>\n\t\t\t\t\t\t{children}\n\t\t\t\t\t\t{glare ? <span className=\"ft-flip-card__light\" aria-hidden=\"true\"></span> : null}\n\t\t\t\t\t</div>\n\n\t\t\t\t\t{/* Back */}\n\t\t\t\t\t<div\n\t\t\t\t\t\tref={backInertRef}\n\t\t\t\t\t\tclassName=\"ft-flip-card__face ft-flip-card__back bg-card text-card-foreground absolute size-full overflow-hidden rounded-2xl border p-4 [backface-visibility:hidden]\"\n\t\t\t\t\t\taria-hidden={showingBack ? undefined : \"true\"}\n\t\t\t\t\t>\n\t\t\t\t\t\t{back}\n\t\t\t\t\t\t{glare ? <span className=\"ft-flip-card__light\" aria-hidden=\"true\"></span> : null}\n\t\t\t\t\t</div>\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t</div>\n\t);\n}\n","type":"registry:ui","target":"@ui/fancy/components/flip-card/FlipCard.tsx"},{"path":"react/src/components/flip-card/flip-card.css","content":"/*\n * FlipCard — ported verbatim from the Svelte <style> block. Every rule is\n * anchored on the root class `.ft-flip-card`, which the Svelte source already\n * names, so nothing leaks once the compiler-scoped rules become global.\n */\n\n/* The one number everything turns on: the card's rotation. Registered so\n   it can transition, and inherited so the faces can light themselves\n   from it. */\n@property --fc-angle {\n\tsyntax: \"<angle>\";\n\tinherits: true;\n\tinitial-value: 0deg;\n}\n\n.ft-flip-card {\n\t--fc-angle: var(--fc-target);\n\toutline: none;\n\t-webkit-tap-highlight-color: transparent;\n}\n\n.ft-flip-card:focus-visible .ft-flip-card__inner {\n\tbox-shadow: 0 0 0 3px color-mix(in oklab, var(--color-ring, #888) 60%, transparent);\n}\n\n.ft-flip-card[data-trigger=\"click\"] {\n\tcursor: pointer;\n}\n\n.ft-flip-card__lift,\n.ft-flip-card__inner {\n\tposition: relative;\n}\n\n.ft-flip-card[data-axis=\"y\"] .ft-flip-card__inner {\n\ttransform: rotateY(var(--fc-angle));\n}\n\n.ft-flip-card[data-axis=\"x\"] .ft-flip-card__inner {\n\ttransform: rotateX(var(--fc-angle));\n}\n\n.ft-flip-card[data-axis=\"y\"] .ft-flip-card__back {\n\ttransform: rotateY(180deg);\n}\n\n.ft-flip-card[data-axis=\"x\"] .ft-flip-card__back {\n\ttransform: rotateX(180deg);\n}\n\n/* The shadow on the \"table\" under the card. */\n.ft-flip-card__shadow {\n\tposition: absolute;\n\tinset: 8% 6% -4%;\n\tborder-radius: 1.25rem;\n\tbackground: rgb(0 0 0 / 0.9);\n\topacity: 0.35;\n\ttransform: translateY(10px) scale(0.92);\n\tfilter: blur(14px);\n\tpointer-events: none;\n}\n\n/* ---- light: off unless the browser can compute it ---------------------- */\n\n.ft-flip-card__light {\n\tdisplay: none;\n}\n\n@supports (opacity: calc(sin(30deg))) {\n\t.ft-flip-card__face {\n\t\t/* the face's own turn: the back starts half a turn round */\n\t\t--fc-face: var(--fc-angle);\n\t\t/* |sin| — 0 facing you, 1 edge-on */\n\t\t--fc-edge: max(sin(var(--fc-face)), -1 * sin(var(--fc-face)));\n\t}\n\n\t.ft-flip-card__back {\n\t\t--fc-face: calc(var(--fc-angle) + 180deg);\n\t}\n\n\t.ft-flip-card__light {\n\t\tdisplay: block;\n\t\tposition: absolute;\n\t\tinset: 0;\n\t\tpointer-events: none;\n\t\tborder-radius: inherit;\n\t\t/* a sheen that sweeps across as the face turns, over a shade that\n\t\t   deepens as the face goes edge-on */\n\t\tbackground:\n\t\t\tlinear-gradient(\n\t\t\t\tvar(--fc-sheen-angle, 105deg),\n\t\t\t\ttransparent 38%,\n\t\t\t\trgb(255 255 255 / 0.55) 50%,\n\t\t\t\ttransparent 62%\n\t\t\t),\n\t\t\trgb(0 0 0 / calc(var(--fc-edge) * 0.55));\n\t\tbackground-size:\n\t\t\t300% 300%,\n\t\t\t100% 100%;\n\t\tbackground-repeat: no-repeat;\n\t\tbackground-position:\n\t\t\tcalc(50% - sin(var(--fc-face)) * 45%) calc(50% - sin(var(--fc-face)) * 45%),\n\t\t\t0 0;\n\t}\n\n\t/* the sheen is brightest part-way round, never facing you or edge-on */\n\t.ft-flip-card__light {\n\t\topacity: calc(min(1, var(--fc-edge) * 2.4));\n\t}\n\n\t.ft-flip-card[data-axis=\"x\"] .ft-flip-card__light {\n\t\t--fc-sheen-angle: 15deg;\n\t}\n}\n\n/* ---- motion ---------------------------------------------------------------- */\n\n@media (prefers-reduced-motion: no-preference) {\n\t.ft-flip-card {\n\t\t/* a slight overshoot, so the card lands rather than stops */\n\t\ttransition: --fc-angle var(--fc-duration) cubic-bezier(0.34, 1.25, 0.64, 1);\n\t}\n}\n\n/* Reduced motion: no turning at all — the faces cross-fade in place. */\n@media (prefers-reduced-motion: reduce) {\n\t/* as specific as the axis rules above, and later, so it wins */\n\t.ft-flip-card[data-axis] .ft-flip-card__inner,\n\t.ft-flip-card[data-axis] .ft-flip-card__back {\n\t\ttransform: none;\n\t}\n\n\t.ft-flip-card__face {\n\t\ttransition: opacity 0.2s ease;\n\t}\n\n\t.ft-flip-card__front,\n\t.ft-flip-card[data-flipped] .ft-flip-card__back {\n\t\topacity: 1;\n\t}\n\n\t.ft-flip-card__back,\n\t.ft-flip-card[data-flipped] .ft-flip-card__front {\n\t\topacity: 0;\n\t}\n\n\t.ft-flip-card__light {\n\t\tdisplay: none;\n\t}\n}\n","type":"registry:ui","target":"@ui/fancy/components/flip-card/flip-card.css"},{"path":"react/src/components/flip-card/index.ts","content":"\"use client\";\n\nexport { FlipCard } from \"./FlipCard\";\nexport type { FlipCardProps, FlipCardTrigger } from \"./FlipCard\";\n","type":"registry:ui","target":"@ui/fancy/components/flip-card/index.ts"}],"meta":{"license":"MIT","package":"fancy-ui-react","version":"0.7.0","source":"https://fancy-ui.rama.app/docs/components/flip-card?framework=react"}}