Skip to content

Toggle

A two-state on/off button for toolbar formatting, mute, and pin controls — with the neobrutalist borders-and-shadows treatment.

import { BookmarkIcon } from "lucide-react"

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

The toggle is a two-state button — pressed or not — that keeps its on state visible until clicked again. Built on the Base UI Toggle component and styled to the neobrutalist recipe: thick borders, hard shadows, and bold type.

Reach for it when:

  • Editor formatting — bold, italic, underline buttons in a text-editor toolbar.
  • Single on/off actions — mute, favorite, pin, follow; the pressed state doubles as the status indicator.
  • View options — show grid, wrap lines, split view; use several to group them into a Toggle Group.

Installation

pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/toggle.json
npx shadcn@latest add https://neobrutalism.com/r/base/toggle.json
yarn dlx shadcn@latest add https://neobrutalism.com/r/base/toggle.json
bunx --bun shadcn@latest add https://neobrutalism.com/r/base/toggle.json

Install the following dependencies:

pnpm add @base-ui/react
npm install @base-ui/react
yarn add @base-ui/react
bun add @base-ui/react

Copy and paste the following code into your project.

components/ui/toggle.tsx
"use client"

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

import { cn } from "@/lib/utils"

const toggleVariants = cva(
  "group/toggle inline-flex items-center justify-center gap-1 rounded border-2 text-sm font-head font-medium whitespace-nowrap shadow-sm transition-all outline-none hover:bg-accent hover:text-accent-foreground hover:translate-y-0.5 active:translate-y-1 active:shadow-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-pressed:bg-primary aria-pressed:text-primary-foreground data-[state=on]:bg-primary data-[state=on]:text-primary-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
  {
    variants: {
      variant: {
        default: "bg-background",
        outline: "bg-background",
      },
      size: {
        default:
          "h-8 min-w-8 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
        sm: "h-7 min-w-7 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3.5",
        lg: "h-9 min-w-9 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  }
)

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

export { Toggle, toggleVariants }

Update the import paths to match your project setup.

Usage

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

Examples

Outline

Use variant="outline" for an outline style.

import { BoldIcon, ItalicIcon } from "lucide-react"

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

With Text

import { ItalicIcon } from "lucide-react"

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

Size

Use the size prop to change the size of the toggle.

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

export function ToggleSizes() {

Disabled

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

export function ToggleDisabled() {

RTL

To enable RTL support in Neobrutalism.com, see the RTL configuration guide.

"use client"

import * as React from "react"

Accessibility

The toggle follows the toggle-button variant of the WAI-ARIA Button pattern: it renders a native <button> with aria-pressed reflecting the current state, so screen readers announce it as "pressed" or "not pressed". Make sure icon-only toggles carry an aria-label.

Keyboard interactions:

KeyAction
Tab / Shift + TabMove focus to / away from the toggle
Space / EnterToggle the pressed state

API Reference

See the Base UI Toggle documentation.