Components

Toggle

A two-state button that can be either on or off.

Loading...

Installation

CLI

npx shadcn@latest add https://exawizards.com/exabase/design/registry/toggle.json

Manual

Copy and paste the following code into your project.

"use client"

import * as React from "react"
import { Toggle as TogglePrimitive } from "@base-ui/react/toggle"
import { cva, type VariantProps } from "class-variance-authority"

import { cn } from "@/lib/utils"
import { buttonVariantOption } from "@/components/ui/button"

const { base, options } = buttonVariantOption

const toggleVariants = cva(
  `${base} text-foreground not-data-disabled:hover:bg-accent not-data-disabled:hover:text-accent-foreground not-data-disabled:active:bg-accent-pressed`,
  {
    variants: {
      variant: {
        default: `data-pressed:bg-secondary-hovered not-data-disabled:data-pressed:hover:bg-secondary-pressed not-data-disabled:data-pressed:active:bg-secondary-hovered`,
        outline: `border-border data-pressed:bg-secondary-hovered not-data-disabled:data-pressed:hover:bg-secondary-pressed not-data-disabled:data-pressed:active:bg-secondary-hovered`,
        primary: `data-pressed:bg-primary-hovered data-pressed:text-primary-foreground not-data-disabled:data-pressed:hover:bg-primary-hovered not-data-disabled:data-pressed:active:bg-primary-pressed`,
        "primary-muted": `data-pressed:bg-primary-text/[.08] data-pressed:text-primary-text not-data-disabled:data-pressed:hover:bg-primary-text/[.16] not-data-disabled:data-pressed:active:bg-primary-text/[.24]`,
      },
      size: options.variants.size,
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  }
)

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

export { Toggle, toggleVariants }

Usage

import { Toggle } from "@/components/ui/toggle"
<Toggle>Toggle</Toggle>

Examples

Variant

Loading...

Size

Loading...

With Icon

Remember to add the data-icon="inline-start" or data-icon="inline-end" attribute to the icon for the correct spacing.

Loading...

Disabled

Loading...

API

Toggle

PropTypeDefaultDescription
variant"default" | "outline" | "primary" | "primary-muted"defaultThe variant of the button.
size"default" | "xs" | "sm" | "lg" | "icon" | "icon-xs" | "icon-sm" | "icon-lg"defaultThe size of the button.

See the Base UI documentation for more information.

On this page