File tree Expand file tree Collapse file tree
apps/web/src/components/ui Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ( / < s p a n [ ^ > ] * r o l e = " s w i t c h " [ ^ > ] * > / ) ?. [ 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+ } ) ;
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments