跳至正文
获取 Pro
动效组件M.13

微倾斜卡片

FreeRegistry
components/linyi/tilt-card.tsx
motion
"use client";
 
import * as React from "react";
import {
motion,
useMotionTemplate,
useMotionValue,
useReducedMotion,
useSpring,
type HTMLMotionProps,
} from "motion/react";
import { cn } from "@/lib/cn";
 
export interface TiltCardProps extends Omit<HTMLMotionProps<"div">, "children"> {
/** Maximum tilt angle in degrees along X and Y axes. */
maxTilt?: number;
/** CSS perspective distance in pixels. */
perspective?: number;
/** Render pointer-following specular glint overlay. */
glint?: boolean;
children: React.ReactNode;
}
 
export function TiltCard({
maxTilt = 12,
perspective = 720,
glint = true,
className,
children,
onPointerMove,
onPointerLeave,
style,
...props
}: TiltCardProps) {
const prefersReducedMotion = useReducedMotion();
 
const rawRotateX = useMotionValue(0);
const rawRotateY = useMotionValue(0);
const glintX = useMotionValue(50);
const glintY = useMotionValue(25);
const glintOpacity = useMotionValue(0);
 
const rotateX = useSpring(rawRotateX, { stiffness: 260, damping: 24, mass: 0.7 });
const rotateY = useSpring(rawRotateY, { stiffness: 260, damping: 24, mass: 0.7 });
const smoothGlintOpacity = useSpring(glintOpacity, { stiffness: 240, damping: 28 });
 
const glintBackground = useMotionTemplate`radial-gradient(circle 120px at ${glintX}% ${glintY}%, rgba(var(--accent-rgb), 0.14), rgba(255, 255, 255, 0.12) 42%, transparent 72%)`;
 
const handlePointerMove = (e: React.PointerEvent<HTMLDivElement>) => {
onPointerMove?.(e);
if (prefersReducedMotion) return;
 
const rect = e.currentTarget.getBoundingClientRect();
if (!rect.width || !rect.height) return;
 
const px = Math.min(1, Math.max(0, (e.clientX - rect.left) / rect.width));
const py = Math.min(1, Math.max(0, (e.clientY - rect.top) / rect.height));
 
rawRotateX.set((0.5 - py) * maxTilt * 2);
rawRotateY.set((px - 0.5) * maxTilt * 2);
glintX.set(px * 100);
glintY.set(py * 100);
glintOpacity.set(1);
};
 
const handlePointerLeave = (e: React.PointerEvent<HTMLDivElement>) => {
onPointerLeave?.(e);
rawRotateX.set(0);
rawRotateY.set(0);
glintOpacity.set(0);
};
 
return (
<motion.div
onPointerMove={handlePointerMove}
onPointerLeave={handlePointerLeave}
style={{
rotateX: prefersReducedMotion ? 0 : rotateX,
rotateY: prefersReducedMotion ? 0 : rotateY,
transformPerspective: perspective,
transformStyle: "preserve-3d",
...style,
}}
className={cn(
"relative overflow-hidden rounded-panel border border-line-strong bg-surface text-fg shadow-panel will-change-transform",
className
)}
{...props}
>
{/* Top 1px specular highlight edge */}
<span
aria-hidden="true"
className="pointer-events-none absolute inset-x-0 top-0 z-20 h-px bg-gradient-to-r from-transparent via-white/80 to-transparent dark:via-white/25"
/>
 
{/* Moving pointer glint */}
{glint && !prefersReducedMotion && (
<motion.span
aria-hidden="true"
className="pointer-events-none absolute inset-0 z-10"
style={{
background: glintBackground,
opacity: smoothGlintOpacity,
}}
/>
)}
 
<div className="relative z-10 h-full w-full">{children}</div>
</motion.div>
);
}