Toggle

A two-state button that is either on or off, solid and off by default.

import { Toggle } from "@produkto/ui/Toggle";

const Example = () => <Toggle>Toggle</Toggle>;

export default Example;

Usage

import { Toggle } from "@produkto/ui/Toggle";
<Toggle>Toggle</Toggle>

Examples

Appearances

appearance paints the toggle on the kit's one scale, as a field is: solid, the default, soft on the grey wash, outline with only its line, and transparent with neither until it is hovered or pressed.

import { Toggle, type ToggleAppearance } from "@produkto/ui/Toggle";

const variants: { appearance: ToggleAppearance; label: string }[] = [
  { appearance: "solid", label: "Solid" },
  { appearance: "soft", label: "Soft" },
  { appearance: "outline", label: "Outline" },
  { appearance: "transparent", label: "Transparent" },
];

const Example = () => (
  <div className="flex flex-wrap items-center gap-2">
    {variants.map((variant) => (
      <Toggle appearance={variant.appearance} key={variant.appearance}>
        {variant.label}
      </Toggle>
    ))}
  </div>
);

export default Example;

Sizes

size sets the height at a Button's heights, xs, sm, base (the default) or lg, with a matching least width, so a toggle holding only an icon is square at each of them.

import { TextBoldIcon } from "@hugeicons/core-free-icons";
import { Icon } from "@produkto/ui/Icon";
import { Toggle, type ToggleSize } from "@produkto/ui/Toggle";

const sizes: { size: ToggleSize; label: string }[] = [
  { size: "xs", label: "Extra small" },
  { size: "sm", label: "Small" },
  { size: "base", label: "Base" },
  { size: "lg", label: "Large" },
];

const Example = () => (
  <div className="flex flex-col gap-4">
    <div className="flex flex-wrap items-center gap-2">
      {sizes.map((item) => (
        <Toggle key={item.size} size={item.size}>
          {item.label}
        </Toggle>
      ))}
    </div>
    <div className="flex flex-wrap items-center gap-2">
      {sizes.map((item) => (
        <Toggle aria-label="Toggle" key={item.size} size={item.size}>
          <Icon icon={TextBoldIcon} />
        </Toggle>
      ))}
    </div>
  </div>
);

export default Example;

Border radius

borderRadius sets the corners as a Button's, on the kit's one scale: none, xs 4px, sm 6px, base 8px, lg 12px, xl 16px, or full to round it, which makes a toggle holding only an icon a circle; base is the default.

import { TextBoldIcon } from "@hugeicons/core-free-icons";
import { Icon } from "@produkto/ui/Icon";
import { Toggle, type ToggleBorderRadius } from "@produkto/ui/Toggle";

const radii: ToggleBorderRadius[] = [
  "none",
  "xs",
  "sm",
  "base",
  "lg",
  "xl",
  "full",
];

const Example = () => (
  <div className="flex flex-wrap items-center gap-2">
    {radii.map((borderRadius) => (
      <Toggle key={borderRadius} borderRadius={borderRadius}>
        {borderRadius}
      </Toggle>
    ))}
    <Toggle aria-label="Toggle" borderRadius="full">
      <Icon icon={TextBoldIcon} />
    </Toggle>
  </div>
);

export default Example;

With icon

An icon goes in as a child beside the words, and data-icon="inline-start" or "inline-end" trims the padding on its side.

import { Bookmark02Icon, StarIcon } from "@hugeicons/core-free-icons";
import { Icon } from "@produkto/ui/Icon";
import { Toggle } from "@produkto/ui/Toggle";

const Example = () => (
  <div className="flex flex-wrap items-center gap-2">
    <Toggle>
      <Icon icon={Bookmark02Icon} data-icon="inline-start" />
      Toggle
    </Toggle>
    <Toggle>
      Toggle
      <Icon icon={StarIcon} data-icon="inline-end" />
    </Toggle>
  </div>
);

export default Example;

Icon only

An icon alone makes the toggle square at every size, and aria-label names it for a screen reader.

import {
  TextBoldIcon,
  TextItalicIcon,
  TextUnderlineIcon,
} from "@hugeicons/core-free-icons";
import { Icon, type IconSvgElement } from "@produkto/ui/Icon";
import { Toggle } from "@produkto/ui/Toggle";

const icons: IconSvgElement[] = [
  TextBoldIcon,
  TextItalicIcon,
  TextUnderlineIcon,
];

const Example = () => (
  <div className="flex flex-wrap items-center gap-2">
    {icons.map((icon, at) => (
      <Toggle aria-label="Toggle" key={at}>
        <Icon icon={icon} />
      </Toggle>
    ))}
  </div>
);

export default Example;

Pressed

defaultPressed, or pressed with onPressedChange, turns the toggle on, beside one off as it is by default.

import { Toggle } from "@produkto/ui/Toggle";

const Example = () => (
  <div className="flex flex-wrap items-center gap-2">
    <Toggle>Toggle</Toggle>
    <Toggle defaultPressed>Toggle</Toggle>
  </div>
);

export default Example;

Disabled

disabled dims the toggle and holds off clicks and the keyboard, beside one as it is by default.

import { Toggle } from "@produkto/ui/Toggle";

const Example = () => (
  <div className="flex flex-wrap items-center gap-2">
    <Toggle>Toggle</Toggle>
    <Toggle disabled>Toggle</Toggle>
  </div>
);

export default Example;

API Reference

Toggle

ToggleProps · components/Toggle/Toggle.types.ts
PropTypeDefault
appearanceToggleAppearance"solid" | "soft" | "outline" | "transparent""solid"
sizeToggleSize"xs" | "sm" | "base" | "lg""base"
borderRadiusToggleBorderRadius"none" | "xs" | "sm" | "base" | "lg" | "xl" | "full""base"

It also takes the props of Radix Toggle.Root.