Skip to content

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

TypeScript

fix(SideNav): the resize handle names the nav it resizes with aria-controls (#7473)

Cindy Zhang13ae375a39f6
diff --git a/.changeset/sidenav-resize-handle-controls.md b/.changeset/sidenav-resize-handle-controls.md
new file mode 100644
index 0000000000..0d64aa2020
--- /dev/null
+++ b/.changeset/sidenav-resize-handle-controls.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `SideNav`: the resize handle now names the navigation pane it resizes with `aria-controls` (the nav gets a generated id when the consumer gives none), as the APG window splitter pattern requires, so assistive technology can relate the separator to the pane.
+
+@cixzhang
diff --git a/packages/core/src/SideNav/SideNav.test.tsx b/packages/core/src/SideNav/SideNav.test.tsx
index 699576baeb..d1fd579655 100644
--- a/packages/core/src/SideNav/SideNav.test.tsx
+++ b/packages/core/src/SideNav/SideNav.test.tsx
@@ -989,6 +989,41 @@ describe('SideNav resizable', () => {
).toBeInTheDocument();
});
+ it('names the nav it resizes with aria-controls (APG window splitter)', () => {
+ render(<SideNav resizable>Content</SideNav>);
+ const nav = screen.getByRole('navigation');
+ const handle = screen.getByRole('separator');
+ expect(nav.id).not.toBe('');
+ expect(handle).toHaveAttribute('aria-controls', nav.id);
+ });
+
+ it("keeps a consumer's nav id and points the handle at it", () => {
+ render(
+ <SideNav resizable id="primary-nav">
+ Content
+ </SideNav>,
+ );
+ expect(screen.getByRole('navigation')).toHaveAttribute('id', 'primary-nav');
+ expect(screen.getByRole('separator')).toHaveAttribute(
+ 'aria-controls',
+ 'primary-nav',
+ );
+ });
+
+ it('keeps the generated nav id when a wrapper forwards id={undefined}', () => {
+ render(
+ <SideNav resizable id={undefined}>
+ Content
+ </SideNav>,
+ );
+ const nav = screen.getByRole('navigation');
+ expect(nav.id).not.toBe('');
+ expect(screen.getByRole('separator')).toHaveAttribute(
+ 'aria-controls',
+ nav.id,
+ );
+ });
+
it('clips the overlay drag handle within the resizable nav bounds', () => {
render(<SideNav resizable>Content</SideNav>);
const nav = screen.getByRole('navigation');
diff --git a/packages/core/src/SideNav/SideNav.tsx b/packages/core/src/SideNav/SideNav.tsx
index ab6442a5e5..8a0dd84998 100644
--- a/packages/core/src/SideNav/SideNav.tsx
+++ b/packages/core/src/SideNav/SideNav.tsx
@@ -22,7 +22,13 @@
* - /packages/cli/assets/templates/blocks/components/SideNav/ (showcase blocks)
*/
-import {useCallback, useImperativeHandle, useRef, type ReactNode} from 'react';
+import {
+ useCallback,
+ useId,
+ useImperativeHandle,
+ useRef,
+ type ReactNode,
+} from 'react';
import type {BaseProps} from '../BaseProps';
import * as stylex from '@stylexjs/stylex';
import type {StyleXStyles} from '@stylexjs/stylex';
@@ -440,6 +446,10 @@ export function SideNav({
);
const navRef = useRef<HTMLElement>(null);
+ // The resize handle names the pane it resizes (APG window splitter), so the
+ // nav needs an id; a consumer's own id wins.
+ const generatedNavId = useId();
+ const navId = props.id ?? generatedNavId;
const mergedNavRef = useMergedRefs(ref, navRef);
const collapseStateRef = useRef<SideNavCollapseState>({
isCollapsed: false,
@@ -611,7 +621,10 @@ export function SideNav({
className,
resizableNavStyle,
)}
- {...props}>
+ {...props}
+ // After the spread: a wrapper forwarding `id={undefined}` must not erase
+ // the id the resize handle's aria-controls points at.
+ id={navId}>
{hasStickyTop && (
<div
{...stylex.props(
@@ -672,6 +685,7 @@ export function SideNav({
xstyle={styles.resizableHandle}
resizable={resizableHook.props}
label={t('@astryx.sideNav.resizeSidebar')}
+ aria-controls={navId}
/>
</div>
) : (