Skip to content

Commit c645ce7

Browse files
feat: tooltip closing transition
1 parent 9b03e2c commit c645ce7

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,6 @@ const Tooltip = ({
8282
}
8383
}, [])
8484

85-
useEffect(() => {
86-
if (!show) {
87-
/**
88-
* this fixes weird behavior when switching between two anchor elements very quickly
89-
* remove the timeout and switch quickly between two adjancent anchor elements to see it
90-
*
91-
* in practice, this means the tooltip is not immediately removed from the DOM on hide
92-
*/
93-
const timeout = setTimeout(() => {
94-
setRendered(false)
95-
}, 150)
96-
return () => {
97-
clearTimeout(timeout)
98-
}
99-
}
100-
return () => null
101-
}, [show])
102-
10385
const handleShow = (value: boolean) => {
10486
if (!mounted.current) {
10587
return
@@ -657,13 +639,21 @@ const Tooltip = ({
657639
styles[variant],
658640
className,
659641
`react-tooltip__place-${actualPlacement}`,
660-
{
661-
'react-tooltip__show': canShow,
662-
[coreStyles['show']]: canShow,
663-
[coreStyles['fixed']]: positionStrategy === 'fixed',
664-
[coreStyles['clickable']]: clickable,
665-
},
642+
coreStyles[canShow ? 'show' : 'closing'],
643+
canShow ? 'react-tooltip__show' : 'react-tooltip__closing',
644+
positionStrategy === 'fixed' && coreStyles['fixed'],
645+
clickable && coreStyles['clickable'],
666646
)}
647+
onTransitionEnd={(event: TransitionEvent) => {
648+
/**
649+
* @warning if `--rt-transition-closing-delay` is set to 0,
650+
* the tooltip will be stuck (but not visible) on the DOM
651+
*/
652+
if (show || event.propertyName !== 'opacity') {
653+
return
654+
}
655+
setRendered(false)
656+
}}
667657
style={{
668658
...externalStyles,
669659
...inlineStyles,
@@ -678,13 +668,7 @@ const Tooltip = ({
678668
coreStyles['arrow'],
679669
styles['arrow'],
680670
classNameArrow,
681-
{
682-
/**
683-
* changed from dash `no-arrow` to camelcase because of:
684-
* https://github.com/indooorsman/esbuild-css-modules-plugin/issues/42
685-
*/
686-
[coreStyles['noArrow']]: noArrow,
687-
},
671+
noArrow && coreStyles['noArrow'],
688672
)}
689673
style={{
690674
...inlineArrowStyles,

src/components/Tooltip/core-styles.module.css

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
.tooltip {
2-
visibility: hidden;
32
position: absolute;
43
top: 0;
54
left: 0;
65
pointer-events: none;
76
opacity: 0;
8-
transition: opacity 0.3s ease-out;
9-
will-change: opacity, visibility;
7+
will-change: opacity;
108
}
119

1210
.fixed {
@@ -27,8 +25,13 @@
2725
}
2826

2927
.show {
30-
visibility: visible;
3128
opacity: var(--rt-opacity);
29+
transition: opacity var(--rt-transition-show-delay) ease-out;
30+
}
31+
32+
.closing {
33+
opacity: 0;
34+
transition: opacity var(--rt-transition-closing-delay) ease-in;
3235
}
3336

3437
/** end - core styles **/

src/tokens.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
--rt-color-warning: #f0ad4e;
77
--rt-color-info: #337ab7;
88
--rt-opacity: 0.9;
9+
--rt-transition-show-delay: 0.15s;
10+
--rt-transition-closing-delay: 0.15s;
911
}

0 commit comments

Comments
 (0)