Switch

A toggle that turns a setting on or off right away.

Usage

Code
import { Switch } from '@leverui/components';
Code
<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.

Code
<Switch.Control>
  <Switch.Thumb />
</Switch.Control>

Same as:

Code
<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.

Code
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>
  );
}
Code
<ClosedSwitch>Email notifications</ClosedSwitch>

Props

Root

PropDefaultTypeDescription
valueonstring | numberInput value, sent on form submit.
colorPalettegray'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'Color palette.
variantsolid'solid' | 'raised'Switch style.
sizemd'xs' | 'sm' | 'md' | 'lg'Switch size.
aslabelNative HTML tagHTML tag to render.
asChild—Solid render functionRender your own element with the switch's props.
unstyledfalsebooleanTurn off the default styles.
checked—booleanOn state, when you control it.
defaultChecked—booleanStart on, when uncontrolled.
disabled—booleanDisable the switch.
ids—Element ID mapIds of the switch elements.
invalid—booleanMark the switch as invalid.
label—stringTranslated name for assistive technology.
name—stringInput name, sent on form submit.
onCheckedChange—(details: { checked: boolean }) => voidCalled when the user clicks the switch.
readOnly—booleanMake the switch read-only.
required—booleanMark 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