Skip to content

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

TypeScript

fix(Switch): put a caller aria-labelledby on the switch control (#7615)

Cindy Zhangc5f72ea4d317
diff --git a/.changeset/switch-caller-labelledby.md b/.changeset/switch-caller-labelledby.md
new file mode 100644
index 0000000000..9e51e6f446
--- /dev/null
+++ b/.changeset/switch-caller-labelledby.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `Switch` now puts a caller's `aria-labelledby` on the switch control. Before, the value landed on the field wrapper, so a visible title elsewhere in the row could not name the switch for assistive technology.
+
+@cixzhang
diff --git a/packages/core/src/Switch/Switch.test.tsx b/packages/core/src/Switch/Switch.test.tsx
index fe1014cbc0..a7d2e66d27 100644
--- a/packages/core/src/Switch/Switch.test.tsx
+++ b/packages/core/src/Switch/Switch.test.tsx
@@ -862,6 +862,27 @@ describe('Switch', () => {
const root = container.querySelector('[data-testid="my-switch"]');
expect(root).not.toHaveAttribute('aria-describedby');
});
+
+ it('names the switch control from a caller aria-labelledby', () => {
+ const {container} = render(
+ <>
+ <span id="row-title">Release notes</span>
+ <Switch
+ label="Pin Release notes"
+ isLabelHidden
+ value={false}
+ onChange={() => {}}
+ data-testid="my-switch"
+ aria-labelledby="row-title"
+ />
+ </>,
+ );
+ const control = screen.getByRole('switch', {name: 'Release notes'});
+ expect(control).toHaveAttribute('aria-labelledby', 'row-title');
+ // 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-labelledby');
+ });
});
describe('pressed state', () => {
diff --git a/packages/core/src/Switch/Switch.tsx b/packages/core/src/Switch/Switch.tsx
index 0dd89d003b..ad71d0c2e5 100644
--- a/packages/core/src/Switch/Switch.tsx
+++ b/packages/core/src/Switch/Switch.tsx
@@ -567,6 +567,7 @@ export function Switch({
style,
ref,
'aria-describedby': ariaDescribedByProp,
+ 'aria-labelledby': ariaLabelledByProp,
...rest
}: SwitchProps) {
const t = useTranslator();
@@ -664,6 +665,10 @@ export function Switch({
}}
onFocus={onFocus}
onBlur={onBlur}
+ // A caller's `aria-labelledby` names the control (a visible title
+ // elsewhere in the row, for example), so it reaches the input rather
+ // than the field wrapper via `...rest`.
+ aria-labelledby={ariaLabelledByProp}
aria-describedby={ariaDescribedBy}
aria-invalid={status?.type === 'error' ? true : undefined}
aria-busy={isBusy || undefined}