Skip to content

fix(topnav): use Popover API + CSS anchor positioning for mega menu - #412

Merged
cixzhang merged 2 commits into
mainfrom
navi/fix/mega-menu-top-layer
Mar 4, 2026
Merged

cixzhang merged 2 commits into
mainfrom
navi/fix/mega-menu-top-layer

Conversation

@cixzhang

@cixzhang cixzhang commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

Problem

The mega menu panel used manual position: absolute + z-index: 100 for layering, which triggered a cascade of z-index band-aids (#377, #378, #379). This is fragile — any consumer stacking context can break it.

XDS already has proper top layer infrastructure (useXDSLayer uses the Popover API), but the mega menu bypassed it entirely.

Fix

Replace manual z-index stacking with the browser's top layer:

  1. Popover API — Panel uses popover="manual" + showPopover()/hidePopover(). Promoted to the top layer automatically — renders above everything without z-index.

  2. CSS anchor positioning — Panel anchors to the nearest positioned ancestor (the nav wrapper div) using position-anchor + anchor() functions. Replaces position: absolute + top: 100% + left: 0 + right: 0.

  3. Mouse event bridging — Since top layer elements are outside the DOM hierarchy for mouse events, added onMouseEnter/onMouseLeave on the panel itself. Without this, mousing from trigger → panel would fire onMouseLeave on the wrapper and close the menu.

  4. Popover toggle sync — Listens for native toggle events to sync popover state back to React.

What changed

Before After
position: absolute + z-index: 100 popover="manual" (top layer)
Panel is DOM child of wrapper Panel is in top layer, positioned via CSS anchors
Mouse events flow through DOM hierarchy Explicit mouse handlers on both wrapper and panel
Consumers need position: relative wrapper for positioning Consumers need position: relative wrapper as CSS anchor

z-index audit (after this PR)

No z-index values remain in XDSTopNavMegaMenu. Remaining z-index in packages/core/src/:

Component z-index Status
AppShell (skip-to-content) 9999 Fine — a11y skip link
AppShell (mobile overlay) 100/101 Being fixed in #341
AppShell (sticky header) 10 Fine — sticky scroll overlap
Various inputs 1 Fine — local internal stacking

Test Plan

  • ✅ All 1071 tests pass
  • ✅ Build passes
  • ✅ Lint clean
  • ✅ No new TypeScript errors
  • API unchanged — no breaking changes for consumers

Closes #411

Related

@cixzhang cixzhang added this to the Soft Launch — March Week 1 milestone Mar 3, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 3, 2026
@github-actions

github-actions Bot commented Mar 3, 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

AppShell · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A 209B N/A
Lines of Code N/A 338 -
Complexity N/A Very High (35) -
TopNav · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A 447B N/A
Lines of Code N/A 946 -
Complexity N/A Very High (53) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@xds/core 8.0KB 13.0KB 2.0KB

Accessibility Audit

Status: No accessibility violations detected.

Component Previews

Includes interactive hover previews as animated GIFs.

XDSAppShell

Top Nav With Side Nav

Screenshot:

Top Nav With Side Nav

Interaction Preview: (view video)

Top Nav With Side Nav interaction

View in Storybook · core-xdsappshell--top-nav-with-side-nav

Side Nav Only

Screenshot:

Side Nav Only

View in Storybook · core-xdsappshell--side-nav-only

Top Nav Only

Screenshot:

Top Nav Only

View in Storybook · core-xdsappshell--top-nav-only

Full Featured

Screenshot:

Full Featured

View in Storybook · core-xdsappshell--full-featured

Auto Height

Screenshot:

Auto Height

View in Storybook · core-xdsappshell--auto-height

Controlled Collapse

Screenshot:

Controlled Collapse

View in Storybook · core-xdsappshell--controlled-collapse

Content Only

Screenshot:

Content Only

View in Storybook · core-xdsappshell--content-only

With Banner

Screenshot:

With Banner

View in Storybook · core-xdsappshell--with-banner

XDSTopNav

Default

Screenshot:

Default

Interaction Preview: (view video)

Default interaction

View in Storybook · navigation-xdstopnav--default

With Logo

Screenshot:

With Logo

View in Storybook · navigation-xdstopnav--with-logo

Title Only

Screenshot:

Title Only

View in Storybook · navigation-xdstopnav--title-only

Nav Item States

Screenshot:

Nav Item States

View in Storybook · navigation-xdstopnav--nav-item-states

Centered Navigation

Screenshot:

Centered Navigation

View in Storybook · navigation-xdstopnav--centered-navigation

Centered With Start Content

Screenshot:

Centered With Start Content

View in Storybook · navigation-xdstopnav--centered-with-start-content

Center Content Without End

Screenshot:

Center Content Without End

View in Storybook · navigation-xdstopnav--center-content-without-end

Full Example

Screenshot:

Full Example

View in Storybook · navigation-xdstopnav--full-example


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

'use client';

import {useCallback, useEffect, useRef, useState, type ReactNode} from 'react';
import {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This isn't using useXDSLayer when I expected it to. That should probably eliminate most of the extra behaviors we needed to add around mouse event handling. More ideally though, we should be using useXDSPopover or XDSPopover with custom trigger and anchor refs.

borderBottomRightRadius: radiusVars['--radius-container'],
// Shadow wraps the panel; the wrapper handles the nav bar's card chrome.
// Together they create a unified card appearance without a backdrop overlay.
boxShadow: `0 8px 24px rgba(0, 0, 0, 0.12), 0 2px 8px rgba(0, 0, 0, 0.08)`,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This should use elevationVars.

borderWidth: 0,
borderStyle: 'none',
// Visual styles
backgroundColor: colorVars['--color-surface'],

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since this is a popover, it should be using the popover background which is slightly different in dark mode.

Mega menu:
- Use useXDSLayer (Popover API + CSS anchor positioning) instead of
  manual position: absolute + z-index: 100
- Panel promoted to top layer — no z-index needed
- CSS anchor positioning via layer.ref() on nearest positioned ancestor
- Use colorVars['--color-popover'] (correct dark mode) and
  elevationVars['--elevation-menu'] (semantic token)
- Animation via :popover-open + @starting-style + allow-discrete
- Mouse event handlers on panel for top-layer hover bridging

AppShell:
- Sticky header z-index: 10 → 1 (only needs to beat auto)

Closes #411
@cixzhang

cixzhang commented Mar 3, 2026

Copy link
Copy Markdown
Contributor Author

Can we have proper stories for the mega menu and top nav menu so they can be inspected for regressions?

Add dedicated stories for XDSTopNavMenu and XDSTopNavMegaMenu so they
can be inspected for regressions in the Storybook preview.

Stories:
- Default: Single nav menu with 4 items (icon + title + description)
- Multiple Menus: Two nav menus side by side
- Mega Menu: Full-width with items grid + featured content
- Mega Menu (Simple): Single column, no featured content
@cixzhang

cixzhang commented Mar 3, 2026

Copy link
Copy Markdown
Contributor Author

Added dedicated Storybook stories for XDSTopNavMenu and XDSTopNavMegaMenu:

  • Default — Single nav menu with 4 items (icon + title + description)
  • Multiple Menus — Two nav menus side by side
  • Mega Menu — Full-width with items grid + featured content
  • Mega Menu (Simple) — Single column, no featured content

These will show up in the Storybook preview once CI deploys.

@cixzhang
cixzhang added this pull request to the merge queue Mar 3, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Mar 3, 2026
@cixzhang
cixzhang added this pull request to the merge queue Mar 4, 2026
Merged via the queue into main with commit bc822bc Mar 4, 2026
11 checks passed
@cixzhang
cixzhang deleted the navi/fix/mega-menu-top-layer branch March 13, 2026 10:51
cixzhang added a commit that referenced this pull request Apr 26, 2026
…412)

* fix(topnav): use top layer for mega menu, reduce z-index in AppShell

Mega menu:
- Use useXDSLayer (Popover API + CSS anchor positioning) instead of
  manual position: absolute + z-index: 100
- Panel promoted to top layer — no z-index needed
- CSS anchor positioning via layer.ref() on nearest positioned ancestor
- Use colorVars['--color-popover'] (correct dark mode) and
  elevationVars['--elevation-menu'] (semantic token)
- Animation via :popover-open + @starting-style + allow-discrete
- Mouse event handlers on panel for top-layer hover bridging

AppShell:
- Sticky header z-index: 10 → 1 (only needs to beat auto)

Closes #411

* feat(storybook): add TopNavMenu and TopNavMegaMenu stories

Add dedicated stories for XDSTopNavMenu and XDSTopNavMegaMenu so they
can be inspected for regressions in the Storybook preview.

Stories:
- Default: Single nav menu with 4 items (icon + title + description)
- Multiple Menus: Two nav menus side by side
- Mega Menu: Full-width with items grid + featured content
- Mega Menu (Simple): Single column, no featured content
cixzhang added a commit that referenced this pull request Jun 21, 2026
…412)

