How to build an accessible Swap in React

Renders a <button role="switch"> with aria-checked reflecting state. Both on/off slots are aria-hidden so screen readers announce the button state, not the icon content.

When to use a Swap

When not to use it

Keyboard interactions

Role switch, verified at WCAG 2.2-AA.

Common mistakes

Avoid: <Swap on="Enable" off="Disable" /> (text content)

Prefer: <Toggle label="Enable notifications" />

Swap is designed for icon transitions; for labeled on/off controls use Toggle

Example

<Swap on={<SunIcon />} off={<MoonIcon />} mode="rotate" aria-label="Toggle theme" />

See the full Swap reference →