Repository navigation
fix(Layer): defer a show() that arrives while another popover is mid show/hide - #6941
Conversation
…show/hide Hiding a popover restores focus to its trigger synchronously, inside hidePopover(); a tooltip on that trigger answers the focusin with show(), and the HTML Standard refuses showPopover() while the document is showing or hiding any popover (InvalidStateError, or a silent refusal on engines still rolling the rule out). useLayer now counts its popover operations per document and replays a show() that arrives inside one in a microtask, after the press that started it has unwound.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
There was a problem hiding this comment.
Thanks for narrowing this to the focus-return case — this looks ready.
The fix keeps the behavior users already expect: closing a popover with Escape returns focus to its trigger, and a tooltip on that trigger now shows a moment later instead of failing while the popover is still closing. Cancelling that pending tooltip before it replays also works, and ordinary non-nested shows are unchanged. I checked the new keyboard sequence end to end (Tab shows the tooltip, Enter opens the popover, Escape closes it with focus back on the trigger and the tooltip shown, no page errors) and the focused Layer, Tooltip, and Popover tests pass.
No public API or default changes here, and the browser fixture being checked in as a story means this sequence stays verifiable.
[Automated review]
PR Analysis Report📚 Storybook Preview🧪 Sandbox PreviewModified ComponentsTooltip (@astryxdesign/core) · View in Storybook
Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Visual Regression14 of 420 shot(s) changed. A change here is a question, not a failure: check whether the after is the
Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
…nly a show it refuses The count of a browser-run hide outlasts the engine's own window: it ends at the element's `toggle`, a task after the hide has returned, while the engine accepts shows again as soon as the hide returns. Deferring every show inside the count delayed a press that light-dismisses one layer at pointerup and opens another at click: a DropdownMenu tapped open beside an open Popover no longer moved focus into the menu, since it reads the open right after `show()`. The ledger keeps the layers' own operations and the browser's hides apart. A show inside one of the layers' own operations is still deferred ahead, as meta#6941 did. A show inside a browser-run hide is asked of the browser first and deferred only when the browser refuses it with an `InvalidStateError` (by name, across documents). The replay runs outside the hide, so a replay that fails is not caught; it runs one microtask per parked show, so one that throws reaches the page alone and leaves the rest to run.
…nly a show it refuses The count of a browser-run hide outlasts the engine's own window: it ends at the element's `toggle`, a task after the hide has returned, while the engine accepts shows again as soon as the hide returns. Deferring every show inside the count delayed a press that light-dismisses one layer at pointerup and opens another at click: a DropdownMenu tapped open beside an open Popover no longer moved focus into the menu, since it reads the open right after `show()`. The ledger keeps the layers' own operations and the browser's hides apart. A show inside one of the layers' own operations is still deferred ahead, as meta#6941 did. A show inside a browser-run hide is asked of the browser first and deferred only when the browser refuses it with an `InvalidStateError` (by name, across documents). The replay runs outside the hide, so a replay that fails is not caught; it runs one microtask per parked show, so one that throws reaches the page alone and leaves the rest to run.
…8543) CI note: this head's checks were blocked by the repository's Actions policy (no jobs ran since 17:43Z); they need a re-run. ## Why, what, how to check **Why.** This comes from one product's effort to get its production JavaScript errors to zero: its log shows this one 12 times from 5 people in 7 days (9 on WebKit, 3 on Chrome). Pressing Escape inside an open popover throws it: the browser closes the popover and returns focus to its button, the tooltip there asks to open while the close still runs, and Chromium 151 and 156 refuse (149 does not). The tooltip stays closed. **What led here.** The Popover API refuses a show while any popover shows or hides, and a hide returns focus inside that window. #6941 makes a show wait while `useLayer` runs its own show or hide; a close the browser runs itself (Escape, a tap outside) never entered that bookkeeping. **Options.** (a) Catch the error and drop it: hides a real failure, and the tooltip still never opens. (b) Wait out every browser close before any show: tried first; it delayed a press that closes one menu and opens another, so focus no longer moved into the new menu. (c) Count the browser's close, try the show at once, retry after the close only if the browser refused. Chosen: it fixes the Escape case and keeps every other path as fast as before. **What.** `useLayer` counts the browser's own popover closes in its ledger, from the close's start to its `toggle` event; a show refused during one replays after it, once. **How to check.** Storybook `Core/Layer` → `BrowserHideWakesAFocusLayer`, Chromium 151+: Tab, Enter, Tab, Escape. Before: `InvalidStateError` in the console, no tooltip. After: no error, the tooltip shows. ## Expected behavior and authority `architecture:layer-runtime` (current): browser-initiated closes are observed through the queued `toggle` event; that holds. Restored: #6941. ## Smallest restoration - `popoverOperations.ts` holds the ledger `useLayer.tsx` kept privately, plus the browser-close count and the replay, tested alone there. No public API, default, record or visual change. ## Scope - [x] One defect is restored. - [x] Separable contradictory or unsettled tagalongs were removed or split. - [x] No new public API, visual representation, default, or interaction model is hidden under the bug-fix label. - [x] Public text and artifacts contain no internal Meta context. ## Testing - Unit tests for Layer, its ledger, Tooltip, DropdownMenu, Popover, Drawer and Selector: 7 files, 652 passed. Without the fix, `useLayer.test.tsx` "defers the show a browser-run hide wakes…" and "keeps counting…" fail; with option (b), "opens a layer at once…" fails; `popoverOperations.test.ts` "ends each hide exactly once…" fails without `clearTimeout`. - Type check, repository checks, lint, formatting: clean. - Browser spec `LayerBrowserHide.a11y.chromium.spec.ts` (3 cases) on this branch's Storybook build: 3 passed. CI's Playwright runs Chromium 149, which allows these shows, so there the spec passes on main too. On Chromium 156 the story's steps throw `InvalidStateError … during another show operation` on main, tooltip hidden, and show it with no error here; a tap outside closes the layer with no error on both; the press that closes one layer and opens another focuses the new one at once. - Open PRs #7732, #8381 and #8014 touch nothing under `packages/core/src/Layer/`; a trial merge with #7732 is clean; the other two conflict with main on their own.
User impact
Anyone who closes a popover from the keyboard when its trigger also carries a tooltip (a toolbar button with a hint that opens a menu). Escape — or any content handler that calls the layer's
hide()— throws on the page:Engines still rolling the rule out refuse silently instead, leaving the hook "open" with nothing shown. Either way the keyboard user loses the tooltip that focus should have shown, and suites gating on page errors go red.
Problem and solution fit
hidePopover(). The tooltip on that element answers thefocusinwithuseLayer.show()→showPopover()while the document is still hiding the other popover, and the HTML Standard's show popover throws exactly then ("If document's showing popover is true, or document's hiding popover nesting count is not 0 … throw an InvalidStateError"). The rule is document-wide; a per-element guard cannot see it.useLayeris where every layer's show and hide passes, so it can observe the window.Expected behavior and authority
Smallest restoration
useLayercounts, per document, theshowPopover()/hidePopover()calls it is running. Ashow()arriving while the count is non-zero remembers itself in the existingpendingShowRefand replays in a microtask — after the script that started the operation and the key event it answered have unwound, so the Escape that closed the popover is not also taken by the tooltip it reveals.hide()already clearspendingShowRef, so a cancelled deferral never fires.No public API, default, visual, or interaction-model change; un-nested shows stay synchronous and the no-Popover-API fallback is untouched.
Evidence
mainwith the page error above and the tooltip closed; the three new Vitest cases fail with the sameInvalidStateErrorfrom a stub mirroring the Standard.packages/coreVitest suite, 367 files / 10,892 tests green (every layer consumer included).useLayer.test.tsx(+2),useTooltip.test.tsx(+1),Tooltip/__tests__/TooltipFocusReturn.a11y.chromium.spec.ts(+1, driving the newa11y/Tooltip focus returnstory). Each is red with the change reverted.Scope
Testing
The Chromium spec launches with
--enable-blink-features=PopoverHintNewBehavior,PopoverHintNestedShowException: Playwright's Chromium 149 carries the Standard's rule behind those features, and without them the nested show silently succeeds. Red onmain, green here.pnpm check:repo,tsc,eslintclean.