ToggleButton

Github
Storybook
Aria pattern

A button that toggles between active and inactive states.

Importing

In a React app, the component can be imported via:

import { ToggleButton } from '@customerio/pluma-components/react';

Usage

The component accepts an isActive prop, which tells it what state to render in. The button itself doesn't control the active state internally - it is up to the consumer to manage that state and change it when the button is clicked.

import { ToggleButton } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
	const [isActive, setIsActive] = useState(false);

	return <ToggleButton
		isActive={isActive}
		onClick={(e) => setIsActive(!isActive)}
	>
		Click me
	</ToggleButton>;
}

Size

A ToggleButton can be medium (default) or small:

<ToggleButton isActive={true}>Medium</ToggleButton>

<ToggleButton size="small" isActive={true}>Small</ToggleButton>

Icons

Just like the Button component, a ToggleButton accepts an icon name to render inside:

import { ToggleButton } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
	const [isActive, setIsActive] = useState(false);

	return <ToggleButton
		icon="analysis"
		isActive={isActive}
		onClick={(e) => setIsActive(!isActive)}
	>
		Measure
	</ToggleButton>;
}

Icon only button

A ToggleButton can be rendered with just an icon by setting the isIconOnly argument to true. This will stop the button from rendering the text content, and adjust the paddings so it looks more balanced:

import { ToggleButton } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
	const [isActive, setIsActive] = useState(false);

	return <ToggleButton
		icon="analysis"
		isIconOnly={true}
		isActive={isActive}
		onClick={(e) => setIsActive(!isActive)}
	/>;
}

Disabled state

A ToggleButton can be disabled by setting the isDisabled prop to true:

<ToggleButton
	isDisabled={true}
	isActive={false}
>
	Inactive
</ToggleButton>
<ToggleButton
	isDisabled={true}
	isActive={true}
>
	Active, but disabled
</ToggleButton>

API

The URL for the toggle, if it should render as a link instead of a button

The name of an icon (from Pluma Icons) to render in the button.

Default:leading

Which side of the text the icon should be rendered on. leading renders the icon before the text, trailing renders it after

Whether the button is in the active (toggled) state.

Disables the button. Use this instead of the native disabled prop, to make sure non-button elements (when used with as) get the correct styles.

When this is set to true, the button won't render any text, and will reduce its side paddings.

Default:md

The size of the button. medium and small are deprecated, use md and sm instead.

Change the color of the icon. By default, the icon inherits the button's text color. Use with care, as it can cause inconsistencies in the UI.

Only use if necessary: allows overriding the icon's size.

Whether the component should render with "legacy" styles.