DocsBlog

Get Started

Components

Data Grid

Signals

Styling

Theming

↑↓Navigate
↵Select
EscClose
  • 1.8.0

  • Day

    Night

    Preview

    Switch mode
  • cerberus

    acheron

    elysium

    oceanus

Get Started
Components
Data Grid
Signals
Styling
Theming

Concepts

OverviewCompositionCerberus ContextTesting

Layout

Aspect RatioBleedBoxCenterContainerContainer QueryDividerFlexFloatGridGroupLink OverlayScrollableStackWrap

Components

AccordionAdmonitionAvatarButtonCarouselCheckboxClipboardCollapsibleComboboxConfirm ModalCTA ModalDate PickerDialogFieldFieldsetFile UploaderIconButtonInputLoading StatesMarqueeMenuNotificationsNumber InputPaginationPin InputPopoverProgress IndicatorsPrompt ModalRadioRatingSelectSliderSplit ButtonSwitchTableTabsTagTextTextareaToggleTooltip

Utilities

Client OnlyDownload TriggerEnvironmentFeature FlagsFocus TrapForFormat ByteFormat NumberFormat Relative TimeFormat Relative TimeFrameHighlightJSON Tree ViewLocalePortalPresenceShowSwapsplitPropsTheme

Toggle

Toggle allows users to toggle between states via an action.

  • npm
  • source
  • Ark
View as Markdown
Open this page in Markdown
Anthropic
Open in Claude
Ask questions about this page
OpenAI
Open in ChatGPT
Ask questions about this page

Import

import { ToggleParts } from '@cerberus/react'

Usage

Warning

If you need to conditionally render content based on a binary condition, use the Show component.

The Toggle component is a two-state button that allows users to toggle between states via an action. This is a headless (unstyled) component to fit any scenario you may need.

Loading example...

Primitives

You can utilize the primitive components or the css prop to customize the toggle.

ComponentDescription
ToggleRootThe context provider for the Toggle parts
ToggleIndicatorThe indicator shown based on the toggle state

Parts

The ToggleParts API is an Object containing the full family of components.

Note

It is best to only use the ToggleParts if you are building a custom solution. Importing Object based components will ship every property it includes into your bundle, regardless if you use it or not.

NameDescription
RootThe ToggleRoot component which is the Provider for the family.
IndicatorThe ToggleIndicator component which displays based on the pressed state.

API

Root

Props

PropTypeRequiredDefaultDescription
asChildbooleanfalseundefinedUse the provided child element as the default rendered element, combining their props and behavior.
defaultPressedbooleanfalseundefinedThe default pressed state of the toggle.
onPressedChange(pressed: boolean) => voidfalseundefinedEvent handler called when the pressed state of the toggle changes.
pressedbooleanfalseundefinedThe pressed state of the toggle.

Data Attributes

AttributeDescription / Value
data-scopetoggle
data-partroot
data-state"on" | "off"
data-pressedPresent when pressed
data-disabledPresent when disabled

Indicator

Props

PropTypeRequiredDefaultDescription
asChildbooleanfalseundefinedUse the provided child element as the default rendered element, combining their props and behavior.
fallbackstring | number | bigint | boolean | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | Promise<...>falseundefinedThe fallback content to render when the toggle is not pressed.

Data Attributes

AttributeDescription / Value
data-scopetoggle
data-partindicator
data-disabledPresent when disabled
data-pressedPresent when pressed
data-state"on" | "off"

Context

API:

PropertyTypeDescription
pressedbooleanWhether the toggle is pressed.
disabledbooleanWhether the toggle is disabled.
setPressed(pressed: boolean) => voidSets the pressed state of the toggle.
getRootProps() => T["element"]Props for the root button element.
getIndicatorProps() => T["element"]Props for the optional visual indicator (e.g. icon) element.

On this page

  • Import
  • Usage
  • Primitives
    • Parts
  • API
    • Root
      • Props
      • Data Attributes
    • Indicator
      • Props
      • Data Attributes
    • Context
Edit this page on Github