Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

feat(topnav): unified card appearance for mega menu (#356)

rubyycheung0bee81d7f3a0
diff --git a/apps/sandbox/src/app/pages/mega-menu/page.tsx b/apps/sandbox/src/app/pages/mega-menu/page.tsx
index 120dbe950e..3b50b43560 100644
--- a/apps/sandbox/src/app/pages/mega-menu/page.tsx
+++ b/apps/sandbox/src/app/pages/mega-menu/page.tsx
@@ -1,5 +1,6 @@
'use client';
+import {useState} from 'react';
import * as stylex from '@stylexjs/stylex';
import {XDSVStack} from '@xds/core/Layout';
import {XDSText, XDSHeading} from '@xds/core/Text';
@@ -21,6 +22,13 @@ const styles = stylex.create({
borderRadius: 12,
overflow: 'visible',
boxShadow: '0 1px 3px rgba(0, 0, 0, 0.08)',
+ transitionProperty: 'box-shadow',
+ transitionDuration: '0.2s',
+ },
+ // Hide wrapper shadow when mega menu is open so the backdrop's
+ // unified card shadow takes over seamlessly.
+ navWrapperMenuOpen: {
+ boxShadow: 'none',
},
});
@@ -212,6 +220,12 @@ const solutionItems = [
* Demo page for XDSTopNavMegaMenu — a nav item with a full-width mega menu.
*/
export default function MegaMenuPage() {
+ const [menuOpen1, setMenuOpen1] = useState(false);
+ const [menuOpen2, setMenuOpen2] = useState(false);
+ const [menuOpen3, setMenuOpen3] = useState(false);
+
+ const isAnyOpen1 = menuOpen1 || menuOpen2;
+
return (
<div {...stylex.props(styles.container)}>
<XDSVStack gap="space6">
@@ -219,16 +233,19 @@ export default function MegaMenuPage() {
<XDSHeading level={1}>Mega Menu</XDSHeading>
<XDSText type="body" color="secondary">
A top nav variation with a full-width mega menu that appears on
- hover. Hover over &quot;Products&quot; or &quot;Solutions&quot; to
- see the dropdown panel with categorized items and a featured content
- area.
+ hover. The nav bar and dropdown panel appear as one unified card.
+ Hover over &quot;Products&quot; or &quot;Solutions&quot; to see it.
</XDSText>
</XDSVStack>
{/* Full mega menu with featured content */}
<XDSVStack gap="space3">
<XDSHeading level={2}>With Featured Content</XDSHeading>
- <div {...stylex.props(styles.navWrapper)}>
+ <div
+ {...stylex.props(
+ styles.navWrapper,
+ isAnyOpen1 && styles.navWrapperMenuOpen,
+ )}>
<XDSTopNav
label="Marketing navigation"
title={
@@ -243,6 +260,7 @@ export default function MegaMenuPage() {
<XDSTopNavMegaMenu
label="Products"
items={productItems}
+ onOpenChange={setMenuOpen1}
featured={{
image:
'https://images.unsplash.com/photo-1551434678-e076c223a692?w=560&h=280&fit=crop',
@@ -257,6 +275,7 @@ export default function MegaMenuPage() {
<XDSTopNavMegaMenu
label="Solutions"
items={solutionItems}
+ onOpenChange={setMenuOpen2}
featured={{
title: 'Customer Stories',
description:
@@ -281,7 +300,11 @@ export default function MegaMenuPage() {
{/* Without featured content */}
<XDSVStack gap="space3">
<XDSHeading level={2}>Without Featured Content</XDSHeading>
- <div {...stylex.props(styles.navWrapper)}>
+ <div
+ {...stylex.props(
+ styles.navWrapper,
+ menuOpen3 && styles.navWrapperMenuOpen,
+ )}>
<XDSTopNav
label="Simple navigation"
title={<XDSTopNavTitle title="App" href="#" />}
@@ -311,6 +334,7 @@ export default function MegaMenuPage() {
},
]}
isSingleColumn
+ onOpenChange={setMenuOpen3}
/>
<XDSTopNavItem label="Pricing" href="#" />
</>
diff --git a/packages/core/src/TopNav/XDSTopNavMegaMenu.tsx b/packages/core/src/TopNav/XDSTopNavMegaMenu.tsx
index 742d288c3a..8438f28f61 100644
--- a/packages/core/src/TopNav/XDSTopNavMegaMenu.tsx
+++ b/packages/core/src/TopNav/XDSTopNavMegaMenu.tsx
@@ -79,6 +79,28 @@ const styles = stylex.create({
chevronOpen: {
transform: 'rotate(180deg)',
},
+ // Backdrop covers the entire positioned ancestor (nav + panel area).
+ // It provides the unified card appearance: shared shadow, rounded corners,
+ // and surface background so the nav bar and panel look like one popover.
+ backdrop: {
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ right: 0,
+ bottom: 0,
+ zIndex: 99,
+ borderRadius: radiusVars['--radius-container'],
+ backgroundColor: colorVars['--color-surface'],
+ boxShadow: elevationVars['--elevation-hover'],
+ opacity: 0,
+ transitionProperty: 'opacity',
+ transitionDuration: '0.2s',
+ transitionTimingFunction: 'ease-out',
+ pointerEvents: 'none',
+ },
+ backdropOpen: {
+ opacity: 1,
+ },
panel: {
position: 'absolute',
top: '100%',
@@ -86,9 +108,11 @@ const styles = stylex.create({
right: 0,
zIndex: 100,
backgroundColor: colorVars['--color-surface'],
- boxShadow: elevationVars['--elevation-menu'],
+ // No separate shadow or border — the backdrop provides the unified
+ // card shadow, and the panel flows seamlessly from the nav bar.
borderBottomLeftRadius: radiusVars['--radius-container'],
borderBottomRightRadius: radiusVars['--radius-container'],
+ boxShadow: elevationVars['--elevation-hover'],
overflow: 'hidden',
opacity: 0,
transform: 'translateY(-4px)',
@@ -280,6 +304,11 @@ export interface XDSTopNavMegaMenuProps {
hideDelay?: number;
/** Whether to use single-column layout for items. @default false */
isSingleColumn?: boolean;
+ /**
+ * Callback fired when the mega menu opens or closes.
+ * Useful for coordinating wrapper styles (e.g. hiding other shadows).
+ */
+ onOpenChange?: (isOpen: boolean) => void;
}
// =============================================================================
@@ -347,12 +376,21 @@ export function XDSTopNavMegaMenu({
delay = 150,
hideDelay = 250,
isSingleColumn = false,
+ onOpenChange,
}: XDSTopNavMegaMenuProps) {
const [isOpen, setIsOpen] = useState(false);
const showTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const hideTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const wrapperRef = useRef<HTMLDivElement | null>(null);
+ const setOpen = useCallback(
+ (open: boolean) => {
+ setIsOpen(open);
+ onOpenChange?.(open);
+ },
+ [onOpenChange],
+ );
+
const clearTimeouts = useCallback(() => {
if (showTimeoutRef.current) {
clearTimeout(showTimeoutRef.current);
@@ -367,16 +405,16 @@ export function XDSTopNavMegaMenu({
const scheduleShow = useCallback(() => {
clearTimeouts();
showTimeoutRef.current = setTimeout(() => {
- setIsOpen(true);
+ setOpen(true);
}, delay);
- }, [clearTimeouts, delay]);
+ }, [clearTimeouts, setOpen, delay]);
const scheduleHide = useCallback(() => {
clearTimeouts();
hideTimeoutRef.current = setTimeout(() => {
- setIsOpen(false);
+ setOpen(false);
}, hideDelay);
- }, [clearTimeouts, hideDelay]);
+ }, [clearTimeouts, setOpen, hideDelay]);
const handleMouseEnter = useCallback(() => {
scheduleShow();
@@ -391,10 +429,10 @@ export function XDSTopNavMegaMenu({
if (e.key === 'Escape') {
e.stopPropagation();
clearTimeouts();
- setIsOpen(false);
+ setOpen(false);
}
},
- [clearTimeouts],
+ [clearTimeouts, setOpen],
);
useEffect(() => {
@@ -420,6 +458,12 @@ export function XDSTopNavMegaMenu({
<ChevronDown />
</span>
</button>
+ {/* Backdrop: covers the entire positioned ancestor to create a unified
+ card appearance (shared shadow + rounded corners) for nav + panel */}
+ <div
+ aria-hidden="true"
+ {...stylex.props(styles.backdrop, isOpen && styles.backdropOpen)}
+ />
<div
role="menu"
aria-label={label}