Edit page

Toggle

Toggle switches things on and off.

Basic Usage

To implement the Toggle component, you need to import it first:

import { Toggle } from '@react-ui-org/react-ui';

And use it:

See API for all available options.

General Guidelines

  • Use the toggle only for boolean (true/false) input with an immediate effect (without confirmation). To toggle things on or off in forms that require to be submitted by a button, use rather the CheckboxField component.

  • Use positive wording for the toggle label, so that it's clear what will happen when the user turns on the toggle. Avoid negations such as “Don't send me any emails” which would mean that the user needs to turn the toggle on in order for something not to happen.

  • Only make the Toggle's label invisible when there is another visual clue to guide users through using the input.

  • When a short label is not enough, use help texts to guide users before they enter anything.

  • Use clear, calm error messages when there's a problem with what they entered.

  • The toggle is designed for switching things on and off. Don't use it to switch between two different things that cannot be described as on or off using a single label, e.g. different viewing modes. In such cases, consider using the ButtonGroup component.

Help Text

You may provide an additional help text to clarify what will be the impact of turning the toggle on or off.

Label Options

In some cases, it may be convenient to visually hide the toggle label. The label remains accessible to assistive technologies.

It's also possible to display the label before the input:

States

Validation States

Validation states visually present the result of validation of the input. You should always provide a validation message for states other than valid so users know what happened and what action they should take or what options they have.

Disabled State

Disabled state makes the input unavailable.

Forwarding HTML Attributes

In addition to the options below in the component's API section, you can specify React synthetic events or you can add whatever HTML attribute you like. All attributes that don't interfere with the API are forwarded to the native HTML <input>. This enables making the component interactive and helps to improve its accessibility.

👉 Refer to the MDN reference for the full list of supported attributes of the checkbox input type.

Forwarding ref

If you provide ref, it is forwarded to the native HTML <input> element.

API

Prop nameTypeDefaultRequiredDescription
disabledboolfalsefalse

If true, the input will be disabled.

helpTextnodenullfalse

Optional help text.

idstringundefinedfalse

ID of the input HTML element. It also serves as a prefix for nested elements:

  • <ID>__label
  • <ID>__labelText
  • <ID>__helpText
  • <ID>__validationText
isLabelVisiblebooltruefalse

If false, the label will be visually hidden (but remains accessible by assistive technologies).

labelstringtrue

Toggle label.

labelPosition'before' │ 'after''after'false

Placement of the label relative to the input.

requiredboolfalsefalse

If true, the input will be required.

validationState'invalid' │ 'valid' │ 'warning'nullfalse

Alter the field to provide feedback based on validation result.

validationTextnodenullfalse

Validation message to be displayed.

Theming

Head to Forms Theming to see shared form theming options. On top of that, the following options are available for Toggle.

Custom PropertyDescription
--rui-FormField--check__input--toggle__widthInput width (height is shared with other check fields)
--rui-FormField--check__input--toggle__border-radiusInput corner radius
--rui-FormField--check__input--toggle__background-sizeInput background size
--rui-FormField--check__input--toggle--default__background-imageBackground image of unchecked input
--rui-FormField--check__input--toggle--default__background-positionBackground position of unchecked input
--rui-FormField--check__input--toggle--checked__background-imageBackground image of checked input
--rui-FormField--check__input--toggle--checked__background-positionBackground position of checked input