Skip to content

    Toggle

    A button that turns a formatting option or view mode on or off. The selected state uses a soft accent fill.

    React

    Preview

    Props

    • variant
    • size
    • disabled
    • iconOnly
    • defaultPressed
    • label
    • icon
    import { IconPin } from "@tabler/icons-react"
    import { Toggle } from "@/components/ui/toggle"
    
    <Toggle>
      <IconPin size={16} />
      Pin item
    </Toggle>

    Usage

    When to use

    • Turn one option on or off, such as pinning an item or applying emphasis.
    • Keep a toolbar action selected until the user turns it off.

    When not to use

    • An action with no persistent on state. Use Button.
    • A setting submitted in a form. Use Switch.
    • Related selection options. Use ToggleGroup.

    Platform support

    What each platform has, from the spec. A selected platform is highlighted.

    NameWebiOSAndroidNote
    variantYesYesYes
    sizeYesYesYes
    disabledYesYesYes
    iconOnlyYesYesYes
    pressedYesYesYes
    defaultPressedYesYesYes
    onPressedChangeYesYesYes
    labelNoYesYesOnly on iOS, Android.
    iconNoYesYesOnly on iOS, Android.
    accessibilityLabelNoYesYesOnly on iOS, Android.

    Variants

    variant

    Transparent when off, or a surface with a neutral border for outline.

    • defaultdefault

    • outline

    import { IconPin } from "@tabler/icons-react"
    import { Toggle } from "@/components/ui/toggle"
    
    <Toggle>
      <IconPin size={16} />
      Pin item
    </Toggle>
    <Toggle variant="outline">
      <IconPin size={16} />
      Pin item
    </Toggle>

    size

    Control height, text and icon size, the same steps as Button: sm 28px, md 32px (the default), lg 36px, xl 44px with a mouse; on touch md is 36px, lg 44px and xl 48px. Small controls keep a minimum hit area.

    • sm

    • mddefault

    • lg

    • xl

    import { IconPin } from "@tabler/icons-react"
    import { Toggle } from "@/components/ui/toggle"
    
    <Toggle size="sm">
      <IconPin size={16} />
      Pin item
    </Toggle>
    <Toggle>
      <IconPin size={16} />
      Pin item
    </Toggle>
    <Toggle size="lg">
      <IconPin size={16} />
      Pin item
    </Toggle>
    <Toggle size="xl">
      <IconPin size={20} />
      Pin item
    </Toggle>

    Examples

    Formatting

    import { Toggle } from "@/components/ui/toggle"
    
    <Toggle>Bold</Toggle>

    Selected initially

    import { Toggle } from "@/components/ui/toggle"
    
    <Toggle variant="outline" defaultPressed>Grid view</Toggle>

    Icon only

    import { IconBold } from "@tabler/icons-react"
    import { Toggle } from "@/components/ui/toggle"
    
    <Toggle iconOnly aria-label="Bold">
      <IconBold size={16} />
    </Toggle>

    Disabled selection

    import { Toggle } from "@/components/ui/toggle"
    
    <Toggle disabled defaultPressed>Locked format</Toggle>

    Icon with text

    An icon and a stable label stay together while the selection changes.

    import { IconPin } from "@tabler/icons-react"
    import { Toggle } from "@/components/ui/toggle"
    
    <Toggle variant="outline" defaultPressed>
      <IconPin size={16} />
      Pin item
    </Toggle>

    Disabled icon states

    import { IconBold, IconItalic } from "@tabler/icons-react"
    import { Toggle } from "@/components/ui/toggle"
    
    <Toggle iconOnly disabled aria-label="Bold">
      <IconBold size={16} />
    </Toggle>
    <Toggle iconOnly disabled defaultPressed aria-label="Italic">
      <IconItalic size={16} />
    </Toggle>

    Do and don't

    Do

    • Use a stable label that names the option.
    • Use iconOnly with aria-label for compact formatting controls.

    Don't

    • Do not use a Toggle for an action that has no persistent state.
    • Do not use it for a setting that applies at once; use Switch.

    Props

    NameTypeValuesDefaultDescription
    variantenumdefault | outlinedefaultTransparent when off, or a surface with a neutral border for outline.
    sizeenumsm | md | lg | xlmdControl height, text and icon size, the same steps as Button: sm 28px, md 32px (the default), lg 36px, xl 44px with a mouse; on touch md is 36px, lg 44px and xl 48px. Small controls keep a minimum hit area.
    disabledboolean-falsePrevents focus and selection and uses disabled colors.
    iconOnlyboolean-falseMakes the button square. Requires aria-label on an icon-only button.
    pressedboolean--Controlled on or off state. Pair with onPressedChange.
    defaultPressedboolean-falseInitial state when uncontrolled.
    onPressedChangefunction--Receives the requested boolean state after activation.
    labelnode--Native only: the content, text or an icon. With iconOnly, pass the icon and name the toggle with accessibilityLabel. Only on iOS, Android.
    iconnode--Native only: an icon before the label. Only on iOS, Android.
    accessibilityLabelstring-Native only: the name read by VoiceOver and TalkBack when the toggle shows only an icon. Set it with iconOnly, as aria-label is on web. Only on iOS, Android.

    Anatomy

    PartDescription
    rootThe toggle button, transparent by default or a bordered surface in outline. On uses the soft accent pair.
    iconAn optional decorative icon, 16px in sm, md and lg and 20px in xl.
    hitAreaA transparent pseudo-element that extends the button target to size-target-min.

    Accessibility

    Role: button

    KeysAction
    Space, EnterToggles pressed state.
    TabMoves focus to the next control.
    • Text children provide the accessible name. iconOnly requires aria-label.
    • aria-pressed exposes the on or off state. Keep the label stable when state changes.
    • The button defaults to type="button" and does not submit its form.
    • Pass size and weight to every icon. Use regular off and optionally fill on.

    Install

    npx werewolf-ui add toggle

    Run npx werewolf-ui init once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See Installation for requirements and the alternative registry setup.