Skip to content

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

TypeScript

fix(core): link Banner expand toggle to its content via aria-controls (#3719)

Baraa Hamodi64f07c979f51
diff --git a/.changeset/banner-expand-controls.md b/.changeset/banner-expand-controls.md
new file mode 100644
index 0000000000..3e9263ef9b
--- /dev/null
+++ b/.changeset/banner-expand-controls.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Banner: the expand/collapse toggle is now linked to its content region via `aria-controls`, completing the disclosure pattern (the toggle already exposed `aria-expanded`). (#3719)
+@bhamodi
diff --git a/packages/core/src/Banner/Banner.test.tsx b/packages/core/src/Banner/Banner.test.tsx
index 8b488e3180..16d1e63c36 100644
--- a/packages/core/src/Banner/Banner.test.tsx
+++ b/packages/core/src/Banner/Banner.test.tsx
@@ -270,6 +270,44 @@ describe('Banner', () => {
expect(expandIndex).toBeLessThan(dismissIndex);
});
+ it('links the expand toggle to its content region via aria-controls', () => {
+ render(
+ <Banner status="info" title="Controls Test" defaultIsExpanded>
+ <div data-testid="region-content">Region content</div>
+ </Banner>,
+ );
+
+ const toggle = screen.getByRole('button', {name: 'Collapse'});
+ const controlsId = toggle.getAttribute('aria-controls');
+ // aria-controls must be present and point at the real content region.
+ expect(controlsId).toBeTruthy();
+ const region = document.getElementById(controlsId as string);
+ expect(region).not.toBeNull();
+ expect(region).toContainElement(screen.getByTestId('region-content'));
+ });
+
+ it('sets aria-controls only while the content region is mounted', async () => {
+ const user = userEvent.setup();
+ render(
+ <Banner status="info" title="Controls Toggle">
+ <div data-testid="region-content">Region content</div>
+ </Banner>,
+ );
+
+ // Collapsed: the region is unmounted, so no dangling aria-controls target.
+ const collapsedToggle = screen.getByRole('button', {name: 'Expand'});
+ expect(collapsedToggle).not.toHaveAttribute('aria-controls');
+
+ // Expanded: aria-controls resolves to the mounted region with the children.
+ await user.click(collapsedToggle);
+ const expandedToggle = screen.getByRole('button', {name: 'Collapse'});
+ const controlsId = expandedToggle.getAttribute('aria-controls');
+ expect(controlsId).toBeTruthy();
+ const region = document.getElementById(controlsId as string);
+ expect(region).not.toBeNull();
+ expect(region).toContainElement(screen.getByTestId('region-content'));
+ });
+
it('does not render content area when no children', () => {
const {container} = render(<Banner status="info" title="No Children" />);
const root = container.firstElementChild;
diff --git a/packages/core/src/Banner/Banner.tsx b/packages/core/src/Banner/Banner.tsx
index bb94a5f2bb..1ad06ca356 100644
--- a/packages/core/src/Banner/Banner.tsx
+++ b/packages/core/src/Banner/Banner.tsx
@@ -31,7 +31,7 @@
* - /packages/cli/templates/blocks/components/Banner/ (showcase blocks)
*/
-import {useState, type ReactNode} from 'react';
+import {useId, useState, type ReactNode} from 'react';
import * as stylex from '@stylexjs/stylex';
import type {BaseProps} from '../BaseProps';
import {Button} from '../Button';
@@ -387,6 +387,11 @@ export function Banner({
}: BannerProps) {
const [isDismissed, setIsDismissed] = useState(false);
const [isExpanded, setIsExpanded] = useState(defaultIsExpanded);
+ // Links the expand/collapse toggle to the content region it shows/hides so
+ // assistive tech can move from the button to its controlled content
+ // (disclosure pattern). The region is conditionally rendered, so aria-controls
+ // below is set only while it's mounted to avoid a dangling reference.
+ const contentId = useId();
const defaultIconName = defaultIconNames[status];
const role = statusRole[status];
const iconColor = statusIconColor[status];
@@ -477,6 +482,7 @@ export function Banner({
}
onClick={handleToggleExpand}
aria-expanded={isExpanded}
+ aria-controls={showContent ? contentId : undefined}
isIconOnly
/>
)}
@@ -497,6 +503,7 @@ export function Banner({
{/* Content area: collapsible card background — theme target ('banner-content') */}
{showContent && (
<div
+ id={contentId}
{...mergeProps(
themeProps('banner-content', {container, status}),
stylex.props(styles.contentArea, isCard && styles.contentAreaCard),