-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathSpotlightCard.tsx
More file actions
77 lines (66 loc) · 2.17 KB
/
SpotlightCard.tsx
File metadata and controls
77 lines (66 loc) · 2.17 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import React, { useRef, useState } from 'react';
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 [isFocused, setIsFocused] = useState<boolean>(false);
const [position, setPosition] = useState<Position>({ x: 0, y: 0 });
const [opacity, setOpacity] = useState<number>(0);
const handleMouseMove: React.MouseEventHandler<HTMLDivElement> = e => {
if (!divRef.current || isFocused) return;
const rect = divRef.current.getBoundingClientRect();
setPosition({ x: e.clientX - rect.left, y: e.clientY - rect.top });
};
const handleFocus = () => {
setIsFocused(true);
setOpacity(0.6);
};
const handleBlur = () => {
setIsFocused(false);
setOpacity(0);
};
const handleMouseEnter = (e: React.MouseEvent<HTMLDivElement>) => {
setOpacity(0.6);
e.currentTarget.style.transform = "scale(1.04)";
e.currentTarget.style.boxShadow = "0 20px 40px rgba(0,0,0,0.25)";
};
const handleMouseLeave = (e: React.MouseEvent<HTMLDivElement>) => {
setOpacity(0);
e.currentTarget.style.transform = "scale(1)";
e.currentTarget.style.boxShadow = "none";
};
return (
<div
ref={divRef}
onMouseMove={handleMouseMove}
onFocus={handleFocus}
onBlur={handleBlur}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
style={{
transition: "transform 0.3s ease, box-shadow 0.3s ease"
}}
className={`relative rounded-3xl border border-neutral-800 bg-neutral-900 overflow-hidden p-8 ${className}`}
>
<div
className="pointer-events-none absolute inset-0 transition-opacity duration-500 ease-in-out"
style={{
opacity,
background: `radial-gradient(circle at ${position.x}px ${position.y}px, ${spotlightColor}, transparent 80%)`
}}
/>
{children}
</div>
);
};
export default SpotlightCard;