Skip to content

fix(Layer): defer a show() that arrives while another popover is mid show/hide - #6941

Merged
cixzhang merged 1 commit into
meta:mainfrom
vjeux:fix/layer-show-reentrancy
Oct 6, 2026
Merged

cixzhang merged 1 commit into
meta:mainfrom
vjeux:fix/layer-show-reentrancy

Conversation

@vjeux

@vjeux vjeux commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

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:

InvalidStateError: Failed to execute 'showPopover' on 'HTMLElement': Invalid to show a popover during another show operation

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

  • Why it occurs. Hiding a popover restores focus to the element that had it — synchronously, inside hidePopover(). The tooltip on that element answers the focusin with useLayer.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.
  • Why it harms the task. The press that should close one surface and reveal its trigger's hint reports an error and leaves no tooltip.
  • Why that matters. Keyboard dismissal is the baseline accessible path and must never throw. useLayer is where every layer's show and hide passes, so it can observe the window.

Expected behavior and authority

  • HTML Standard, show popover steps 2–3 (document-wide "showing popover" flag and "hiding popover nesting count").
  • WCAG 1.4.13 / Tooltip contract: keyboard focus shows the tooltip; one Escape dismisses one layer.

Smallest restoration

useLayer counts, per document, the showPopover() / hidePopover() calls it is running. A show() arriving while the count is non-zero remembers itself in the existing pendingShowRef and 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 clears pendingShowRef, 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

  • Reproduction before the change: the new Chromium spec fails on main with the page error above and the tooltip closed; the three new Vitest cases fail with the same InvalidStateError from a stub mirroring the Standard.
  • Result after the change: all pass — popover closed, focus on the trigger, tooltip shown once, no error.
  • Representative unchanged path: packages/core Vitest suite, 367 files / 10,892 tests green (every layer consumer included).
  • Regression test or other mutation-sensitive proof: useLayer.test.tsx (+2), useTooltip.test.tsx (+1), Tooltip/__tests__/TooltipFocusReturn.a11y.chromium.spec.ts (+1, driving the new a11y/Tooltip focus return story). Each is red with the change reverted.

Scope

  • One defect is restored.
  • Separable contradictory or unsettled tagalongs were removed or split.
  • No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
  • Public text and artifacts contain no internal Meta context.

Testing

pnpm vitest run --project ui packages/core/src/Layer packages/core/src/Tooltip   # 104 passed
pnpm vitest run --project ui packages/core                                       # 367 files, 10892 passed
pnpm storybook:build && pnpm exec playwright test --project=chromium TooltipFocusReturn.a11y.chromium.spec.ts

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 on main, green here. pnpm check:repo, tsc, eslint clean.

…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.
@vercel

vercel Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Oct 4, 2026 10:01pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 4, 2026
@astracat-bot

astracat-bot Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Oct 4, 2026, 10:09 PM UTC

@astracat-bot astracat-bot Bot left a comment

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.

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]

@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR

🧪 Sandbox Preview

View Sandbox for this PR

Modified Components

Tooltip (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 544 -
Complexity N/A Very High (56) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.9KB 1.2KB

Accessibility Audit

Status: No accessibility violations detected.

Visual Regression

14 of 420 shot(s) changed.

A change here is a question, not a failure: check whether the after is the
picture you intended. Record that review on the PR. Baseline maintenance is an
explicit dispatch of CI; this report never rewrites the baseline or adds a release gate.

component story theme mode pixels
Chat Full AI Chat probe light 155,733
Chat Full AI Chat probe dark 155,005
Chat Default neutral dark 33,205
Chat Default neutral light 32,876
Chat Empty State probe light 23,531
Chat Empty State probe dark 22,622
ChatAutoScroll Scroll Behavior Comparison neutral dark 15,378
ChatAutoScroll Scroll Behavior Comparison neutral light 14,107
Chat With Attachments probe light 13,229
Chat With Attachments probe dark 11,065
Markdown Default probe dark 897
Markdown Default probe light 897
Markdown Default neutral dark 882
Markdown Default neutral light 858

Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@cixzhang
cixzhang merged commit d4d2151 into meta:main Oct 6, 2026
39 checks passed
@vjeux
vjeux deleted the fix/layer-show-reentrancy branch October 7, 2026 13:08
vjeux added a commit to vjeux/astryx that referenced this pull request Oct 26, 2026
…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.
vjeux added a commit to vjeux/astryx that referenced this pull request Oct 27, 2026
…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.
cixzhang pushed a commit that referenced this pull request Oct 31, 2026
…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.

This branch was successfully deployed

1 active deployment
Preview — b2ceb13e Deployed Oct 4, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants