Skip to main content

Switch

Switch toggles a single setting on or off. It features a motion-driven thumb and an optional in-track animated border for a modern, polished feel.

Import

import * as React from 'react';
import { Switch } from '@libdev-ui/base';

export default function Example(): JSX.Element {
const [on, setOn] = React.useState(false);
return <Switch checked={on} onChange={(_, c) => setOn(c)} />;
}

Basic usage

import * as React from 'react';
import { Switch } from '@libdev-ui/base';

export default function Basic(): JSX.Element {
return <Switch defaultChecked />;
}

Labeled switch

With text label
import * as React from 'react';
import { Switch } from '@libdev-ui/base';

export default function Labeled(): JSX.Element {
return <Switch startDecorator="Wi‑Fi" />;
}

Controlled vs. uncontrolled

Controlled example
import * as React from 'react';
import { Switch, Button } from '@libdev-ui/base';
import { FiSun, FiMoon } from 'react-icons/fi';

export default function Controlled(): JSX.Element {
const [on, setOn] = React.useState(false);
return (
<>
<Switch
checked={on}
onChange={(_, c) => setOn(c)}
startDecorator={(s) => (s.checked ? <FiMoon /> : <FiSun />)}
endDecorator={(s) => (s.checked ? 'On' : 'Off')}
/>
<Button onClick={() => setOn(v => !v)}>Toggle</Button>
</>
);
}

Variants

filled
outlined
ghost
import * as React from 'react';
import { Switch } from '@libdev-ui/base';

export default function Variants(): JSX.Element {
return (
<>
<Switch variant="filled" />
<Switch variant="outlined" />
<Switch variant="ghost" />
</>
);
}

Animated border

gradient (custom colors)
glow
pulse
import * as React from 'react';
import { Switch } from '@libdev-ui/base';

export default function AnimatedBorder(): JSX.Element {
return (
<>
<Switch
variant="outlined"
borderAnimation="gradient"
borderColors={['info', 'warning', 'danger', 'success']}
borderAnimationSpeed={1600}
/>
<Switch variant="filled" color="success" borderAnimation="glow" />
<Switch variant="ghost" color="warning" borderAnimation="pulse" />
</>
);
}

Sizes & colors

Sizes

sm
md
lg

Colors

primary
success
danger
warning
info

Customization with sl

Style overrides via sl
import * as React from 'react';
import { Switch } from '@libdev-ui/base';

export default function StyleOverrides(): JSX.Element {
return (
<Switch
variant="ghost"
sl={{ backgroundColor: 'background.level2', p: 8, borderRadius: 'lg' }}
/>
);
}

API

Props

NameTypeDefaultDescription
checkedbooleanControlled checked state.
defaultCheckedbooleanUncontrolled initial state.
onChange(event: React.ChangeEvent<HTMLInputElement>, checked: boolean) => voidFired when state changes.
disabledbooleanfalseDisables interaction and visuals.
readOnlybooleanfalsePrevents state changes while focusable.
requiredbooleanfalseMarks the input as required.
autoFocusbooleanfalseAutofocus on mount.
clearOnEscapebooleanfalseWhen true, Escape clears (sets false).
colorColor | string"primary"Semantic color token or any CSS color.
variant"filled" | "outlined" | "ghost""filled"Visual variant.
size"sm" | "md" | "lg""md"Component size.
radiusRadius | number | string"xl"Corner radius for track/thumb.
borderAnimation"none" | "gradient" | "glow" | "pulse""gradient"Animated border style (in-track).
borderWidthnumber | string2Track border thickness for animated modes.
borderColorsstring[]Custom gradient stops (LD tokens or CSS colors).
borderAnimationSpeednumberGradient speed in ms.
componentReact.ElementType"label"Polymorphic root.
slots{ root, track, thumb, action, input, startDecorator, endDecorator }Replace internal elements.
slotPropssame shape as slotsProps per slot (supports functions with ownerState).
slStyleArg | StyleArg[]System style overrides.
startDecoratorReact.ReactNode | (state) => React.ReactNodeElement rendered before the switch.
endDecoratorReact.ReactNode | (state) => React.ReactNodeElement rendered after the switch.

Accessibility: role="switch" and aria-checked are applied to the hidden input; keyboard interaction uses Space.