Toggle

A toggle button whose pressed state is the tinted fill, for options outside a list.

controls

Apple: Toggle buttons

· Base UI: Toggle

Toggle buttons

"use client"

import {
  FilterIcon,
  TextBoldIcon,
  TextItalicIcon,
} from "@hugeicons/core-free-icons"

import { Icon } from "@applecn/ui/components/icon"
import { Toggle } from "@applecn/ui/components/toggle"

export default function ToggleBasic() {
  return (
    <div className="flex items-center gap-3">
      <Toggle defaultPressed aria-label="Filter">
        <Icon icon={FilterIcon} /> Filter
      </Toggle>
      <Toggle shape="circle" aria-label="Bold">
        <Icon icon={TextBoldIcon} weight="bold" />
      </Toggle>
      <Toggle shape="circle" aria-label="Italic">
        <Icon icon={TextItalicIcon} weight="bold" />
      </Toggle>
    </div>
  )
}

Install

From the registry this site publishes, with dependencies resolved by the CLI.

npx shadcn@latest add https://applecn.vercel.app/r/toggle.json

Source

packages/ui/src/components/toggle.tsx

"use client"

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

/**
 * A toggle button (HIG › Toggles, outside lists): a button whose pressed state is the tinted
 * fill, the way the Phone app's filter button lights up. Sizes and corners follow the platform's
 * button metrics. Use a `Switch` inside list rows.
 */
const toggleVariants = cva(
  "group/toggle inline-flex shrink-0 items-center justify-center gap-1.5 bg-transparent leading-none font-semibold whitespace-nowrap text-foreground transition-[background-color,transform,opacity] duration-(--duration-press) ease-(--ease-standard) outline-none select-none hover:bg-fill-4 focus-visible:ring-4 focus-visible:ring-ring/60 active:scale-[0.97] disabled:pointer-events-none disabled:opacity-40 data-pressed:bg-primary/15 data-pressed:text-primary motion-reduce:active:scale-100 macos:font-normal macos:active:scale-100 web:font-normal [&_svg]:pointer-events-none [&_svg]:shrink-0",
  {
    variants: {
      size: {
        mini: "h-(--control-height-mini) min-w-(--control-height-mini) rounded-(--control-radius-mini) px-(--control-padding-x-mini) text-[length:var(--control-font-mini)]",
        small:
          "h-(--control-height-small) min-w-(--control-height-small) rounded-(--control-radius-small) px-(--control-padding-x-small) text-[length:var(--control-font-small)]",
        regular:
          "h-(--control-height-regular) min-w-(--control-height-regular) rounded-(--control-radius-regular) px-(--control-padding-x-regular) text-[length:var(--control-font-regular)]",
        large:
          "h-(--control-height-large) min-w-(--control-height-large) rounded-(--control-radius-large) px-(--control-padding-x-large) text-[length:var(--control-font-large)]",
        xl: "h-(--control-height-xl) min-w-(--control-height-xl) rounded-(--control-radius-xl) px-(--control-padding-x-xl) text-[length:var(--control-font-xl)]",
      },
      shape: {
        automatic: "",
        capsule: "rounded-full",
        rounded: "rounded-lg",
        circle: "aspect-square rounded-full px-0",
      },
    },
    defaultVariants: {
      size: "regular",
      shape: "automatic",
    },
  }
)

type ToggleProps = TogglePrimitive.Props & VariantProps<typeof toggleVariants>

function Toggle({
  className,
  size = "regular",
  shape = "automatic",
  ...props
}: ToggleProps) {
  return (
    <TogglePrimitive
      data-slot="toggle"
      data-size={size}
      data-shape={shape}
      className={cn(toggleVariants({ size, shape }), className)}
      {...props}
    />
  )
}

export { Toggle, toggleVariants }
export type { ToggleProps }