Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline
A lightning-button-stateful component represents a button that toggles
between states, similar to a Like button on social media. Stateful buttons can
show a different label and icon based on their selected states.
To handle the state change when the button is clicked, use the onclick event
handler. This example enables you to toggle the button between states,
showing the “Follow” label by default, and replacing it with “Following”
when the button is selected. Selecting the button toggles the selected state to true,
and deselecting it toggles selected state to false. When the selected state is true, the
button shows “Unfollow” when you mouse over it or when it receives focus.
lightning-button-stateful implements the buttons blueprint in the Salesforce Lightning Design System (SLDS). The buttons adapt to SLDS 1 or SLDS 2 styling based on the org’s theme or the container app that you use.
Use a combination of icon names, variants, and utility classes to style your stateful buttons.
Icons
Use the icon-name attribute to add a utility icon to the button.
The SLDS utility icon category provides nearly 200 utility
icons that can be used in lightning-button-stateful along with a text label. Although SLDS provides several categories of icons, only the utility category can be used with this component.
When applying SLDS classes or icons, check that they are
available in the SLDS release tied to your org. The latest
SLDS resources become available only when the new release
is available in your org.
Variants
Use the variant attribute with one of these values to apply styling.
neutral is the default variant, a plain uncolored button.
brand is a blue button, used to draw attention to the primary action on a page.
destructive is a red button used to warn users that its action has a negative effect.
inverse uses the background color and light text, useful for dark backgrounds.
success is a green button used to indicate a successful action.
text is a button without a border, which gives it the look of a plain text link.
Utility Classes
To apply additional styling, use the SLDS utility classes with the class attribute.
This example adds padding on the button using an SLDS class.
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.
lightning-button-stateful contains the same customizable elements as lightning-button, which supports --slds-c-button-* custom properties. CSS custom properties for stateful buttons work only with particular lightning-button-stateful variants.