Switch
An accessible toggle switch backed by a hidden checkbox, with animated thumb and keyboard support.
Examples
import { Switch } from "@g4rcez/components";
function BasicSwitch() {
return (
<div className="flex flex-col gap-4">
<Switch name="auto-save">Enable auto-save</Switch>
<Switch name="dark-mode" defaultChecked>Dark mode</Switch>
</div>
);
}"use client";
import { useState } from "react";
import { Switch } from "@g4rcez/components";
function ControlledSwitch() {
const [notifications, setNotifications] = useState(false);
const [marketing, setMarketing] = useState(true);
return (
<div className="flex flex-col gap-4">
<Switch
name="notifications"
checked={notifications}
onCheck={setNotifications}
>
Email notifications — {notifications ? "on" : "off"}
</Switch>
<Switch
name="marketing"
checked={marketing}
onCheck={setMarketing}
>
Marketing emails — {marketing ? "on" : "off"}
</Switch>
</div>
);
}import { Switch } from "@g4rcez/components";
function DisabledAndError() {
return (
<div className="flex flex-col gap-4">
<Switch name="locked" disabled defaultChecked>
Feature locked (enabled)
</Switch>
<Switch name="broken" error="This setting cannot be changed until the account security review is complete and an administrator approves the update.">
Experimental feature
</Switch>
</div>
);
}When to use Switch
Use when
You need the documented Switch interaction or presentation in a product workflow.
Avoid when
A native HTML element already provides the Switch behavior you need.
Accessibility
Keep the Switch labeled and use its documented keyboard interaction. The component preserves visible focus and communicates state without relying on color alone.
- Keep keyboard focus visible and follow the documented key commands.
- Provide an accessible name and connect descriptions or errors to the control.