Light tilt card
A card that tilts gently toward the pointer while a soft light follows it, brightening the border and adding a faint glare on the surface. Hidden content such as specs or a call to action slides up from the bottom on hover, focus or tap, and the main content lifts forward for depth. Three intensities run from quiet (border light only, no tilt) to bold. It renders as a div, link or button, the light is drawn from your primary colour so it works in light and dark, and the hidden content is always reachable by keyboard. Touch screens get tap-to-reveal without tilt, and reduced motion keeps it flat with a still light.
Source
import { useEffect, useRef, useState, type CSSProperties, type ElementType, type MouseEvent, type PointerEvent, type ReactNode } from "react";
import { cn } from "@/lib/utils";
type Intensity = "quiet" | "normal" | "bold";
export type LightTiltCardProps = {
children: ReactNode;
/** Hidden content that slides up on hover, focus or tap. Keep it free of links when `as` is "a" or "button". */
reveal?: ReactNode;
intensity?: Intensity;
/** Largest tilt in degrees (quiet never tilts). */
maxTilt?: number;
glare?: boolean;
as?: "div" | "a" | "button";
href?: string;
onClick?: (event: MouseEvent<HTMLElement>) => void;
/** Start in the lit, revealed state (useful for showcases); the pointer takes over once it moves. */
defaultActive?: boolean;
"aria-label"?: string;
className?: string;
/** Classes for the card surface itself (padding, min-height). */
cardClassName?: string;
};
const PRESETS: Record<Intensity, { tilt: number; border: number; glare: number; lift: number; radius: number }> = {
quiet: { tilt: 0, border: 55, glare: 0, lift: 0, radius: 220 },
normal: { tilt: 0.75, border: 75, glare: 5, lift: 14, radius: 260 },
bold: { tilt: 1, border: 100, glare: 8, lift: 22, radius: 320 },
};
const focusRing = "outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background";
function useReducedMotion() {
const [reduced, setReduced] = useState(false);
useEffect(() => {
const list = window.matchMedia("(prefers-reduced-motion: reduce)");
const update = () => setReduced(list.matches);
update();
list.addEventListener("change", update);
return () => list.removeEventListener("change", update);
}, []);
return reduced;
}
export function LightTiltCard({
children,
reveal,
intensity = "normal",
maxTilt = 8,
glare = true,
as = "div",
href,
onClick,
defaultActive = false,
className,
cardClassName,
...aria
}: LightTiltCardProps) {
const preset = PRESETS[intensity];
const reduced = useReducedMotion();
const cardRef = useRef<HTMLElement>(null);
const frame = useRef(0);
const pointer = useRef({ x: 0, y: 0 });
const lastPointerType = useRef("mouse");
const [hovered, setHovered] = useState(defaultActive);
const [tapped, setTapped] = useState(false);
const tiltLimit = reduced ? 0 : Math.min(maxTilt, 8) * preset.tilt;
const apply = (px: number, py: number) => {
const card = cardRef.current;
if (!card) return;
card.style.setProperty("--mx", `${(px * 100).toFixed(1)}%`);
card.style.setProperty("--my", `${(py * 100).toFixed(1)}%`);
card.style.setProperty("--ry", `${((px - 0.5) * 2 * tiltLimit).toFixed(2)}deg`);
card.style.setProperty("--rx", `${(-(py - 0.5) * 2 * tiltLimit).toFixed(2)}deg`);
};
useEffect(() => {
if (reduced) apply(0, 0);
else if (defaultActive) apply(0.78, 0.16);
return () => cancelAnimationFrame(frame.current);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [reduced]);
const onPointerMove = (event: PointerEvent<HTMLElement>) => {
lastPointerType.current = event.pointerType;
if (event.pointerType === "touch" || reduced) return;
pointer.current = { x: event.clientX, y: event.clientY };
if (frame.current) return;
frame.current = requestAnimationFrame(() => {
frame.current = 0;
const box = cardRef.current?.getBoundingClientRect();
if (!box) return;
apply(
Math.min(1, Math.max(0, (pointer.current.x - box.left) / box.width)),
Math.min(1, Math.max(0, (pointer.current.y - box.top) / box.height)),
);
});
};
const onPointerEnter = (event: PointerEvent<HTMLElement>) => {
lastPointerType.current = event.pointerType;
if (event.pointerType !== "touch") setHovered(true);
};
const onPointerLeave = (event: PointerEvent<HTMLElement>) => {
if (event.pointerType === "touch") return;
setHovered(false);
cancelAnimationFrame(frame.current);
frame.current = 0;
cardRef.current?.style.setProperty("--rx", "0deg");
cardRef.current?.style.setProperty("--ry", "0deg");
};
const handleClick = (event: MouseEvent<HTMLElement>) => {
// On touch the first tap shows the hidden content; the next one acts.
if (lastPointerType.current === "touch" && reveal && !tapped) {
setTapped(true);
if (as !== "div") event.preventDefault();
return;
}
if (as === "div" && lastPointerType.current === "touch") setTapped((value) => !value);
onClick?.(event);
};
const active = hovered || tapped;
const Tag = as as ElementType;
const style = {
"--mx": "0%",
"--my": "0%",
"--rx": "0deg",
"--ry": "0deg",
"--lift": active ? `${reduced ? 0 : preset.lift}px` : "0px",
transform: "rotateX(var(--rx)) rotateY(var(--ry))",
transformStyle: "preserve-3d",
} as CSSProperties;
const lightColor = (amount: number) => `color-mix(in oklab, var(--primary) ${amount}%, transparent)`;
return (
<div className={cn("[perspective:1100px]", className)}>
<Tag
ref={cardRef}
href={as === "a" ? href : undefined}
type={as === "button" ? "button" : undefined}
onClick={handleClick}
onPointerEnter={onPointerEnter}
onPointerMove={onPointerMove}
onPointerLeave={onPointerLeave}
data-active={active || undefined}
className={cn(
"group relative block h-full w-full rounded-xl border border-border bg-card text-left text-card-foreground shadow-sm",
"motion-safe:transition-[transform,box-shadow] motion-safe:duration-500 motion-safe:ease-[cubic-bezier(.2,.8,.2,1)]",
active && "shadow-[0_24px_48px_-24px_rgb(0_0_0/0.35)]",
as !== "div" && focusRing,
as !== "div" && "cursor-pointer",
)}
style={style}
{...aria}
>
{/* Border light: a radial gradient shown only in the 1px ring around the card. */}
<span
aria-hidden="true"
className={cn(
"pointer-events-none absolute -inset-px rounded-[inherit] p-px transition-opacity duration-300",
active || reduced ? "opacity-100" : "opacity-0 group-focus-within:opacity-100",
)}
style={{
background: `radial-gradient(${preset.radius}px circle at var(--mx) var(--my), ${lightColor(preset.border)}, transparent 70%)`,
mask: "linear-gradient(#000 0 0) content-box exclude, linear-gradient(#000 0 0)",
WebkitMask: "linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0)",
WebkitMaskComposite: "xor",
}}
/>
{glare && preset.glare > 0 && (
<span
aria-hidden="true"
className={cn("pointer-events-none absolute inset-0 rounded-[inherit] transition-opacity duration-300", active ? "opacity-100" : "opacity-0")}
style={{ background: `radial-gradient(${preset.radius * 1.4}px circle at var(--mx) var(--my), ${lightColor(preset.glare)}, transparent 65%)` }}
/>
)}
<div
className={cn("relative h-full motion-safe:transition-transform motion-safe:duration-500 motion-safe:ease-[cubic-bezier(.2,.8,.2,1)]", cardClassName ?? "p-6")}
style={{ transform: "translateZ(var(--lift))" }}
>
{children}
</div>
{reveal && (
<div
className={cn(
"absolute inset-x-3 bottom-3 rounded-lg border border-border bg-card/95 p-4 shadow-lg backdrop-blur-sm",
"transition-opacity duration-300 motion-safe:transition-[opacity,translate] motion-safe:duration-[300ms,450ms] motion-safe:ease-[cubic-bezier(.2,.8,.2,1)]",
active
? "pointer-events-auto translate-y-0 opacity-100"
: "pointer-events-none translate-y-3 opacity-0 group-focus-within:pointer-events-auto group-focus-within:translate-y-0 group-focus-within:opacity-100",
)}
style={{ transform: "translateZ(calc(var(--lift) + 2px))" }}
>
{reveal}
</div>
)}
</Tag>
</div>
);
}
