-
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathsafe-hover.tsx
More file actions
141 lines (129 loc) · 3.56 KB
/
Copy pathsafe-hover.tsx
File metadata and controls
141 lines (129 loc) · 3.56 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import Trigger from '@rc-component/trigger';
import type { TriggerRef } from '@rc-component/trigger';
import React, { useState } from 'react';
import { getSafeHoverAreaPolygons } from '../../src/util/safeHover';
import type { SafeHoverPoint } from '../../src/util/safeHover';
import '../../assets/index.less';
interface SafeHoverPolygon {
points: SafeHoverPoint[];
fill: string;
stroke: string;
}
const safeHoverPolygonStyles = [
{
fill: 'rgba(255, 176, 32, 0.22)',
stroke: 'rgba(222, 121, 0, 0.6)',
},
{
fill: 'rgba(22, 119, 255, 0.16)',
stroke: 'rgba(22, 119, 255, 0.55)',
},
];
const builtinPlacements = {
top: {
points: ['bc', 'tc'],
offset: [0, -56],
},
};
const popupStyle: React.CSSProperties = {
width: 240,
padding: 12,
background: '#fff',
border: '1px solid #d9d9d9',
boxShadow: '0 6px 16px rgba(0, 0, 0, 0.12)',
};
const SafeHoverDemo: React.FC = () => {
const triggerRef = React.useRef<TriggerRef>(null);
const [safeHoverPolygons, setPolygons] = useState<SafeHoverPolygon[]>([]);
const updateSafeHoverPolygons = (
event: React.MouseEvent<HTMLElement> | React.PointerEvent<HTMLElement>,
) => {
const target = triggerRef.current?.nativeElement;
const popup = triggerRef.current?.popupElement;
if (!target || !popup) {
setPolygons([]);
return;
}
const leavePoint: SafeHoverPoint = [event.clientX, event.clientY];
setPolygons(
getSafeHoverAreaPolygons(
leavePoint,
target.getBoundingClientRect(),
popup.getBoundingClientRect(),
).map((points, i) => ({ points, ...safeHoverPolygonStyles[i] })),
);
};
return (
<div style={{ minHeight: 320, padding: '160px 80px 80px' }}>
{safeHoverPolygons.length > 0 && (
<svg
aria-hidden
style={{
position: 'fixed',
inset: 0,
width: '100vw',
height: '100vh',
pointerEvents: 'none',
zIndex: 999,
}}
>
{safeHoverPolygons.map(({ points, fill, stroke }, index) => {
return (
<polygon
// eslint-disable-next-line react/no-array-index-key
key={`polygon-${index}`}
points={points.map((point) => point.join(',')).join(' ')}
fill={fill}
stroke={stroke}
strokeDasharray="4 3"
strokeWidth={1}
/>
);
})}
</svg>
)}
<Trigger
ref={triggerRef}
action={['hover']}
mouseLeaveDelay={0.12}
popupPlacement="top"
builtinPlacements={builtinPlacements}
popupStyle={popupStyle}
onOpenChange={(nextOpen) => {
if (!nextOpen) {
setPolygons([]);
}
}}
popup={
<div onMouseEnter={() => setPolygons([])}>
<strong>Safe hover popup</strong>
<div style={{ marginTop: 8 }}>
Move through the gap to reach me.
</div>
</div>
}
>
<button
style={{ padding: '8px 16px' }}
type="button"
onMouseLeave={updateSafeHoverPolygons}
onPointerLeave={updateSafeHoverPolygons}
>
trigger
</button>
</Trigger>
<div
style={{
width: 240,
marginTop: 16,
color: '#666',
fontSize: 13,
lineHeight: 1.5,
}}
>
The popup is offset upward, leaving a blank hover gap.
</div>
</div>
);
};
export default SafeHoverDemo;