Skip to content

fix: remove a tooltip that closes before it shows - #1293

Merged
danielbarion merged 2 commits into
ReactTooltip:masterfrom
gogakoreli:fix/tooltip-removal
Nov 13, 2026
Merged

danielbarion merged 2 commits into
ReactTooltip:masterfrom
gogakoreli:fix/tooltip-removal

Conversation

@gogakoreli

@gogakoreli gogakoreli commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #1292.

A tooltip is rendered at once and shown 10 ms later. The fallback that takes a closed tooltip out of the DOM was armed only when show changed. A tooltip that never got to show had no change of show, so it stayed in the DOM with react-tooltip__closing and opacity 0. With clickable it also took clicks.

  • use-tooltip-removal.tsx (new) owns removal, with one rule: a tooltip that is neither showing nor asked to show leaves the DOM. Tooltip.tsx passes it rendered, show and whether a show was requested, and gets the onTransitionEnd handler back. The fallback timer, the delay lookup and the afterHide call moved into the hook.
  • The timer and the transition end only mark the removal as due. A layout effect carries it out, where show is current. Either of them can run just before a tooltip shows again, and removing it right there left show true with nothing in the DOM.
  • .clickable.show replaces .clickable for pointer-events: auto. A tooltip that is not showing is invisible, and it could be hovered and clicked before it was positioned.

What changes for users

Headless Chromium, built dist, React 19. "Stuck" means still in the DOM 1.5 s later.

Case master this branch
open() and close() in the same tick stuck 10 of 10 0 of 10
open(), close() 3 ms later, during a 30 ms long task stuck 10 of 10 0 of 10
Controlled: isOpen true, then false 3 ms later stuck 10 of 10 0 of 10
Controlled: isOpen={false}, pointer moves onto the anchor stuck 10 of 10 0 of 10
Leave the anchor, come back 70 ms later, during a 300 ms long task no tooltip while hovering, 10 of 10 0 of 10
close(), then open() 120 to 200 ms later (21 delays, 4 runs each) no tooltip afterwards, 11 of 84 0 of 84
clickable + delayShow, pointer where the unpositioned tooltip mounts tooltip hovered, then closes itself, 30 of 30 0 of 30

Unchanged on both: an ordinary close still fades (opacity between 0 and 0.9 at 85 ms, removed by 485 ms, 10 of 10), an open tooltip stays open, and a click inside a showing clickable tooltip reaches it (10 of 10).

Behavior to know about

  • afterHide is not called for a tooltip that never showed. afterShow was not called for it either.
  • afterHide is now called from an effect, as afterShow already is. It used to be called from the timer or the transitionend handler.
  • A transitionend for a property other than opacity no longer cancels the fallback timer. On master that left the tooltip in the DOM when no opacity transition followed.
  • A clickable tooltip stops taking the pointer when it starts to fade out, about 150 ms earlier than before.

A correction to the issue

The first sketch in #1292 removed the tooltip straight from a timer. That is wrong: after a long task the timer can run before a pending setShow(true) renders, and the tooltip is removed just as it shows. In Chromium it lost the tooltip 3 of 10 times, and four existing tests failed. This PR decides in an effect instead.

Tests

  • src/test/tooltip-removal.spec.js: 10 tests. On master 8 fail. The other 2 pin behavior that must not change (afterHide once when the anchor is removed, none after unmount).
  • Full suite on React 19: 205 pass, 1 fails. imperative ref reports open for numeric zero content fails on master too.
  • Also run on React 16.14, 17 and 18 with scripts/configure-react-version.mjs. Each version fails only the tests that fail on master for that version.
  • On React 18, tooltip props › basic tooltip fails in some full-suite runs on master and on this branch, and passes on its own on both. I did not run it enough times to compare the two rates.
  • ESLint, Prettier, tsc, yarn build and bundlesize pass (react-tooltip.min.mjs 9.18 kB gzip, limit 10 kB).
  • The CSS change is not covered by Jest: the module class names are not applied in jsdom. It is covered by the browser runs above.

Not in this PR

One thing I saw on master and left alone to keep this focused. I can open an issue for it if useful:

  • After Escape closes a clickable tooltip while the pointer is on it, the next hover does not close on leave (10 of 10, same on this branch).

If you would rather have a smaller patch that keeps the logic inline in Tooltip.tsx, I am happy to restack it that way.


I used an AI coding assistant to help write and test this change, and reviewed all of it myself.

Summary by CodeRabbit

  • Bug Fixes
    • Tooltips now close reliably after their hide transition, including when a transition event is missed or the tooltip never becomes visible.
    • Reopening a tooltip while it is closing keeps the same tooltip visible, preventing it from disappearing during rapid interactions.
    • Clickable tooltips only receive pointer events while displayed.
    • Hiding a tooltip after its anchor is removed triggers the hide callback once.

A tooltip is rendered first and shown 10ms later. The fallback that
removes a closed tooltip was armed when `show` changed, so a tooltip
that closed before that timer ran, or that a controlled `isOpen` never
let show, stayed in the DOM with the closing class.

Removal now has its own hook with one rule: a tooltip that is neither
showing nor asked to show leaves the DOM. The timer and the transition
end only mark the removal as due, and it is carried out where `show`
is current. A tooltip that is hovered again while it closes is no
longer removed just before it shows.

Refs ReactTooltip#1292
A clickable tooltip took pointer events from the moment it was rendered:
before it was positioned, before it was visible, and while it faded out.
An invisible tooltip in the top-left corner could be hovered and clicked.

Refs ReactTooltip#1292
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Tooltip removal now handles tooltips closed before their show timer fires. A new hook coordinates removal with transition events and visibility requests. Clickable tooltips receive pointer events only while shown. Tests cover removal and reopening behavior.

