Skip to content

fix(top-nav): ignore trigger click immediately after hover-open - #3851

Closed
rizkydwicmt wants to merge 1 commit into
meta:mainfrom
rizkydwicmt:fix/top-nav-hover-click-guard
Closed

rizkydwicmt wants to merge 1 commit into
meta:mainfrom
rizkydwicmt:fix/top-nav-hover-click-guard

Conversation

@rizkydwicmt

Copy link
Copy Markdown

Problem

Fixes #3121.

The top-nav menus open on both hover and click. On a mouse, hover-then-click is one natural gesture — but a naive toggle means the click that follows a hover-open immediately closes the menu ("the menu blinks").

Approach

The Vercel-style ~500 ms click guard described (and approved) in the issue thread:

  • The menu records when a hover opened it (hoverOpenedAtRef, stamped only on a hover-driven closed→open transition).
  • A click on an already-open menu is swallowed if that hover-open happened less than HOVER_CLICK_GUARD_MS (500 ms) ago; otherwise it closes the menu as before.

Why this stays safe on other input modes:

  • Touch: no hover, so the timestamp stays 0 and taps toggle open/close cleanly.
  • Keyboard: never enters hover mode / never stamps, so Enter/Space toggling is unchanged.
  • Click-open → click-close: the click-open path never stamps, so it still toggles normally.
  • Re-hovering an open menu does not re-arm the guard (timestamp is set only when hover actually opens), and the timestamp is reset on close so a stale value can't swallow a later click.

The guard is applied in both places that implement hover+click triggers:

  • packages/core/src/hooks/useMenuHover.ts — shared hook used by TopNavHeading, TopNavMenu, and SideNavHeading.
  • packages/core/src/TopNav/TopNavMegaMenu.tsx — DefaultMegaMenu has its own hover/click handlers (it does not use the hook), and is the component the issue reproduces on.

No public API changes; the guard is internal with a 500 ms constant shared between both code paths.

Tests

  • TopNav.test.tsx (TopNavHeading via useMenuHover): hover-open + immediate click stays open; click after the guard window closes; re-hovering an open menu does not re-arm the guard.
  • TopNavMegaMenu.test.tsx: hover-open + immediate click stays open; click after the guard window closes; click-open → click-close still toggles.
  • Manually verified in Storybook (Core/TopNavMenu → MegaMenu): hover + immediate click keeps the panel open; delayed click closes; keyboard and hover-away dismissal unchanged.

🤖 Generated with Claude Code

@rizkydwicmt
rizkydwicmt requested a review from cixzhang as a code owner July 12, 2026 00:33
Copilot AI review requested due to automatic review settings July 12, 2026 00:33
@vercel

vercel Bot commented Jul 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, Comment Jul 28, 2026 9:35pm

Request Review

@meta-cla

meta-cla Bot commented Jul 12, 2026

Copy link
Copy Markdown

Hi @rizkydwicmt!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Jul 12, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

🟡 Maintainer judgment recommended — behavior change in a core navigation interaction (hover + click) that can affect UX across consumers.

Triage: bug fix · non-breaking · moderate blast radius (TopNav) → standard path

Changes:

  • Add a ~500ms guard to ignore trigger clicks immediately after a hover-driven open (prevents “blink” close).
  • Apply the same guard to TopNavMegaMenu’s bespoke hover/click handlers.
  • Add regression tests covering hover-open + immediate click and post-guard click-to-close behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
packages/core/src/TopNav/TopNavMegaMenu.tsx Adds hover-open timestamping and ignores immediate click-to-close within the guard window.
packages/core/src/TopNav/TopNavMegaMenu.test.tsx Adds tests for the new hover→click guard behavior in mega menu default mode.
packages/core/src/TopNav/TopNav.test.tsx Adds tests for the new hover→click guard behavior via TopNavHeading/useMenuHover and adds jsdom popover/hover-mode setup.
packages/core/src/hooks/useMenuHover.ts Implements shared hover-open timestamping and click-to-close guard in the shared hook.

borderVars,
} from '../theme/tokens.stylex';
import {usePopover} from '../Popover/usePopover';
import {HOVER_CLICK_GUARD_MS} from '../hooks/useMenuHover';

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 I guess the surprising thing to me is that this doesn't use the hook for hover menus which does this interaction. Is there something we may need to do to get the menu hover hook to work so we can reuse it here instead of rebuilding it piecemeal?

