Skip to content

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

TypeScript

fix(SideNavItem): let a consumer aria-label win in collapsed mode (#5644)

Ojas Mutreja9ed4d0075fbd
diff --git a/.changeset/sidenavitem-consumer-aria-label.md b/.changeset/sidenavitem-consumer-aria-label.md
new file mode 100644
index 0000000000..57c133f87b
--- /dev/null
+++ b/.changeset/sidenavitem-consumer-aria-label.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] SideNavItem: a consumer-provided `aria-label` no longer gets overwritten by the collapsed-rail fallback, in both the icon-only and popover-trigger paths.
+
+@HelloOjasMutreja
diff --git a/packages/core/src/SideNav/SideNav.doc.mjs b/packages/core/src/SideNav/SideNav.doc.mjs
index ece54d53c7..e809ce5bcb 100644
--- a/packages/core/src/SideNav/SideNav.doc.mjs
+++ b/packages/core/src/SideNav/SideNav.doc.mjs
@@ -132,7 +132,7 @@ export const docs = {
{guidance: true, description: 'Use sections to group related navigation items and help users scan for their destination.'},
{guidance: true, description: 'Pair outline and filled icon variants so the selected state is visually distinct.'},
{guidance: true, description: 'Mark the current page with isSelected: it sets aria-current="page", so the current destination is announced rather than carried by color alone.'},
- {guidance: true, description: 'SideNav renders a navigation landmark, and a collapsible item follows the WAI-ARIA APG Disclosure pattern (https://www.w3.org/WAI/ARIA/apg/patterns/disclosure/): the toggle carries aria-expanded and aria-controls, and the group it owns is inert while collapsed. Keep item labels short; they are the accessible name in both expanded and icon-only modes.'},
+ {guidance: true, description: 'SideNav renders a navigation landmark, and a collapsible item follows the WAI-ARIA APG Disclosure pattern (https://www.w3.org/WAI/ARIA/apg/patterns/disclosure/): the toggle carries aria-expanded and aria-controls, and the group it owns is inert while collapsed. Keep item labels short; they name icon-only items unless a meaningful aria-label supplies more context.'},
{guidance: true, description: 'While the nav is collapsed, an item with children shows them in a submenu flyout. On a device that can hover, pointing at the item opens it after a short delay and moving away closes it; a flyout opened by clicking stays open until it is dismissed. On touch, it opens on tap. Do not put an action in there that has no other route to it.'},
{guidance: false, description: 'Include a SideNavHeading when a TopNav is already providing app identity; this duplicates branding.'},
{guidance: false, description: 'Use for filtering content; use tabs or filter buttons instead.'},
@@ -154,7 +154,7 @@ export const docsZh = {
{guidance: true, description: 'Use sections to group related navigation items and help users scan for their destination.'},
{guidance: true, description: 'Pair outline and filled icon variants so the selected state is visually distinct.'},
{guidance: true, description: 'Mark the current page with isSelected: it sets aria-current="page", so the current destination is announced rather than carried by color alone.'},
- {guidance: true, description: 'SideNav renders a navigation landmark, and a collapsible item follows the WAI-ARIA APG Disclosure pattern (https://www.w3.org/WAI/ARIA/apg/patterns/disclosure/): the toggle carries aria-expanded and aria-controls, and the group it owns is inert while collapsed. Keep item labels short; they are the accessible name in both expanded and icon-only modes.'},
+ {guidance: true, description: 'SideNav renders a navigation landmark, and a collapsible item follows the WAI-ARIA APG Disclosure pattern (https://www.w3.org/WAI/ARIA/apg/patterns/disclosure/): the toggle carries aria-expanded and aria-controls, and the group it owns is inert while collapsed. Keep item labels short; they name icon-only items unless a meaningful aria-label supplies more context.'},
{guidance: true, description: 'While the nav is collapsed, an item with children shows them in a submenu flyout. On a device that can hover, pointing at the item opens it after a short delay and moving away closes it; a flyout opened by clicking stays open until it is dismissed. On touch, it opens on tap. Do not put an action in there that has no other route to it.'},
{guidance: false, description: 'Include a SideNavHeading when a TopNav is already providing app identity; this duplicates branding.'},
{guidance: false, description: 'Use for filtering content; use tabs or filter buttons instead.'},
@@ -176,7 +176,7 @@ export const docsDense = {
{guidance: true, description: 'Use sections to group related navigation items and help users scan for their destination.'},
{guidance: true, description: 'Pair outline and filled icon variants so the selected state is visually distinct.'},
{guidance: true, description: 'Mark the current page with isSelected: it sets aria-current="page", so the current destination is announced rather than carried by color alone.'},
- {guidance: true, description: 'SideNav renders a navigation landmark, and a collapsible item follows the WAI-ARIA APG Disclosure pattern (https://www.w3.org/WAI/ARIA/apg/patterns/disclosure/): the toggle carries aria-expanded and aria-controls, and the group it owns is inert while collapsed. Keep item labels short; they are the accessible name in both expanded and icon-only modes.'},
+ {guidance: true, description: 'SideNav renders a navigation landmark, and a collapsible item follows the WAI-ARIA APG Disclosure pattern (https://www.w3.org/WAI/ARIA/apg/patterns/disclosure/): the toggle carries aria-expanded and aria-controls, and the group it owns is inert while collapsed. Keep item labels short; they name icon-only items unless a meaningful aria-label supplies more context.'},
{guidance: true, description: 'While the nav is collapsed, an item with children shows them in a submenu flyout. On a device that can hover, pointing at the item opens it after a short delay and moving away closes it; a flyout opened by clicking stays open until it is dismissed. On touch, it opens on tap. Do not put an action in there that has no other route to it.'},
{guidance: false, description: 'Include a SideNavHeading when a TopNav is already providing app identity; this duplicates branding.'},
{guidance: false, description: 'Use for filtering content; use tabs or filter buttons instead.'},
diff --git a/packages/core/src/SideNav/SideNav.test.tsx b/packages/core/src/SideNav/SideNav.test.tsx
index 00dc8600c0..de31cbc867 100644
--- a/packages/core/src/SideNav/SideNav.test.tsx
+++ b/packages/core/src/SideNav/SideNav.test.tsx
@@ -1138,6 +1138,88 @@ describe('SideNavItem (collapsed)', () => {
expect(trigger).toHaveAttribute('aria-label', 'Settings');
});
+ describe('consumer aria-label (#5641)', () => {
+ it('keeps a consumer aria-label on the collapsed link, instead of falling back to label', () => {
+ renderCollapsed(
+ <SideNavItem
+ label="Home"
+ icon={StubIcon}
+ href="/home"
+ aria-label="Open Home, 3 items need attention"
+ />,
+ );
+ expect(screen.getByRole('link')).toHaveAttribute(
+ 'aria-label',
+ 'Open Home, 3 items need attention',
+ );
+ });
+
+ it('keeps a consumer aria-label on the collapsed button, instead of falling back to label', () => {
+ renderCollapsed(
+ <SideNavItem
+ label="Home"
+ icon={StubIcon}
+ aria-label="Open Home, 3 items need attention"
+ />,
+ );
+ expect(screen.getByRole('button')).toHaveAttribute(
+ 'aria-label',
+ 'Open Home, 3 items need attention',
+ );
+ });
+
+ it('keeps a consumer aria-label on the collapsed popover trigger, instead of falling back to label', () => {
+ renderCollapsed(
+ <SideNavItem
+ label="Settings"
+ icon={StubIcon}
+ data-testid="parent"
+ aria-label="Settings, 2 updates available">
+ <SideNavItem label="General" />
+ </SideNavItem>,
+ );
+ expect(screen.getByTestId('parent')).toHaveAttribute(
+ 'aria-label',
+ 'Settings, 2 updates available',
+ );
+ });
+
+ it('falls back to label when a consumer aria-label is empty or whitespace', () => {
+ const {rerender} = renderCollapsed(
+ <SideNavItem
+ label="Dashboard"
+ icon={StubIcon}
+ href="/dashboard"
+ aria-label=""
+ />,
+ );
+ expect(screen.getByRole('link')).toHaveAttribute(
+ 'aria-label',
+ 'Dashboard',
+ );
+
+ rerender(
+ <SideNavCollapseContext value={COLLAPSED_CONTEXT}>
+ <SideNavItem label="Dashboard" icon={StubIcon} aria-label=" " />
+ </SideNavCollapseContext>,
+ );
+ expect(screen.getByRole('button')).toHaveAttribute(
+ 'aria-label',
+ 'Dashboard',
+ );
+ });
+
+ it('still falls back to label when no consumer aria-label is given', () => {
+ renderCollapsed(
+ <SideNavItem label="Dashboard" icon={StubIcon} href="/dashboard" />,
+ );
+ expect(screen.getByRole('link')).toHaveAttribute(
+ 'aria-label',
+ 'Dashboard',
+ );
+ });
+ });
+
it('opens popover on click showing children in expanded form', async () => {
const user = userEvent.setup();
renderCollapsed(
diff --git a/packages/core/src/SideNav/SideNavItem.tsx b/packages/core/src/SideNav/SideNavItem.tsx
index 1197085b72..6c581d4218 100644
--- a/packages/core/src/SideNav/SideNavItem.tsx
+++ b/packages/core/src/SideNav/SideNavItem.tsx
@@ -9,6 +9,8 @@
* @position Core implementation; used inside SideNav children
*
* Navigation item with icon, selected state, row-level actions, and nesting.
+ * Collapsed icon-only controls use a meaningful consumer `aria-label` when
+ * provided and fall back to `label` when it is missing or blank.
*
* Collapsed items with children open their submenu flyout through
* `useMenuHover`, the shared hover-intent hook (same one `SideNavHeading` and
@@ -571,6 +573,11 @@ export function SideNavItem({
),
);
+ const collapsedAccessibleLabel =
+ rest['aria-label'] != null && rest['aria-label'].trim() !== ''
+ ? rest['aria-label']
+ : label;
+
// Items with children: popover trigger + popover
if (hasChildren) {
return (
@@ -580,7 +587,7 @@ export function SideNavItem({
type="button"
{...rest}
{...hoverTriggerProps}
- aria-label={label}
+ aria-label={collapsedAccessibleLabel}
data-testid={testId}
{...popover.triggerProps}
{...collapsedItemStyles}>
@@ -607,7 +614,7 @@ export function SideNavItem({
const collapsedAriaProps = {
'aria-current': isSelected ? ('page' as const) : undefined,
'aria-disabled': isDisabled || undefined,
- 'aria-label': label,
+ 'aria-label': collapsedAccessibleLabel,
'data-testid': testId,
};