Lightning Stateful Button Component

The lightning:buttonStateful can be used to represent a button state that toggles between states just like you experience on social media. The stateful button can show different labels and icons dynamically.

Below are some examples:

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="buttonstate" type="Boolean" default="false"/>
<lightning:buttonStateful
labelWhenOff="Follow"
labelWhenOn="Following"
labelWhenHover="Unfollow"
iconNameWhenOff="utility:add"
iconNameWhenOn="utility:check"
iconNameWhenHover="utility:close"
state="{! v.buttonstate }"
onclick="{! c.handleClick }"
/>
</aura:component>

({
handleClick : function (cmp, event, helper) {
var buttonstate = cmp.get('v.buttonstate');
cmp.set('v.buttonstate', !buttonstate);
}
})

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="buttonstate" type="Boolean" default="false"/>
<lightning:buttonStateful
state="{! v.buttonstate }"
variant="text"
labelWhenOff="Like"
labelWhenOn="Liked"
iconNameWhenOff="utility:like"
iconNameWhenOn="utility:like"
onclick="{! c.handleClick }"
/>
</aura:component>
view raw LikeButtonCmp hosted with ❤ by GitHub

({
handleClick : function (cmp, event, helper) {
var buttonstate = cmp.get('v.buttonstate');
cmp.set('v.buttonstate', !buttonstate);
}
})

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="buttonstate" type="Boolean" default="false"/>
<aura:attribute name="button1state" type="Boolean" default="false"/>
<lightning:buttonStateful
labelWhenOff="Mute"
labelWhenOn="Muted"
labelWhenHover="UnMute"
iconNameWhenOff="utility:unmuted"
iconNameWhenOn="utility:muted"
iconNameWhenHover="utility:unmuted"
state="{! v.buttonstate }"
onclick="{! c.handleClick }"
/>
</aura:component>
view raw MuteButtonCmp hosted with ❤ by GitHub

({
handleClick : function (cmp, event, helper) {
var buttonstate = cmp.get('v.buttonstate');
cmp.set('v.buttonstate', !buttonstate);
}
})

References:

https://www.lightningdesignsystem.com/components/buttons/#flavor-stateful

https://lightningdesignsystem.com/icons/#utility

https://developer.salesforce.com/docs/component-library/bundle/lightning:buttonStateful/example#lightningcomponentdemo:exampleButtonStateful

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s