Changes: Tooltip lifecycle

Layer / File(s) Summary
Removal lifecycle and integration
src/components/Tooltip/Tooltip.tsx, src/components/Tooltip/use-tooltip-removal.tsx, src/components/Tooltip/core-styles.module.css
useTooltipRemoval coordinates removal through a timer or transition end and checks whether the tooltip is visible or requested to show. Tooltip uses the hook, and clickable tooltips receive pointer events only while shown.
Removal and reopening tests
src/test/tooltip-removal.spec.js
Tests cover tooltips closed before showing, controlled tooltips, transition events, anchor removal, unmounting during removal, and reopening while closing.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested labels: Bug

Suggested reviewers: danielbarion

Merge Risk: Minimal · up to bd852

The change removes tooltips that are closed before they show, and limits pointer events on clickable tooltips to the shown state. No actionable merge-blocking issue remains after review.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix: remove a tooltip that closes before it shows" matches the main change. The PR adds use-tooltip-removal.tsx, which removes a tooltip that was rendered but closed before its show timer f…
Linked Issues check Passed Issue #1292 has one active direct link, so its coding requirements apply. (1) A tooltip that is rendered but never shown must leave the DOM. use-tooltip-removal.tsx removes a tooltip when it is neit…
Out of Scope Changes check Passed All changed files serve the #1292 fix or its tests. Tooltip.tsx (+18/-44) delegates removal to the new hook and drops the old missed-transition timer; this is the mechanism for the fix. The `afterHi…
Full details: Title check

Explanation

The title "fix: remove a tooltip that closes before it shows" matches the main change. The PR adds use-tooltip-removal.tsx, which removes a tooltip that was rendered but closed before its show timer fired. The changeset also includes a CSS change that limits pointer-events to shown clickable tooltips, which the title does not mention. That gap is minor because the title names the primary fix from the linked issue.

Full details: Linked Issues check

Explanation

Issue #1292 has one active direct link, so its coding requirements apply. (1) A tooltip that is rendered but never shown must leave the DOM. use-tooltip-removal.tsx removes a tooltip when it is neither show nor showRequested, and it arms the fallback timer only while the tooltip is rendered and not requested to show. The timer and the transitionend handler only set removalDue. A layout effect then removes the tooltip with current state, so the 'closed before show' race (open then close in one tick, or controlled isOpen true then false) ends in removal. (2) Clickable pointer events apply only while shown. core-styles.module.css changes .clickable to .clickable.show for pointer-events: auto, which covers the unpositioned and invisible states named in the issue. The issue's proposed fix used a fixed 200 ms delay; the PR uses the CSS show-transition delay plus 25 ms, which meets the same goal. The author reports browser runs (headless Chromium) and 10 new tests in src/test/tooltip-removal.spec.js, 8 of which fail on master. I did not run the test suite or browser runs. This verdict rests on the code read and the reported results.

Full details: Out of Scope Changes check

Explanation

All changed files serve the #1292 fix or its tests. Tooltip.tsx (+18/-44) delegates removal to the new hook and drops the old missed-transition timer; this is the mechanism for the fix. The afterHide timing change (now called from an effect) and the change so that non-opacity transitionend no longer cancels the fallback follow directly from the new removal logic. The author lists these as behavior changes. The Escape-then-hover issue for clickable tooltips is explicitly excluded from this PR, and the diff does not include it. The test file covers only the removal and reopen behavior. No unrelated change was found.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/components/Tooltip/Tooltip.tsx

Oops! Something went wrong! :(

ESLint: 10.2.0

TypeError [ERR_IMPORT_ATTRIBUTE_MISSING]: Module "file:///.eslintrc.json?mtime=1794524481272" needs an import attribute of "type: json"
at validateAttributes (node:internal/modules/esm/assert:110:15)
at defaultLoadSync (node:internal/modules/esm/load:163:3)
at #loadAndMaybeBlockOnLoaderThread (node:internal/modules/esm/loader:806:12)
at #loadSync (node:internal/modules/esm/loader:838:53)
at ModuleLoader.load (node:internal/modules/esm/loader:787:26)
at ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:498:31)
at #getOrCreateModuleJobAfterResolve (node:internal/modules/esm/loader:564:36)
at afterResolve (node:internal/modules/esm/loader:611:52)
at ModuleLoader.getOrCreateModuleJob (node:internal/modules/esm/loader:617:12)
at node:internal/modules/esm/loader:636:32

src/components/Tooltip/core-styles.module.css

ESLint skipped: the matched ESLint configuration already failed (config-incompatibility).

src/components/Tooltip/use-tooltip-removal.tsx

ESLint skipped: the matched ESLint configuration already failed (config-incompatibility).

  • 1 others

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 12152ffc-4be8-4f87-a817-1b647fb9a86c
Commits

Reviewing files that changed from the base of the PR and between 20b2259 and bd8525c.

Files selected for processing: 4

  • src/components/Tooltip/Tooltip.tsx
  • src/components/Tooltip/core-styles.module.css
  • src/components/Tooltip/use-tooltip-removal.tsx
  • src/test/tooltip-removal.spec.js

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@danielbarion

Copy link
Copy Markdown
Member

Hello!

Thanks for the PR, I do prefer that approach.

About the Esc edge case, I do believe it's ok for now since it's not common to keep the pointer in the tooltip and press Esc, it is an edge case, if it impacts someone and they report it, then I believe it worth it to work on it

@danielbarion
danielbarion merged commit 1903b88 into ReactTooltip:master Nov 13, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] A tooltip closed before its 10 ms show timer fires stays in the DOM, and a clickable one keeps taking clicks

2 participants