Skip to main content

Toggle

A simple boolean toggle.

Examples

Plain Toggle

An on/off switch. Available in two sizes.

import { Toggle } from '@curology/ui-components-web/react/Toggle';

<Toggle
size="{sm|md(default)}"
checked
/>

As Radio Button

A Toggle can also function like a radio button, allowing only one toggle with a matching name to be selected at a time.

import { Toggle } from '@curology/ui-components-web/react/Toggle';

<Toggle
...
name="some-name"
type="radio"
/>

With label

A label can (and should!) be added to each toggle to indicate its purpose. It can also have additional meta text applied beneath.

import { Toggle } from '@curology/ui-components-web/react/Toggle';

<Toggle
...
label="A toggle with a label"
meta="And some meta text"
/>

Critical, Warning, Success, and Disabled States

There are four additional states the Toggle can be in, to indicate danger, warning, success or the inability to be controlled.

import { Toggle } from '@curology/ui-components-web/react/Toggle';

<Toggle
...
state="{''(default)|critical|warning|success}"
/>

Controlled Toggle

Like all form inputs, a Toggle can either be uncontrolled, or controlled (by providing a value and onChange listener).

import { Toggle } from '@curology/ui-components-web/react/Toggle';

<Toggle
size="{sm|md(default)}"
checked
onChange={(evt, checked) => ...}
/>

As a list

A list of toggles, complete with a label and helperText at the top, can be displayed by wrapping multiple Toggles in a ToggleGroup.

import { Toggle } from '@curology/ui-components-web/react/Toggle';
import { ToggleGroup } from '@curology/ui-components-web/react/Toggle/ToggleGroup';

<ToggleGroup
name="choice"
label="A list of toggle choices"
helperText="Just pick one"
>
<Toggle value="a" defaultChecked />
<Toggle value="b" />
<Toggle value="c" />
</ToggleGroup>

As a controlled list

Often, a single Toggle doesn't have much use, so the best way to utilize a Toggle is to group it with others inside a ToggleGroup. This group exposes and accepts the selected value with onChange and value.

import { Toggle } from '@curology/ui-components-web/react/Toggle';
import { ToggleGroup } from '@curology/ui-components-web/react/Toggle/ToggleGroup';

<ToggleGroup
...
values={['a']}
onChange={(evt, values) => ...}
>
<Toggle value="a" />
<Toggle value="b" />
<Toggle value="c" />
</ToggleGroup>

As a controlled radio list

When passing a type of radio, the list of toggles will only allow one value selected at a time.

import { Toggle } from '@curology/ui-components-web/react/Toggle';
import { ToggleGroup } from '@curology/ui-components-web/react/Toggle/ToggleGroup';

<ToggleGroup
...
type="radio"
value="a"
onChange={(evt, value) => ...}
>
<Toggle value="a" />
<Toggle value="b" />
<Toggle value="c" />
</ToggleGroup>

Props

ToggleProps

NameTypeDefault ValueRequiredDescription
All attributes of a <input />
labelReactNodeNoA string or component to display as a label above the input
metaReactNodeNoA string or component to display as meta text beneath the input
onChangeFormControlChangeEventHandlerNoEmit a new value when the input's selected value changes.
size"md" | "sm"'md'NoThe size of the field
state"" | "critical" | "warning" | "success"''NoThe state of the field
type"checkbox" | "radio"'checkbox'NoShould this render/behave as a checkbox or radio button?

ToggleGroupProps

NameTypeDefault ValueRequiredDescription
All attributes of a <div />
brand"agency" | "curology"''NoThe brand of the component.
defaultValuestring | number | readonly string[] | (string | number | readonly string[] | undefined)[]NoDefault value to use the first time the context is created.
disabledfalse | truefalseNoShould the field be considered disabled?
fieldClassNamestringNoA className to apply to the field.
fieldPropsRecord<string, unknown>NoAny props to spread onto the field container.
helperTextReactNodeNoA string or component to display as helper text beneath the input
highlightReactNodeNoHighlighted helper text to display at the bottom of the multiline text input
highlightClassNamestringNoA className to apply to the highlight.
idstring''NoA unique ID for this form control.
labelReactNodeNoA string or component to display as a label above the input
namestring''NoThe name for the radio group.
onChangeOnChangeSingle | OnChangeMultipleNoEmit a new value when the group's selected values change.
requiredfalse | truefalseNoIs the field required?
resizefalse | truefalseNoEnable or disable resizing.
size"md" | "sm"'md'NoThe size of the field.
state"" | "critical" | "warning" | "success"''NoThe state of the field
type"checkbox" | "radio"checkboxNoShould this render/behave as a checkbox or radio button?
valuestring | number | readonly string[]NoThe currently selected value. Don't allow value when dealing with a checkbox.
values(string | number | readonly string[] | undefined)[][]NoDon't allow values when dealing with a radio. A list of selected values.