Button Stateful

lightning:buttonStateful

A button that toggles between states.

For Aura components only. For LWC development, use lightning-button-stateful.

For Use In

Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline

A lightning:buttonStateful 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 states.

Use the variant and class attributes to apply additional styling.

The Lightning Design System utility icon category provides nearly 200 utility icons that can be used in lightning:button along with a text label. Although the Lightning Design System provides several categories of icons, only the utility category can be used with this component.

Visit https://lightningdesignsystem.com/icons/#utility to view the utility icons.

This component implements styling from stateful buttons in the Lightning Design System.

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, displaying the “Follow” label by default, and replacing it with “Following” when the button is selected. Selecting the button toggles the state to true, and deselecting it toggles the state to false. When the state is true, the button displays “Unfollow” when you mouse over it or when it receives focus.

1<aura:component>
2  <aura:attribute name="buttonstate" type="Boolean" default="false" />
3  <lightning:buttonStateful
4    labelWhenOff="Follow"
5    labelWhenOn="Following"
6    labelWhenHover="Unfollow"
7    iconNameWhenOff="utility:add"
8    iconNameWhenOn="utility:check"
9    iconNameWhenHover="utility:close"
10    state="{! v.buttonstate }"
11    onclick="{! c.handleClick }"
12  />
13</aura:component>

The client-side controller toggles the state via the buttonstate attribute.

1({
2  handleClick: function (cmp, event, helper) {
3    var buttonstate = cmp.get("v.buttonstate");
4    cmp.set("v.buttonstate", !buttonstate);
5  },
6});

Accessibility 

This component uses aria-live="polite", which means the button label is read after the current user task or content.

To inform screen readers that a button is disabled, set the disabled attribute to true.

Attributes 

NameDescriptionTypeDefaultRequired
accesskeySpecifies a shortcut key to activate or focus an element.String
bodyThe body of the component. In markup, this is everything in the body of the tag.Aura.Component[]
iconNameWhenHoverThe name of the icon to be used in the format \'utility:close\' when the state is true and the button receives focus.String
iconNameWhenOffThe name of the icon to be used in the format \'utility:add\' when the state is false.String
iconNameWhenOnThe name of the icon to be used in the format \'utility:check\' when the state is true.String
labelWhenHoverThe text to be displayed inside the button when state is true and the button receives focus.String
labelWhenOffThe text to be displayed inside the button when state is false.String
labelWhenOnThe text to be displayed inside the button when state is true.String
onblurThe action triggered when the element releases focus.Aura.Action
onclickThe action triggered when the button is clicked.Aura.Action
onfocusThe action triggered when the element receives focus.Aura.Action
stateThe state of the button, which shows whether the button has been selected or not. The default state is false.Booleanfalse
tabindexSpecifies the tab order of an element when the Tab key is used for navigating. The tabindex value can be set to 0 or -1. The default is 0, which means that the component is focusable and participates in sequential keyboard navigation. -1 means that the component is focusable but does not participate in keyboard navigation.Integer
variantThe variant changes the appearance of the button. Accepted variants include brand, destructive, inverse, neutral, success, and text. This value defaults to neutral.Stringneutral

Methods 

NameDescriptionArgument NameArgument TypeArgument Description
focusSets focus on the element.