* fix(topnav): use top layer for mega menu, reduce z-index in AppShell

Mega menu:
- Use useXDSLayer (Popover API + CSS anchor positioning) instead of
  manual position: absolute + z-index: 100
- Panel promoted to top layer — no z-index needed
- CSS anchor positioning via layer.ref() on nearest positioned ancestor
- Use colorVars['--color-popover'] (correct dark mode) and
  elevationVars['--elevation-menu'] (semantic token)
- Animation via :popover-open + @starting-style + allow-discrete
- Mouse event handlers on panel for top-layer hover bridging

AppShell:
- Sticky header z-index: 10 → 1 (only needs to beat auto)

Closes #411

* feat(storybook): add TopNavMenu and TopNavMegaMenu stories

Add dedicated stories for XDSTopNavMenu and XDSTopNavMegaMenu so they
can be inspected for regressions in the Storybook preview.

Stories:
- Default: Single nav menu with 4 items (icon + title + description)
- Multiple Menus: Two nav menus side by side
- Mega Menu: Full-width with items grid + featured content
- Mega Menu (Simple): Single column, no featured content
cixzhang added a commit that referenced this pull request Jun 21, 2026
…412)

* fix(topnav): use top layer for mega menu, reduce z-index in AppShell

Mega menu:
- Use useXDSLayer (Popover API + CSS anchor positioning) instead of
  manual position: absolute + z-index: 100
- Panel promoted to top layer — no z-index needed
- CSS anchor positioning via layer.ref() on nearest positioned ancestor
- Use colorVars['--color-popover'] (correct dark mode) and
  elevationVars['--elevation-menu'] (semantic token)
- Animation via :popover-open + @starting-style + allow-discrete
- Mouse event handlers on panel for top-layer hover bridging

AppShell:
- Sticky header z-index: 10 → 1 (only needs to beat auto)

Closes #411

* feat(storybook): add TopNavMenu and TopNavMegaMenu stories

Add dedicated stories for XDSTopNavMenu and XDSTopNavMegaMenu so they
can be inspected for regressions in the Storybook preview.

Stories:
- Default: Single nav menu with 4 items (icon + title + description)
- Multiple Menus: Two nav menus side by side
- Mega Menu: Full-width with items grid + featured content
- Mega Menu (Simple): Single column, no featured content
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.

Eliminate z-index stacking — use top layer (Popover API) for all overlays

1 participant