Toggle

A two-state button, and ToggleGroup for toolbars: single choice like alignment or multiple like bold and italic.

Status: beta. Category: Actions.

import { Toggle, ToggleGroup, ToggleGroupItem } from '@glyphui/ui'

Exports: Toggle, ToggleGroup, ToggleGroupItem, toggleStyles.

Notes

  • Icon-only toggles need an aria-label.
  • Use Switch for settings that apply immediately and SegmentedControl for switching views; Toggle fits editor toolbars.

Toggle props

PropTypeDefaultDescription
asChildboolean
size"sm" | "md" | "lg"
variant"default" | "outline"

Example: Formatting toolbar

type="multiple" for independent options, type="single" for one choice.

import { Separator, ToggleGroup, ToggleGroupItem } from '@glyphui/ui'
import { AlignCenter, AlignLeft, AlignRight, Bold, Italic, Underline } from 'lucide-react'

export default function Example() {
  return (
    <div className="flex items-center gap-2 rounded-lg border border-border bg-surface p-1 shadow-xs">
      <ToggleGroup type="multiple" size="sm" defaultValue={['bold']} aria-label="Text style">
        <ToggleGroupItem value="bold" aria-label="Bold">
          <Bold />
        </ToggleGroupItem>
        <ToggleGroupItem value="italic" aria-label="Italic">
          <Italic />
        </ToggleGroupItem>
        <ToggleGroupItem value="underline" aria-label="Underline">
          <Underline />
        </ToggleGroupItem>
      </ToggleGroup>
      <Separator orientation="vertical" className="h-5" />
      <ToggleGroup type="single" size="sm" defaultValue="left" aria-label="Alignment">
        <ToggleGroupItem value="left" aria-label="Align left">
          <AlignLeft />
        </ToggleGroupItem>
        <ToggleGroupItem value="center" aria-label="Align center">
          <AlignCenter />
        </ToggleGroupItem>
        <ToggleGroupItem value="right" aria-label="Align right">
          <AlignRight />
        </ToggleGroupItem>
      </ToggleGroup>
    </div>
  )
}

Example: Single toggle

import { Text, Toggle } from '@glyphui/ui'
import { Star } from 'lucide-react'
import { useState } from 'react'

export default function Example() {
  const [starred, setStarred] = useState(false)
  return (
    <div className="flex items-center gap-3">
      <Toggle
        variant="outline"
        pressed={starred}
        onPressedChange={setStarred}
        aria-label="Star repository"
      >
        <Star className={starred ? 'fill-current text-warning' : undefined} />
        {starred ? 'Starred' : 'Star'}
      </Toggle>
      <Text size="small" tone="muted">
        {starred ? '1,205' : '1,204'} stars
      </Text>
    </div>
  )
}

Example: Variants and sizes

import { Toggle } from '@glyphui/ui'
import { Pin } from 'lucide-react'

export default function Example() {
  return (
    <div className="flex flex-wrap items-center gap-2">
      <Toggle size="sm" aria-label="Pin small">
        <Pin />
      </Toggle>
      <Toggle size="md" aria-label="Pin medium" defaultPressed>
        <Pin />
      </Toggle>
      <Toggle size="lg" aria-label="Pin large">
        <Pin />
      </Toggle>
      <Toggle variant="outline" size="sm">
        Small
      </Toggle>
      <Toggle variant="outline" defaultPressed>
        Medium
      </Toggle>
      <Toggle variant="outline" size="lg">
        Large
      </Toggle>
    </div>
  )
}

Live page: https://glyphui.xyz/components/toggle