Components
Toggle
Turn a boolean setting on or off.
Example
Requires React 18.2+ or 19 and shared styles. See setup.
Default
Loading preview…
Code
'use client';
import { useState } from 'react';
import { Toggle } from '@swiftuijs/ui';
import { VStack } from '@swiftuijs/ui';
import { Text } from '@swiftuijs/ui';
const ToggleDemo = () => {
const [isOn, setIsOn] = useState(false);
return (<VStack spacing={16}>
<Text>Toggle is {isOn ? 'on' : 'off'}</Text>
<Toggle aria-label="Notifications" isOn={isOn} onChange={setIsOn}/>
<Toggle aria-label="Enabled setting" isOn={true} onChange={() => { }}/>
<Toggle aria-label="Disabled setting" isOn={false} onChange={() => { }} disabled/>
</VStack>);
};
export default function Example() {
return (<ToggleDemo />);
}Usage and limitations
- Provide
isOnand booleanonChange; state is controlled by the parent.disabledblocks interaction. - Use a clear visible label or
aria-labelnaming the setting. Native keyboard activation and on/off feedback are provided. - Group related settings and keep their state in React.
API reference
| Prop | Type | Required | Description |
|---|---|---|---|
isOn | boolean | Yes | Whether the toggle is currently on. |
onChange | (value: boolean) => void | Yes | Callback fired when the toggle state changes. |
disabled | boolean | No | Whether the toggle is disabled. Default: false |
Inherits additional props from Omit<
IBaseElementComponent<'input'>,
'type' | 'checked' | 'onChange' | 'children'
>.