← Components

Mouse Tilt Card

A card that leans toward the cursor with a moving glare.

Cards · FreeInstall ↓
Open preview ↗
Loading the demoNew tab ↗

Customize

12 °
1000 px
0.35
1.030
Your code
<MouseTiltCard children={children} />

Changes show live and stay in the address, so a link keeps them.

Code

"use client";

import { useRef, type CSSProperties, type ReactNode } from "react";
import { gsap } from "gsap";
import { useGSAP } from "@gsap/react";

// A card that leans toward the cursor in 3D, with a soft light that slides across it.
// The pointer is read on a still outer box and only the inner layer tilts, so the edge of the card
// never flickers when it rotates under the cursor. Everything eases with GSAP quickTo, so a fast
// swipe stays smooth and the card settles back gently when the cursor leaves.
// Touch: press and drag on the card to tilt it. Keyboard: tabbing into the card lifts it and lights it.

export type MouseTiltCardProps = {
  /** What sits on the card. Anything works: an image, text, buttons. */
  children: ReactNode;
  /** The most the card leans when the cursor is at an edge, in degrees. */
  maxTilt?: number;
  /** How strong the 3D is, in pixels. Smaller is more dramatic, larger is flatter. */
  perspective?: number;
  /** How bright the light on the card is, from 0 (no light) to 1 (a hard white spot). */
  glare?: number;
  /** How much the card grows while it is active. 1 keeps its size. */
  scale?: number;
  /** Extra classes for the outer box. Add a rounded-* class so the light is clipped to your card's corners. */
  className?: string;
};

const clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value));

