PeakUI

ToggleButton

A two-state button for on/off and selected states

Usage

import {ToggleButton} from "@peakui/react";

onChange fires with the new state, and isSelected controls it.

Examples

Variants

Icon Only

An icon-only button has no visible text, so give it an aria-label.

Sizes

Disabled

Controlled

Children can be a function that receives the button's state.

Customization

Tailwind CSS

Pass utilities through className. They are merged with the button's own classes.

<ToggleButton className="rounded-full px-6 font-semibold">Rounded</ToggleButton>

Global CSS

Override the button's classes in the components layer. Prefer the component variables where one exists: the selected state derives from them.

@layer components {
  .toggle-button--default {
    --toggle-button-bg: var(--color-accent-soft);
    --toggle-button-bg-hover: var(--color-accent-soft-hover);
    --toggle-button-bg-pressed: var(--color-accent-soft-hover);
    --toggle-button-fg: var(--color-accent-soft-foreground);
  }

  .toggle-button--icon-only {
    @apply rounded-lg;
  }
}

Styling Reference

PeakUI follows the BEM methodology, so variants and states are plain class names you can target.

CSS Classes

The ToggleButton component uses these CSS classes (view source styles):

Base & Size Classes

  • .toggle-button - Base toggle button styles
  • .toggle-button--sm - Small size
  • .toggle-button--md - Medium size
  • .toggle-button--lg - Large size

Variant Classes

  • .toggle-button--default - Neutral background, accent-soft when selected
  • .toggle-button--ghost - Transparent background until hovered or selected

Modifier Classes

  • .toggle-button--icon-only - Square button for a single icon
  • .toggle-button--icon-only.toggle-button--sm - Small icon-only
  • .toggle-button--icon-only.toggle-button--lg - Large icon-only

Slots

  • [data-slot="toggle-button"]

CSS Variables

VariablePurpose
--toggle-button-bgBackground color
--toggle-button-bg-hoverBackground color when hovered
--toggle-button-bg-pressedBackground color when pressed
--toggle-button-fgText and icon color
--toggle-button-bg-selectedBackground color when selected
--toggle-button-bg-selected-hoverBackground color when selected and hovered
--toggle-button-bg-selected-pressedBackground color when selected and pressed
--toggle-button-fg-selectedText and icon color when selected
--toggle-button-borderBorder color
--toggle-button-radiusCorner radius

Interactive States

The toggle button supports both CSS pseudo-classes and data attributes:

  • Selected: [data-selected="true"] (accent-soft background and foreground)
  • Hover: :hover or [data-hovered]
  • Pressed: :active or [data-pressed] (includes scale transform)
  • Focus: :focus-visible or [data-focus-visible] (shows the focus ring)
  • Disabled: :disabled, [data-disabled] or [aria-disabled="true"]

API Reference

ToggleButton

PropTypeDefaultDescription
variant'default' | 'ghost''default'Visual style
size'sm' | 'md' | 'lg''md'Size of the toggle button
isIconOnlybooleanfalseWhether the button contains only an icon
isSelectedboolean-Controlled selected state
defaultSelectedbooleanfalseDefault selected state (uncontrolled)
isDisabledbooleanfalseWhether the toggle button is disabled
onChange(isSelected: boolean) => void-Handler called when selection changes
onPress(e: PressEvent) => void-Handler called when the button is pressed
childrenReact.ReactNode | (values: ToggleButtonRenderProps) => React.ReactNode-Button content, or a function of the button's state
classNamestring | (values: ToggleButtonRenderProps) => string-Additional classes, or a function of the button's state
renderDOMRenderFunction<'button', ToggleButtonRenderProps>-Replaces the rendered element. It must still return a <button>

There is no separate isPressed prop: a toggle button's pressed style comes from the interaction itself. Every other React Aria ToggleButton prop is accepted.

Render Props

When children or className is a function, it receives:

PropTypeDescription
isSelectedbooleanWhether the button is currently selected
isPressedbooleanWhether the button is currently pressed
isHoveredbooleanWhether the button is hovered
isFocusedbooleanWhether the button is focused
isFocusVisiblebooleanWhether the button is focused by keyboard
isDisabledbooleanWhether the button is disabled

On this page