Skip to content

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

TypeScript

fix(TopNav): use CSS grid for true centerContent centering (#314)

Cindy Zhangf3d58d5cb7ba
diff --git a/apps/storybook/stories/TopNav.stories.tsx b/apps/storybook/stories/TopNav.stories.tsx
index 8c30d1849e..eee7e3c069 100644
--- a/apps/storybook/stories/TopNav.stories.tsx
+++ b/apps/storybook/stories/TopNav.stories.tsx
@@ -267,6 +267,29 @@ export const CenteredWithStartContent: Story = {
),
};
+export const CenterContentWithoutEnd: Story = {
+ args: {
+ label: 'Main navigation',
+ title: (
+ <XDSTopNavTitle
+ title="My App"
+ logo={
+ <XDSTopNavTitleIcon
+ icon={<CubeIcon style={{width: 16, height: 16}} />}
+ />
+ }
+ href="#"
+ />
+ ),
+ centerContent: (
+ <>
+ <XDSTopNavItem label="Home" href="#" isSelected />
+ <XDSTopNavItem label="Products" href="#" />
+ </>
+ ),
+ },
+};
+
export const FullExample: Story = {
render: () => (
<XDSTopNav
diff --git a/packages/core/src/TopNav/README.md b/packages/core/src/TopNav/README.md
index 92104d9b3e..1e5884c215 100644
--- a/packages/core/src/TopNav/README.md
+++ b/packages/core/src/TopNav/README.md
@@ -207,19 +207,21 @@ import {XDSTopNav, XDSTopNavTitle, XDSTopNavItem} from '@xds/core/TopNav';
```
┌─────────────────────────────────────────────────────────────┐
│ [title] [start...] [centerContent...] [...endContent] │
-│ └─ flex: 1 ──────── centered ──────────── flex: 1 ────────┤
+│ └─ 1fr start ──┘ └── auto center ──┘ └── 1fr end ───┤
└─────────────────────────────────────────────────────────────┘
```
-When `centerContent` is provided, the left and right sections flex equally
-(`flex: 1 1 0%`) so the center content stays visually centered regardless
-of how much content is in the start or end slots.
+When `centerContent` is provided, the layout switches to a CSS grid with
+`gridTemplateColumns: '1fr auto 1fr'`. This guarantees the center column
+is always at the exact horizontal center of the nav bar, regardless of
+left/right content widths. The right column is always rendered (even when
+`endContent` is absent) to maintain the three-column grid structure.
## Implementation Notes
- XDSTopNav uses `role="navigation"` and accepts `aria-label` via the `label` prop
- Without `centerContent`: title and startContent grow to push endContent right
-- With `centerContent`: left and right sections use equal flex basis for true centering
+- With `centerContent`: switches to CSS grid (`1fr auto 1fr`) for true centering
- XDSTopNavItem supports `aria-current="page"` when `isSelected` is true
- XDSTopNavTitleIcon uses `--color-accent` background with `--color-icon-on-media` for contrast
- Default height is 48px with 16px horizontal padding
diff --git a/packages/core/src/TopNav/XDSTopNav.test.tsx b/packages/core/src/TopNav/XDSTopNav.test.tsx
index 3f9eb0da94..02d36d23de 100644
--- a/packages/core/src/TopNav/XDSTopNav.test.tsx
+++ b/packages/core/src/TopNav/XDSTopNav.test.tsx
@@ -87,6 +87,36 @@ describe('XDSTopNav', () => {
expect(screen.getByTestId('end')).toBeInTheDocument();
});
+ it('renders centerContent without endContent', () => {
+ render(
+ <XDSTopNav
+ title={<span data-testid="title">Title</span>}
+ centerContent={<span data-testid="center">Center</span>}
+ />,
+ );
+ expect(screen.getByTestId('title')).toBeInTheDocument();
+ expect(screen.getByTestId('center')).toBeInTheDocument();
+
+ const nav = screen.getByRole('navigation');
+ // 3 child divs: left section, center section, right section (even without endContent)
+ expect(nav.children).toHaveLength(3);
+ });
+
+ it('renders centerContent without startContent', () => {
+ render(
+ <XDSTopNav
+ centerContent={<span data-testid="center">Center</span>}
+ endContent={<span data-testid="end">End</span>}
+ />,
+ );
+ expect(screen.getByTestId('center')).toBeInTheDocument();
+ expect(screen.getByTestId('end')).toBeInTheDocument();
+
+ const nav = screen.getByRole('navigation');
+ // 3 child divs: left section, center section, right section
+ expect(nav.children).toHaveLength(3);
+ });
+
it('forwards ref correctly', () => {
const ref = vi.fn();
render(<XDSTopNav ref={ref} />);
diff --git a/packages/core/src/TopNav/XDSTopNav.tsx b/packages/core/src/TopNav/XDSTopNav.tsx
index f9547a41ef..37021ec14c 100644
--- a/packages/core/src/TopNav/XDSTopNav.tsx
+++ b/packages/core/src/TopNav/XDSTopNav.tsx
@@ -27,7 +27,6 @@ import type {StyleXStyles as ThemeStyleXStyles} from '../theme/types';
*/
const styles = stylex.create({
base: {
- display: 'flex',
alignItems: 'center',
width: '100%',
height: spacingVars['--spacing-12'],
@@ -35,6 +34,15 @@ const styles = stylex.create({
backgroundColor: colorVars['--color-navbar'],
boxSizing: 'border-box',
},
+ // Flex layout (default, used when no centerContent)
+ baseFlex: {
+ display: 'flex',
+ },
+ // Grid layout (used when centerContent is present)
+ baseGrid: {
+ display: 'grid',
+ gridTemplateColumns: '1fr auto 1fr',
+ },
leftSection: {
display: 'flex',
alignItems: 'center',
@@ -57,18 +65,14 @@ const styles = stylex.create({
alignItems: 'center',
justifyContent: 'center',
gap: spacingVars['--spacing-1'],
- flexShrink: 0,
},
- endContent: {
+ rightSection: {
display: 'flex',
alignItems: 'center',
justifyContent: 'flex-end',
gap: spacingVars['--spacing-2'],
- flex: '1 1 0%',
- minWidth: 0,
},
- // When there's no centerContent, revert to the simpler layout
- endContentNoCenterContent: {
+ endContent: {
display: 'flex',
alignItems: 'center',
gap: spacingVars['--spacing-2'],
@@ -104,9 +108,10 @@ export interface XDSTopNavProps extends Omit<
*/
startContent?: ReactNode;
/**
- * Center content slot - navigation items or content centered in the nav bar.
- * When present, the layout switches to a three-column mode where
- * start and end sections flex equally to keep center content visually centered.
+ * Center content slot - typically tabs, search bar, or primary navigation.
+ * Positioned at the horizontal center of the nav bar.
+ * When provided, the layout switches to a three-column CSS grid to ensure
+ * true centering regardless of start/end content widths.
*/
centerContent?: ReactNode;
/**
@@ -129,8 +134,9 @@ export interface XDSTopNavProps extends Omit<
* left-aligned, centerContent is centered, and endContent is right-aligned.
*
* When `centerContent` is provided, the layout switches to a three-column
- * mode where the left and right sections flex equally to keep the center
- * content visually centered in the nav bar.
+ * CSS grid (`1fr auto 1fr`) to ensure the center content is always at the
+ * exact horizontal center of the nav bar, regardless of left/right content
+ * widths.
*
* Positioning is handled by the layout system (e.g. XDSAppShell applies sticky
* behavior in auto height mode). TopNav itself is a pure content component.
@@ -183,7 +189,11 @@ export const XDSTopNav = forwardRef<HTMLElement, XDSTopNavProps>(
ref={ref}
role="navigation"
aria-label={label}
- {...stylex.props(styles.base, rootOverride)}
+ {...stylex.props(
+ styles.base,
+ hasCenterContent ? styles.baseGrid : styles.baseFlex,
+ rootOverride,
+ )}
{...props}>
<div {...stylex.props(styles.leftSection)}>
{title && <div {...stylex.props(styles.title)}>{title}</div>}
@@ -194,15 +204,12 @@ export const XDSTopNav = forwardRef<HTMLElement, XDSTopNavProps>(
{hasCenterContent && (
<div {...stylex.props(styles.centerContent)}>{centerContent}</div>
)}
- {endContent && (
- <div
- {...stylex.props(
- hasCenterContent
- ? styles.endContent
- : styles.endContentNoCenterContent,
- )}>
- {endContent}
- </div>
+ {hasCenterContent ? (
+ <div {...stylex.props(styles.rightSection)}>{endContent}</div>
+ ) : (
+ endContent && (
+ <div {...stylex.props(styles.endContent)}>{endContent}</div>
+ )
)}
</nav>
);