fix(web): switches announce their real state to screen readers (#11580)

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
Khai Shern, TohandClaude Opus 5 authored and GitHub committed 2026-09-24 00:36:12 -03:00
1 parent 9030a60eaf
commit 21e2b7de00
2 files changed
+24 -1

No files matched your search

@@ -0,0 +1,20 @@
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it } from "vite-plus/test";
import { Switch } from "./switch";
function renderSwitchRoot(props: Partial<Parameters<typeof Switch>[0]>) {
const html = renderToStaticMarkup(<Switch aria-label="Enable Claude Code" {...props} />);
return html.match(/<span[^>]*role="switch"[^>]*>/)?.[0] ?? "";
}
describe("Switch accessibility", () => {
it("exposes the checked state to assistive tech", () => {
expect(renderSwitchRoot({ checked: true })).toContain('aria-checked="true"');
expect(renderSwitchRoot({ checked: false })).toContain('aria-checked="false"');
});
it("exposes the mixed state", () => {
expect(renderSwitchRoot({ checked: false, mixed: true })).toContain('aria-checked="mixed"');
});
});
+4 -1
View File
@@ -27,7 +27,10 @@ function Switch({
data-size={size}
data-slot="switch"
data-mixed={mixed ? "" : undefined}
aria-checked={mixed ? "mixed" : undefined}
// Base UI copies every key we pass, even `undefined`, over its own
// aria-checked. Only pass the attribute when mixed so the real state
// survives for screen readers.
{...(mixed ? { "aria-checked": "mixed" as const } : {})}
{...props}
>
<SwitchPrimitive.Thumb