Skip to content

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
microsoft:masterfrom
mainframev:fix/tooltip-visible-strict-mode
Closed

Victor Genaev (mainframev) wants to merge 4 commits into
microsoft:masterfrom
mainframev:fix/tooltip-visible-strict-mode

Conversation

@mainframev

@mainframev Victor Genaev (mainframev) commented Sep 30, 2025 •

Copy link
Copy Markdown
Contributor

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 useEffect call to restart the timer on mount and "fix" the useEffect in useBrowserTimer that 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.

type PendingVisible = {
  ev: React.PointerEvent<HTMLElement> | React.FocusEvent<HTMLElement> | undefined;
  visible: boolean;
  delayUntil: number;
};
const [pendingVisible, setPendingVisible] = React.useState<PendingVisible | undefined>(undefined);

const setDelayedVisible = React.useCallback(
  (ev, visible, delay) => setPendingVisible({ ev, visible, delayUntil: Date.now() + delay }),
  [],
);

// Run the timer when there is a pending visible change
React.useEffect(() => {
  if (pendingVisible) {
    const { ev, visible, delayUntil } = pendingVisible;
    const delay = Math.max(0, delayUntil - Date.now());
    const id = targetWindow?.setTimeout(() => setVisible(ev, { visible }), delay);
    return () => {
      targetWindow?.clearTimeout(id);
    };
  }
}, [pendingVisible, targetWindow, setVisible]);

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 div currently used for the content slot. 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)

@mainframev
Victor Genaev (mainframev) marked this pull request as draft September 30, 2025 16:53
@github-actions

github-actions Bot commented Sep 30, 2025 •

Copy link
Copy Markdown

📊 Bundle size report

Package & Exports Baseline (minified/GZIP) PR Change
react-charts
AreaChart
384.803 kB
117.317 kB
385.068 kB
117.402 kB
265 B
85 B
react-charts
DeclarativeChart
681.289 kB
195.605 kB
681.554 kB
195.736 kB
265 B
131 B
react-charts
GanttChart
368.041 kB
110.814 kB
368.306 kB
110.936 kB
265 B
122 B
react-charts
GaugeChart
311.221 kB
92.228 kB
311.486 kB
92.316 kB
265 B
88 B
react-charts
GroupedVerticalBarChart
371.572 kB
111.942 kB
371.837 kB
112.065 kB
265 B
123 B
react-charts
HeatMapChart
370.069 kB
112.439 kB
370.334 kB
112.521 kB
265 B
82 B
react-charts
HorizontalBarChart
295.674 kB
86.998 kB
295.939 kB
87.117 kB
265 B
119 B
react-charts
LineChart
394.308 kB
118.854 kB
394.573 kB
118.949 kB
265 B
95 B
react-charts
ScatterChart
375.828 kB
113.387 kB
376.093 kB
113.508 kB
265 B
121 B
react-charts
VerticalBarChart
410.642 kB
118.919 kB
410.907 kB
119.018 kB
265 B
99 B
react-charts
VerticalStackedBarChart
382.456 kB
114.785 kB
382.721 kB
114.862 kB
265 B
77 B
react-components
react-components: entire library
1.277 MB
321.009 kB
1.278 MB
321.122 kB
265 B
113 B
react-tooltip
Tooltip
57.922 kB
20.084 kB
58.187 kB
20.183 kB
265 B
99 B
Unchanged fixtures
Package & Exports Size (minified/GZIP)
react-avatar
Avatar
48.518 kB
15.508 kB
react-avatar
AvatarGroup
19.235 kB
7.617 kB
react-avatar
AvatarGroupItem
62.662 kB
19.727 kB
react-charts
DonutChart
296.931 kB
88.744 kB
react-charts
FunnelChart
288.17 kB
85.701 kB
react-charts
HorizontalBarChartWithAxis
63 B
83 B
react-charts
Legends
235.208 kB
69.59 kB
react-charts
SankeyChart
198.878 kB
61.101 kB
react-charts
Sparkline
91.123 kB
28.692 kB
react-checkbox
Checkbox
34.335 kB
11.786 kB
react-combobox
Combobox (including child components)
106.952 kB
34.798 kB
react-combobox
Dropdown (including child components)
107.582 kB
34.719 kB
react-components
react-components: Button, FluentProvider & webLightTheme
68.796 kB
19.903 kB
react-components
react-components: Accordion, Button, FluentProvider, Image, Menu, Popover
236.943 kB
68.636 kB
react-components
react-components: FluentProvider & webLightTheme
43.63 kB
14.257 kB
react-datepicker-compat
DatePicker Compat
226.669 kB
64.105 kB
react-dialog
Dialog (including children components)
103.578 kB
30.891 kB
react-field
Field
22.657 kB
8.602 kB
react-input
Input
27.148 kB
9.108 kB
react-list
List
88.922 kB
26.387 kB
react-list
ListItem
112.381 kB
33.224 kB
react-persona
Persona
55.409 kB
17.389 kB
react-portal-compat
PortalCompatProvider
8.386 kB
2.624 kB
react-progress
ProgressBar
16.593 kB
6.613 kB
react-radio
Radio
31.8 kB
10.006 kB
react-radio
RadioGroup
14.889 kB
6.09 kB
react-select
Select
26.95 kB
9.833 kB
react-slider
Slider
37.211 kB
12.461 kB
react-spinbutton
SpinButton
34.431 kB
11.449 kB
react-swatch-picker
@fluentui/react-swatch-picker - package
106.047 kB
30.539 kB
react-switch
Switch
34.51 kB
11.014 kB
react-table
DataGrid
161.122 kB
45.55 kB
react-table
Table (Primitives only)
41.94 kB
13.559 kB
react-table
Table as DataGrid
131.446 kB
36.336 kB
react-table
Table (Selection only)
69.808 kB
19.693 kB
react-table
Table (Sort only)
68.451 kB
19.308 kB
react-tag-picker
@fluentui/react-tag-picker - package
188.343 kB
56.458 kB
react-tags
InteractionTag
14.561 kB
5.865 kB
react-tags
Tag
30.379 kB
9.779 kB
react-tags
TagGroup
83.125 kB
24.561 kB
react-textarea
Textarea
25.53 kB
9.361 kB
react-timepicker-compat
TimePicker
109.935 kB
36.335 kB
react-tree
FlatTree
149.326 kB
42.711 kB
react-tree
PersonaFlatTree
150.082 kB
42.837 kB
react-tree
PersonaTree
146.34 kB
41.686 kB
react-tree
Tree
145.588 kB
41.562 kB
🤖 This report was generated against 572f92d4d85ea6619124ca8174a955a039c951d0

@github-actions

Copy link
Copy Markdown

Pull request demo site: URL

@@ -0,0 +1,48 @@
import * as React from 'react';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Victor Genaev (@mainframev) just to confirm our offline convo, this is the correct approach in terms where the test should live. ty

@mainframev

Copy link
Copy Markdown
Contributor Author

solved in #35282

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Tooltips of MenuItem is not showing when open menu button by keyboard

3 participants