export function MouseTiltCard({ children, maxTilt = 12, perspective = 1000, glare = 0.35, scale = 1.03, className = "" }: MouseTiltCardProps) {
  const boxRef = useRef<HTMLDivElement>(null);
  const tiltRef = useRef<HTMLDivElement>(null);
  const glareRef = useRef<HTMLDivElement>(null);
  // Read when the pointer moves, so changing them never rebuilds the effect
  const settings = useRef({ maxTilt, scale });
  settings.current = { maxTilt, scale };

  useGSAP(
    () => {
      const box = boxRef.current;
      const tilt = tiltRef.current;
      const light = glareRef.current;
      if (!box || !tilt || !light) return;

      // matchMedia re-runs this when the visitor flips their motion setting while the page is open
      const mm = gsap.matchMedia();
      mm.add({ reduce: "(prefers-reduced-motion: reduce)", motion: "(prefers-reduced-motion: no-preference)" }, (context) => {
        // Reduced motion: the card stays flat and the same size. Only the light follows the pointer.
        const reduced = Boolean(context.conditions?.reduce);

        // The light's position and strength live on a plain object and are copied into CSS variables
        const spot = { x: 50, y: 50, strength: 0 };
        const paint = () => {
          light.style.setProperty("--tilt-x", `${spot.x}%`);
          light.style.setProperty("--tilt-y", `${spot.y}%`);
          light.style.opacity = String(spot.strength);
        };
        paint();

        // One quickTo per value: cheap to call on every move, eased toward the newest target
        const leanX = gsap.quickTo(tilt, "rotationX", { duration: 0.6, ease: "power3.out" });
        const leanY = gsap.quickTo(tilt, "rotationY", { duration: 0.6, ease: "power3.out" });
        // quickTo ignores the "scale" shortcut, so width and height are eased together instead
        const growX = gsap.quickTo(tilt, "scaleX", { duration: 0.6, ease: "power3.out" });
        const growY = gsap.quickTo(tilt, "scaleY", { duration: 0.6, ease: "power3.out" });
        const grow = (size: number) => {
          growX(size);
          growY(size);
        };
        const slideX = gsap.quickTo(spot, "x", { duration: 0.35, ease: "power3.out", onUpdate: paint });
        const slideY = gsap.quickTo(spot, "y", { duration: 0.35, ease: "power3.out", onUpdate: paint });
        const glow = gsap.quickTo(spot, "strength", { duration: 0.4, ease: "power2.out", onUpdate: paint });

        let hovering = false;
        let focused = false;

        const follow = (event: PointerEvent) => {
          const rect = box.getBoundingClientRect();
          if (rect.width === 0 || rect.height === 0) return;
          // 0 to 1 across the card, so the middle is 0.5
          const across = clamp((event.clientX - rect.left) / rect.width, 0, 1);
          const down = clamp((event.clientY - rect.top) / rect.height, 0, 1);
          if (!reduced) {
            leanY((across - 0.5) * 2 * settings.current.maxTilt);
            leanX(-(down - 0.5) * 2 * settings.current.maxTilt);
            grow(settings.current.scale);
          }
          slideX(across * 100);
          slideY(down * 100);
          glow(1);
          box.dataset.active = "true";
        };

        // Back to flat, unless the pointer or keyboard focus is still on the card
        const rest = () => {
          if (hovering || focused) return;
          leanX(0);
          leanY(0);
          grow(1);
          glow(0);
          delete box.dataset.active;
        };

        const onPointerEnter = (event: PointerEvent) => {
          hovering = true;
          follow(event);
        };
        const onPointerMove = (event: PointerEvent) => {
          hovering = true;
          follow(event);
        };
        const onPointerLeave = () => {
          hovering = false;
          rest();
        };
        // A finger lifting is the same as the cursor leaving
        const onPointerUp = (event: PointerEvent) => {
          if (event.pointerType === "mouse") return;
          hovering = false;
          rest();
        };

        // Keyboard: tabbing in lights the card up and lifts it, with no lean. A mouse click doesn't trigger this.
        const onFocusIn = (event: FocusEvent) => {
          if (!(event.target instanceof Element) || !event.target.matches(":focus-visible")) return;
          focused = true;
          if (!reduced) grow(settings.current.scale);
          slideX(50);
          slideY(30);
          glow(1);
          box.dataset.active = "true";
        };
        const onFocusOut = (event: FocusEvent) => {
          if (event.relatedTarget instanceof Node && box.contains(event.relatedTarget)) return;
          focused = false;
          rest();
        };

        box.addEventListener("pointerenter", onPointerEnter);
        box.addEventListener("pointermove", onPointerMove);
        box.addEventListener("pointerleave", onPointerLeave);
        box.addEventListener("pointercancel", onPointerLeave);
        box.addEventListener("pointerup", onPointerUp);
        box.addEventListener("focusin", onFocusIn);
        box.addEventListener("focusout", onFocusOut);

        return () => {
          box.removeEventListener("pointerenter", onPointerEnter);
          box.removeEventListener("pointermove", onPointerMove);
          box.removeEventListener("pointerleave", onPointerLeave);
          box.removeEventListener("pointercancel", onPointerLeave);
          box.removeEventListener("pointerup", onPointerUp);
          box.removeEventListener("focusin", onFocusIn);
          box.removeEventListener("focusout", onFocusOut);
          delete box.dataset.active;
          gsap.set(tilt, { clearProps: "transform" });
          light.style.opacity = "0";
        };
      });

      return () => mm.revert();
    },
    { scope: boxRef },
  );

  return (
    // The outer box holds the perspective and listens for the pointer. It never moves.
    // touch-action: pan-y lets a finger scroll the page up and down, while a sideways drag tilts the card.
    <div ref={boxRef} className={`relative inline-block data-[active=true]:z-10 ${className}`} style={{ perspective, touchAction: "pan-y" }}>
      {/* preserve-3d lets children use translateZ to float above the card and shift as it leans */}
      <div ref={tiltRef} className="relative block h-full w-full rounded-[inherit]" style={{ transformStyle: "preserve-3d", willChange: "transform" }}>
        {children}
        {/* The light: one soft white spot that follows the pointer. 1px forward so it sits over the card face but under anything floated higher. */}
        <div
          ref={glareRef}
          aria-hidden="true"
          className="pointer-events-none absolute inset-0 rounded-[inherit] opacity-0"
          style={
            {
              "--tilt-glare": glare,
              "--tilt-x": "50%",
              "--tilt-y": "50%",
              transform: "translateZ(1px)",
              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%)",
            } as CSSProperties
          }
        />
      </div>
    </div>
  );
}

