Skip to content

fix(react-tooltip): In StrictMode, Tooltip now shows correctly on elements that are focused when created - #34331

Closed
Ben Howell (behowell) wants to merge 3 commits into
microsoft:masterfrom
behowell:tooltip-delay-visible-strict-mode
Closed

Ben Howell (behowell) wants to merge 3 commits into
microsoft:masterfrom
behowell:tooltip-delay-visible-strict-mode

Conversation

@behowell

Copy link
Copy Markdown
Contributor

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)

@github-actions

github-actions Bot commented Apr 25, 2025 •

Copy link
Copy Markdown

📊 Bundle size report

Package & Exports Baseline (minified/GZIP) PR Change
react-charts
AreaChart
384.401 kB
117.185 kB
384.666 kB
117.269 kB
265 B
84 B
react-charts
DeclarativeChart
680.701 kB
195.46 kB
680.966 kB
195.577 kB
265 B
117 B
react-charts
GanttChart
367.639 kB
110.699 kB
367.904 kB
110.822 kB
265 B
123 B
react-charts
GaugeChart
310.819 kB
92.091 kB
311.084 kB
92.182 kB
265 B
91 B
react-charts
GroupedVerticalBarChart
371.17 kB
111.809 kB
371.435 kB
111.928 kB
265 B
119 B
react-charts
HeatMapChart
369.667 kB
112.316 kB
369.932 kB
112.4 kB
265 B
84 B
react-charts
HorizontalBarChart
295.272 kB
86.894 kB
295.537 kB
87.013 kB
265 B
119 B
react-charts
LineChart
393.906 kB
118.73 kB
394.171 kB
118.828 kB
265 B
98 B
react-charts
ScatterChart
375.426 kB
113.248 kB
375.691 kB
113.366 kB
265 B
118 B
react-charts
VerticalBarChart
410.24 kB
118.79 kB
410.505 kB
118.887 kB
265 B
97 B
react-charts
VerticalStackedBarChart
382.06 kB
114.654 kB
382.325 kB
114.731 kB
265 B
77 B
react-components
react-components: entire library
1.284 MB
323.352 kB
1.284 MB
323.466 kB
265 B
114 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.516 kB
88.592 kB
react-charts
FunnelChart
287.743 kB
85.563 kB
react-charts
HorizontalBarChartWithAxis
63 B
83 B
react-charts
Legends
234.845 kB
69.479 kB
react-charts
SankeyChart
198.839 kB
61.09 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.691 kB
68.567 kB
react-components
react-components: FluentProvider & webLightTheme
43.63 kB
14.257 kB
react-datepicker-compat
DatePicker Compat
226.653 kB
64.089 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.061 kB
45.539 kB
react-table
Table (Primitives only)
41.978 kB
13.573 kB
react-table
Table as DataGrid
131.484 kB
36.345 kB
react-table
Table (Selection only)
69.846 kB
19.704 kB
react-table
Table (Sort only)
68.489 kB
19.321 kB
react-tag-picker
@fluentui/react-tag-picker - package
188.33 kB
56.457 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 79b08d95a8b7c2058ab6809fc36d301489bd01e3

@github-actions

Copy link
Copy Markdown

Pull request demo site: URL

@behowell
Ben Howell (behowell) marked this pull request as ready for review April 25, 2025 23:03
@behowell
Ben Howell (behowell) requested a review from a team as a code owner April 25, 2025 23:03

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.

This seems like a good option for now.

@dmytrokirpa

Copy link
Copy Markdown
Contributor

Hey folks, this is a fix for the last known issue with the React 19 label. Victor Genaev (@mainframev) Marcos Moura (@marcosmoura), could you both check it out, test, review, and merge when you get a chance?

Thank you!

@mainframev
Victor Genaev (mainframev) force-pushed the tooltip-delay-visible-strict-mode branch from 1866f29 to 1611f9e Compare September 15, 2025 05:45
@mainframev

Copy link
Copy Markdown
Contributor

Ben Howell (@behowell)

hi Ben :) could I ask you to add a license comment ? 🙏🏻

@eliasnystrom

Copy link
Copy Markdown

It's been two weeks now ... Can something be done to speed this up?

@Hotell

Copy link
Copy Markdown
Contributor

Hey folks, this is a fix for the last known issue with the React 19 label. Victor Genaev (@mainframev) Marcos Moura (@marcosmoura), could you both check it out, test, review, and merge when you get a chance?

Thank you!

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

@mainframev

Copy link
Copy Markdown
Contributor

closing in favour of #35253

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

6 participants