Switch
A toggle that turns a setting on or off right away.
Usage
import { Switch } from '@leverui/components';
<Switch.Root>
<Switch.HiddenInput />
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label />
</Switch.Root>
For a single component, see the snippet below.
Shortcuts
SwitchControl
Switch.Control renders a Switch.Thumb by default.
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
Same as:
<Switch.Control />
Examples
Sizes
Pass the size prop to the Switch.Root component to change the size of the switch component.
Variants
Pass the variant prop to the Switch.Root component to change the visual style of the switch.
Colors
Pass the colorPalette prop to the Switch.Root component to change the color scheme of the component.
gray
red
green
blue
teal
pink
purple
cyan
orange
yellow
Controlled
Use the checked and onCheckedChange prop to control the state of the switch.
Hook Form
Here's an example of integrating the switch with form state held in a Solid signal.
Disabled
Pass the disabled prop to the Switch.Root component to disable the switch.
Invalid
Pass the invalid prop to the Switch.Root component to indicate an error state for the switch.
Tooltip
Here's an example of how to compose a switch with a tooltip.
Track Indicator
Use the Switch.Indicator component to display different indicators based on the checked state.
Thumb Indicator
Use the Switch.ThumbIndicator component to add an icon to the switch thumb.
Closed Component
One component that wraps the parts.
import { Show, omit } from 'solid-js';
import type { JSX } from '@solidjs/web';
import { Switch, type SwitchRootProps } from '@leverui/components';
interface SwitchLabels {
on: JSX.Element;
off: JSX.Element;
}
export type ClosedSwitchProps = SwitchRootProps & {
inputProps?: JSX.InputHTMLAttributes<HTMLInputElement> | undefined;
trackLabel?: SwitchLabels | undefined;
thumbLabel?: SwitchLabels | undefined;
};
export function ClosedSwitch(props: ClosedSwitchProps) {
const root = omit(props, 'inputProps', 'trackLabel', 'thumbLabel', 'children');
return (
<Switch.Root {...root}>
<Switch.HiddenInput {...props.inputProps} />
<Switch.Control>
<Switch.Thumb>
<Show when={props.thumbLabel}>
{(labels) => (
<Switch.ThumbIndicator fallback={labels().off}>{labels().on}</Switch.ThumbIndicator>
)}
</Show>
</Switch.Thumb>
<Show when={props.trackLabel}>
{(labels) => <Switch.Indicator fallback={labels().off}>{labels().on}</Switch.Indicator>}
</Show>
</Switch.Control>
<Show when={props.children}>
<Switch.Label>{props.children}</Switch.Label>
</Show>
</Switch.Root>
);
}
<ClosedSwitch>Email notifications</ClosedSwitch>
Props
Root
| Prop | Default | Type | Description |
|---|---|---|---|
value | on | string | number | Input value, sent on form submit. |
colorPalette | gray | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink' | Color palette. |
variant | solid | 'solid' | 'raised' | Switch style. |
size | md | 'xs' | 'sm' | 'md' | 'lg' | Switch size. |
as | label | Native HTML tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the switch's props. |
unstyled | false | boolean | Turn off the default styles. |
checked | — | boolean | On state, when you control it. |
defaultChecked | — | boolean | Start on, when uncontrolled. |
disabled | — | boolean | Disable the switch. |
ids | — | Element ID map | Ids of the switch elements. |
invalid | — | boolean | Mark the switch as invalid. |
label | — | string | Translated name for assistive technology. |
name | — | string | Input name, sent on form submit. |
onCheckedChange | — | (details: { checked: boolean }) => void | Called when the user clicks the switch. |
readOnly | — | boolean | Make the switch read-only. |
required | — | boolean | Mark the switch input as required. |
Root renders a label, so clicking the control toggles the hidden checkbox.
With another as tag, add the labeling and the click behavior yourself.
Keep HiddenInput in every Switch. It provides the checkbox role, focus,
validation, and form submission.
FormData includes the value only when the switch is on. To submit an
explicit false, control the switch with a signal.
Indicator and ThumbIndicator show their children when on and their
fallback when off.
RootProvider takes state you create with useSwitch. PropsProvider gives
Root default state and appearance props, and RootProvider only the
appearance defaults.
Context and useSwitchContext give the current switch's state, for building
your own parts.
Customize with CSS
Override the --leverui-* theme tokens, or target the .leverui-switch__*
classes for one-off styling. class and style work on the root and on every
part.
Set unstyled on Root to turn off the default styles for the whole switch,
or on one part for that part only.
Explorer
Click a part name to highlight it in the preview.
Component Anatomy
Hover to highlight, click to select parts