Loading...
Skip to main content

Toggler

Toggle attributes or classes on click using the Toggler component. Works with any attribute except id.

Toggler is an interactive component that can be used to avoid writing small or one-off JavaScript snippets to create interactive elements. Instead of writing custom JavaScript, use its attribute, value, and target props to toggle attribute values, changes classes, and more on click events.

Examples

Toggle class

Toggle the class of an element by setting attribute="class" and passing the class name as value. Without a target, the component toggles itself.

Click to toggle `fg-danger`
<Toggler
as="div"
attribute="class"
className="w-100 p-3 rounded border"
value="fg-danger"
>
{' '}Click to toggle `fg-danger`{' '}
</Toggler>

Remove existing

If the element already has the class specified in value, toggling will remove it. Clicking again adds it back.

Starts with `bg-subtle-info`. Click to remove it.
<Toggler
as="div"
attribute="class"
className="w-100 p-3 rounded border bg-subtle-info"
value="bg-subtle-info"
>
{' '}Starts with `bg-subtle-info`. Click to remove it.{' '}
</Toggler>

Target element

Use target to toggle the class of another element. Target elements can supply their own data-bs-attribute and data-bs-value overrides.

Use button to change `bg-2`
<div className="d-flex gap-2 flex-column align-items-start">
<Toggler
target="#togglerExample1"
theme="primary"
variant="solid"
>
Toggle
</Toggler>
<div
className="w-100 p-3 rounded border"
data-bs-attribute="class"
data-bs-value="bg-2"
id="togglerExample1"
>
{' '}Use button to change `bg-2`{' '}
</div>
</div>

Multiple elements

Use a class name selector in target to toggle the class of multiple elements.

Use button to toggle `bg-subtle-info`
Use button to toggle `bg-subtle-warning`
<Stack gap={2}>
<Toggler
className="align-self-start"
target=".togglerExampleClass1"
theme="primary"
variant="solid"
>
Toggle
</Toggler>
<div
className="w-100 p-3 rounded border togglerExampleClass1"
data-bs-attribute="class"
data-bs-value="bg-subtle-info"
>
{' '}Use button to toggle `bg-subtle-info`{' '}
</div>
<div
className="w-100 p-3 rounded border togglerExampleClass1"
data-bs-attribute="class"
data-bs-value="bg-subtle-warning"
>
{' '}Use button to toggle `bg-subtle-warning`{' '}
</div>
</Stack>

Toggle attribute

Toggle attributes by setting the attribute and value props. For example, toggling the hidden attribute:

Click to toggle `hidden` attribute
<Toggler
as="div"
attribute="hidden"
className="w-100 p-3 rounded border"
value="true"
>
{' '}Click to toggle `hidden` attribute{' '}
</Toggler>

You could also use this to toggle the disabled attribute on a fieldset element to disable all the controls within. Or any other ideas you can think of with attributes.

Usage

Options

NameTypeDefaultDescription
as'button', 'div' or 'span''button'Trigger element. Non-button triggers receive a button role and keyboard support.
targetstring—Selector for affected elements. Omit it to toggle the trigger itself.
attributestring'class'Attribute to toggle. Changing id is not supported.
valuestring, number, boolean or null—Class or attribute value to toggle.

Events

Use onToggled(elements) to respond after the matched elements have been updated.

Documentation navigation
On this page
On this page