Skip to content

MudTooltip: Only open on keyboard-visible focus - #14073

Merged
versile2 merged 6 commits into
MudBlazor:devfrom
versile2:fix/tooltip-focus-visible-13553
Nov 7, 2026
Merged

versile2 merged 6 commits into
MudBlazor:devfrom
versile2:fix/tooltip-focus-visible-13553

Conversation

@versile2

@versile2 versile2 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

ShowOnFocus opened the tooltip on every focusin, including the focus the browser restores when you switch back to the tab and the focus a dialog restores to its trigger after closing. Nothing then hovered or moved focus away, so those tooltips stayed open until the user clicked elsewhere.
The tooltip now opens on focus only when the focused element matches :focus-visible, the browser's own signal for keyboard focus. MUI's Tooltip uses the same check.

Fixes #13553
Fixes #4319
Related to #7962, #8207

Changes

  • MudTooltip: on focusin, ask the browser whether the focused element matches :focus-visible (new mudElementRef.isFocusVisible) and open only if it does. For text inputs, isFocusVisible also returns false when a pointer press moved focus there (recorded per element with capture keydown/pointerdown/focusin listeners; Ctrl/Alt/Meta chords ignored). A version counter drops a result that arrives after focus has left. No interop call when ShowOnFocus is off, the tooltip is already open, or it is disabled or has nothing to show.
  • ShowOnClick now opens on the first click. Before, the click's focus opened the tooltip and the pointer-up toggle closed it again.
  • ShowOnFocus XML docs and the Activation Events docs describe the :focus-visible behavior. The example's "Focus the element" button, which focused by script after a click, is replaced with a Tab hint.
  • bUnit tests: opens on focus-visible, stays closed otherwise, focus leaving before the check completes, no interop when nothing can show, ShowOnClick first click. Existing focus tests set the interop result explicitly.
  • No public API changes.

Behavior change

  • Focus moved by script after a pointer action, such as FocusAsync() from a button click, no longer opens the tooltip. Keyboard focus still does. To show a tooltip from code, set or bind Visible.
  • Touch: tapping a tooltip-wrapped button no longer opens its tooltip through focus, since touch focus is not :focus-visible.
  • Text inputs: browsers mark every text-input focus :focus-visible, so for text inputs the tooltip also checks which input moved focus there. Focus from a click or tap doesn't open it, even if the user then types; returning to the browser tab keeps the original focus type. Tab-key focus still opens it. Hover still opens it (Which will almost always fire before and after a click)

Visual Evidence

Viewer (WASM), Chrome 155, 1280x900. Measured results below; before/after screenshots will be added as attachments.

Scenario Before (dev) After
Click trigger, switch tab, return tooltip stuck open, 3/3 runs closed, 3/3
Open dialog from trigger, close with mouse (Chrome, Firefox) tooltip stuck open, 3/3 closed, 3/3
ShowOnClick first click (Chrome, Firefox) opens then closes opens
Text field (Docs TextField, Debounce Interval): click, type, switch tab, return tooltip stuck open closed
Keyboard Tab in / out, MudTabs arrow keys, hover correct correct, unchanged

Tested With

  • dotnet test --project src/MudBlazor.UnitTests/MudBlazor.UnitTests.csproj (full suite: 0 failed; ToolTip filter 57/57; ToolTip/TooltipViewer/Tabs/DynamicTabs/ColorPicker/Table/Dialog/FocusTrap/ButtonGroup/ChartToolTip filter 554/554). New tests fail on dev.
  • dotnet test --project src/MudBlazor.UnitTests.Docs/MudBlazor.UnitTests.Docs.csproj -p:GenerateDocsTests=true (1423/1423)
  • dotnet build of the Viewer (Bun lint, tsc, bun tests 53/53) and of src/MudBlazor for net8.0, net9.0, net10.0: 0 warnings
  • dotnet format whitespace --folder --include <changed files>
  • Playwright, Chrome 155 and Firefox 151, Viewer Release and Debug plus Docs.Server (Interactive Server): hover, click, keyboard Tab/Shift+Tab, tab return, dialog close (pointer, Escape, keyboard), MudTabs arrow keys, text fields, popovers (Autocomplete, Select, Menu), virtualized Table/DataGrid rows, conditional rendering, focus trap cycles, navigation and forced circuit disconnect. Before and after, mudElementRef call counts were identical on 34 Viewer routes except the new isFocusVisible, and console errors were identical.

