$npx @dinachi/cli@latest add toggle
variantsizedefaultPresseddisabledbold-toggle.tsx
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>
)
}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>
);
}Active formatting: none
Pressed state held in React, with a live status.
import React from 'react';
import { Toggle } from '@/components/ui/toggle';
import { Bold, Italic, Underline } from 'lucide-react';
export function Example() {
const [formatting, setFormatting] = React.useState({
bold: false,
italic: false,
underline: false
});
return (
<div className="space-y-4">
<div className="flex items-center gap-2">
<Toggle
pressed={formatting.bold}
onPressedChange={(pressed) => setFormatting(prev => ({ ...prev, bold: pressed }))}
aria-label="Toggle bold"
>
<Bold className="h-4 w-4" />
</Toggle>
<Toggle
pressed={formatting.italic}
onPressedChange={(pressed) => setFormatting(prev => ({ ...prev, italic: pressed }))}
aria-label="Toggle italic"
>
<Italic className="h-4 w-4" />
</Toggle>
<Toggle
pressed={formatting.underline}
onPressedChange={(pressed) => setFormatting(prev => ({ ...prev, underline: pressed }))}
aria-label="Toggle underline"
>
<Underline className="h-4 w-4" />
</Toggle>
</div>
<div className="text-sm text-muted-foreground">
Active: {Object.entries(formatting)
.filter(([, active]) => active)
.map(([format]) => format)
.join(', ') || 'none'}
</div>
</div>
);
}Default and outline.
import { Toggle } from '@/components/ui/toggle';
import { Bold, Italic } from 'lucide-react';
export function Example() {
return (
<div className="flex items-center gap-2">
<Toggle variant="default" aria-label="Bold">
<Bold className="h-4 w-4" />
</Toggle>
<Toggle variant="outline" aria-label="Italic">
<Italic className="h-4 w-4" />
</Toggle>
</div>
);
}Small, default and large.
import { Toggle } from '@/components/ui/toggle';
import { Bold } from 'lucide-react';
export function Example() {
return (
<div className="flex items-center gap-2">
<Toggle size="sm" aria-label="Small">
<Bold className="h-3 w-3" />
</Toggle>
<Toggle size="default" aria-label="Default">
<Bold className="h-4 w-4" />
</Toggle>
<Toggle size="lg" aria-label="Large">
<Bold className="h-5 w-5" />
</Toggle>
</div>
);
}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
Runs for people and agents. agent
(pressed, details) => voiddetails.cancel() refuses the change.variant"default" | "outline" = "default"
Outline adds a border.size"default" | "sm" | "lg" = "default"
Height and padding.disabled
Blocks people and agents.agent
boolean = falsevaluestring
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
labelThe aria-label, or the button's text.
checkedWhether it is pressed.
disabledTrue 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.
Keyboard
SpaceEnterFlips the pressed state
SpaceEnterFlips the pressed state