Repository navigation
fix(react-tooltip): In StrictMode, Tooltip now shows correctly on elements that are focused when created - #34331
Ben Howell (behowell) wants to merge 3 commits into
Conversation
📊 Bundle size reportUnchanged fixtures
|
|
Pull request demo site: URL |
Marcos Moura (marcosmoura)
left a comment
There was a problem hiding this comment.
This seems like a good option for now.
4219cb1 to
1866f29
Compare
|
Hey folks, this is a fix for the last known issue with the Thank you! |
1866f29 to
1611f9e
Compare
|
hi Ben :) could I ask you to add a license comment ? 🙏🏻 |
|
It's been two weeks now ... Can something be done to speed this up? |
also make sure to add e2e coverage for this behaviour, ATM our R19 is green but it seems there are important missing parts like this one. ty |
|
closing in favour of #35253 |

Previous Behavior
Tooltip uses a custom utilitiy
useTimeout(useBrowserTimer) to manage the timer that delays showing the Tooltip. This utility creates the timer through an imperative function, but clears the timer in a useEffect cleanup function. This results in the tooltip never being shown if it is unmounted and remounted after the Tooltip was triggered but before it was shown. This can happen in StrictMode for a component that receives initial focus when it is created, such as a MenuItem.New Behavior
Add a ref that saves the arguments to the pending visibility change, whenever a delayed visibility change is triggered. Then, add a useEffect that runs when the component is mounted, and checks the ref to see if there is a pending change that was never completed. If so, restart the timer.
Discussion
There are a number of different options for a fix here, with varying levels of impact on runtime performance.
The fix in this PR has the least impact on runtime performance and behavior. However, it adds an additional suboptimal
useEffectcall to restart the timer on mount and "fix" theuseEffectinuseBrowserTimerthat cancels the timer on unmount.The "correct" fix would be to move the timer to be started and cleared entirely within a
useEffect. Then use a state variable to track that the tooltip was triggered, which would cause the useEffect to be run upon re-rendering. Example code is below. This has a major drawback of re-rendering the tooltipped component any time the Tooltip timer is started or stopped, which happens on every pointerEnter, pointerLeave, focus, or blur event, in addition to re-rendering whenever the tooltip shows or hides (and in React <=17, it actually adds two extra renders per show or hide, because state changes are not coalesced when using timers).The net effect is that showing a tooltip goes from 1 re-render to 2 or 3 re-renders (same for hiding the tooltip), and a quick mouseover/mouseout goes from 0 re-renders to 2-re-renders.
There are a few other possible fixes that involve moving the timer to be within a separate child component of the Tooltip. E.g. we could move all of the visibility state management into a new "TooltipContent" component that would replace the
divcurrently used for thecontentslot. The major benefit to that would be that the tooltipped component would never have any re-renders caused by the tooltip (it currently re-renders once every time the tooltip shows or hides). Unfortunately that would most likely be a breaking change to the API of Tooltip, so it's not really an option for a bug fix.Related Issue(s)