Repository navigation
fix(top-nav): ignore trigger click immediately after hover-open - #3851
rizkydwicmt wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Hi @rizkydwicmt! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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?
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); |
| afterEach(() => { | ||
| window.matchMedia = originalMatchMedia; | ||
| vi.restoreAllMocks(); | ||
| }); |
PR Analysis ReportNo new or modified components detected. Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Generated by PR Enrichment workflow | View full report |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
cixzhang
left a comment
There was a problem hiding this comment.
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]>
9479441 to
32e7e68
Compare
There was a problem hiding this comment.
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.
|
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! |
|
Closing this one — the fix it describes is now on 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. |
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:
hoverOpenedAtRef, stamped only on a hover-driven closed→open transition).HOVER_CLICK_GUARD_MS(500 ms) ago; otherwise it closes the menu as before.Why this stays safe on other input modes:
0and taps toggle open/close cleanly.The guard is applied in both places that implement hover+click triggers:
packages/core/src/hooks/useMenuHover.ts— shared hook used byTopNavHeading,TopNavMenu, andSideNavHeading.packages/core/src/TopNav/TopNavMegaMenu.tsx—DefaultMegaMenuhas 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 viauseMenuHover): 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.Core/TopNavMenu→ MegaMenu): hover + immediate click keeps the panel open; delayed click closes; keyboard and hover-away dismissal unchanged.🤖 Generated with Claude Code