Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline
A checkbox toggle presents two values for single selection. Use the
message-toggle-active and message-toggle-inactive attributes to specify labels displayed under the toggle for each state. By default, the labels are Active and Inactive. To omit labels, set these attributes to empty strings.
A toggle is similar to a checkbox; it presents a binary choice. However, a toggle is self-contained and is designed to be used in a form with only one field. When you switch a toggle on or off, the change for that item should save immediately.
By default, the toggle component expands to 100% of the available width. To limit the size of the toggle component, wrap it with an element that specifies the appropriate width.
lightning-input implements designs in the Salesforce Lightning Design System (SLDS). The input types adapt to SLDS 1 or SLDS 2 styling based on the org’s theme or the container app that you use.
To ensure the toggle is enabled before form submission, use the required attribute. When required is used, the toggle must be enabled to pass validation.
To validate the toggle, use the checkValidity() method. If the value isn’t valid, the reportValidity() method shows the error message below the toggle.
1import{LightningElement}from "lwc";23export default class ToggleValidation extends LightningElement{4 handleSubmit(){5 const toggle = this.refs.termsToggle;6 console.log(toggle.validity.valid); // Returns true or false7 if(!toggle.checkValidity()){8 toggle.reportValidity();9 return;10}11 // Proceed with form submission12}13}
The validity attribute returns an object with read-only boolean properties. For the toggle type, these attributes apply:
badInput - Indicates that the value is invalid for any input type
customError - Indicates that a custom error has been set using setCustomValidity()
valueMissing - Indicates that an empty value is provided when the required attribute is set for any input type
valid - True if none of the preceding properties are true
Component Styling
Use a combination of variants and utility classes to customize your checkbox toggle.
Variants
Use the variant attribute with one of these values to position the labels differently relative to the fields.
standard is the default, which displays the label next to the field.
label-hidden hides the label but make it available to assistive technology. If you provide a value for field-level-help, the tooltip icon is still displayed.
label-inline aligns the label and field horizontally.
The label-stacked variant isn’t supported for the checkbox toggle.
Note
Utility Classes
To apply additional styling, use the SLDS utility classes with the class attribute.
Styling Hooks
Component styling hooks provide CSS custom properties that use the --slds-c-* prefix and they change styling for specific elements or properties of a component. Component styling hooks are supported for SLDS 1 only. See the SLDS 1 component blueprints for available component styling hooks.
When you use the label attribute, the component generates a unique ID for the internal <label> and uses a standard for attribute to link it to the toggle.
If you use the label-hidden variant, the component maintains the for attribute to link between the toggle and the label.
If the toggle fails validation, the component adds aria-invalid="true" and links the error message to the input by using aria-describedby.