For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/en/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/en/llms-full.txt, and this page is available as Markdown at https://ifhover.github.io/leaf-ui/en/components/switch.md.

Switch

Turn a setting on or off immediately.

Basic usage

Use the switch to toggle on/off state and provide an associated label.

basic.tsx
import { Switch } from '@ifhover/leaf-ui';

export function SwitchBasic() {
  return <Switch>Enable notifications</Switch>;
}

Examples

Disabled and loading

disabled prevents interaction. Set loading while saving to prevent repeated changes; the application performs the async operation.

Disabled
Loading
states.tsx
import { Flex, Grid, Switch, Text } from '@ifhover/leaf-ui';

export function SwitchStates() {
  return (
    <Grid columns={{ xs: 1, sm: 2 }} gap={16} align="center">
      <Text tone="muted">Disabled</Text>
      <Flex align="center" wrap>
        <Switch disabled defaultChecked>
          Enable notifications
        </Switch>
      </Flex>
      <Text tone="muted">Loading</Text>
      <Flex align="center" wrap>
        <Switch loading defaultChecked>
          Enable notifications
        </Switch>
      </Flex>
    </Grid>
  );
}

Sizes

Use size to set the switch size.

Small
Medium
Large
sizes.tsx
import { Flex, Grid, Switch, Text } from '@ifhover/leaf-ui';
import { Fragment } from 'react';

const options = [
  { value: 'sm', label: 'Small' },
  { value: 'md', label: 'Medium' },
  { value: 'lg', label: 'Large' },
] as const;

export function SwitchSizes() {
  return (
    <Grid columns={{ xs: 1, sm: 2 }} gap={16} align="center">
      {options.map(({ value, label }) => (
        <Fragment key={value}>
          <Text tone="muted">{label}</Text>
          <Flex align="center" wrap>
            <Switch size={value}>Enable notifications</Switch>
          </Flex>
        </Fragment>
      ))}
    </Grid>
  );
}

Controlled state

Pass checked and read the boolean from event.target.checked in onChange.

Email notifications enabled
controlled.tsx
import { Flex, Switch, Text } from '@ifhover/leaf-ui';
import { useState } from 'react';

export function SwitchControlled() {
  const [checked, setChecked] = useState(true);
  return (
    <Flex direction="column" gap="md" align="start">
      <Switch checked={checked} onChange={(event) => setChecked(event.target.checked)}>
        Receive email
      </Switch>
      <Text tone="muted" role="status">
        {checked ? 'Email notifications enabled' : 'Email notifications disabled'}
      </Text>
    </Flex>
  );
}

With name, an enabled, checked switch submits its value. Unchecked, disabled or loading switches are not submitted. Uncontrolled switches return to defaultChecked on form reset; reset application state as well for a controlled switch.

API

Switch API

Configure checked state, form submission and interaction state.

Supports native checkbox attributes such as name, value, required and form. Provide text or aria-label as its accessible name. className and style apply to the label container.

PropertyTypeDefaultDescription
size'sm' | 'md' | 'lg''md'Control size
loadingbooleanfalseShow loading and disable interaction
checkedboolean—Controlled switch state.
defaultCheckedboolean—Initial switch state for uncontrolled usage.
onChangeChangeEventHandler<HTMLInputElement>—Read the switch state from event.target.checked.
disabledbooleanfalseDisable interaction.
childrenReactNode—Clickable text label.
refRef<HTMLInputElement>—DOM reference