An open source design system that's fully customizable and agent ready
TypeScript
fix(Switch): compose a caller aria-describedby onto the switch control (#7595)
Cindy Zhang2fc0a0db771d
diff --git a/.changeset/switch-caller-describedby.md b/.changeset/switch-caller-describedby.mdnew file mode 100644index 0000000000..dcebdf4e20--- /dev/null+++ b/.changeset/switch-caller-describedby.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `Switch` now adds a caller's `aria-describedby` to the switch control, ahead of its own description, status, and disabled-reason ids. Before, the value landed on the field wrapper, so a hint rendered elsewhere on the page could not describe the switch to assistive technology.++@cixzhangdiff --git a/packages/core/src/Switch/Switch.test.tsx b/packages/core/src/Switch/Switch.test.tsxindex f3a765aa2e..fe1014cbc0 100644--- a/packages/core/src/Switch/Switch.test.tsx+++ b/packages/core/src/Switch/Switch.test.tsx@@ -834,6 +834,34 @@ describe('Switch', () => {expect(root).toHaveAttribute('id', 'switch-1');expect(root).toHaveAttribute('aria-label', 'Toggle notifications');});++ it('composes a caller aria-describedby onto the switch control', () => {+ const {container} = render(+ <>+ <p id="external-hint">Applies to every repository</p>+ <Switch+ label="Notifications"+ description="Sends a daily digest"+ value={false}+ onChange={() => {}}+ data-testid="my-switch"+ aria-describedby="external-hint"+ />+ </>,+ );+ const control = screen.getByRole('switch');+ const description = screen.getByText('Sends a daily digest');+ expect(control.getAttribute('aria-describedby')?.split(' ')).toEqual([+ 'external-hint',+ description.id,+ ]);+ expect(control).toHaveAccessibleDescription(+ 'Applies to every repository Sends a daily digest',+ );+ // The id reference lives on the control only, not on the field wrapper.+ const root = container.querySelector('[data-testid="my-switch"]');+ expect(root).not.toHaveAttribute('aria-describedby');+ });});describe('pressed state', () => {diff --git a/packages/core/src/Switch/Switch.tsx b/packages/core/src/Switch/Switch.tsxindex df64e0a9ad..0dd89d003b 100644--- a/packages/core/src/Switch/Switch.tsx+++ b/packages/core/src/Switch/Switch.tsx@@ -566,6 +566,7 @@ export function Switch({className,style,ref,+ 'aria-describedby': ariaDescribedByProp,...rest}: SwitchProps) {const t = useTranslator();@@ -610,7 +611,12 @@ export function Switch({// Only include descriptionID when the element actually renders.// FieldLabel renders the description (with descriptionID) even when the// label is visually hidden — it's sr-only, so keep it linked.+ // A consumer's own `aria-describedby` describes the control, so it comes+ // first here rather than landing on the field wrapper via `...rest`.const describedByParts: string[] = [];+ if (ariaDescribedByProp) {+ describedByParts.push(ariaDescribedByProp);+ }if (description) {describedByParts.push(descriptionID);}