Skip to content
All components
Primitives

Toggle

A button that stays pressed or unpressed, for a single option that applies to the current context, such as bold text or captions.

A two-state button.

Installation

tdpxl is not an npm dependency of your app. The @tdpxl/cli command (not published to npm yet) copies the source into your project, with the registry items and npm packages it imports.

npx @tdpxl/cli add toggle

Then import it from @/components/ui/toggle. New project? Start with the installation guide.

Examples

Each example is one branch of the official demo, rendered in its own frame. The code is that branch as you would write it, with imports pointing at the default install paths.

Pressed

Show an action that remains pressed after activation. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { BoldIcon } from "lucide-react"

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

export function TogglePressed() {
  return (
    <div className="flex flex-wrap items-center gap-2">
      <Toggle aria-label="Bold" defaultPressed>
        <BoldIcon className="size-4" />
      </Toggle>
    </div>
  )
}

Stroke

Inspect the toggle's bordered visual treatment. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { ItalicIcon } from "lucide-react"

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

export function ToggleStroke() {
  return (
    <div className="flex flex-wrap items-center gap-2">
      <Toggle aria-label="Italic" variant="stroke">
        <ItalicIcon className="size-4" />
      </Toggle>
    </div>
  )
}

Large

Compare the large toggle size. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { UnderlineIcon } from "lucide-react"

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

export function ToggleLarge() {
  return (
    <div className="flex flex-wrap items-center gap-2">
      <Toggle aria-label="Underline" size="lg">
        <UnderlineIcon className="size-4" />
      </Toggle>
    </div>
  )
}

With label

Use visible text with an icon so the toggle needs no aria-label. The label stays the same; aria-pressed reports the state.

"use client"

import { StarIcon } from "lucide-react"

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

export function ToggleWithLabel() {
  return (
    <div className="flex flex-wrap items-center gap-2">
      <div className="flex items-center gap-2">
        <Toggle variant="stroke">
          <StarIcon />
          Favorite
        </Toggle>
        <Toggle variant="stroke" defaultPressed>
          <StarIcon />
          Pinned
        </Toggle>
      </div>
    </div>
  )
}

Sizes

Compare the small, medium and large toggle sizes. Every size stays above the 24 by 24 pixel minimum target.

"use client"

import { BoldIcon } from "lucide-react"

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

export function ToggleSizes() {
  return (
    <div className="flex flex-wrap items-center gap-2">
      <div className="flex items-center gap-2">
        <Toggle size="sm" variant="stroke" aria-label="Bold (small size)">
          <BoldIcon />
        </Toggle>
        <Toggle size="md" variant="stroke" aria-label="Bold (medium size)">
          <BoldIcon />
        </Toggle>
        <Toggle size="lg" variant="stroke" aria-label="Bold (large size)">
          <BoldIcon />
        </Toggle>
      </div>
    </div>
  )
}

Controlled

Control the pressed state and reflect it in nearby text. pressed and onPressedChange are owned by the parent component.

"use client"

import * as React from "react"
import { CaptionsIcon } from "lucide-react"

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

function CaptionsToggle() {
  const [pressed, setPressed] = React.useState(false)

  return (
    <div className="flex items-center gap-3">
      <Toggle variant="stroke" pressed={pressed} onPressedChange={setPressed}>
        <CaptionsIcon />
        Captions
      </Toggle>
      <span className="text-paragraph-sm text-sub-600">
        {pressed ? "Captions are shown" : "Captions are hidden"}
      </span>
    </div>
  )
}

export function ToggleControlled() {
  return (
    <div className="flex flex-wrap items-center gap-2">
      <CaptionsToggle />
    </div>
  )
}

API reference

Generated from the component's TypeScript signatures. Inherited DOM and primitive props are listed separately.

Toggle

Represent an action with a pressed or unpressed state.

