Switch
A binary toggle with real switch semantics — for settings that apply immediately.
"use client";
import { useState } from "react";
import { Switch } from "astralis-ui";
export function SwitchDemo() {
const [isPublic, setIsPublic] = useState(false);
return (
<Switch checked={isPublic} onChange={(e) => setIsPublic(e.target.checked)}>
{isPublic ? "Profile is public" : "Profile is private"}
</Switch>
);
}Import#
import { Switch } from "astralis-ui";Usage#
Use a Switch when flipping it takes effect immediately (settings); use a
Checkbox when the choice is submitted with a
form. The API is the native checkbox event model: checked +
onChange(e) reading e.target.checked.
Sizes, colors and states#
"use client";
import { Switch, HStack, VStack } from "astralis-ui";
export function SwitchStates() {
return (
<VStack gap="4" alignItems="start">
<HStack gap="5">
<Switch size="sm" defaultChecked>sm</Switch>
<Switch size="md" defaultChecked>md</Switch>
<Switch size="lg" defaultChecked>lg</Switch>
</HStack>
<HStack gap="5">
<Switch colorScheme="green" defaultChecked>green</Switch>
<Switch colorScheme="orange" defaultChecked>orange</Switch>
<Switch disabled>disabled</Switch>
<Switch disabled defaultChecked>disabled on</Switch>
</HStack>
</VStack>
);
}Props#
| Prop | Type | Default | Description |
|---|---|---|---|
checked / defaultChecked / onChange | boolean / boolean / (e) => void | — / false / — | Controlled / uncontrolled API. |
size | "sm" | "md" | "lg" | "md" | Track and thumb dimensions. |
colorScheme | all 11 schemes | "brand" | On-state hue. |
children | ReactNode | — | Label rendered beside the track. |
invalid / disabled / readOnly | boolean | false | State flags — inherited from Field. |
Keyboard#
| Key | Action |
|---|---|
Space | Toggle |
Accessibility#
Renders <input type="checkbox" role="switch"> with aria-checked, so
assistive tech announces it as an on/off switch, not a checkbox.