The Toggle component renders a switch to toggle between enabled or disabled states.

Importing

The component can be imported via:

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

Usage

At a minimum, the following props should be passed to the component:

  • an isChecked state
  • an onChange handler
  • a label
import { Toggle } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
	const [checked, setChecked] = useState(false);

	return <Toggle
		value="toggle_value"
		label="Default toggle"
		isChecked={checked}
		onChange={(e) => setChecked(e.currentTarget.checked)}
	/>
}

More examples of available props:

<Box display="flex" flexDirection="column" gap="200">
	<Toggle
		value="enabled_toggle"
		label="Enabled toggle"
		isChecked={true}
		onChange={() => {}}
	/>

	<Toggle
		value="unchecked_toggle"
		label="Unchecked toggle"
		isChecked={false}
		onChange={() => {}}
	/>

	<Toggle
		value="disabled_toggle"
		label="Disabled toggle"
		isChecked={false}
		isDisabled={true}
		onChange={() => {}}
	/>
</Box>

Labels

A Toggle must always have an associated label. The component accepts a label string prop, which renders the label next to the toggle.

Alternatively, ariaLabelledby may be used to associate the toggle with a label elsewhere in the DOM. To make sure the association is set up both ways, it's recommended to also set for on the label, pointing to an id on the toggle:

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

export default function Example() {
	const [checked, setChecked] = useState(false);

	return <>
		<Box
			as="label"
			id="my_label_id"
			htmlFor="my_aria_toggle"
		>
			Accept terms and conditions
		</Box>
		<Box>
			<Toggle
				id="my_aria_toggle"
				ariaLabelledby="my_label_id"
				isChecked={checked}
				onChange={(e) => setChecked(e.currentTarget.checked)}
			/>
		</Box>
	</>
}

Finally, the ariaLabel prop can be used if no other element is suitable for a label.

Customizing labels

If a string label is not sufficient, a component can be used to render the label.

In React, the label prop accepts any ReactNode.

import { Toggle, Icon } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
  const [checked, setChecked] = useState(false);

  return <Toggle
    label={<>Custom label <Icon name="campaigns" isInline={true} size="sm" /></>}
    isChecked={checked}
    onChange={(e) => {
      setChecked(e.currentTarget.checked);
    }}
  />
}

API

Element (or elements) that describe the form field. Also used for error messages, as aria-errormessage isn't supported in all assistive technologies: https://a11ysupport.io/tech/aria/aria-errormessage_attribute

String value that labels the form field.

Element (or elements) that label the form field.

A description for the field, providing additional context or hints.

An error message associated with the form field. Boolean: Ember-only. If the error named block is used, this prop can be set to true to indicate an error is present, which will make the error block render.

An optional id to be assigned to the input element. Also used to generate ids for labels and error messages. If one isn't provided, it will be generated.

Whether the toggle is in the "checked" state

Whether the form field is disabled.

Whether the form field is in an invalid state.

The label text to show with the form field.

The text shown inside the toggle when it is in the "unchecked" state

The text shown inside the toggle when it is in the "checked" state

The name shared by toggles in the same group

The change event handler for the input element. Fired when a toggle is checked or unchecked. In React, the event is a React SyntheticEvent; in Ember it's a native change event.

Whether to allow browser autofill and password managers. When false (default), adds attributes to disable password managers (1Password, LastPass, Bitwarden, Dashlane). Set to true for fields where autofill is desired (e.g., email, password, address fields).

Whether the generated description id should be included in the input's aria-describedby attribute. Disable this when a label`` tag surrounds the input as well as it's description text, for example in an OptionCard` component.

Whether the generated label id should be included in the input's aria-labelledby attribute. Disable this when the label tag surrounds the input as well as it's label text, for example in an OptionCard component.

By default, form elements will throw an error if no label or aria-label is provided. Disable this to suppress the error, for example when building a custom form element, and you want to handle labeling yourself.

Default:'md'

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

Additional classes to be applied to the description element.

Additional classes to be applied to the error element.

Additional classes to be applied to the field node.

Additional classes to be applied to the footer element.

Ember-only: this is used internally to detect whether a label named block is present, and to ignore empty label props if so.

Additional classes to be applied to the input node.

Sets the size attribute on the input element. This controls the visible width of the input in characters.

Whether the component should render with "legacy" styles.

Additional classes to be applied to the label node.

The value associated with this toggle

Whether to apply the center-baseline variant.

On this page