Skip to content

fix(core): TopNavMegaMenu hover-then-click no longer dismisses the panel (#3121) - #4555

Merged
imdreamrunner merged 3 commits into
mainfrom
claude/fix/topnav-mega-menu-click-guard
Aug 11, 2026
Merged

imdreamrunner merged 3 commits into
mainfrom
claude/fix/topnav-mega-menu-click-guard

Conversation

@imdreamrunner

@imdreamrunner imdreamrunner commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3121.

Summary

The Top Nav mega menu flickered shut when you hovered a nav item and then clicked it—the click that naturally follows a hover dismissed the menu.

Root cause: the panel is a native popover="auto", and the trigger button sits outside it. Without a native invoker relationship, the browser can light-dismiss the hover-opened panel during the trigger's pointer interaction, before React's click guard decides what the click means. jsdom does not implement this native event ordering, so unit tests alone cannot reproduce the original failure.

Fix (packages/core/src/TopNav/TopNavMegaMenu.tsx):

  • Keep the panel as popover="auto" so native outside-click dismissal, Escape handling, focus restoration, and sibling-popover exclusivity remain intact.
  • Register the trigger as the panel's native invoker with popoverTarget, which exempts the trigger interaction from auto-popover light-dismiss.
  • Cancel the invoker's default toggle with preventDefault() so the component's interaction logic remains the single source of truth.
  • Apply a Vercel-style 500 ms hover→click guard: a click shortly after a hover-open confirms and pins the panel instead of closing it.
  • Click and keyboard opens are pinned and persist past mouse-leave; hover opens remain transient. Enter/Space opens and focuses the panel, while Escape closes and restores focus.

No public API change.

Test plan

Automated

  • packages/core/src/TopNav/TopNavMegaMenu.test.tsx: 37 passing, covering the 500 ms guard, pinned versus transient behavior, native-invoker wiring, default-toggle cancellation, native-dismiss state reconciliation, sibling-popover exclusivity, panel exit-hover behavior, and keyboard behavior.
  • Full packages/core/src/TopNav suite: 103 passing.
  • pnpm -F @astryxdesign/core typecheck and ESLint: clean.

The unit tests intentionally lock in the required wiring, but jsdom cannot exercise native popoverTarget/light-dismiss ordering. The interaction below was therefore verified in a real Chromium browser against the Storybook core-topnavmenu--mega-menu story.

Real browser

Interaction Result
hover → immediate click stays open
second click closes
hover → wait over 500 ms → click closes
hover → move away closes (transient)
click → move away stays open (pinned)
click outside closes
open one mega menu → open a sibling first menu closes
Enter / Space → open, Escape → close works; focus returns to trigger

Browsers without the Popover API retain the component's existing degraded useLayer fallback; this PR does not add a legacy outside-dismiss polyfill.

Before

Hover → immediate click dismisses the menu:

Mega.Menu.repro.mov

After

Hover → immediate click keeps the menu open:

Mega.Menu.fix.mov

@vercel

vercel Bot commented Jul 30, 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 7, 2026 5:10pm

Request Review

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

github-actions Bot commented Jul 30, 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

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

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

@imdreamrunner

imdreamrunner commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Just found #3851 (thanks Cindy for sharing that). will iterate on that PR since that's older

Register the trigger as the native auto-popover invoker and add a 500 ms hover-to-click confirmation guard. Preserve native light dismiss, Escape behavior, focus restoration, and sibling-popover exclusivity while pinning deliberate click and keyboard opens.
@imdreamrunner
imdreamrunner force-pushed the claude/fix/topnav-mega-menu-click-guard branch from 3f05972 to f2261bd Compare August 4, 2026 16:58
@imdreamrunner
imdreamrunner marked this pull request as ready for review August 4, 2026 17:07
github-actions Bot added a commit that referenced this pull request Aug 4, 2026
@imdreamrunner

Copy link
Copy Markdown
Contributor Author

#3851 hasn't been updated for a while so I am reopening this PR

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

Hmm trying in the deployment but seems like it doesn't open the mega menu at all, so let me take a closer look.

@github-actions
github-actions Bot requested a review from cixzhang August 7, 2026 09:53
@github-actions github-actions Bot added the needs:design-review Affects visuals — Design should review label Aug 7, 2026
@imdreamrunner

imdreamrunner commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor Author

@cixzhang Thanks for reviewing!

Follow-up on the docsite issue:

Why Storybook worked while the docsite did not:

  • Storybook renders TopNavMegaMenu inside its required TopNav.
  • The docsite rendered it standalone.
  • The closed panel also forced display: flex, creating an invisible layer that intercepted the trigger.

Commit b40fee9f fixes this by:

  • Wrapping the docsite rendering in TopNav.
  • Applying display: flex only under :popover-open.
  • Testing that hover-then-click retains the existing pinned behavior.

We also discovered that this PR fixes the production issue tracked in #4785: hovering a closed panel’s invisible area could reopen it. The PR separates the panel and trigger hover handlers so only the trigger can schedule an open.

Verified in Chrome; 37 component tests and 303 docsite tests pass.

github-actions Bot added a commit that referenced this pull request Aug 7, 2026
@imdreamrunner
imdreamrunner merged commit e19719c into main Aug 11, 2026
21 checks passed
@github-actions
github-actions Bot deleted the claude/fix/topnav-mega-menu-click-guard branch August 13, 2026 06:36
cixzhang added a commit that referenced this pull request Aug 14, 2026
…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.
cixzhang added a commit that referenced this pull request Aug 14, 2026
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.
cixzhang added a commit that referenced this pull request Aug 15, 2026
#4936)

* fix(hooks): useMenuHover hover-then-click guard, synchronous focus, invoker 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.

* refactor(core): put every hover menu on useMenuHover

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.

* test(core): cover the shared hover-menu contract and its a11y

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.

* fix(hooks): a closed menu no longer reopens under a stationary pointer

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.

* fix(hooks): move close bookkeeping out of render; trim comments

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.

* fix(hooks): let a pointer-only consumer keep its own focus and toggle

#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 added a commit that referenced this pull request Aug 15, 2026
…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.
cixzhang added a commit that referenced this pull request Aug 15, 2026
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.

This branch was successfully deployed

1 active deployment
Preview — 0077070f Deployed Aug 7, 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. needs:design-review Affects visuals — Design should review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Minor UX suggestion for Top Nav Mega Menu trigger behavior

2 participants