import { Toggle } from "@registry/components/ui/toggle";

export function ToggleExample() {
  return (
    <div className="flex flex-wrap items-center gap-4">
      <Toggle>Toggle</Toggle>
    </div>
  );
}

Examples

Sizes

Available sizes: default, sm, lg.

import { Toggle } from "@registry/components/ui/toggle";

export function ToggleSizesExample() {
  return (
    <div className="flex flex-wrap items-center gap-4">
      <Toggle size="sm">Small</Toggle>
      <Toggle size="default">Default</Toggle>
      <Toggle size="lg">Large</Toggle>
    </div>
  );
}

With icons

Use an icon inside the toggle for toolbars and formatting controls.

import { Toggle } from "@registry/components/ui/toggle";
import { TextBIcon, TextItalicIcon, TextStrikethroughIcon } from "@phosphor-icons/react";

export function ToggleWithIconsExample() {
  return (
    <div className="flex flex-wrap items-center gap-2">
      <Toggle size="sm" aria-label="Toggle bold">
        <TextBIcon />
      </Toggle>
      <Toggle size="sm" aria-label="Toggle italic">
        <TextItalicIcon />
      </Toggle>
      <Toggle size="sm" aria-label="Toggle strikethrough">
        <TextStrikethroughIcon />
      </Toggle>
    </div>
  );
}

Installation

Copy the source code below into your project:

import { Toggle as BaseToggle } from "@base-ui/react/toggle";
import { cn } from "@registry/lib/utils";
import { cva } from "class-variance-authority";
import type { VariantProps } from "class-variance-authority";

const toggleVariants = cva(
  "relative inline-flex shrink-0 cursor-pointer items-center justify-center gap-2 rounded-lg bg-transparent text-sm font-medium whitespace-nowrap text-foreground outline-none select-none hover:bg-muted focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-50 data-pressed:bg-secondary data-pressed:text-accent pointer-coarse:after:absolute pointer-coarse:after:size-full pointer-coarse:after:min-h-11 pointer-coarse:after:min-w-11 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
  {
    defaultVariants: {
      size: "default",
      variant: "default",
    },
    variants: {
      size: {
        default: "h-8 min-w-8 px-[calc(--spacing(2)-1px)]",
        lg: "h-9 min-w-9 px-[calc(--spacing(2.5)-1px)]",
        sm: "h-7 min-w-7 px-[calc(--spacing(1.5)-1px)]",
      },
      variant: {
        default: "border-transparent",
      },
    },
  },
);

function Toggle({
  className,
  variant,
  size,
  ...props
}: BaseToggle.Props & VariantProps<typeof toggleVariants>) {
  return (
    <BaseToggle
      className={cn(toggleVariants({ className, size, variant }))}
      data-slot="toggle"
      {...props}
    />
  );
}

export { Toggle, toggleVariants };

API Reference

The Toggle component extends the Base UI Toggle props and adds the following:

PropTypeDefaultDescription
variant"default""default"Visual style of the toggle.
size"default" | "sm" | "lg""default"Size of the toggle.