TypeScript
fix(web): switches announce their real state to screen readers (#11580)
Khai Shern, Toh21e2b7de0062
diff --git a/apps/web/src/components/ui/switch.test.tsx b/apps/web/src/components/ui/switch.test.tsxnew file mode 100644index 00000000000..b63fa79e384--- /dev/null+++ b/apps/web/src/components/ui/switch.test.tsx@@ -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"');+ });+});diff --git a/apps/web/src/components/ui/switch.tsx b/apps/web/src/components/ui/switch.tsxindex 33114c5512b..e6bf20aa64c 100644--- a/apps/web/src/components/ui/switch.tsx+++ b/apps/web/src/components/ui/switch.tsx@@ -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