A lens component from Aceternity UI.
From Aceternity UI
1"use client";
2
3import React, { useRef, useState } from "react";
4import { AnimatePresence, motion } from "motion/react";
5
6interface LensProps {
7 children: React.ReactNode;
8 zoomFactor?: number;
9 lensSize?: number;
10 position?: {
11 x: number;
12 y: number;
13 };
14 isStatic?: boolean;
15 isFocusing?: () => void;
16 hovering?: boolean;
17 setHovering?: (hovering: boolean) => void;
18}
19
20export const Lens: React.FC<LensProps> = ({
21 children,
22 zoomFactor = 1.5,
23 lensSize = 170,
24 isStatic = false,
25 position = { x: 200, y: 150 },
26 hovering,
27 setHovering,
28}) => {
29 const containerRef = useRef<HTMLDivElement>(null);
30
31 const [localIsHovering, setLocalIsHovering] = useState(false);
32
33 const isHovering = hovering !== undefined ? hovering : localIsHovering;
34 const setIsHovering = setHovering || setLocalIsHovering;
35
36 // const [isHovering, setIsHovering] = useState(false);
37 const [mousePosition, setMousePosition] = useState({ x: 100, y: 100 });
38
39 const handleMouseMove = (e: React.MouseEvent<HTMLDivElement>) => {
40 const rect = e.currentTarget.getBoundingClientRect();
41 const x = e.clientX - rect.left;
42 const y = e.clientY - rect.top;
43 setMousePosition({ x, y });
44 };
45
46 return (
47 <div
48 ref={containerRef}
49 className="relative overflow-hidden rounded-lg z-20"
50 onMouseEnter={() => {
51 setIsHovering(true);
52 }}
53 onMouseLeave={() => setIsHovering(false)}
54 onMouseMove={handleMouseMove}
55 >
56 {children}
57
58 {isStatic ? (
59 <div>
60 <motion.div
61 initial={{ opacity: 0, scale: 0.58 }}
62 animate={{ opacity: 1, scale: 1 }}
63 exit={{ opacity: 0, scale: 0.8 }}
64 transition={{ duration: 0.3, ease: "easeOut" }}
65 className="absolute inset-0 overflow-hidden"
66 style={{
67 maskImage: `radial-gradient(circle ${lensSize / 2}px at ${
68 position.x
69 }px ${position.y}px, black 100%, transparent 100%)`,
70 WebkitMaskImage: `radial-gradient(circle ${lensSize / 2}px at ${
71 position.x
72 }px ${position.y}px, black 100%, transparent 100%)`,
73 transformOrigin: `${position.x}px ${position.y}px`,
74 }}
75 >
76 <div
77 className="absolute inset-0"
78 style={{
79 transform: `scale(${zoomFactor})`,
80 transformOrigin: `${position.x}px ${position.y}px`,
81 }}
82 >
83 {children}
84 </div>
85 </motion.div>
86 </div>
87 ) : (
88 <AnimatePresence>
89 {isHovering && (
90 <div>
91 <motion.div
92 initial={{ opacity: 0, scale: 0.58 }}
93 animate={{ opacity: 1, scale: 1 }}
94 exit={{ opacity: 0, scale: 0.8 }}
95 transition={{ duration: 0.3, ease: "easeOut" }}
96 className="absolute inset-0 overflow-hidden"
97 style={{
98 maskImage: `radial-gradient(circle ${lensSize / 2}px at ${
99 mousePosition.x
100 }px ${mousePosition.y}px, black 100%, transparent 100%)`,
101 WebkitMaskImage: `radial-gradient(circle ${
102 lensSize / 2
103 }px at ${mousePosition.x}px ${
104 mousePosition.y
105 }px, black 100%, transparent 100%)`,
106 transformOrigin: `${mousePosition.x}px ${mousePosition.y}px`,
107 zIndex: 50,
108 }}
109 >
110 <div
111 className="absolute inset-0"
112 style={{
113 transform: `scale(${zoomFactor})`,
114 transformOrigin: `${mousePosition.x}px ${mousePosition.y}px`,
115 }}
116 >
117 {children}
118 </div>
119 </motion.div>
120 </div>
121 )}
122 </AnimatePresence>
123 )}
124 </div>
125 );
126};
127