Repository navigation
fix(react-tooltip): In StrictMode, Tooltip now shows correctly on elements that are focused when created - #35253
Closed
Victor Genaev (mainframev) wants to merge 4 commits into
Closed
Victor Genaev (mainframev) wants to merge 4 commits into
Victor Genaev (mainframev) wants to merge 4 commits into
Conversation
Victor Genaev (mainframev)
force-pushed
the
fix/tooltip-visible-strict-mode
branch
from
September 30, 2025 16:46
59a962b to
428e4a4
Compare
Victor Genaev (mainframev)
force-pushed
the
fix/tooltip-visible-strict-mode
branch
from
September 30, 2025 16:47
428e4a4 to
4d47fdc
Compare
Victor Genaev (mainframev)
marked this pull request as draft
September 30, 2025 16:53
📊 Bundle size reportUnchanged fixtures
|
|
Pull request demo site: URL |
Victor Genaev (mainframev)
force-pushed
the
fix/tooltip-visible-strict-mode
branch
3 times, most recently
from
September 30, 2025 18:41
aec487b to
9ff1f27
Compare
Victor Genaev (mainframev)
marked this pull request as ready for review
September 30, 2025 18:42
Victor Genaev (mainframev)
force-pushed
the
fix/tooltip-visible-strict-mode
branch
from
September 30, 2025 18:43
9ff1f27 to
1c5a108
Compare
Martin Hochel (Hotell)
requested review from
Martin Hochel (Hotell) and
Dmytro Kirpa (dmytrokirpa)
October 1, 2025 12:07
Victor Genaev (mainframev)
marked this pull request as draft
October 1, 2025 13:09
| @@ -0,0 +1,48 @@ | |||
| import * as React from 'react'; | |||
Contributor
There was a problem hiding this comment.
Victor Genaev (@mainframev) just to confirm our offline convo, this is the correct approach in terms where the test should live. ty
Contributor
Author
|
solved in #35282 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

a copy of #34331 with a test
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)