A draggable-card component from Aceternity UI.
From Aceternity UI
1"use client";
2import { cn } from "@/lib/utils";
3import React, { useRef, useState, useEffect } from "react";
4import {
5 motion,
6 useMotionValue,
7 useSpring,
8 useTransform,
9 animate,
10 useVelocity,
11 useAnimationControls,
12} from "motion/react";
13
14export const DraggableCardBody = ({
15 className,
16 children,
17}: {
18 className?: string;
19 children?: React.ReactNode;
20}) => {
21 const mouseX = useMotionValue(0);
22 const mouseY = useMotionValue(0);
23 const cardRef = useRef<HTMLDivElement>(null);
24 const controls = useAnimationControls();
25 const [constraints, setConstraints] = useState({
26 top: 0,
27 left: 0,
28 right: 0,
29 bottom: 0,
30 });
31
32 // physics biatch
33 const velocityX = useVelocity(mouseX);
34 const velocityY = useVelocity(mouseY);
35
36 const springConfig = {
37 stiffness: 100,
38 damping: 20,
39 mass: 0.5,
40 };
41
42 const rotateX = useSpring(
43 useTransform(mouseY, [-300, 300], [25, -25]),
44 springConfig,
45 );
46 const rotateY = useSpring(
47 useTransform(mouseX, [-300, 300], [-25, 25]),
48 springConfig,
49 );
50
51 const opacity = useSpring(
52 useTransform(mouseX, [-300, 0, 300], [0.8, 1, 0.8]),
53 springConfig,
54 );
55
56 const glareOpacity = useSpring(
57 useTransform(mouseX, [-300, 0, 300], [0.2, 0, 0.2]),
58 springConfig,
59 );
60
61 useEffect(() => {
62 // Update constraints when component mounts or window resizes
63 const updateConstraints = () => {
64 if (typeof window !== "undefined") {
65 setConstraints({
66 top: -window.innerHeight / 2,
67 left: -window.innerWidth / 2,
68 right: window.innerWidth / 2,
69 bottom: window.innerHeight / 2,
70 });
71 }
72 };
73
74 updateConstraints();
75
76 // Add resize listener
77 window.addEventListener("resize", updateConstraints);
78
79 // Clean up
80 return () => {
81 window.removeEventListener("resize", updateConstraints);
82 };
83 }, []);
84
85 const handleMouseMove = (e: React.MouseEvent<HTMLDivElement>) => {
86 const { clientX, clientY } = e;
87 const { width, height, left, top } =
88 cardRef.current?.getBoundingClientRect() ?? {
89 width: 0,
90 height: 0,
91 left: 0,
92 top: 0,
93 };
94 const centerX = left + width / 2;
95 const centerY = top + height / 2;
96 const deltaX = clientX - centerX;
97 const deltaY = clientY - centerY;
98 mouseX.set(deltaX);
99 mouseY.set(deltaY);
100 };
101
102 const handleMouseLeave = () => {
103 mouseX.set(0);
104 mouseY.set(0);
105 };
106
107 return (
108 <motion.div
109 ref={cardRef}
110 drag
111 dragConstraints={constraints}
112 onDragStart={() => {
113 document.body.style.cursor = "grabbing";
114 }}
115 onDragEnd={(event, info) => {
116 document.body.style.cursor = "default";
117
118 controls.start({
119 rotateX: 0,
120 rotateY: 0,
121 transition: {
122 type: "spring",
123 ...springConfig,
124 },
125 });
126 const currentVelocityX = velocityX.get();
127 const currentVelocityY = velocityY.get();
128
129 const velocityMagnitude = Math.sqrt(
130 currentVelocityX * currentVelocityX +
131 currentVelocityY * currentVelocityY,
132 );
133 const bounce = Math.min(0.8, velocityMagnitude / 1000);
134
135 animate(info.point.x, info.point.x + currentVelocityX * 0.3, {
136 duration: 0.8,
137 ease: [0.2, 0, 0, 1],
138 bounce,
139 type: "spring",
140 stiffness: 50,
141 damping: 15,
142 mass: 0.8,
143 });
144
145 animate(info.point.y, info.point.y + currentVelocityY * 0.3, {
146 duration: 0.8,
147 ease: [0.2, 0, 0, 1],
148 bounce,
149 type: "spring",
150 stiffness: 50,
151 damping: 15,
152 mass: 0.8,
153 });
154 }}
155 style={{
156 rotateX,
157 rotateY,
158 opacity,
159 willChange: "transform",
160 }}
161 animate={controls}
162 whileHover={{ scale: 1.02 }}
163 onMouseMove={handleMouseMove}
164 onMouseLeave={handleMouseLeave}
165 className={cn(
166 "relative min-h-96 w-80 overflow-hidden rounded-md bg-neutral-100 p-6 shadow-2xl transform-3d dark:bg-neutral-900",
167 className,
168 )}
169 >
170 {children}
171 <motion.div
172 style={{
173 opacity: glareOpacity,
174 }}
175 className="pointer-events-none absolute inset-0 bg-white select-none"
176 />
177 </motion.div>
178 );
179};
180
181export const DraggableCardContainer = ({
182 className,
183 children,
184}: {
185 className?: string;
186 children?: React.ReactNode;
187}) => {
188 return (
189 <div className={cn("[perspective:3000px]", className)}>{children}</div>
190 );
191};
192