Toggle
A two-state button on Base UI Toggle.
For one setting that is on or off and takes effect at once, such as a toolbar option. For a setting in a form use @khaeli/switch or @khaeli/checkbox, and for several toggles that belong together @khaeli/toggle-group.
Variants
import { Toggle } from "@/components/ui/toggle"
export default function ToggleVariants() {
return (
<div className="flex flex-wrap items-center justify-center gap-2">
<Toggle>Wrap lines</Toggle>
<Toggle variant="outline">Show archived</Toggle>
</div>
)
}
Sizes
import { Toggle } from "@/components/ui/toggle"
export default function ToggleSizes() {
return (
<div className="flex flex-wrap items-center justify-center gap-2">
<Toggle variant="outline" size="sm">
Small
</Toggle>
<Toggle variant="outline">Default</Toggle>
<Toggle variant="outline" size="lg">
Large
</Toggle>
</div>
)
}
With icon
import { BellIcon, PinIcon } from "@keyline-icons/react"
import { Toggle } from "@/components/ui/toggle"
export default function ToggleWithIcon() {
return (
<div className="flex flex-wrap items-center justify-center gap-2">
<Toggle variant="outline">
<PinIcon data-icon="inline-start" />
Pin to top
</Toggle>
<Toggle variant="outline" defaultPressed>
<BellIcon data-icon="inline-start" />
Notify me
</Toggle>
</div>
)
}
Icon only
import { BoldIcon, ItalicIcon, UnderlineIcon } from "@keyline-icons/react"
import { Toggle } from "@/components/ui/toggle"
export default function ToggleIconOnly() {
return (
<div className="flex flex-wrap items-center justify-center gap-1">
<Toggle aria-label="Bold" defaultPressed>
<BoldIcon />
</Toggle>
<Toggle aria-label="Italic">
<ItalicIcon />
</Toggle>
<Toggle aria-label="Underline">
<UnderlineIcon />
</Toggle>
</div>
)
}
States
import { Toggle } from "@/components/ui/toggle"
export default function ToggleStates() {
return (
<div className="flex flex-wrap items-center justify-center gap-2">
<Toggle variant="outline" defaultPressed>
Pressed
</Toggle>
<Toggle variant="outline" disabled>
Disabled
</Toggle>
<Toggle variant="outline" defaultPressed disabled>
Pressed and disabled
</Toggle>
<Toggle variant="outline" aria-invalid>
Invalid
</Toggle>
</div>
)
}
Controlled
Not starred
"use client"
import * as React from "react"
import { StarIcon } from "@keyline-icons/react"
import { StarIcon as StarFilledIcon } from "@keyline-icons/react/fill"
import { Toggle } from "@/components/ui/toggle"
export default function ToggleControlled() {
const [starred, setStarred] = React.useState(false)
return (
<div className="flex items-center gap-3">
<Toggle variant="outline" aria-label="Star the project" pressed={starred} onPressedChange={setStarred}>
{starred ? <StarFilledIcon /> : <StarIcon />}
</Toggle>
<p className="text-sm text-muted-foreground">{starred ? "In your starred projects" : "Not starred"}</p>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/togglebunx --bun shadcn@latest add @khaeli/toggleInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
- class-variance-authority ^0.7.1
Keyboard
SpaceEnter- Presses or releases the toggle.
Tab- Moves focus to the toggle. A
disabledtoggle is a native disabled button: it leaves the tab order.
Accessible name
- The name is the toggle's text. An icon-only toggle needs an
aria-label. - The state is
aria-pressed, set by the item. Keep the name the same in both states (Star the project, not Unstar), or the state is said twice.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/toggle.json