Comment on lines +45 to +47
afterEach(() => {
vi.restoreAllMocks();
});
Comment on lines +60 to +63
afterEach(() => {
window.matchMedia = originalMatchMedia;
vi.restoreAllMocks();
});
@github-actions

Copy link
Copy Markdown
Contributor

PR Analysis Report

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.6KB 0B

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | View full report

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

meta-cla Bot commented Jul 12, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

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

Thanks for fixing this. The changes look generally good. I might go investigate the use of that hook separately. There's merge conflicts so you'll need to rebase before I can merge.

Hover-opening a nav menu and then clicking the trigger — a single natural
gesture on mouse — toggled the menu closed on the very click that was meant
to confirm it.

Add a 500 ms click guard: a click on an already-open menu is swallowed when
the menu was hover-opened less than HOVER_CLICK_GUARD_MS ago. The timestamp
is stamped only when hover actually opens the menu, so:

- touch is unaffected (no hover, timestamp stays 0, taps toggle cleanly)
- keyboard is unaffected (no hover mode)
- click-open then click-close still toggles normally
- re-hovering an already-open menu does not re-arm the guard

Applied in both hover/click code paths: the shared useMenuHover hook
(TopNavHeading, TopNavMenu, SideNavHeading) and TopNavMegaMenu's own
DefaultMegaMenu handlers.

Fixes meta#3121

Co-Authored-By: Claude Fable 5 <[email protected]>
@cixzhang
cixzhang force-pushed the fix/top-nav-hover-click-guard branch from 9479441 to 32e7e68 Compare July 28, 2026 21:31
@cixzhang
cixzhang requested a review from ejhammond as a code owner July 28, 2026 21:31
@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Jul 28, 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.

Thanks for working on the issue! Really like this direction — flagging one subtle thing before it lands, because I got bitten by the exact same thing on #4555.

Root cause: the panel is popover="auto" (usePopover defaults hasLightDismiss: true), and the trigger sits outside it, so the browser's
native light-dismiss closes the popover during the click's pointer interaction — before React's onClick (and the guard) runs. The guard does
fire and correctly avoids re-toggling; there's just nothing left open. jsdom doesn't implement that path (the mocks only flip state on explicit
showPopover/hidePopover), which is why the unit tests stay green and it's invisible in review — I trusted the green units too at first.

What worked for me: take the panel off native dismiss — hasLightDismiss: false (→ popover="manual") — and reproduce outside-click
dismissal in JS (a document pointerdown listener that closes on outside clicks but ignores the trigger and the panel), plus explicit Escape
handling that manual mode needs. One caveat worth calling out: each useMenuHover consumer calls usePopover at its own site (all currently
auto), so the manual switch has to be applied per call site — the guard in the hook is necessary, but the manual popover is what makes it
actually take effect.

@imdreamrunner

Copy link
Copy Markdown
Contributor

I found a different approach and make it work at #4555.

I initially suggested switching to a manual popover. Further testing revealed another problem with that approach: it opts out of the native popover stack, allowing adjacent mega menus to remain open simultaneously unless we recreate coordination ourselves.

The new #4555 keeps the same 500 ms interaction guard, but registers the trigger as the panel’s native invoker using popoverTarget and cancels the native toggle with preventDefault(). This preserves popover="auto" behavior, outside dismissal, Escape handling, and sibling exclusivity, while allowing the guard to decide whether a click confirms or closes the panel.

#4555 is also scoped specifically to TopNavMegaMenu, where #3121 reproduces, while this PR changes the shared useMenuHover behavior for several other navigation components.

Your work was valuable in getting to this direction. I’d appreciate any thoughts you have!

@cixzhang

Copy link
Copy Markdown
Contributor

Closing this one — the fix it describes is now on main by two other routes: #4555 landed the TopNavMegaMenu-scoped version of your 500 ms guard, and #4936 then consolidated that guard into useMenuHover — the shared-hook approach you proposed here. So both halves of your idea shipped, just split across two PRs.

Thank you for this, genuinely. You diagnosed #3121 correctly and the hover-timestamp guard was the right mechanism; the discussion on this PR is what got us to the final shape. Sorry it took a supersede rather than a merge — that is on us for not landing it sooner.

Please do send more; happy to look at anything you pick up next.

@cixzhang cixzhang closed this Aug 16, 2026

This branch was successfully deployed

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

Labels

area:core CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team) type:fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants