A pointer-highlight component from Aceternity UI.
From Aceternity UI
1"use client";
2import { cn } from "@/lib/utils";
3import { motion } from "motion/react";
4import { useRef, useEffect, useState } from "react";
5
6export function PointerHighlight({
7 children,
8 rectangleClassName,
9 pointerClassName,
10 containerClassName,
11}: {
12 children: React.ReactNode;
13 rectangleClassName?: string;
14 pointerClassName?: string;
15 containerClassName?: string;
16}) {
17 const containerRef = useRef<HTMLDivElement>(null);
18 const [dimensions, setDimensions] = useState({ width: 0, height: 0 });
19
20 useEffect(() => {
21 if (containerRef.current) {
22 const { width, height } = containerRef.current.getBoundingClientRect();
23 setDimensions({ width, height });
24 }
25
26 const resizeObserver = new ResizeObserver((entries) => {
27 for (const entry of entries) {
28 const { width, height } = entry.contentRect;
29 setDimensions({ width, height });
30 }
31 });
32
33 if (containerRef.current) {
34 resizeObserver.observe(containerRef.current);
35 }
36
37 return () => {
38 if (containerRef.current) {
39 resizeObserver.unobserve(containerRef.current);
40 }
41 };
42 }, []);
43
44 return (
45 <div
46 className={cn("relative w-fit", containerClassName)}
47 ref={containerRef}
48 >
49 {children}
50 {dimensions.width > 0 && dimensions.height > 0 && (
51 <motion.div
52 className="pointer-events-none absolute inset-0 z-0"
53 initial={{ opacity: 0, scale: 0.95, originX: 0, originY: 0 }}
54 animate={{ opacity: 1, scale: 1 }}
55 transition={{ duration: 0.5, ease: "easeOut" }}
56 >
57 <motion.div
58 className={cn(
59 "absolute inset-0 border border-neutral-800 dark:border-neutral-200",
60 rectangleClassName,
61 )}
62 initial={{
63 width: 0,
64 height: 0,
65 }}
66 whileInView={{
67 width: dimensions.width,
68 height: dimensions.height,
69 }}
70 transition={{
71 duration: 1,
72 ease: "easeInOut",
73 }}
74 />
75 <motion.div
76 className="pointer-events-none absolute"
77 initial={{ opacity: 0 }}
78 whileInView={{
79 opacity: 1,
80 x: dimensions.width + 4,
81 y: dimensions.height + 4,
82 }}
83 style={{
84 rotate: -90,
85 }}
86 transition={{
87 opacity: { duration: 0.1, ease: "easeInOut" },
88 duration: 1,
89 ease: "easeInOut",
90 }}
91 >
92 <Pointer
93 className={cn("h-5 w-5 text-blue-500", pointerClassName)}
94 />
95 </motion.div>
96 </motion.div>
97 )}
98 </div>
99 );
100}
101
102const Pointer = ({ ...props }: React.SVGProps<SVGSVGElement>) => {
103 return (
104 <svg
105 stroke="currentColor"
106 fill="currentColor"
107 strokeWidth="1"
108 strokeLinecap="round"
109 strokeLinejoin="round"
110 viewBox="0 0 16 16"
111 height="1em"
112 width="1em"
113 xmlns="http://www.w3.org/2000/svg"
114 {...props}
115 >
116 <path d="M14.082 2.182a.5.5 0 0 1 .103.557L8.528 15.467a.5.5 0 0 1-.917-.007L5.57 10.694.803 8.652a.5.5 0 0 1-.006-.916l12.728-5.657a.5.5 0 0 1 .556.103z"></path>
117 </svg>
118 );
119};
120