Skip to content

Toggle

The Toggle component is a button that switches between two states — selected and unselected. It supports variants, sizes, icons, and an optional tooltip, and can be used in controlled or uncontrolled mode.

Variations

Controlled and uncontrolled

The Toggle component can be used in both controlled and uncontrolled modes. In controlled mode, the selected prop drives the toggle’s state and onChange fires with the new boolean when the user activates it. In uncontrolled mode, defaultValue sets the initial state and the component manages its own state internally.

Variants

The variant prop controls the unselected style: outline, ghost, or transparent. The selectedVariant prop controls the style when the toggle is selected, with options primary and secondary.

Sizes

The size prop controls the toggle’s overall dimensions. Supported values are md (default), sm, and xs.

Rounded

Pass rounded to give the underlying button fully rounded corners.

Icon only

Pass iconOnly to size the toggle to fit a single icon, suitable for icon-only buttons. When iconOnly is set, the prefixIcon is the only icon displayed and the text prop becomes the button’s aria-label.

With tooltip

Pass tooltipProps to wrap the toggle in a Tooltip. It accepts the title, content, side, and align properties from TooltipProps.

Installation

import { Toggle } from "@harnessio/ui/components";
<Toggle
size="sm"
variant="outline"
selectedVariant="primary"
rounded
tooltipProps={{
content: <p>This is a tooltip</p>,
}}
text="Toggle"
/>;