Toggle

A button that stays pressed or unpressed.

Source
$npx @dinachi/cli@latest add toggle
variant
size
defaultPressed
disabled

Usage

import { Toggle } from "@/components/ui/toggle"
import { Bold } from "lucide-react"

export function BoldToggle() {
  return (
    <Toggle aria-label="Toggle bold">
      <Bold className="h-4 w-4" />
    </Toggle>
  )
}

Examples

An icon toggle for bold.
import { Toggle } from '@/components/ui/toggle';
import { Bold } from 'lucide-react';

export function Example() {
  return (
    <Toggle aria-label="Toggle bold">
      <Bold className="h-4 w-4" />
    </Toggle>
  );
}

Notes

Several toggles

For a set, use Toggle Group. It holds the shared value and agents read it as one control.

toggleVariants

toggleVariants gives another element the same look.

API

pressedboolean
Controlled pressed state.
defaultPressedboolean = false
Starting state when uncontrolled.
onPressedChange
agent
(pressed, details) => void
Runs for people and agents. details.cancel() refuses the change.
variant"default" | "outline" = "default"
Outline adds a border.
size"default" | "sm" | "lg" = "default"
Height and padding.
disabled
agent
boolean = false
Blocks people and agents.
valuestring
Identifies the toggle inside a ToggleGroup.
renderReactElement
Swap the element, keep the behaviour.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as toggle.

Reads

label

The aria-label, or the button's text.

checked

Whether it is pressed.

disabled

True when the toggle is blocked.

Actions

toggle()

Flips the pressed state.

setChecked(true)

Sets it pressed or unpressed. A no-op if already there.

An agent's change runs through onPressedChange like anyone's. isAgentChange(details) is true for it; details.cancel() refuses it and the agent is told the app refused. A disabled toggle refuses on its own.

await agent.act({
  steps: [{ ref: "Toggle bold", action: "toggle" }],
})

Accessibility

  • Renders a <button> with aria-pressed.
  • An icon-only toggle needs an aria-label. The agent reads the same name.
  • Keep the label the same in both states; aria-pressed carries the state.