@versini/ui-tooltip 6.3.5 → 6.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +71 -154
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
@versini/ui-tooltip v6.3.
|
|
2
|
+
@versini/ui-tooltip v6.3.6
|
|
3
3
|
© 2026 gizmette.com
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -8,28 +8,15 @@ import { useClickOutside } from "@versini/ui-hooks/use-click-outside";
|
|
|
8
8
|
import { useInterval } from "@versini/ui-hooks/use-interval";
|
|
9
9
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
10
10
|
import clsx from "clsx";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
11
|
const TOOLTIP_CLASSNAME = "av-tooltip";
|
|
15
12
|
const TOOLTIP_ARROW_CLASSNAME = "av-tooltip-arrow";
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
13
|
const ARROW_PADDING = 4;
|
|
26
14
|
const calculateTooltipPosition = (triggerRect, tooltipRect, placement, sideOffset, viewportWidth, viewportHeight)=>{
|
|
27
|
-
|
|
28
|
-
|
|
15
|
+
const side = placement.split("-")[0] || "top";
|
|
16
|
+
const alignment = placement.includes("-") ? placement.split("-")[1] : "center";
|
|
29
17
|
let top = 0;
|
|
30
18
|
let left = 0;
|
|
31
19
|
let resolvedSide = side;
|
|
32
|
-
// Calculate base position based on side
|
|
33
20
|
switch(side){
|
|
34
21
|
case "bottom":
|
|
35
22
|
top = triggerRect.bottom + sideOffset;
|
|
@@ -44,59 +31,53 @@ const calculateTooltipPosition = (triggerRect, tooltipRect, placement, sideOffse
|
|
|
44
31
|
left = triggerRect.left - tooltipRect.width - sideOffset;
|
|
45
32
|
break;
|
|
46
33
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
left = triggerRect.left + (triggerRect.width - tooltipRect.width) / 2;
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
} else {
|
|
61
|
-
switch(alignment){
|
|
62
|
-
case "start":
|
|
63
|
-
top = triggerRect.top;
|
|
64
|
-
break;
|
|
65
|
-
case "end":
|
|
66
|
-
top = triggerRect.bottom - tooltipRect.height;
|
|
67
|
-
break;
|
|
68
|
-
default:
|
|
69
|
-
top = triggerRect.top + (triggerRect.height - tooltipRect.height) / 2;
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
34
|
+
if ("bottom" === side || "top" === side) switch(alignment){
|
|
35
|
+
case "start":
|
|
36
|
+
left = triggerRect.left;
|
|
37
|
+
break;
|
|
38
|
+
case "end":
|
|
39
|
+
left = triggerRect.right - tooltipRect.width;
|
|
40
|
+
break;
|
|
41
|
+
default:
|
|
42
|
+
left = triggerRect.left + (triggerRect.width - tooltipRect.width) / 2;
|
|
43
|
+
break;
|
|
72
44
|
}
|
|
73
|
-
|
|
74
|
-
|
|
45
|
+
else switch(alignment){
|
|
46
|
+
case "start":
|
|
47
|
+
top = triggerRect.top;
|
|
48
|
+
break;
|
|
49
|
+
case "end":
|
|
50
|
+
top = triggerRect.bottom - tooltipRect.height;
|
|
51
|
+
break;
|
|
52
|
+
default:
|
|
53
|
+
top = triggerRect.top + (triggerRect.height - tooltipRect.height) / 2;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
if ("bottom" === side && top + tooltipRect.height > viewportHeight) {
|
|
75
57
|
const flippedTop = triggerRect.top - tooltipRect.height - sideOffset;
|
|
76
58
|
if (flippedTop >= 0) {
|
|
77
59
|
top = flippedTop;
|
|
78
60
|
resolvedSide = "top";
|
|
79
61
|
}
|
|
80
|
-
} else if (
|
|
62
|
+
} else if ("top" === side && top < 0) {
|
|
81
63
|
const flippedTop = triggerRect.bottom + sideOffset;
|
|
82
64
|
if (flippedTop + tooltipRect.height <= viewportHeight) {
|
|
83
65
|
top = flippedTop;
|
|
84
66
|
resolvedSide = "bottom";
|
|
85
67
|
}
|
|
86
|
-
} else if (
|
|
68
|
+
} else if ("right" === side && left + tooltipRect.width > viewportWidth) {
|
|
87
69
|
const flippedLeft = triggerRect.left - tooltipRect.width - sideOffset;
|
|
88
70
|
if (flippedLeft >= 0) {
|
|
89
71
|
left = flippedLeft;
|
|
90
72
|
resolvedSide = "left";
|
|
91
73
|
}
|
|
92
|
-
} else if (
|
|
74
|
+
} else if ("left" === side && left < 0) {
|
|
93
75
|
const flippedLeft = triggerRect.right + sideOffset;
|
|
94
76
|
if (flippedLeft + tooltipRect.width <= viewportWidth) {
|
|
95
77
|
left = flippedLeft;
|
|
96
78
|
resolvedSide = "right";
|
|
97
79
|
}
|
|
98
80
|
}
|
|
99
|
-
/* v8 ignore stop */ // Clamp to viewport bounds
|
|
100
81
|
left = Math.max(0, Math.min(left, viewportWidth - tooltipRect.width));
|
|
101
82
|
top = Math.max(0, Math.min(top, viewportHeight - tooltipRect.height));
|
|
102
83
|
return {
|
|
@@ -115,7 +96,7 @@ const calculateArrowPosition = (triggerRect, tooltipTop, tooltipLeft, tooltipWid
|
|
|
115
96
|
const staticSide = staticSideMap[resolvedSide];
|
|
116
97
|
let arrowLeft = "";
|
|
117
98
|
let arrowTop = "";
|
|
118
|
-
if (
|
|
99
|
+
if ("top" === resolvedSide || "bottom" === resolvedSide) {
|
|
119
100
|
const triggerCenterX = triggerRect.left + triggerRect.width / 2;
|
|
120
101
|
const rawArrowLeft = triggerCenterX - tooltipLeft - arrowWidth / 2;
|
|
121
102
|
const clampedArrowLeft = Math.max(ARROW_PADDING, Math.min(rawArrowLeft, tooltipWidth - arrowWidth - ARROW_PADDING));
|
|
@@ -135,34 +116,24 @@ const calculateArrowPosition = (triggerRect, tooltipTop, tooltipLeft, tooltipWid
|
|
|
135
116
|
staticSideValue: `-${Math.round(arrowHeight / 2)}px`
|
|
136
117
|
};
|
|
137
118
|
};
|
|
138
|
-
const getTooltipBaseClasses = ()=>
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
"bg-surface-darker":
|
|
144
|
-
"bg-surface-lighter": mode === "light" || mode === "alt-system",
|
|
145
|
-
"dark:bg-surface-lighter": mode === "system",
|
|
146
|
-
"dark:bg-surface-darker": mode === "alt-system"
|
|
119
|
+
const getTooltipBaseClasses = ()=>clsx("fixed w-max py-1 px-2 rounded-sm text-sm z-50");
|
|
120
|
+
const getTooltipBackgroundColorClasses = ({ mode })=>clsx({
|
|
121
|
+
"bg-surface-darker": "dark" === mode || "system" === mode,
|
|
122
|
+
"bg-surface-lighter": "light" === mode || "alt-system" === mode,
|
|
123
|
+
"dark:bg-surface-lighter": "system" === mode,
|
|
124
|
+
"dark:bg-surface-darker": "alt-system" === mode
|
|
147
125
|
});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
"text-copy-
|
|
152
|
-
"text-copy-
|
|
153
|
-
"dark:text-copy-dark": mode === "system",
|
|
154
|
-
"dark:text-copy-light": mode === "alt-system"
|
|
126
|
+
const getTooltipTextCopyColorClasses = ({ mode })=>clsx({
|
|
127
|
+
"text-copy-light": "dark" === mode || "system" === mode,
|
|
128
|
+
"text-copy-dark": "light" === mode || "alt-system" === mode,
|
|
129
|
+
"dark:text-copy-dark": "system" === mode,
|
|
130
|
+
"dark:text-copy-light": "alt-system" === mode
|
|
155
131
|
});
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
"shadow-md": mode === "light" || mode === "alt-system",
|
|
160
|
-
"dark:shadow-md": mode === "system"
|
|
132
|
+
const getTooltipShadowClasses = ({ mode })=>clsx({
|
|
133
|
+
"shadow-md": "light" === mode || "alt-system" === mode,
|
|
134
|
+
"dark:shadow-md": "system" === mode
|
|
161
135
|
});
|
|
162
|
-
|
|
163
|
-
const getTooltipArrowBaseClasses = ()=>{
|
|
164
|
-
return clsx("absolute", "size-2", "transform rotate-45");
|
|
165
|
-
};
|
|
136
|
+
const getTooltipArrowBaseClasses = ()=>clsx("absolute", "size-2", "transform rotate-45");
|
|
166
137
|
const getTooltipClasses = ({ mode, className, tooltipClassName, arrowClassName, animate })=>{
|
|
167
138
|
const wrapper = clsx("inline-block", className);
|
|
168
139
|
const tooltip = clsx(TOOLTIP_CLASSNAME, getTooltipBaseClasses(), getTooltipBackgroundColorClasses({
|
|
@@ -183,51 +154,23 @@ const getTooltipClasses = ({ mode, className, tooltipClassName, arrowClassName,
|
|
|
183
154
|
wrapper
|
|
184
155
|
};
|
|
185
156
|
};
|
|
186
|
-
|
|
187
|
-
* The fade-in itself is the `.av-tooltip-animate` rule ui-styles injects: its
|
|
188
|
-
* `animation` names the `av-tooltip-fade-in` keyframes and reads the duration
|
|
189
|
-
* from the custom property below, with the default duration as the fallback.
|
|
190
|
-
* The component must never name the keyframes itself, as the inline `animation`
|
|
191
|
-
* it used to set did. A consumer minifying with cssnano's advanced preset drops
|
|
192
|
-
* a `@keyframes` no stylesheet references and renames the ones it keeps, so an
|
|
193
|
-
* inline reference pointed either at nothing or at the old name, and the
|
|
194
|
-
* tooltip appeared instantly (versini-org/ui-components#2016).
|
|
195
|
-
*
|
|
196
|
-
* Both names are private: they are the contract with ui-styles, not a styling
|
|
197
|
-
* hook for consumers, and the class only fades correctly once the tooltip has
|
|
198
|
-
* been positioned.
|
|
199
|
-
*
|
|
200
|
-
*/ const TOOLTIP_ANIMATE_CLASSNAME = "av-tooltip-animate";
|
|
157
|
+
const TOOLTIP_ANIMATE_CLASSNAME = "av-tooltip-animate";
|
|
201
158
|
const TOOLTIP_ANIMATION_DURATION_PROPERTY = "--av-tooltip-animation-duration";
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
* default and a consumer can still theme the duration from the cascade
|
|
205
|
-
* (`--av-tooltip-animation-duration` on any ancestor); an explicit prop wins.
|
|
206
|
-
*/ const getAnimationStyles = ({ animationDuration })=>{
|
|
207
|
-
if (animationDuration === undefined) {
|
|
208
|
-
return {};
|
|
209
|
-
}
|
|
159
|
+
const getAnimationStyles = ({ animationDuration })=>{
|
|
160
|
+
if (void 0 === animationDuration) return {};
|
|
210
161
|
return {
|
|
211
162
|
[TOOLTIP_ANIMATION_DURATION_PROPERTY]: `${animationDuration}ms`
|
|
212
163
|
};
|
|
213
164
|
};
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
165
|
function useTooltipPosition({ triggerRef, tooltipRef, arrowRef, placement, sideOffset, isOpen }) {
|
|
218
166
|
const [isPositioned, setIsPositioned] = useState(false);
|
|
219
167
|
const updatePosition = useCallback(()=>{
|
|
220
|
-
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
168
|
+
if (!triggerRef.current || !tooltipRef.current || !arrowRef.current) return;
|
|
223
169
|
const triggerRect = triggerRef.current.getBoundingClientRect();
|
|
224
170
|
const tooltipRect = tooltipRef.current.getBoundingClientRect();
|
|
225
171
|
const viewportWidth = window.innerWidth;
|
|
226
172
|
const viewportHeight = window.innerHeight;
|
|
227
173
|
const { top, left, resolvedSide } = calculateTooltipPosition(triggerRect, tooltipRect, placement, sideOffset, viewportWidth, viewportHeight);
|
|
228
|
-
// Use offsetWidth/offsetHeight to get the untransformed arrow size,
|
|
229
|
-
// since the arrow has transform:rotate(45deg) which inflates
|
|
230
|
-
// getBoundingClientRect() dimensions.
|
|
231
174
|
const arrowWidth = arrowRef.current.offsetWidth;
|
|
232
175
|
const arrowHeight = arrowRef.current.offsetHeight;
|
|
233
176
|
const arrowPosition = calculateArrowPosition(triggerRect, top, left, tooltipRect.width, tooltipRect.height, resolvedSide, arrowWidth, arrowHeight);
|
|
@@ -245,7 +188,7 @@ function useTooltipPosition({ triggerRef, tooltipRef, arrowRef, placement, sideO
|
|
|
245
188
|
[arrowPosition.staticSide]: arrowPosition.staticSideValue
|
|
246
189
|
});
|
|
247
190
|
setIsPositioned(true);
|
|
248
|
-
|
|
191
|
+
}, [
|
|
249
192
|
triggerRef,
|
|
250
193
|
tooltipRef,
|
|
251
194
|
arrowRef,
|
|
@@ -253,13 +196,10 @@ function useTooltipPosition({ triggerRef, tooltipRef, arrowRef, placement, sideO
|
|
|
253
196
|
sideOffset
|
|
254
197
|
]);
|
|
255
198
|
useEffect(()=>{
|
|
256
|
-
if (isOpen) {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
} else {
|
|
261
|
-
setIsPositioned(false);
|
|
262
|
-
}
|
|
199
|
+
if (isOpen) requestAnimationFrame(()=>{
|
|
200
|
+
updatePosition();
|
|
201
|
+
});
|
|
202
|
+
else setIsPositioned(false);
|
|
263
203
|
}, [
|
|
264
204
|
isOpen,
|
|
265
205
|
updatePosition
|
|
@@ -268,27 +208,20 @@ function useTooltipPosition({ triggerRef, tooltipRef, arrowRef, placement, sideO
|
|
|
268
208
|
isPositioned
|
|
269
209
|
};
|
|
270
210
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
211
|
const DEFAULT_DEACTIVATION_DELAY = 5000;
|
|
279
212
|
const Tooltip = ({ trigger, label, placement = "top", mode = "system", animationDuration, delay = 0, className, tooltipClassName, arrowClassName })=>{
|
|
280
213
|
const referenceRef = useClickOutside(()=>{
|
|
281
|
-
|
|
282
|
-
|
|
214
|
+
delayedRestartTooltip.stop(), setDisabled(false);
|
|
215
|
+
});
|
|
283
216
|
const tooltipRef = useRef(null);
|
|
284
217
|
const arrowRef = useRef(null);
|
|
285
218
|
const showTimeoutRef = useRef(null);
|
|
286
219
|
const [showTooltip, setShowTooltip] = useState(false);
|
|
287
220
|
const [disabled, setDisabled] = useState(false);
|
|
288
|
-
|
|
221
|
+
const delayedRestartTooltip = useInterval(()=>{
|
|
289
222
|
setDisabled(false);
|
|
290
223
|
}, DEFAULT_DEACTIVATION_DELAY);
|
|
291
|
-
|
|
224
|
+
const { isPositioned } = useTooltipPosition({
|
|
292
225
|
triggerRef: referenceRef,
|
|
293
226
|
tooltipRef,
|
|
294
227
|
arrowRef,
|
|
@@ -303,13 +236,9 @@ const Tooltip = ({ trigger, label, placement = "top", mode = "system", animation
|
|
|
303
236
|
arrowClassName,
|
|
304
237
|
animate: isPositioned
|
|
305
238
|
});
|
|
306
|
-
useEffect(()=>{
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
clearTimeout(showTimeoutRef.current);
|
|
310
|
-
}
|
|
311
|
-
};
|
|
312
|
-
/* v8 ignore stop */ }, []);
|
|
239
|
+
useEffect(()=>()=>{
|
|
240
|
+
if (showTimeoutRef.current) clearTimeout(showTimeoutRef.current);
|
|
241
|
+
}, []);
|
|
313
242
|
const handleMouseClick = ()=>{
|
|
314
243
|
if (showTimeoutRef.current) {
|
|
315
244
|
clearTimeout(showTimeoutRef.current);
|
|
@@ -317,31 +246,23 @@ const Tooltip = ({ trigger, label, placement = "top", mode = "system", animation
|
|
|
317
246
|
}
|
|
318
247
|
setShowTooltip(false);
|
|
319
248
|
setDisabled(true);
|
|
320
|
-
// disabling tooltip for 5 seconds
|
|
321
249
|
delayedRestartTooltip.start();
|
|
322
250
|
};
|
|
323
251
|
const handleMouseEnter = ()=>{
|
|
324
|
-
if (!disabled) {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
}, delay);
|
|
330
|
-
/* v8 ignore stop */ } else {
|
|
331
|
-
setShowTooltip(true);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
252
|
+
if (!disabled) if (delay > 0) showTimeoutRef.current = setTimeout(()=>{
|
|
253
|
+
setShowTooltip(true);
|
|
254
|
+
showTimeoutRef.current = null;
|
|
255
|
+
}, delay);
|
|
256
|
+
else setShowTooltip(true);
|
|
334
257
|
};
|
|
335
|
-
|
|
258
|
+
const handleMouseLeave = ()=>{
|
|
336
259
|
if (showTimeoutRef.current) {
|
|
337
260
|
clearTimeout(showTimeoutRef.current);
|
|
338
261
|
showTimeoutRef.current = null;
|
|
339
262
|
}
|
|
340
|
-
if (!disabled)
|
|
341
|
-
setShowTooltip(false);
|
|
342
|
-
}
|
|
263
|
+
if (!disabled) setShowTooltip(false);
|
|
343
264
|
};
|
|
344
|
-
|
|
265
|
+
return jsxs("div", {
|
|
345
266
|
className: tooltipClasses.wrapper,
|
|
346
267
|
ref: referenceRef,
|
|
347
268
|
onMouseEnter: handleMouseEnter,
|
|
@@ -349,7 +270,7 @@ const Tooltip = ({ trigger, label, placement = "top", mode = "system", animation
|
|
|
349
270
|
onClick: handleMouseClick,
|
|
350
271
|
children: [
|
|
351
272
|
trigger,
|
|
352
|
-
showTooltip &&
|
|
273
|
+
showTooltip && jsxs("div", {
|
|
353
274
|
role: "tooltip",
|
|
354
275
|
ref: tooltipRef,
|
|
355
276
|
className: tooltipClasses.tooltip,
|
|
@@ -357,11 +278,11 @@ const Tooltip = ({ trigger, label, placement = "top", mode = "system", animation
|
|
|
357
278
|
...getAnimationStyles({
|
|
358
279
|
animationDuration
|
|
359
280
|
}),
|
|
360
|
-
opacity: isPositioned ?
|
|
281
|
+
opacity: isPositioned ? void 0 : 0
|
|
361
282
|
},
|
|
362
283
|
children: [
|
|
363
284
|
label,
|
|
364
|
-
|
|
285
|
+
jsx("div", {
|
|
365
286
|
ref: arrowRef,
|
|
366
287
|
className: tooltipClasses.arrow
|
|
367
288
|
})
|
|
@@ -370,8 +291,4 @@ const Tooltip = ({ trigger, label, placement = "top", mode = "system", animation
|
|
|
370
291
|
]
|
|
371
292
|
});
|
|
372
293
|
};
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
294
|
export { TOOLTIP_ARROW_CLASSNAME, TOOLTIP_CLASSNAME, Tooltip };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versini/ui-tooltip",
|
|
3
|
-
"version": "6.3.
|
|
3
|
+
"version": "6.3.6",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"publishConfig": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"@testing-library/jest-dom": "7.0.1"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@versini/ui-hooks": "6.2.
|
|
25
|
+
"@versini/ui-hooks": "6.2.4",
|
|
26
26
|
"clsx": "2.1.1",
|
|
27
27
|
"tailwindcss": "4.3.3"
|
|
28
28
|
},
|