-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathSpotlightCard.tsx
More file actions
59 lines (49 loc) · 1.61 KB
/
SpotlightCard.tsx
File metadata and controls
59 lines (49 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React, { useRef } from 'react';
import './SpotlightCard.css';
interface Position {
x: number;
y: number;
}
interface SpotlightCardProps extends React.PropsWithChildren {
className?: string;
spotlightColor?: `rgba(${number}, ${number}, ${number}, ${number})`;
}
const SpotlightCard: React.FC<SpotlightCardProps> = ({
children,
className = '',
spotlightColor = 'rgba(255, 255, 255, 0.25)'
}) => {
const divRef = useRef<HTMLDivElement>(null);
const handleMouseMove: React.MouseEventHandler<HTMLDivElement> = e => {
if (!divRef.current) return;
const rect = divRef.current.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
divRef.current.style.setProperty('--mouse-x', `${x}px`);
divRef.current.style.setProperty('--mouse-y', `${y}px`);
divRef.current.style.setProperty('--spotlight-color', spotlightColor);
};
const handleMouseEnter: React.MouseEventHandler<HTMLDivElement> = (e) => {
e.currentTarget.style.transform = "scale(1.04)";
e.currentTarget.style.boxShadow = "0 20px 40px rgba(0,0,0,0.25)";
};
const handleMouseLeave: React.MouseEventHandler<HTMLDivElement> = (e) => {
e.currentTarget.style.transform = "scale(1)";
e.currentTarget.style.boxShadow = "none";
};
return (
<div
ref={divRef}
onMouseMove={handleMouseMove}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
style={{
transition: "transform 0.3s ease, box-shadow 0.3s ease"
}}
className={`card-spotlight ${className}`}
>
{children}
</div>
);
};
export default SpotlightCard;