Direction Aware Hover
aceternity/direction-aware-hoverFreeComponent
aceternity publishes no description for this item.
Install it
npx shadcn@latest add https://ui.aceternity.com/registry/direction-aware-hover.jsonSource
1"use client";23import { useRef, useState } from "react";45import { AnimatePresence, motion } from "motion/react";6import { cn } from "@/lib/utils";78export const DirectionAwareHover = ({9 imageUrl,10 children,11 childrenClassName,12 imageClassName,13 className,14}: {15 imageUrl: string;16 children: React.ReactNode | string;17 childrenClassName?: string;18 imageClassName?: string;19 className?: string;20}) => {21 const ref = useRef<HTMLDivElement>(null);2223 const [direction, setDirection] = useState<24 "top" | "bottom" | "left" | "right" | string25 >("left");2627 const handleMouseEnter = (28 event: React.MouseEvent<HTMLDivElement, MouseEvent>29 ) => {30 if (!ref.current) return;3132 const direction = getDirection(event, ref.current);33 console.log("direction", direction);34 switch (direction) {35 case 0:36 setDirection("top");37 break;38 case 1:39 setDirection("right");40 break;41 case 2:42 setDirection("bottom");43 break;44 case 3:45 setDirection("left");46 break;47 default:48 setDirection("left");49 break;50 }51 };5253 const getDirection = (54 ev: React.MouseEvent<HTMLDivElement, MouseEvent>,55 obj: HTMLElement56 ) => {57 const { width: w, height: h, left, top } = obj.getBoundingClientRect();58 const x = ev.clientX - left - (w / 2) * (w > h ? h / w : 1);59 const y = ev.clientY - top - (h / 2) * (h > w ? w / h : 1);60 const d = Math.round(Math.atan2(y, x) / 1.57079633 + 5) % 4;61 return d;62 };6364 return (65 <motion.div66 onMouseEnter={handleMouseEnter}67 ref={ref}68 className={cn(69 "md:h-96 w-60 h-60 md:w-96 bg-transparent rounded-lg overflow-hidden group/card relative",70 className71 )}72 >73 <AnimatePresence mode="wait">74 <motion.div75 className="relative h-full w-full"76 initial="initial"77 whileHover={direction}78 exit="exit"79 >80 <motion.div className="group-hover/card:block hidden absolute inset-0 w-full h-full bg-black/40 z-10 transition duration-500" />81 <motion.div82 variants={variants}83 className="h-full w-full relative bg-gray-50 dark:bg-black"84 transition={{85 duration: 0.2,86 ease: "easeOut",87 }}88 >89 <img90 alt="image"91 className={cn(92 "h-full w-full object-cover scale-[1.15]",93 imageClassName94// … truncated
What it pulls in
npm packages
Files it writes
More from aceternity
All 275 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| 3d Card3d-card | aceternity | Components | Free | no deps | |
| 3d Globe3d-globe | aceternity | Components | Free | 3 deps | |
| 3d Marquee3d-marquee | aceternity | Components | Free | no deps | |
| 3d Pin3d-pin | aceternity | Components | Free | 1 dep | |
| Animated Modalanimated-modal | aceternity | Components | Free | 1 dep | |
| Animated Testimonialsanimated-testimonials | aceternity | Components | Free | 2 deps | |
| Animated Tooltipanimated-tooltip | aceternity | Components | Free | 1 dep | |
| Apple Cards Carouselapple-cards-carousel | aceternity | Components | Free | 2 deps |
