Switch
On/off toggle whose thumb always stays inside the track, as a full-width labelled row.
Example
import { Switch } from "@porcelana/ui";
export default function Example() {
return (
<div style={{ display: "grid", maxWidth: 420 }}>
<Switch label="Alarm" description="Weekdays at 06:00" defaultChecked />
<Switch label="Gradual wake light" size="lg" />
<Switch aria-label="Standalone switch" />
</div>
);
}Import
import { Switch } from "@porcelana/ui";
import "@porcelana/ui/styles.css"; // once, at your app rootProps
Generated from the TypeScript types. Standard HTML attributes are passed through and not listed.
Switch
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label | string | – | Defines a string value that labels the current element. @see aria-labelledby. |
asChild | boolean | – | Render the single child element instead, merging props and behaviour onto it. |
checked | boolean | – | Controlled checked state. Use with onCheckedChange. |
defaultChecked | boolean | – | Initial checked state when uncontrolled. |
description | ReactNode | – | Secondary text under the label; becomes the accessible description. |
disabled | boolean | – | Prevents interaction and dims the control. |
id | string | – | Element id. Generated automatically when omitted. |
label | ReactNode | – | Visible label rendered as a full-width row; tapping the row toggles. |
onCheckedChange | ((checked: boolean) => void) | – | Called with the new checked state. |
required | boolean | – | Marks the control as required in forms. |
size | "md" | "lg" | md | md is 64 × 36px, lg is 96 × 48px. |
type | "button" | "submit" | "reset" | – | Button type (defaults to "button"). |
Accessibility
- role="switch" with aria-checked; Space and Enter toggle.
- description becomes the switch's accessible description.
Mobile
- With label, the whole row toggles, so the target is far larger than the switch.
- Two sizes: 64 × 36px and 96 × 48px.