Notes

  • Interactive Server: a keyboard-focus tooltip now appears one round trip later, because the :focus-visible check is a JS call (localhost median 12.9 ms before, 13.7 ms after).
  • Firefox: after a dialog opened with the pointer is closed with Escape, Firefox does not mark the restored trigger :focus-visible (Chrome does), so the tooltip follows each browser's focus ring.
  • If :focus-visible is unsupported or a stale MudBlazor.min.js lacks the new function, Release builds fall back to the previous open-on-every-focus behavior.
  • Edge cases measured with an artificial 300 ms delay: if the tooltip is removed while its check is pending, a bound VisibleChanged can fire after removal (same end state as dev); after a dropped Server connection, a stale tooltip can open when the interop call times out instead of immediately as on dev.
  • Overlaps MudTooltip: Fix flex/grid layouts by making the wrapper layout-transparent #13310 only textually in MudTooltip.razor.cs.
  • Out of scope: Escape-to-dismiss and hoverable tooltip content (WCAG 1.4.13).
  • Not verified: Safari/WebKit, real touch hardware, MAUI.

-- Coded by Codebringer / AI
-- Codebringer for versile2
-- versile2 requested changes

Browsers raise focusin when a browser tab regains focus and when a closing
dialog restores focus to its trigger, so a tooltip opened by an earlier click
reappeared and stayed open without user intent (MudBlazor#13553, MudBlazor#4319).

ShowOnFocus now opens the tooltip only when the focused element matches
:focus-visible, checked through a stateless mudElementRef.isFocusVisible().
Unsupported selectors and failed interop fall back to the previous behavior.
A focus version drops a pending check once focus has left, and the check is
skipped when the tooltip is already visible or ShowOnFocus is off.

With ShowOnClick, the focus from a pointer press no longer opens the tooltip
ahead of pointer up, so the first click opens it instead of toggling it shut.
@mudbot mudbot Bot added the bug Unexpected behavior or functionality not working as intended label Oct 20, 2026
@versile2

Copy link
Copy Markdown
Contributor Author

@sahilsao thanks for reporting #13553. This draft PR should fix it, and since you hit it in your own app, would you be able to test the branch against your scenario?

Branch: versile2:fix/tooltip-focus-visible-13553

git fetch https://github.com/versile2/MudBlazor.git fix/tooltip-focus-visible-13553
git checkout FETCH_HEAD

Then reference src/MudBlazor/MudBlazor.csproj from your app in place of the package. With the fix, a tooltip opens on focus only when the browser treats that focus as keyboard focus (:focus-visible). Clicking a button, switching tabs and coming back should no longer leave its tooltip stuck open. Tabbing to it with the keyboard should still show it, and hover works as before.

Before/after screenshots (Chrome):

Click, switch tab, return

Before After
1-before-tab-return-after-click 1-after-tab-return-after-click

Close a dialog with the mouse (focus returns to the trigger)

Before After
2-before-dialog-close-mouse 2-after-dialog-close-mouse

Keyboard Tab onto the trigger (should still open)

Before After
3-before-keyboard-tab-in 3-after-keyboard-tab-in

-- Coded by Codebringer / AI
-- Codebringer for versile2
-- versile2 requested changes

@sahilsao

Copy link
Copy Markdown

Looks Perfect, Thanks for the fix.

@versile2

Copy link
Copy Markdown
Contributor Author

Looks Perfect, Thanks for the fix.

doing a visual check now then I'll submit. Did you pull and test it?

Port isFocusVisible to the TypeScript mudElementReference, add its bun test, and record it in the API surface snapshot.
@sahilsao

Copy link
Copy Markdown

Looks Perfect, Thanks for the fix.

doing a visual check now then I'll submit. Did you pull and test it?

Not tested in my machine some error occurs unable to start the project but based on the screenshots i think the problem is fixed.

@versile2
versile2 marked this pull request as ready for review October 31, 2026 01:16

@mudbot mudbot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I found one problem. The component change itself looks correct. The version counter drops a check result that comes back after focus has left. No interop call is made when the tooltip can't open. Tests run with loose JS interop, so existing tooltip tests that never set up isFocusVisible get false and don't break. The TS function, its test, the API snapshot and the XML docs all match the new behavior. The problem is that the new Activation Events text on the docs page says a pointer click never opens the tooltip, which is wrong for text inputs. There were no earlier findings to account for.


Docs page says a click never opens the tooltip, but text inputs still do

This sentence says that "a pointer click or focus that returns after a pointer action ... does not open it". Text inputs always match :focus-visible, though. So a tooltip around a MudTextField still opens on click and when you return to the browser tab. The ShowOnFocus XML docs already say this ("or any focus on a text input"), and the PR's Behavior change section does too.

AGENTS.md says docs descriptions must agree with current behavior. Please add the text-input exception here, for example: "...such as moving focus with the Tab key or focusing a text input, so a pointer click on a button...".

src/MudBlazor.Docs/Pages/Components/Tooltip/TooltipPage.razor:92

@mudbot mudbot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The earlier docs finding is fixed. The diff now records which input moved focus to each element, and treats pointer focus on a text input as not keyboard focus. The docs page, the ShowOnFocus XML docs and the TS tests all match that. The C# side looks correct: the version counter drops results that arrive after focus has left, and there's no interop call when the tooltip can't open. One problem is left. The PR description's Behavior change section says text fields still open their tooltips on click, which the code no longer does, so a real breaking change is described the wrong way round.


PR description says the opposite of what the code does for text inputs

This line returns false when a pointer press moved focus to a text input, textarea or contenteditable element. So a ShowOnFocus tooltip around a MudTextField no longer opens on click or tap, or when you come back to the browser tab after clicking into the field. The docs page and XML docs in this diff say so ("A click or tap, including on a text input, does not open it"), and so does the bun test isFocusVisible for a text input after a pointer press.

The PR description's Behavior change section still says "Text inputs always match :focus-visible, so tooltips around text fields still open on click and on tab return, as before." That is no longer true. It's also a user-visible breaking change for every tooltip-wrapped input. AGENTS.md says breaking changes must be called out explicitly in the PR description. Please update that bullet so it says text fields no longer open their tooltip on pointer focus. Also re-check the Visual Evidence and Tested With claims about text fields, since they may have been measured before this change.

src/MudBlazor/TScripts/mudElementReference.ts:611

@danielchalmers danielchalmers left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖 Claude: Tested on head 77e9d6d in Chrome with real mouse and keyboard input, driven over raw CDP with real tab activation (so the page sees blur, visibilitychange, focus and a second focusin on return).

Tab-switch return, same scratch page on dev and on this branch:

  • Click a ShowOnFocus button, move the mouse away, switch tab, return: dev leaves the tooltip open, this branch leaves it closed.
  • Same with a button that has hover off: dev open, branch closed.
  • Click into a text input and type, switch tab, return: dev open, branch closed.
  • Keyboard Tab onto a button: tooltip opens, and is still open after switching tab and returning, on both.

Also checked with Playwright in Chromium, all matching the description: Tab and Shift+Tab open and close the tooltip, text input click does not open it but Tab into it does, ShowOnClick opens on the first click and closes on the second, closing a dialog with the mouse leaves no tooltip on the restored trigger, script focus after a click does not open it, and touch tap on a focus tooltip does not open it. No console errors.

Not tested: Firefox, Safari, real touch hardware, MAUI, and Escape-to-close on a dialog.

The :focus-visible gate matches what MUI and Floating UI do, and Radix has the same stuck-tooltip bug open (radix-ui/primitives#1800) with the same fix proposed. I did not find another library that uses pointer tracking for text inputs, but it is needed since browsers always treat a focused text input as :focus-visible.

One thing left from mudbot's last review: the Behavior change section of the description says text fields still open their tooltip on click, which the code no longer does.

@versile2

versile2 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

🤖 Claude: Tested on head 77e9d6d in Chrome with real mouse and keyboard input, driven over raw CDP with real tab activation (so the page sees blur, visibilitychange, focus and a second focusin on return).

Tab-switch return, same scratch page on dev and on this branch:

  • Click a ShowOnFocus button, move the mouse away, switch tab, return: dev leaves the tooltip open, this branch leaves it closed.
  • Same with a button that has hover off: dev open, branch closed.
  • Click into a text input and type, switch tab, return: dev open, branch closed.
  • Keyboard Tab onto a button: tooltip opens, and is still open after switching tab and returning, on both.

Also checked with Playwright in Chromium, all matching the description: Tab and Shift+Tab open and close the tooltip, text input click does not open it but Tab into it does, ShowOnClick opens on the first click and closes on the second, closing a dialog with the mouse leaves no tooltip on the restored trigger, script focus after a click does not open it, and touch tap on a focus tooltip does not open it. No console errors.

Not tested: Firefox, Safari, real touch hardware, MAUI, and Escape-to-close on a dialog.

The :focus-visible gate matches what MUI and Floating UI do, and Radix has the same stuck-tooltip bug open (radix-ui/primitives#1800) with the same fix proposed. I did not find another library that uses pointer tracking for text inputs, but it is needed since browsers always treat a focused text input as :focus-visible.

One thing left from mudbot's last review: the Behavior change section of the description says text fields still open their tooltip on click, which the code no longer does.

Edit made. Thanks!

This was referenced Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Unexpected behavior or functionality not working as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tooltip remains visible after switching browser tabs Tooltip remains active after clicking child element and switching browser tabs

3 participants