/
Tooltip.js
181 lines (146 loc) · 4.37 KB
/
Tooltip.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import {
useRef,
useEffect,
useState
} from 'preact/hooks';
import { useTooltipContext } from '../../hooks/useTooltipContext';
import { createPortal } from 'preact/compat';
/**
* @param {Object} props
* @param {String} props.forId
* @param {String} props.value
*/
export default function TooltipWrapper(props) {
const {
forId,
element
} = props;
const contextDescription = useTooltipContext(forId, element);
const value = props.value || contextDescription;
if (!value) {
return props.children;
}
return <Tooltip { ...props } value={ value } forId={ prefixId(forId) } />;
}
function Tooltip(props) {
const {
forId,
value,
parent,
direction = 'right',
position
} = props;
const [ visible, setShow ] = useState(false);
const [ focusedViaKeyboard, setFocusedViaKeyboard ] = useState(false);
let timeout = null;
const wrapperRef = useRef(null);
const tooltipRef = useRef(null);
const showTooltip = async (event) => {
const show = () => setShow(true);
if (!visible && !timeout) {
if (event instanceof MouseEvent) {
timeout = setTimeout(show, 200);
} else {
show();
setFocusedViaKeyboard(true);
}
}
};
const hideTooltip = () => {
setShow(false);
setFocusedViaKeyboard(false);
};
const hideTooltipViaEscape = (e) => {
e.code === 'Escape' && hideTooltip();
};
const isTooltipHovered = ({ x, y }) => {
const tooltip = tooltipRef.current;
const wrapper = wrapperRef.current;
return tooltip && (
inBounds(x, y, wrapper.getBoundingClientRect()) ||
inBounds(x, y, tooltip.getBoundingClientRect())
);
};
useEffect(() => {
const { current } = wrapperRef;
if (!current) {
return;
}
const hideHoveredTooltip = (e) => {
const isFocused = document.activeElement === wrapperRef.current
|| document.activeElement.closest('.bio-properties-panel-tooltip');
if (visible && !isTooltipHovered({ x: e.x, y: e.y }) && !(isFocused && focusedViaKeyboard)) {
hideTooltip();
}
};
const hideFocusedTooltip = (e) => {
const { relatedTarget } = e;
const isTooltipChild = (el) => !!el.closest('.bio-properties-panel-tooltip');
if (visible && !isHovered(wrapperRef.current) && relatedTarget && !isTooltipChild(relatedTarget)) {
hideTooltip();
}
};
document.addEventListener('wheel', hideHoveredTooltip);
document.addEventListener('focusout', hideFocusedTooltip);
document.addEventListener('mousemove', hideHoveredTooltip);
return () => {
document.removeEventListener('wheel', hideHoveredTooltip);
document.removeEventListener('mousemove', hideHoveredTooltip);
document.removeEventListener('focusout', hideFocusedTooltip);
};
}, [ wrapperRef.current, visible, focusedViaKeyboard ]);
const renderTooltip = () => {
return (
<div
class={ `bio-properties-panel-tooltip ${direction}` }
role="tooltip"
id="bio-properties-panel-tooltip"
aria-labelledby={ forId }
style={ position || getTooltipPosition(wrapperRef.current) }
ref={ tooltipRef }
onClick={ (e)=> e.stopPropagation() }
>
<div class="bio-properties-panel-tooltip-content">
{value}
</div>
<div class="bio-properties-panel-tooltip-arrow" />
</div>
);};
return (
<div class="bio-properties-panel-tooltip-wrapper" tabIndex="0"
ref={ wrapperRef }
onMouseEnter={ showTooltip }
onMouseLeave={ ()=> {
clearTimeout(timeout);
timeout = null;
} }
onFocus={ showTooltip }
onKeyDown={ hideTooltipViaEscape }
>
{props.children}
{visible ?
(parent ?
createPortal(renderTooltip(), parent.current)
: renderTooltip()
) : null
}
</div>
);
}
// helper
function inBounds(x, y, bounds) {
const { top, right, bottom, left } = bounds;
return x >= left && x <= right && y >= top && y <= bottom;
}
function getTooltipPosition(refElement) {
const refPosition = refElement.getBoundingClientRect();
const right = `calc(100% - ${refPosition.x}px)`;
const top = `${refPosition.top - 10}px`;
return `right: ${right}; top: ${top};`;
}
function isHovered(element) {
return element.matches(':hover');
}
function prefixId(id) {
return `bio-properties-panel-${ id }`;
}