Repository navigation
MudTooltip: Only open on keyboard-visible focus - #14073
Conversation
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.
|
@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: Then reference Before/after screenshots (Chrome): Click, switch tab, return
Close a dialog with the mouse (focus returns to the trigger)
Keyboard Tab onto the trigger (should still open)
-- Coded by Codebringer / AI |
|
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.
Not tested in my machine some error occurs unable to start the project but based on the screenshots i think the problem is fixed. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
🤖 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! |






Summary
ShowOnFocusopened the tooltip on everyfocusin, 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: onfocusin, ask the browser whether the focused element matches:focus-visible(newmudElementRef.isFocusVisible) and open only if it does. For text inputs,isFocusVisiblealso returns false when a pointer press moved focus there (recorded per element with capturekeydown/pointerdown/focusinlisteners; Ctrl/Alt/Meta chords ignored). A version counter drops a result that arrives after focus has left. No interop call whenShowOnFocusis off, the tooltip is already open, or it is disabled or has nothing to show.ShowOnClicknow opens on the first click. Before, the click's focus opened the tooltip and the pointer-up toggle closed it again.ShowOnFocusXML docs and the Activation Events docs describe the:focus-visiblebehavior. The example's "Focus the element" button, which focused by script after a click, is replaced with a Tab hint.ShowOnClickfirst click. Existing focus tests set the interop result explicitly.Behavior change
FocusAsync()from a button click, no longer opens the tooltip. Keyboard focus still does. To show a tooltip from code, set or bindVisible.:focus-visible.: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.
ShowOnClickfirst click (Chrome, Firefox)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 buildof the Viewer (Bun lint, tsc, bun tests 53/53) and ofsrc/MudBlazorfor net8.0, net9.0, net10.0: 0 warningsdotnet format whitespace --folder --include <changed files>mudElementRefcall counts were identical on 34 Viewer routes except the newisFocusVisible, and console errors were identical.Notes
:focus-visiblecheck is a JS call (localhost median 12.9 ms before, 13.7 ms after).:focus-visible(Chrome does), so the tooltip follows each browser's focus ring.:focus-visibleis unsupported or a staleMudBlazor.min.jslacks the new function, Release builds fall back to the previous open-on-every-focus behavior.VisibleChangedcan 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.MudTooltip.razor.cs.-- Coded by Codebringer / AI
-- Codebringer for versile2
-- versile2 requested changes