Toggle props
PropTypeDefaultDescription
pressedboolean | undefined—The controlled state of the toggle.
defaultPressedboolean | undefined—The state of the toggle when initially rendered. Use defaultPressed if you do not need to control the state of the toggle.
onPressedChange((pressed: boolean) => void) | undefined—The callback that fires when the state of the toggle changes.
inputMode"search" | "text" | "none" | "tel" | "url" | "email" | "numeric" | "decimal" | undefined—Hints at the type of data that might be entered by the user while editing the element or its contents
aria-describedbystring | undefined—Identifies the element (or elements) that describes the object.
aria-invalidboolean | "false" | "true" | "grammar" | "spelling" | undefined—Indicates the entered value does not conform to the format expected by the application.
aria-labelstring | undefined—Defines a string value that labels the current element.
aria-labelledbystring | undefined—Identifies the element (or elements) that labels the current element.
childrenReact.ReactNode—Represents all of the things React can render.
size"sm" | "md" | "lg" | null | undefined"md"Visual size of this part. Choose a supported size while preserving readable content and usable interaction targets.
variant"stroke" | "ghost" | null | undefined"ghost"Visual treatment of the toggle: stroke or ghost. Pressed state is controlled separately.
Inherited props (19)
Toggle inherited props
PropTypeDefaultDescription
formstring | undefined—ID of the form associated with this control, including when it is rendered outside that form element.
classNamestring | undefined—Additional classes for this part. Preserve contrast, visible focus and responsive content when adapting styles.
defaultCheckedboolean | undefined—Initial checked state for a control supporting uncontrolled selection. Inherited DOM typing does not give a layout wrapper checkbox behavior.
defaultValuestring | number | readonly string[] | undefined—Initial value for a control supporting uncontrolled state. Inherited DOM typing alone does not give a layout wrapper input behavior.
autoFocusboolean | undefined—Request automatic focus when the element mounts or the primitive opens, according to its source behavior. Choose initial focus deliberately in context.
idstring | undefined—Stable DOM identifier used by labels, descriptions or other explicit associations.
roleReact.AriaRole | undefined—Semantic role appropriate to the rendered element. Preserve required relationships and keyboard behavior.
onFocusReact.FocusEventHandler<HTMLButtonElement> | undefined—Receive the native focus event. Preserve the component's existing keyboard and focus behavior.
onBlurReact.FocusEventHandler<HTMLButtonElement> | undefined—Receive the native blur event when focus leaves this control.
onChangeReact.ChangeEventHandler<HTMLButtonElement, Element> | undefined—Receive the native change event; read the actual control value or checked state from its event target.
onKeyDownReact.KeyboardEventHandler<HTMLButtonElement> | undefined—Receive keyboard events. Preserve built-in keyboard behavior unless deliberately implementing an accessible alternative.
onClickReact.MouseEventHandler<HTMLButtonElement> | undefined—Receive activation events. The consumer owns the action performed and its result.
onSelectReact.ReactEventHandler<HTMLButtonElement> | undefined—Handle the selection event defined by this part's source signature. DOM text-selection events differ from menu or command activation callbacks.
asChildboolean | undefined—Merge this part into one child instead of its default element. Preserve that child's semantics, ref support and accessibility attributes.
disabledboolean | undefined—Make the supported control unavailable through its native or primitive disabled semantics. This does not disable arbitrary child links.
namestring | undefined—Name used by the control's form submission or primitive form integration.
type"button" | "submit" | "reset" | undefined—Native element or primitive mode defined by the source union. Choose a supported value; its meaning depends on the component.
valuestring | number | readonly string[] | undefined—Current value in the shape defined by the source type. For controlled interactive inputs, apply updates from the change callback.
refReact.Ref<HTMLButtonElement> | undefined—Allows getting a ref to the component instance. Once the component unmounts, React will set ref.current to null (or call the ref with null if you passed a callback ref).

Accessibility & usage

From registry/ui/toggle.meta.json (authored guidance; human review may still be pending). Behavior is checked by Storybook tests, not certified here.

When to use

  • A formatting or tool option in an editor or toolbar: bold, italic, show grid, mute.
  • A view option that the user flips often and sees take effect immediately, such as captions or a pinned panel.
  • Marking an item, such as favorite or pin, when the pressed state is the only thing that changes.

When not to use

  • A preference or setting saved to an account; a switch reads as on/off and belongs in forms and settings.
  • A choice between mutually exclusive options; use a toggle group or radio group.
  • A one-off action such as 'Save' or 'Delete' that has no persistent state.

Anatomy

Toggle
Radix Toggle root: a native button with aria-pressed and data-state on/off. variant is ghost (default) or stroke; size is sm (32px), md (36px, default) or lg (40px).
Icon and label
Children: an icon, text, or both. SVGs default to 16px. Text children become the accessible name.

Keyboard & interaction

  • Tab focuses the toggle; Enter or Space flips the pressed state and fires onPressedChange.
  • Uncontrolled with defaultPressed, or controlled with pressed and onPressedChange.
  • Focus is shown only for keyboard focus (focus-visible) with the focus shadow.
  • Disabled toggles are skipped by Tab, ignore pointer input and keep their pressed state.
  • The state change applies immediately; there is no confirmation step, so use it only for easily reversible options.

Responsive behavior

  • All sizes are at least 32x32 CSS pixels and meet the 24x24 minimum (WCAG 2.5.8); prefer lg (40px) or extra spacing on touch-first screens.
  • Toggles keep whitespace-nowrap; in narrow toolbars prefer icon-only toggles with names, or move rarely used toggles into an overflow menu.
  • Keep toggles in a fixed position as they change state; the label and width must not jump between states.

Content guidelines

  • Label the option, not the action: 'Bold', 'Captions', 'Favorite'. The pressed state says whether it is on.
  • Icon-only toggles need an aria-label with the same wording a visible label would use.
  • If the current state is not obvious from the effect, show it in text nearby ('Captions are shown').

Alternatives

  • Switch — An on/off setting, typically with a visible label in a form or settings list.
  • Toggle group — Several related options with single or multiple selection.
  • Checkbox — A boolean value submitted with a form.
  • Button — An action without a persistent pressed state.

Resources