Skip to content

fix(core): consolidate the hover→click guard into useMenuHover (#3121) - #4936

Merged
cixzhang merged 7 commits into
mainfrom
fix/menu-hover-click-guard
Aug 15, 2026
Merged

cixzhang merged 7 commits into
mainfrom
fix/menu-hover-click-guard

Conversation

@cixzhang

Copy link
Copy Markdown
Contributor

Consolidates the hover-then-click interaction that #4555 fixed locally in TopNavMegaMenu into the shared useMenuHover hook — because the same bug was live in every other consumer.

Fixes the #3121 shape for TopNavMenu, TopNavHeading, SideNavHeading and DropdownMenuSubMenu; TopNavMegaMenu now runs on the shared machine instead of its own copy.

Why

I reproduced the bug rather than assuming it. Against main, with matchMedia reporting a hover-capable pointer:

component after hover after the click that follows
TopNavMenu open closed
SideNavHeading open closed
TopNavHeading open closed
TopNavMegaMenu (post-#4555) open open ✅

No unit test caught it because the shared jsdom setup reported matches: false for every media query, so useMediaQuery('(hover: hover)') made the hook's entire hover path unreachable in tests. That setup now reports (hover: hover) as matching, which is what a desktop browser reports.

What moved into the hook

  • The hover→click guard — within clickGuardMs (500, matching fix(core): TopNavMegaMenu hover-then-click no longer dismisses the panel (#3121) #4555) of a hover-open, a click confirms and pins the menu instead of dismissing it.
  • Pinned vs transient — stickyRef was the inverse of the hook's existing hoverModeRef; one concept, one name now. Re-entering the trigger of an open menu no longer un-pins it or re-arms the guard.
  • Native invoker wiring — popoverId makes the hook stamp popoverTarget on the trigger and cancel the invoker's default toggle, so an auto popover's light dismiss can't close the menu before the guard sees the click. Documented in the hook's docblock, alongside when a consumer does not need it (popover="manual", or a trigger inside the panel).
  • Keyboard activation — always opens and moves focus in, never toggles an open menu shut. A hover-open leaves focus on the trigger, so that is the one state where a keyboard user can activate the trigger of an open menu; closing there would strand them.

Focus, and no requestAnimationFrame

Focus now moves synchronously in the event handler. useLayer.show() calls showPopover() in the same tick and the layer's children are always mounted, so the items are focusable the moment show() returns — the deferred focus was unnecessary. Removed from useMenuHover and DropdownMenuSubMenu.

The hook also passes skipAutoFocus: true on click-open and owns focus itself: usePopover's auto-focus targets the first tabbable node, a menu wants its first item, and menu items are tabindex="-1" under roving tabindex — so both were running and focusing different elements a frame apart.

Closing now returns focus to the trigger instead of dropping it to <body> (usePopover tracked the trigger element for exactly this and never used it). The nav headings' in-panel heading replica is a close affordance, not the trigger, so it routes through the hook's close() rather than borrowing trigger-toggle semantics.

Found in the browser, not in jsdom

A panel positioned over its own trigger (the nav headings) puts the trigger back under a stationary pointer when it closes, and the browser fires a fresh mouseenter. With a zero show delay the menu reopened instantly — Escape and the in-panel close button looked inert. The old one-shot "skip next mouseenter" is now a 300 ms window after any close, so that enter is ignored while a deliberate re-hover seconds later still opens the menu.

Test plan

Unit — 21 new hook tests plus per-component guard/focus tests for both nav headings and the dropdown submenu, all a11y-shaped: who holds focus after each way of opening and closing, keyboard activation on an open menu, Escape restore, and that a device with no hover-capable pointer cannot be tricked into opening a menu. packages/core: 6192 passing (the 6 failures are pre-existing perf-budget and load flakes — they fail on main too and pass in isolation in both trees).

Real Chromium (Playwright against local Storybook — jsdom implements neither light dismiss nor invokers):

TopNavMenu TopNavMegaMenu
hover opens ✅ ✅
hover → immediate click stays open ✅ ✅
pinned: stays open when pointer leaves ✅ ✅
later click closes ✅ ✅
hover-open → leave closes ✅ ✅
Enter opens, focus moves into panel ✅ ✅
Escape closes, focus returns to trigger ✅ ✅

Known gaps (not addressed here)

  • SideNavHeading's menu contract is unspecified. Its popup has no role="menu" and the story passes plain links, so [role="menuitem"] matches nothing: Enter opens the panel but focus never enters it, and arrow keys do nothing. Pre-existing, and a component-contract decision rather than a hook fix.
  • useMenuHover still bundles a useListFocus instance that TopNavMenu and DropdownMenuSubMenu override with their own (they need typeahead, boundary scoping, roving tabindex). Hover intent and the keyboard model should probably be separate hooks.
  • requestAnimationFrame-deferred focus remains in usePopover, DropdownMenu, ContextMenu and TabMenu. usePopover is the root one and the highest-risk to change.

@vercel

vercel Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Aug 15, 2026 2:24pm

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 12, 2026
@github-actions

github-actions Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

DropdownMenu (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 1402 -
Complexity N/A Very High (152) -
SideNav (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 1950 -
Complexity N/A Very High (186) -
TopNav (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 2065 -
Complexity N/A Very High (150) -

Bundle Size Summary

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

Accessibility Audit

Status: No accessibility violations detected.


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

github-actions Bot added a commit that referenced this pull request Aug 12, 2026
@cixzhang
cixzhang marked this pull request as ready for review August 12, 2026 14:19
github-actions Bot added a commit that referenced this pull request Aug 12, 2026

@imdreamrunner imdreamrunner 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.

This looks good to ship. In TopNavMegaMenu, keyboard activation already focused the first panel link, while a pointer click left focus on the trigger. Moving to useMenuHover now makes the focus transfer synchronous and applies it consistently to both keyboard and pointer activation, including a click that confirms a hover-opened panel. Hover alone still leaves focus unchanged, and Escape reliably returns focus to the trigger. This consistent behavior looks good to me.

As a non-blocking follow-up, the current ARIA model is:

<button
  aria-haspopup="true"
  aria-expanded="false"
  aria-controls="products-panel"
>
  Products
</button>

<div id="products-panel" popover="auto">
  <div role="group" aria-label="Products">
    <a href="…">Analytics</a>
    <a href="…">Messaging</a>
  </div>
</div>

aria-haspopup="true" tells assistive technology to expect a menu, while the popup intentionally contains a group of ordinary navigation links. For the disclosure-navigation model, this would be more consistent:

<button
  aria-expanded="false"
  aria-controls="products-panel"
>
  Products
</button>

<div id="products-panel" popover="auto">
  <div role="group" aria-label="Products">
    <a href="…">Analytics</a>
    <a href="…">Messaging</a>
  </div>
</div>

This ARIA mismatch predates the PR and does not need to block it.

…nvoker wiring

Consolidates the hover/click interaction the TopNavMegaMenu fix (#4555)
solved locally into the shared hook, so every menu that opens on hover
gets it.
TopNavMegaMenu drops its private hover/click machine (#4555) for the
shared hook; DropdownMenuSubMenu takes the guard through confirmHoverOpen
and loses its rAF focus; the nav headings' in-popup dismiss affordances
route through the hook's close so focus returns to the trigger.

jsdom now reports (hover: hover) as matching, which is what a desktop
browser reports — hover paths were unreachable in tests before.
19 hook tests through TopNavMenu plus per-component guard/focus tests for
both nav headings and the dropdown submenu: who holds focus after each way
of opening and closing, keyboard activation on an open menu, Escape
restore, and that a device without a hover-capable pointer cannot be
tricked into opening one.
Found in Chrome, not jsdom: a panel positioned over its own trigger (the
nav headings) puts the trigger back under a pointer that never moved when
it closes, and the browser fires a fresh mouseenter for that. With a zero
show delay the menu reopened instantly, which made Escape and the in-panel
close affordance look inert.

Replaces the one-shot skip-next-mouseenter with a 300ms window after any
close, so the enter caused by the panel vanishing is ignored while a
deliberate re-hover seconds later still opens the menu.
The reopen-suppression timestamp was taken during render, which the purity
lint rule rightly rejects. It moves to a layout effect — which also has to
land before the browser hit-tests the vanished panel, so it is the correct
phase rather than just the legal one.

Comments cut back to what the code cannot say.
@cixzhang
cixzhang force-pushed the fix/menu-hover-click-guard branch from f8731e4 to 84d927a Compare August 14, 2026 01:52
github-actions Bot added a commit that referenced this pull request Aug 14, 2026
Conflicts were both in SideNav, from #4880's audit pass landing on the same
lines this branch touched:
- SideNavHeading popover heading button: keep main's focus-visible outline,
  keep this branch's close-only onClick (it is a dismiss affordance, not the
  trigger).
- SideNav.test.tsx: both sides appended describe blocks; kept both.
#4880 landed SideNavItem on useMenuHover while this branch was rewriting it,
wiring the pointer half only: its flyout is a focus-trapped dialog of links,
not a menu of items, and usePopover owns focus there.

- `ownsFocus` (default true) stops the hook suppressing the popover's
  auto-focus and reaching for a first item that flyout does not have.
- SideNavItem opts out of the hover→click guard: its flyout opens beside the
  rail rather than over the icon, so the click after a hover-open is a
  dismissal, not the #3121 confirmation.
- A real mouseleave now clears the reopen-suppression window, so a deliberate
  leave-and-return reopens immediately rather than waiting it out.
- Its fireEvent clicks say `detail: 1`; a detail-0 click is Enter/Space to
  the hook, which always opens instead of toggling.
@cixzhang
cixzhang merged commit 2abfff4 into main Aug 15, 2026
19 checks passed
github-actions Bot added a commit that referenced this pull request Aug 15, 2026

This branch was successfully deployed

1 active deployment
Preview — 31c22d9d Deployed Aug 15, 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