{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"mouse-tilt-card","type":"registry:component","title":"Mouse Tilt Card","description":"A card that leans toward the cursor with a moving glare.","author":"DiMaac (HankTheTank) <https://dimaac.com>","dependencies":["gsap","@gsap/react"],"files":[{"path":"registry/dimaac/mouse-tilt-card/mouse-tilt-card.tsx","type":"registry:component","target":"components/dimaac/mouse-tilt-card.tsx","content":"\"use client\";\n\nimport { useRef, type CSSProperties, type ReactNode } from \"react\";\nimport { gsap } from \"gsap\";\nimport { useGSAP } from \"@gsap/react\";\n\n// A card that leans toward the cursor in 3D, with a soft light that slides across it.\n// The pointer is read on a still outer box and only the inner layer tilts, so the edge of the card\n// never flickers when it rotates under the cursor. Everything eases with GSAP quickTo, so a fast\n// swipe stays smooth and the card settles back gently when the cursor leaves.\n// Touch: press and drag on the card to tilt it. Keyboard: tabbing into the card lifts it and lights it.\n\nexport type MouseTiltCardProps = {\n  /** What sits on the card. Anything works: an image, text, buttons. */\n  children: ReactNode;\n  /** The most the card leans when the cursor is at an edge, in degrees. */\n  maxTilt?: number;\n  /** How strong the 3D is, in pixels. Smaller is more dramatic, larger is flatter. */\n  perspective?: number;\n  /** How bright the light on the card is, from 0 (no light) to 1 (a hard white spot). */\n  glare?: number;\n  /** How much the card grows while it is active. 1 keeps its size. */\n  scale?: number;\n  /** Extra classes for the outer box. Add a rounded-* class so the light is clipped to your card's corners. */\n  className?: string;\n};\n\nconst clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value));\n\nexport function MouseTiltCard({ children, maxTilt = 12, perspective = 1000, glare = 0.35, scale = 1.03, className = \"\" }: MouseTiltCardProps) {\n  const boxRef = useRef<HTMLDivElement>(null);\n  const tiltRef = useRef<HTMLDivElement>(null);\n  const glareRef = useRef<HTMLDivElement>(null);\n  // Read when the pointer moves, so changing them never rebuilds the effect\n  const settings = useRef({ maxTilt, scale });\n  settings.current = { maxTilt, scale };\n\n  useGSAP(\n    () => {\n      const box = boxRef.current;\n      const tilt = tiltRef.current;\n      const light = glareRef.current;\n      if (!box || !tilt || !light) return;\n\n      // matchMedia re-runs this when the visitor flips their motion setting while the page is open\n      const mm = gsap.matchMedia();\n      mm.add({ reduce: \"(prefers-reduced-motion: reduce)\", motion: \"(prefers-reduced-motion: no-preference)\" }, (context) => {\n        // Reduced motion: the card stays flat and the same size. Only the light follows the pointer.\n        const reduced = Boolean(context.conditions?.reduce);\n\n        // The light's position and strength live on a plain object and are copied into CSS variables\n        const spot = { x: 50, y: 50, strength: 0 };\n        const paint = () => {\n          light.style.setProperty(\"--tilt-x\", `${spot.x}%`);\n          light.style.setProperty(\"--tilt-y\", `${spot.y}%`);\n          light.style.opacity = String(spot.strength);\n        };\n        paint();\n\n        // One quickTo per value: cheap to call on every move, eased toward the newest target\n        const leanX = gsap.quickTo(tilt, \"rotationX\", { duration: 0.6, ease: \"power3.out\" });\n        const leanY = gsap.quickTo(tilt, \"rotationY\", { duration: 0.6, ease: \"power3.out\" });\n        // quickTo ignores the \"scale\" shortcut, so width and height are eased together instead\n        const growX = gsap.quickTo(tilt, \"scaleX\", { duration: 0.6, ease: \"power3.out\" });\n        const growY = gsap.quickTo(tilt, \"scaleY\", { duration: 0.6, ease: \"power3.out\" });\n        const grow = (size: number) => {\n          growX(size);\n          growY(size);\n        };\n        const slideX = gsap.quickTo(spot, \"x\", { duration: 0.35, ease: \"power3.out\", onUpdate: paint });\n        const slideY = gsap.quickTo(spot, \"y\", { duration: 0.35, ease: \"power3.out\", onUpdate: paint });\n        const glow = gsap.quickTo(spot, \"strength\", { duration: 0.4, ease: \"power2.out\", onUpdate: paint });\n\n        let hovering = false;\n        let focused = false;\n\n        const follow = (event: PointerEvent) => {\n          const rect = box.getBoundingClientRect();\n          if (rect.width === 0 || rect.height === 0) return;\n          // 0 to 1 across the card, so the middle is 0.5\n          const across = clamp((event.clientX - rect.left) / rect.width, 0, 1);\n          const down = clamp((event.clientY - rect.top) / rect.height, 0, 1);\n          if (!reduced) {\n            leanY((across - 0.5) * 2 * settings.current.maxTilt);\n            leanX(-(down - 0.5) * 2 * settings.current.maxTilt);\n            grow(settings.current.scale);\n          }\n          slideX(across * 100);\n          slideY(down * 100);\n          glow(1);\n          box.dataset.active = \"true\";\n        };\n\n        // Back to flat, unless the pointer or keyboard focus is still on the card\n        const rest = () => {\n          if (hovering || focused) return;\n          leanX(0);\n          leanY(0);\n          grow(1);\n          glow(0);\n          delete box.dataset.active;\n        };\n\n        const onPointerEnter = (event: PointerEvent) => {\n          hovering = true;\n          follow(event);\n        };\n        const onPointerMove = (event: PointerEvent) => {\n          hovering = true;\n          follow(event);\n        };\n        const onPointerLeave = () => {\n          hovering = false;\n          rest();\n        };\n        // A finger lifting is the same as the cursor leaving\n        const onPointerUp = (event: PointerEvent) => {\n          if (event.pointerType === \"mouse\") return;\n          hovering = false;\n          rest();\n        };\n\n        // Keyboard: tabbing in lights the card up and lifts it, with no lean. A mouse click doesn't trigger this.\n        const onFocusIn = (event: FocusEvent) => {\n          if (!(event.target instanceof Element) || !event.target.matches(\":focus-visible\")) return;\n          focused = true;\n          if (!reduced) grow(settings.current.scale);\n          slideX(50);\n          slideY(30);\n          glow(1);\n          box.dataset.active = \"true\";\n        };\n        const onFocusOut = (event: FocusEvent) => {\n          if (event.relatedTarget instanceof Node && box.contains(event.relatedTarget)) return;\n          focused = false;\n          rest();\n        };\n\n        box.addEventListener(\"pointerenter\", onPointerEnter);\n        box.addEventListener(\"pointermove\", onPointerMove);\n        box.addEventListener(\"pointerleave\", onPointerLeave);\n        box.addEventListener(\"pointercancel\", onPointerLeave);\n        box.addEventListener(\"pointerup\", onPointerUp);\n        box.addEventListener(\"focusin\", onFocusIn);\n        box.addEventListener(\"focusout\", onFocusOut);\n\n        return () => {\n          box.removeEventListener(\"pointerenter\", onPointerEnter);\n          box.removeEventListener(\"pointermove\", onPointerMove);\n          box.removeEventListener(\"pointerleave\", onPointerLeave);\n          box.removeEventListener(\"pointercancel\", onPointerLeave);\n          box.removeEventListener(\"pointerup\", onPointerUp);\n          box.removeEventListener(\"focusin\", onFocusIn);\n          box.removeEventListener(\"focusout\", onFocusOut);\n          delete box.dataset.active;\n          gsap.set(tilt, { clearProps: \"transform\" });\n          light.style.opacity = \"0\";\n        };\n      });\n\n      return () => mm.revert();\n    },\n    { scope: boxRef },\n  );\n\n  return (\n    // The outer box holds the perspective and listens for the pointer. It never moves.\n    // touch-action: pan-y lets a finger scroll the page up and down, while a sideways drag tilts the card.\n    <div ref={boxRef} className={`relative inline-block data-[active=true]:z-10 ${className}`} style={{ perspective, touchAction: \"pan-y\" }}>\n      {/* preserve-3d lets children use translateZ to float above the card and shift as it leans */}\n      <div ref={tiltRef} className=\"relative block h-full w-full rounded-[inherit]\" style={{ transformStyle: \"preserve-3d\", willChange: \"transform\" }}>\n        {children}\n        {/* The light: one soft white spot that follows the pointer. 1px forward so it sits over the card face but under anything floated higher. */}\n        <div\n          ref={glareRef}\n          aria-hidden=\"true\"\n          className=\"pointer-events-none absolute inset-0 rounded-[inherit] opacity-0\"\n          style={\n            {\n              \"--tilt-glare\": glare,\n              \"--tilt-x\": \"50%\",\n              \"--tilt-y\": \"50%\",\n              transform: \"translateZ(1px)\",\n              background: \"radial-gradient(circle at var(--tilt-x) var(--tilt-y), rgba(255,255,255,var(--tilt-glare)) 0%, rgba(255,255,255,0) 65%)\",\n            } as CSSProperties\n          }\n        />\n      </div>\n    </div>\n  );\n}\n"}],"categories":["cards","gsap","cursor","3d","card","glare"],"docs":"import { MouseTiltCard } from \"@/components/dimaac/mouse-tilt-card\". Give its parent a size: it fills it. Docs, playground and props: https://ui.dimaac.com/components/mouse-tilt-card","meta":{"tier":"free","preview":"https://ui.dimaac.com/components/mouse-tilt-card","credits":[{"by":"West Kast","url":"https://www.lummi.ai/photo/fashion-portrait-with-orange-visor-damql","what":"Orange visor portrait","source":"Lummi","licence":"Lummi License (free for personal and commercial use)"},{"by":"Dona Mara","url":"https://www.lummi.ai/photo/elegant-perfume-bottle-j5t5p","what":"Amber perfume","source":"Lummi","licence":"Lummi License (free for personal and commercial use)"},{"by":"SHIHO","url":"https://www.lummi.ai/photo/lush-green-hills-mdvvk","what":"Misty hills","source":"Lummi","licence":"Lummi License (free for personal and commercial use)"}]}}