Skip to content

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

TypeScript

fix(SideNavHeading): render end content beside a whole-heading link (#8029)

Cindy Zhangf81b8bba401d
diff --git a/.changeset/sidenav-heading-end-content-beside-link.md b/.changeset/sidenav-heading-end-content-beside-link.md
new file mode 100644
index 0000000000..5d0be2bbbd
--- /dev/null
+++ b/.changeset/sidenav-heading-end-content-beside-link.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] SideNavHeading: with `headingHref` as its only link, `headerEndContent` now renders beside the heading link instead of inside it. A button placed there was nested in the link, so pressing it also followed the link. The link's accessible name no longer includes the end content.
+
+@cixzhang
diff --git a/apps/storybook/stories/SideNav.stories.tsx b/apps/storybook/stories/SideNav.stories.tsx
index 05ed4caa8a..ae13ad4983 100644
--- a/apps/storybook/stories/SideNav.stories.tsx
+++ b/apps/storybook/stories/SideNav.stories.tsx
@@ -513,6 +513,25 @@ export const HeaderEndContent: Story = {
),
};
+export const HeaderEndContentAction: Story = {
+ name: 'Header End Content: Action Beside A Heading Link',
+ render: () => (
+ <SideNav
+ header={
+ <SideNavHeading
+ icon={<NavIcon icon={<CubeIcon style={{width: 16, height: 16}} />} />}
+ heading="My App"
+ headingHref="/"
+ headerEndContent={<Button label="Create" variant="ghost" size="sm" />}
+ />
+ }>
+ <SideNavSection title="Main">
+ <SideNavItem label="Dashboard" icon={HomeIcon} isSelected />
+ </SideNavSection>
+ </SideNav>
+ ),
+};
+
// =============================================================================
// Header End Content + Menu
// =============================================================================
diff --git a/packages/core/src/SideNav/SideNav.test.tsx b/packages/core/src/SideNav/SideNav.test.tsx
index 045e6cd30b..6c980b109f 100644
--- a/packages/core/src/SideNav/SideNav.test.tsx
+++ b/packages/core/src/SideNav/SideNav.test.tsx
@@ -668,18 +668,27 @@ describe('SideNavHeading headerEndContent', () => {
expect(screen.getByTestId('end-badge')).toBeInTheDocument();
});
- it('renders headerEndContent inside the link in isWholeHeadingLink path', () => {
+ it('renders headerEndContent beside the link, not inside it, in isWholeHeadingLink path', () => {
+ const onPress = vi.fn();
render(
<SideNavHeading
heading="My App"
headingHref="/home"
- headerEndContent={<span data-testid="end-badge">3</span>}
+ headerEndContent={
+ <button type="button" onClick={onPress}>
+ Create
+ </button>
+ }
/>,
);
- const badge = screen.getByTestId('end-badge');
- expect(badge).toBeInTheDocument();
- // Badge renders inside the link
- expect(badge.closest('a')).not.toBeNull();
+ const link = screen.getByRole('link', {name: 'My App'});
+ expect(link).toHaveAttribute('href', '/home');
+ // A control inside a link is invalid and its press would follow the link.
+ const button = screen.getByRole('button', {name: 'Create'});
+ expect(button.closest('a')).toBeNull();
+ expect(link.parentElement).toBe(button.parentElement?.parentElement);
+ fireEvent.click(button);
+ expect(onPress).toHaveBeenCalledTimes(1);
});
it('renders headerEndContent in isWholeHeadingTrigger path', () => {
diff --git a/packages/core/src/SideNav/SideNavHeading.tsx b/packages/core/src/SideNav/SideNavHeading.tsx
index 7e8a062e95..e38e2140f6 100644
--- a/packages/core/src/SideNav/SideNavHeading.tsx
+++ b/packages/core/src/SideNav/SideNavHeading.tsx
@@ -189,6 +189,17 @@ const styles = stylex.create({
// today. The 28px min box still wins over Icon's width/height.
fontSize: 'inherit',
},
+ // The link part of a whole-heading link row that also has end content.
+ linkBesideEndContent: {
+ display: 'flex',
+ alignItems: 'center',
+ alignSelf: 'stretch',
+ gap: spacingVars['--spacing-2'],
+ flexGrow: 1,
+ minWidth: 0,
+ textDecoration: 'none',
+ color: 'inherit',
+ },
headerEndContent: {
flexShrink: 0,
display: 'flex',
@@ -640,6 +651,36 @@ export function SideNavHeading({
</button>
);
+ // Whole heading is a link (no menu, single headingHref). End content may
+ // be a control, which must not sit inside the link (no interactive content
+ // in an <a>), so with end content the row is a div holding the link and the
+ // end content side by side, as the mixed mode does.
+ if (isWholeHeadingLink && headingHref && headerEndContentElement) {
+ return (
+ <div
+ ref={ref}
+ data-testid={testId}
+ {...mergeProps(
+ themeProps('side-nav-heading'),
+ stylex.props(styles.root, xstyle),
+ className,
+ style,
+ )}>
+ <LinkComponent
+ href={headingHref}
+ {...focusOutlineProps.focusVisible(
+ styles.linkBesideEndContent,
+ styles.menuTrigger,
+ )}
+ {...(props as React.AnchorHTMLAttributes<HTMLAnchorElement>)}>
+ {icon && <span {...stylex.props(styles.icon)}>{icon}</span>}
+ {renderTextContent()}
+ </LinkComponent>
+ {headerEndContentElement}
+ </div>
+ );
+ }
+
// Whole heading is a link (no menu, single headingHref)
if (isWholeHeadingLink && headingHref) {
return (