Skip to content

Commit 948d45d

Browse files
committed
fix: remove element from DOM when is not showing
1 parent eda0168 commit 948d45d

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,34 @@ const Tooltip = ({
4444
const [inlineStyles, setInlineStyles] = useState({})
4545
const [inlineArrowStyles, setInlineArrowStyles] = useState({})
4646
const [show, setShow] = useState(false)
47+
const [rendered, setRendered] = useState(false)
4748
const wasShowing = useRef(false)
4849
const [calculatingPosition, setCalculatingPosition] = useState(false)
4950
const lastFloatPosition = useRef<IPosition | null>(null)
5051
const { anchorRefs, setActiveAnchor: setProviderActiveAnchor } = useTooltip(id)
5152
const [activeAnchor, setActiveAnchor] = useState<React.RefObject<HTMLElement>>({ current: null })
5253
const hoveringTooltip = useRef(false)
5354

54-
const handleShow = (value: boolean) => {
55-
if (setIsOpen) {
56-
setIsOpen(value)
57-
} else if (isOpen === undefined) {
58-
setShow(value)
55+
useEffect(() => {
56+
if (!show) {
57+
setRendered(false)
5958
}
59+
}, [show])
60+
61+
const handleShow = (value: boolean) => {
62+
setRendered(true)
63+
64+
/**
65+
* this `timeout` is necessary because the component
66+
* needs to be rendered to calculate the position correctly
67+
*/
68+
setTimeout(() => {
69+
if (setIsOpen) {
70+
setIsOpen(value)
71+
} else if (isOpen === undefined) {
72+
setShow(value)
73+
}
74+
}, 10)
6075
}
6176

6277
useEffect(() => {
@@ -386,13 +401,19 @@ const Tooltip = ({
386401
}, [])
387402

388403
const hasContentOrChildren = Boolean(html || content || children)
404+
const canShow = Boolean(
405+
hasContentOrChildren &&
406+
!calculatingPosition &&
407+
(isOpen || show) &&
408+
Object.keys(inlineStyles).length > 0,
409+
)
389410

390-
return (
411+
return rendered ? (
391412
<WrapperElement
392413
id={id}
393414
role="tooltip"
394415
className={classNames('react-tooltip', styles['tooltip'], styles[variant], className, {
395-
[styles['show']]: hasContentOrChildren && !calculatingPosition && (isOpen || show),
416+
[styles['show']]: canShow,
396417
[styles['fixed']]: positionStrategy === 'fixed',
397418
[styles['clickable']]: clickable,
398419
})}
@@ -409,7 +430,7 @@ const Tooltip = ({
409430
ref={tooltipArrowRef}
410431
/>
411432
</WrapperElement>
412-
)
433+
) : null
413434
}
414435

415436
export default Tooltip

0 commit comments

Comments
 (0)