Skip to content

Commit 4e376da

Browse files
Leos-Khaiclaude
andcommitted
fix(web): switches announce their real state to screen readers
The shared Switch passed aria-checked={undefined} for every non-mixed toggle. Base UI's mergeProps copies every key it is given, including undefined ones, so that overwrote its own aria-checked and the attribute never reached the DOM. A role="switch" without aria-checked reads as "off" no matter what, so every toggle in web and desktop was announced as off and never updated after toggling. Only pass aria-checked when the switch is mixed so Base UI's real value survives, and add a test that fails without the fix. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent a493946 commit 4e376da

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { renderToStaticMarkup } from "react-dom/server";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import { Switch } from "./switch";
5+
6+
function renderSwitchRoot(props: Partial<Parameters<typeof Switch>[0]>) {
7+
const html = renderToStaticMarkup(<Switch aria-label="Enable Claude Code" {...props} />);
8+
return html.match(/<span[^>]*role="switch"[^>]*>/)?.[0] ?? "";
9+
}
10+
11+
describe("Switch accessibility", () => {
12+
it("exposes the checked state to assistive tech", () => {
13+
expect(renderSwitchRoot({ checked: true })).toContain('aria-checked="true"');
14+
expect(renderSwitchRoot({ checked: false })).toContain('aria-checked="false"');
15+
});
16+
17+
it("exposes the mixed state", () => {
18+
expect(renderSwitchRoot({ checked: false, mixed: true })).toContain('aria-checked="mixed"');
19+
});
20+
});

‎apps/web/src/components/ui/switch.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ function Switch({
2727
data-size={size}
2828
data-slot="switch"
2929
data-mixed={mixed ? "" : undefined}
30-
aria-checked={mixed ? "mixed" : undefined}
30+
// Base UI copies every key we pass, even `undefined`, over its own
31+
// aria-checked. Only pass the attribute when mixed so the real state
32+
// survives for screen readers.
33+
{...(mixed ? { "aria-checked": "mixed" as const } : {})}
3134
{...props}
3235
>
3336
<SwitchPrimitive.Thumb

0 commit comments

Comments
 (0)