{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"tilt-card","type":"registry:component","title":"Tilt card","description":"Leans toward your cursor with a glare that follows the light.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"components/tilt-card.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useState } from \"react\";\nimport {\n  motion,\n  useMotionTemplate,\n  useMotionValue,\n  useSpring,\n  useTransform,\n  type MotionValue,\n} from \"motion/react\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { cn } from \"@/lib/utils\";\n\n// Past about 12 degrees the text skews enough to become hard to read.\nconst MAX_TILT = 12;\n// Settles in roughly 300ms with no overshoot, so the card feels heavy\n// rather than wobbly.\nconst FOLLOW = { stiffness: 300, damping: 30 };\n\nconst GRAIN = `url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E\")`;\n\n// A glossy surface reflects light as a streak, not a spot: one broad bright\n// band with a thin echo beside it, like light on polished glass. The oversized\n// background lets it slide fully across as the card tilts.\nconst STREAK =\n  \"linear-gradient(115deg, transparent 32%, oklch(1 0 0 / 0.05) 40%, oklch(1 0 0 / 0.3) 47%, oklch(1 0 0 / 0.05) 53%, transparent 56%, transparent 59%, oklch(1 0 0 / 0.14) 61%, transparent 63%)\";\nconst STREAK_SIZE = \"250% 250%\";\n\n// Paints only the 1px padding ring, so the gradient shows as a lit edge.\nconst RING_MASK =\n  \"linear-gradient(#000 0 0) content-box exclude, linear-gradient(#000 0 0)\";\n\nexport function TiltCard({\n  children,\n  className,\n}: {\n  children: React.ReactNode;\n  className?: string;\n}) {\n  const reduceMotion = useReducedMotion();\n  const [lit, setLit] = useState(false);\n\n  // Pointer position across the card, 0 to 1 on each axis.\n  const pointerX = useMotionValue(0.5);\n  const pointerY = useMotionValue(0.5);\n  const x = useSpring(pointerX, FOLLOW);\n  const y = useSpring(pointerY, FOLLOW);\n\n  // The edge under the pointer dips away, as if pressed.\n  const rotateX = useTransform(y, [0, 1], [MAX_TILT, -MAX_TILT]);\n  const rotateY = useTransform(x, [0, 1], [-MAX_TILT, MAX_TILT]);\n  const transform = useMotionTemplate`perspective(800px) rotateX(${rotateX}deg) rotateY(${rotateY}deg)`;\n\n  // The light sits above the card, so the shadow slides away from the lifted edge.\n  const shadowX = useTransform(x, [0, 1], [10, -10]);\n  const shadowY = useTransform(y, [0, 1], [18, 6]);\n  const shadow = useMotionTemplate`translate(${shadowX}px, ${shadowY}px)`;\n\n  const px = usePercent(x);\n  const py = usePercent(y);\n  const edge = useMotionTemplate`radial-gradient(circle at ${px} ${py}, oklch(1 0 0 / 0.7), transparent 50%)`;\n  const streakPosition = useMotionTemplate`${px} ${py}`;\n\n  const reset = () => {\n    pointerX.set(0.5);\n    pointerY.set(0.5);\n    setLit(false);\n  };\n\n  // Half-lit at rest, so the card still reads as glossy before it's touched.\n  const light = cn(\n    \"pointer-events-none absolute inset-0 rounded-[inherit] opacity-50 transition-[opacity] duration-300 ease-out\",\n    lit && \"opacity-100\",\n  );\n\n  return (\n    <div\n      className=\"relative\"\n      onPointerMove={(e) => {\n        // Touch would fight the page scroll, so tilting is for mouse and pen.\n        if (reduceMotion || e.pointerType === \"touch\") return;\n        const rect = e.currentTarget.getBoundingClientRect();\n        pointerX.set((e.clientX - rect.left) / rect.width);\n        pointerY.set((e.clientY - rect.top) / rect.height);\n        setLit(true);\n      }}\n      onPointerLeave={reset}\n    >\n      <motion.div\n        aria-hidden\n        className=\"absolute inset-x-8 top-10 bottom-0 rounded-[20px] bg-black/40 blur-xl\"\n        style={{ transform: shadow }}\n      />\n      <motion.div\n        className={cn(\"relative transform-3d\", className)}\n        style={{ transform }}\n      >\n        <div\n          aria-hidden\n          className=\"pointer-events-none absolute inset-0 rounded-[inherit] opacity-[0.14] mix-blend-overlay\"\n          style={{ backgroundImage: GRAIN }}\n        />\n        <motion.div\n          aria-hidden\n          className={cn(light, \"mix-blend-plus-lighter\")}\n          style={{\n            backgroundImage: STREAK,\n            backgroundSize: STREAK_SIZE,\n            backgroundPosition: streakPosition,\n          }}\n        />\n        <motion.div\n          aria-hidden\n          className={cn(light, \"p-px\")}\n          style={{ background: edge, mask: RING_MASK }}\n        />\n        {children}\n      </motion.div>\n    </div>\n  );\n}\n\nfunction usePercent(value: MotionValue<number>) {\n  return useTransform(value, (v) => `${v * 100}%`);\n}\n\nexport default function TiltCardDemo() {\n  return (\n    <TiltCard className=\"aspect-[1.586] w-80 rounded-[20px] bg-[linear-gradient(145deg,#1d1d1f,#0b0b0c)] text-white shadow-[inset_0_0_0_1px_oklch(1_0_0/0.08)]\">\n      {/* Raised off the card's surface, so it drifts against it as it tilts. */}\n      <div className=\"relative flex h-full translate-z-8 flex-col justify-between p-5\">\n        <div className=\"flex items-baseline justify-between text-sm\">\n          <span className=\"font-medium\">ui lab</span>\n          <span className=\"font-mono text-xs text-white/50 tabular-nums\">\n            004\n          </span>\n        </div>\n        <div className=\"flex items-baseline justify-between\">\n          <span className=\"text-2xl font-medium tracking-tight\">xevrion</span>\n          <span className=\"font-mono text-xs text-white/50 tabular-nums\">\n            2026\n          </span>\n        </div>\n      </div>\n    </TiltCard>\n  );\n}\n"},{"path":"hooks/use-reduced-motion.ts","type":"registry:hook","target":"hooks/use-reduced-motion.ts","content":"\"use client\";\n\nimport { useSyncExternalStore } from \"react\";\n\n// Motion's useReducedMotion reads the media query on the very first client\n// render, but the server can't know it, so every component that renders\n// differently under reduced motion broke hydration for those users. This\n// reports false during hydration, matching the server HTML, then the real\n// preference straight after, and follows it if it changes.\nconst query = \"(prefers-reduced-motion: reduce)\";\n\nfunction subscribe(onChange: () => void) {\n  const media = window.matchMedia(query);\n  media.addEventListener(\"change\", onChange);\n  return () => media.removeEventListener(\"change\", onChange);\n}\n\nexport function useReducedMotion() {\n  return useSyncExternalStore(\n    subscribe,\n    () => window.matchMedia(query).matches,\n    () => false,\n  );\n}\n"}],"categories":["cards"],"docs":"From ui lab: https://lab.xevrion.dev/lab/tilt-card"}