|
| 1 | +"use client"; |
| 2 | +import Image from "next/image"; |
| 3 | +import React, { useState } from "react"; |
| 4 | +import { |
| 5 | + motion, |
| 6 | + useTransform, |
| 7 | + AnimatePresence, |
| 8 | + useMotionValue, |
| 9 | + useSpring, |
| 10 | +} from "framer-motion"; |
| 11 | + |
| 12 | +export const AnimatedTooltip = ({ |
| 13 | + items, |
| 14 | +}: { |
| 15 | + items: { |
| 16 | + id: number; |
| 17 | + name: string; |
| 18 | + designation: string; |
| 19 | + image: string; |
| 20 | + }[]; |
| 21 | +}) => { |
| 22 | + const [hoveredIndex, setHoveredIndex] = useState<number | null>(null); |
| 23 | + const springConfig = { stiffness: 100, damping: 5 }; |
| 24 | + const x = useMotionValue(0); // going to set this value on mouse move |
| 25 | + // rotate the tooltip |
| 26 | + const rotate = useSpring( |
| 27 | + useTransform(x, [-100, 100], [-45, 45]), |
| 28 | + springConfig |
| 29 | + ); |
| 30 | + // translate the tooltip |
| 31 | + const translateX = useSpring( |
| 32 | + useTransform(x, [-100, 100], [-50, 50]), |
| 33 | + springConfig |
| 34 | + ); |
| 35 | + const handleMouseMove = (event: any) => { |
| 36 | + const halfWidth = event.target.offsetWidth / 2; |
| 37 | + x.set(event.nativeEvent.offsetX - halfWidth); // set the x value, which is then used in transform and rotate |
| 38 | + }; |
| 39 | + |
| 40 | + return ( |
| 41 | + <> |
| 42 | + {items.map((item, idx) => ( |
| 43 | + <div |
| 44 | + className="-mr-4 relative group" |
| 45 | + key={item.name} |
| 46 | + onMouseEnter={() => setHoveredIndex(item.id)} |
| 47 | + onMouseLeave={() => setHoveredIndex(null)} |
| 48 | + > |
| 49 | + <AnimatePresence mode="wait"> |
| 50 | + {hoveredIndex === item.id && ( |
| 51 | + <motion.div |
| 52 | + initial={{ opacity: 0, y: 20, scale: 0.6 }} |
| 53 | + animate={{ |
| 54 | + opacity: 1, |
| 55 | + y: 0, |
| 56 | + scale: 1, |
| 57 | + transition: { |
| 58 | + type: "spring", |
| 59 | + stiffness: 260, |
| 60 | + damping: 10, |
| 61 | + }, |
| 62 | + }} |
| 63 | + exit={{ opacity: 0, y: 20, scale: 0.6 }} |
| 64 | + style={{ |
| 65 | + translateX: translateX, |
| 66 | + rotate: rotate, |
| 67 | + whiteSpace: "nowrap", |
| 68 | + }} |
| 69 | + className="absolute -top-16 -left-1/2 translate-x-1/2 flex text-xs flex-col items-center justify-center rounded-md bg-black z-50 shadow-xl px-4 py-2" |
| 70 | + > |
| 71 | + <div className="absolute inset-x-10 z-30 w-[20%] -bottom-px bg-gradient-to-r from-transparent via-emerald-500 to-transparent h-px " /> |
| 72 | + <div className="absolute left-10 w-[40%] z-30 -bottom-px bg-gradient-to-r from-transparent via-sky-500 to-transparent h-px " /> |
| 73 | + <div className="font-bold text-white relative z-30 text-base"> |
| 74 | + {item.name} |
| 75 | + </div> |
| 76 | + <div className="text-white text-xs">{item.designation}</div> |
| 77 | + </motion.div> |
| 78 | + )} |
| 79 | + </AnimatePresence> |
| 80 | + <Image |
| 81 | + onMouseMove={handleMouseMove} |
| 82 | + height={100} |
| 83 | + width={100} |
| 84 | + src={item.image} |
| 85 | + alt={item.name} |
| 86 | + className="object-cover !m-0 !p-0 object-top rounded-full h-14 w-14 border-2 group-hover:scale-105 group-hover:z-30 border-white relative transition duration-500" |
| 87 | + /> |
| 88 | + </div> |
| 89 | + ))} |
| 90 | + </> |
| 91 | + ); |
| 92 | +}; |
0 commit comments