Switch

Markdown

A versatile Switch, managing state, context, and styling.

<Flex gap="4" wrap="wrap" align="center" justify="center" className="w-full">
  <Switch.Root variant="outlined">
    <Switch.Label>Show All</Switch.Label>
  </Switch.Root>
</Flex>

Anatomy

<Switch.Root>
  <Switch.Label />
</Switch.Root>

Examples

Variants

Switch components come with three built in variants, solid, outlined, and muted.

<Flex gap="4" wrap="wrap" align="center" justify="center" className="w-full">
  {["outlined", "solid", "muted"].map((variant) => (
    <Switch.Root key={variant} variant={variant}>
      <Switch.Label>
        {variant.charAt(0).toUpperCase() + variant.slice(1)} Switch
      </Switch.Label>
    </Switch.Root>
  ))}
</Flex>

Label Positions

The label element <label> is available in the switch component anatomy. If included it can be easily positioned with the position prop.

<Flex gap="4" wrap="wrap" align="center" justify="center" className="w-full">
  {["top", "bottom", "left", "right"].map((position) => (
    <Switch.Root key={position} variant="outlined">
      <Switch.Label position={position}>
        {position.charAt(0).toUpperCase() + position.slice(1)} Label
      </Switch.Label>
    </Switch.Root>
  ))}
</Flex>

Sizes

To manage the size of the switch component you can provide a default or custom size through the size prop.

<Flex gap="4" wrap="wrap" align="center" justify="center" className="w-full">
  {["xs", "sm", "md", "lg", "xl"].map((size) => (
    <Switch.Root key={size} size={size} variant="muted">
      <Switch.Label>{size.toUpperCase()}</Switch.Label>
    </Switch.Root>
  ))}
  <Switch.Root size="80px" variant="muted">
    <Switch.Label>Custom Size</Switch.Label>
  </Switch.Root>
</Flex>

Colors

The switch component uses the color prop to allow for predefined and custom colors.

<Flex gap="4" wrap="wrap" align="center" justify="center" className="w-full">
  {[
    "primary",
    "secondary",
    "tertiary",
    "success",
    "danger",
    "warning",
    "info",
  ].map((color) => (
    <Switch.Root defaultChecked key={color} variant="muted" color={color}>
      <Switch.Label>
        {color.charAt(0).toUpperCase() + color.slice(1)}
      </Switch.Label>
    </Switch.Root>
  ))}
</Flex>

Alignments

To set alignment of the switch and its label content you can use the align prop.

<Flex gap="4" wrap="wrap" align="center" justify="center" className="w-full">
  {["start", "center", "end"].map((alignment) => (
    <Switch.Root
      key={alignment}
      align={alignment}
      variant="muted"
      className="w-1/4"
    >
      <Switch.Label>
        This switch is {alignment} aligned relative to its label.
      </Switch.Label>
    </Switch.Root>
  ))}
</Flex>

States

Switches can be given the disabled, readonly, and defaultChecked states.

<Flex gap="4" wrap="wrap" align="center" justify="center" className="w-full">
  <Switch.Root disabled variant="muted">
    <Switch.Label>Disabled</Switch.Label>
  </Switch.Root>
  <Switch.Root readonly variant="muted">
    <Switch.Label>Readonly</Switch.Label>
  </Switch.Root>
  <Switch.Root defaultChecked variant="muted">
    <Switch.Label>Default Checked</Switch.Label>
  </Switch.Root>
</Flex>