Card Tilt
scrollxui/card-tiltFreeComponent
smooth card tilt that responds naturally to your cursor, giving the card a more polished, modern look.
Live preview
live · rendered by the registry
Rendered by scrollxui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://scrollxui.dev/registry/card-tilt.jsonSource
1'use client';23import * as React from 'react';4import {5 motion,6 useMotionValue,7 useSpring,8 useTransform,9 MotionValue,10} from 'motion/react';11import { cn } from '@/lib/utils';1213interface CardTiltProps {14 children: React.ReactNode;15 className?: string;16 tiltMaxAngle?: number;17 tiltReverse?: boolean;18 glareEnable?: boolean;19 scale?: number;20}2122interface CardTiltContentProps {23 children: React.ReactNode;24 className?: string;25}2627const CardTiltContext = React.createContext<{28 rotateX: MotionValue<number>;29 rotateY: MotionValue<number>;30 scale: MotionValue<number>;31} | null>(null);3233const CardTilt = React.forwardRef<HTMLDivElement, CardTiltProps>(34 (35 {36 children,37 className,38 tiltMaxAngle = 15,39 tiltReverse = false,40 scale = 1.05,41 ...props42 },43 forwardedRef,44 ) => {45 const containerRef = React.useRef<HTMLDivElement>(null);46 const x = useMotionValue(0);47 const y = useMotionValue(0);4849 const mouseXSpring = useSpring(x, { stiffness: 300, damping: 30 });50 const mouseYSpring = useSpring(y, { stiffness: 300, damping: 30 });5152 const rotateX = useTransform(53 mouseYSpring,54 [-0.5, 0.5],55 tiltReverse56 ? [tiltMaxAngle, -tiltMaxAngle]57 : [-tiltMaxAngle, tiltMaxAngle],58 );59 const rotateY = useTransform(60 mouseXSpring,61 [-0.5, 0.5],62 tiltReverse63 ? [-tiltMaxAngle, tiltMaxAngle]64 : [tiltMaxAngle, -tiltMaxAngle],65 );6667 const scaleValue = useSpring(1, { stiffness: 300, damping: 30 });6869 const handleMouseMove = (e: React.MouseEvent<HTMLDivElement>) => {70 if (!containerRef.current) return;7172 const rect = containerRef.current.getBoundingClientRect();73 const width = rect.width;74 const height = rect.height;75 const mouseX = e.clientX - rect.left;76 const mouseY = e.clientY - rect.top;77 const xPct = mouseX / width - 0.5;78 const yPct = mouseY / height - 0.5;7980 x.set(xPct);81 y.set(yPct);82 scaleValue.set(scale);83 };8485 const handleMouseLeave = () => {86 x.set(0);87 y.set(0);88 scaleValue.set(1);89 };9091 React.useImperativeHandle(forwardedRef, () => containerRef.current!);9293 return (94 <CardTiltContext.Provider value={{ rotateX, rotateY, scale: scaleValue }}>95 <div96 ref={containerRef}97 onMouseMove={handleMouseMove}98 onMouseLeave={handleMouseLeave}99 className={cn('relative inline-block', className)}100// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from scrollxui
All 180 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | scrollxui | Components | Free | 4 deps | |
| Alert Dialogalert-dialog | scrollxui | Components | Free | 7 deps | |
| Animated Buttonanimated-button | scrollxui | Components | Free | 4 deps | |
| Animated Tabsanimated-tabs | scrollxui | Components | Free | 1 dep | |
| Animated Testimonialsanimated-testimonials | scrollxui | Components | Free | 2 deps | |
| Animated TextGenerateanimated-textgenerate | scrollxui | Components | Free | 3 deps | |
| Announcementannouncement | scrollxui | Components | Free | 5 deps | |
| Aspect Ratioaspect-ratio | scrollxui | Components | Free | 1 dep |