The component. Installs to components/dimaac/mouse-tilt-card.tsx.

Install

With the shadcn CLI

npx shadcn@latest add https://ui.dimaac.com/r/mouse-tilt-card.json

It adds components/dimaac/mouse-tilt-card.tsx and installs what it imports. With the @dimaac namespace in components.json (Docs) it's just npx shadcn@latest add @dimaac/mouse-tilt-card.

By hand

npm install gsap @gsap/react
  1. Install the packages it uses: gsap, @gsap/react.
  2. Copy the component file below into components/dimaac/mouse-tilt-card.tsx.
  3. It's styled with Tailwind v4 classes. If that folder sits outside the places Tailwind scans, add it with @source in your CSS.
  4. Import it with import { MouseTiltCard } from "@/components/dimaac/mouse-tilt-card". Give its parent a size: it fills it.

Props

PropTypeDefaultDescription
children (required)ReactNode-What sits on the card. Anything works: an image, text, buttons.
maxTiltnumber12The most the card leans when the cursor is at an edge, in degrees.
perspectivenumber1000How strong the 3D is, in pixels. Smaller is more dramatic, larger is flatter.
glarenumber0.35How bright the light on the card is, from 0 (no light) to 1 (a hard white spot).
scalenumber1.03How much the card grows while it is active. 1 keeps its size.
classNamestring""Extra classes for the outer box. Add a rounded-* class so the light is clipped to your card's corners.

Use with AI

Agent prompt

Paste it into Claude Code, Cursor or any coding agent, or open it straight in a chat. It carries your Customize settings.

Prompt
Add the Mouse Tilt Card component (<MouseTiltCard />) from DiMaac UI to this project.

Docs: https://ui.dimaac.com/components/mouse-tilt-card
Source and dependencies (shadcn registry JSON): https://ui.dimaac.com/r/mouse-tilt-card.json

Install it with:
npx shadcn@latest add https://ui.dimaac.com/r/mouse-tilt-card.json

Then use it like this:
import { MouseTiltCard } from "@/components/dimaac/mouse-tilt-card";
<MouseTiltCard children={children} />

If the shadcn CLI isn't set up here, fetch the registry JSON above, write files[0].content to components/dimaac/mouse-tilt-card.tsx and install gsap, @gsap/react.
It uses Tailwind v4 classes, so make sure Tailwind scans that folder. Give its parent a size: it fills it. Keep light and dark mode working and keep its prefers-reduced-motion fallback.
Full index for agents: https://ui.dimaac.com/llms.txt

Files agents read

llms.txt
Every component and how to install it
mouse-tilt-card.md
This page as Markdown: install, props and the full source
mouse-tilt-card.json
The shadcn registry item

About

Wrap anything in it and it tilts in 3D toward the cursor while a soft light slides across its face. The pointer is read on a box that never moves, so the edge never flickers, and every value eases with GSAP quickTo so it stays smooth and settles back gently. Press and drag tilts it on touch, tabbing into it lifts and lights it for keyboard users, and it stays flat with only the light when the visitor prefers reduced motion. Children can use translateZ to float above the card and shift as it leans. Ported from the first DiMaac UI and rebuilt without re-rendering on every mouse move.

Installs to
components/dimaac/mouse-tilt-card.tsx
Uses
gsap, @gsap/react
Tags
gsap, cursor, 3d, card, glare

Orange visor portrait by West Kast on Lummi (Lummi License (free for personal and commercial use)). Amber perfume by Dona Mara on Lummi (Lummi License (free for personal and commercial use)). Misty hills by SHIHO on Lummi (Lummi License (free for personal and commercial use)).