An open source design system that's fully customizable and agent ready
TypeScript
fix(TopNav): clamp mega menu panel to viewport width (#4558)
Cindy Zhang90ae6c6d049e
diff --git a/.changeset/fix-topnav-megamenu-overflow.md b/.changeset/fix-topnav-megamenu-overflow.mdnew file mode 100644index 0000000000..1220d09cc2--- /dev/null+++ b/.changeset/fix-topnav-megamenu-overflow.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] TopNavMegaMenu: keep the desktop mega-menu panel within the viewport — cap its height to the space below the nav (scrolling internally) and clamp its width — so a tall or wide menu no longer overflows the screen edge and clips content+@cixzhangdiff --git a/packages/core/src/TopNav/TopNavMegaMenu.tsx b/packages/core/src/TopNav/TopNavMegaMenu.tsxindex 6c8cbc8184..5cec0d7f3a 100644--- a/packages/core/src/TopNav/TopNavMegaMenu.tsx+++ b/packages/core/src/TopNav/TopNavMegaMenu.tsx@@ -123,6 +123,18 @@ const styles = stylex.create({transform: 'translateY(-4px)',},},+ // Clamp the anchored layer to the space available below the nav so a tall+ // menu never runs off the bottom of the viewport. The layer is positioned+ // with position-area: self-block-end, so its containing block spans from+ // the nav's block-end to the viewport edge — 100% is exactly that space.+ // The layer is a flex column so panelContainer can shrink and scroll its+ // own content, keeping the surface radius/shadow static at the edges.+ // Internal scroll is a stopgap until the mobile bottom-sheet lands.+ panelViewportFit: {+ display: 'flex',+ flexDirection: 'column',+ maxHeight: `calc(100% - ${spacingVars['--spacing-3']})`,+ },// Visual styles for the panel content container.panelContainer: {backgroundColor: colorVars['--color-background-popover'],@@ -132,6 +144,11 @@ const styles = stylex.create({borderRadius: radiusVars['--radius-container'],boxShadow: shadowVars['--shadow-low'],overflow: 'hidden',+ // Allow the container to shrink inside the height-clamped layer so its+ // content (panelContent) can scroll rather than overflow the viewport.+ display: 'flex',+ flexDirection: 'column',+ minHeight: 0,},panelContent: {display: 'flex',@@ -139,7 +156,14 @@ const styles = stylex.create({gap: spacingVars['--spacing-6'],paddingBlock: spacingVars['--spacing-3'],paddingInline: spacingVars['--spacing-3'],- maxWidth: 960,+ // Clamp to the viewport (minus a gutter) so the anchored panel never+ // overflows the screen edge on narrow viewports; caps at 960px otherwise.+ maxWidth: `min(960px, calc(100dvw - ${spacingVars['--spacing-4']}))`,+ boxSizing: 'border-box',+ // Scroll internally when the menu is taller than the available space+ // below the nav (paired with panelViewportFit on the layer).+ overflowY: 'auto',+ overscrollBehavior: 'contain',},menuWrapper: {flexGrow: 2,@@ -484,7 +508,7 @@ function DefaultMegaMenu({{placement: 'below',alignment: slot,- xstyle: styles.panelAnimation,+ xstyle: [styles.panelAnimation, styles.panelViewportFit],},)}</>