Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 18 additions & 44 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,15 @@ import React, {
} from 'react'
import { createPortal } from 'react-dom'
import clsx from 'clsx'
import {
useIsomorphicLayoutEffect,
computeTooltipPosition,
cssTimeToMs,
clearTimeoutRef,
} from '../../utils'
import { useIsomorphicLayoutEffect, computeTooltipPosition, clearTimeoutRef } from '../../utils'
import type { IComputedPosition } from '../../utils'
import coreStyles from './core-styles.module.css'
import styles from './styles.module.css'
import useTooltipAnchors from './use-tooltip-anchors'
import useTooltipEvents from './use-tooltip-events'
import useTooltipRemoval from './use-tooltip-removal'
import type { IPosition, ITooltip, TooltipImperativeOpenOptions } from './TooltipTypes'

// Shared across all tooltip instances — the CSS variable is on :root and never changes per-instance
let globalTransitionShowDelay: number | null = null

const Tooltip = ({
// props
forwardRef,
Expand Down Expand Up @@ -76,13 +69,13 @@ const Tooltip = ({
const tooltipShowDelayTimerRef = useRef<NodeJS.Timeout | null>(null)
const tooltipHideDelayTimerRef = useRef<NodeJS.Timeout | null>(null)
const tooltipAutoCloseTimerRef = useRef<NodeJS.Timeout | null>(null)
const missedTransitionTimerRef = useRef<NodeJS.Timeout | null>(null)
const [computedPosition, setComputedPosition] = useState<IComputedPosition>({
tooltipStyles: {},
tooltipArrowStyles: {},
place,
})
const [show, setShow] = useState(false)
const [showRequested, setShowRequested] = useState(false)
const [rendered, setRendered] = useState(false)
const [imperativeOptions, setImperativeOptions] = useState<TooltipImperativeOpenOptions | null>(
null,
Expand Down Expand Up @@ -121,6 +114,7 @@ const Tooltip = ({
if (!mounted.current) {
return
}
setShowRequested(value)
if (value) {
setRendered(true)
}
Expand Down Expand Up @@ -199,33 +193,23 @@ const Tooltip = ({
if (show === wasShowing.current) {
return
}
clearTimeoutRef(missedTransitionTimerRef)
wasShowing.current = show
if (show) {
afterShow?.()
} else {
/**
* see `onTransitionEnd` on tooltip wrapper
*/
if (globalTransitionShowDelay === null) {
const style = getComputedStyle(document.body)
globalTransitionShowDelay = cssTimeToMs(
style.getPropertyValue('--rt-transition-show-delay'),
)
}
const transitionShowDelay = globalTransitionShowDelay
missedTransitionTimerRef.current = setTimeout(() => {
/**
* if the tooltip switches from `show === true` to `show === false` too fast
* the transition never runs, so `onTransitionEnd` callback never gets fired
*/
setRendered(false)
setImperativeOptions(null)
afterHide?.()
// +25ms just to make sure `onTransitionEnd` (if it gets fired) has time to run
}, transitionShowDelay + 25)
}
}, [afterHide, afterShow, show])
}, [afterShow, show])

const handleTransitionEnd = useTooltipRemoval({
rendered,
show,
// a controlled tooltip shows when `isOpen` says so, whatever was requested
showRequested: isOpen === undefined ? showRequested : isOpen,
afterHide,
onRemove: () => {
setRendered(false)
setImperativeOptions(null)
},
})

useEffect(() => {
clearTimeoutRef(tooltipAutoCloseTimerRef)
Expand Down Expand Up @@ -550,7 +534,6 @@ const Tooltip = ({
clearTimeoutRef(tooltipShowDelayTimerRef)
clearTimeoutRef(tooltipHideDelayTimerRef)
clearTimeoutRef(tooltipAutoCloseTimerRef)
clearTimeoutRef(missedTransitionTimerRef)
}
}, [defaultIsOpen, handleShow])

Expand Down Expand Up @@ -639,7 +622,6 @@ const Tooltip = ({
clearTimeoutRef(tooltipShowDelayTimerRef)
clearTimeoutRef(tooltipHideDelayTimerRef)
clearTimeoutRef(tooltipAutoCloseTimerRef)
clearTimeoutRef(missedTransitionTimerRef)
}
}, [])

Expand All @@ -660,15 +642,7 @@ const Tooltip = ({
positionStrategy === 'fixed' && coreStyles['fixed'],
clickable && coreStyles['clickable'],
)}
onTransitionEnd={(event: TransitionEvent) => {
clearTimeoutRef(missedTransitionTimerRef)
if (show || event.propertyName !== 'opacity') {
return
}
setRendered(false)
setImperativeOptions(null)
afterHide?.()
}}
onTransitionEnd={handleTransitionEnd}
style={tooltipStyle}
ref={tooltipRef}
>
Expand Down
3 changes: 2 additions & 1 deletion src/components/Tooltip/core-styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
display: none;
}

.clickable {
/* only while it shows: a tooltip that is not showing is invisible and must not take the pointer */
.clickable.show {
pointer-events: auto;
}

Expand Down
97 changes: 97 additions & 0 deletions src/components/Tooltip/use-tooltip-removal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import { cssTimeToMs, useIsomorphicLayoutEffect } from '../../utils'

// Shared across all tooltip instances — the CSS variable is on :root and never changes per-instance
let globalTransitionShowDelay: number | null = null

const getMissedTransitionDelay = () => {
if (globalTransitionShowDelay === null) {
const style = getComputedStyle(document.body)
globalTransitionShowDelay = cssTimeToMs(style.getPropertyValue('--rt-transition-show-delay'))
}
// +25ms just to make sure `onTransitionEnd` (if it gets fired) has time to run
return globalTransitionShowDelay + 25
}

/**
* Takes a tooltip out of the DOM once it is neither showing nor asked to show,
* and calls `afterHide` if it had shown.
*
* The tooltip is rendered a moment before it shows, and stays rendered while it fades out.
* The closing transition ends that (see the returned `onTransitionEnd` handler).
* A timer ends it when no transition runs:
* - the tooltip showed and closed too fast for the transition to start
* - the tooltip closed before it showed, so `show` never changed
* - a controlled `isOpen` never let the tooltip show
*/
const useTooltipRemoval = ({
rendered,
show,
showRequested,
afterHide,
onRemove,
}: {
rendered: boolean
show: boolean
/** the tooltip was asked to show, and `show` follows a moment later */
showRequested: boolean
afterHide?: () => void
onRemove: () => void
}) => {
/**
* the timer and the transition end only mark the removal as due.
* either can run in the same turn as a request to show that has not rendered yet,
* and removing the tooltip right there would take it out just as it shows again
*/
const [removalDue, setRemovalDue] = useState(false)
// the tooltip showed, and `afterHide` has not been called for it yet
const afterHidePending = useRef(false)

useEffect(() => {
if (show) {
afterHidePending.current = true
}
}, [show])

useEffect(() => {
if (show || showRequested || (!rendered && !afterHidePending.current)) {
return undefined
}
const timer = setTimeout(() => {
setRemovalDue(true)
}, getMissedTransitionDelay())
return () => {
clearTimeout(timer)
}
}, [rendered, show, showRequested])

/**
* `show` and `showRequested` are current here, so this is where a due removal
* is carried out or dropped. a layout effect, so the tooltip is gone before the next paint
*/
useIsomorphicLayoutEffect(() => {
if (!removalDue) {
return
}
setRemovalDue(false)
if (show || showRequested) {
return
}
onRemove()
if (afterHidePending.current) {
afterHidePending.current = false
afterHide?.()
}
}, [removalDue, show, showRequested, onRemove, afterHide])

return useCallback(
(event: TransitionEvent) => {
if (!show && event.propertyName === 'opacity') {
setRemovalDue(true)
}
},
[show],
)
}

export default useTooltipRemoval
Loading
Loading