Switch
A toggle switch for turning something on or off.
View in Storybook (opens in a new tab)
Installation
import { Switch } from "@ginga-ui/core";Basic usage
A simple switch example.
switch.tsx
import { Switch } from "@ginga-ui/core";
export function BasicExample() {
return <Switch>Allow notifications</Switch>;
}Selected
A switch that is on initially.
switch.tsx
import { Switch } from "@ginga-ui/core";
export function SelectedExample() {
return <Switch selected>On by default</Switch>;
}Disabled
An example of a disabled switch.
switch.tsx
switch.module.css
import { Switch } from "@ginga-ui/core";
import styles from "./switch.module.css";
export function DisabledExample() {
return (
<div className={styles.disabledList}>
<Switch disabled>Disabled (off)</Switch>
<Switch disabled selected>
Disabled (on)
</Switch>
</div>
);
}Controlled switch
A switch whose state is controlled externally.
switch.tsx
import { Switch } from "@ginga-ui/core";
export function ControlledExample() {
return (
<Switch onChange={(isSelected) => console.log("State:", isSelected)}>
Dark mode
</Switch>
);
}Props
Switch
| Name | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Required. Label of the switch. |
selected | boolean | - | Whether the switch is on (controlled). Use it with onChange. |
disabled | boolean | false | Disables the switch. |
readOnly | boolean | false | Prevents the user from changing the value. |
Also accepts the props of React Aria's Switch (opens in a new tab).