Button Icon Stateful

lightning:buttonIconStateful

An icon-only button that retains state. This component requires API version 41.0 and later.

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

For Use In

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

A lightning:buttonIconStateful component represents an icon-only button element that toggles between two states. For example, you can use this component for capturing a customer’s feedback on a blog post (like or dislike). Clicking the button triggers the client-side controller method set for onclick and changes the state of the icon using the selected attribute.

The Lightning Design System utility icon category offers nearly 200 utility icons that can be used in lightning:buttonIconStateful. 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.

When applying Lightning Design System classes or icons, check that they are available in the Lightning Design System release tied to your org. The latest Lightning Design System resources become available only when the new release is available in your org.

This component implements styling from button icons in the Lightning Design System.

You can use a combination of the variant, size, and class attributes to customize the button and icon styles. To customize styling on the button container, use the class attribute.

This example creates a like button that toggles between two states. The like button is selected by default. The button’s state is stored in the selected attribute.

1<aura:component>
2  <aura:attribute name="liked" type="Boolean" default="true" />
3  <lightning:buttonIconStateful
4    iconName="utility:like"
5    selected="{!v.liked}"
6    alternativeText="Like"
7    onclick="{! c.handleToggle }" />
8  <aura:component></aura:component
9></aura:component>

Selecting the dislike button also toggles the state on the like button and deselects it.

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

Usage Considerations 

Icons are not available in Lightning Out, but they are available in Lightning Components for Visualforce and other experiences.

Attributes 

NameDescriptionTypeDefaultRequired
accesskeySpecifies a shortcut key to activate or focus an element.String
alternativeTextThe alternative text used to describe the icon. This text should describe what happens when you click the button, for example 'Upload File', not what the icon looks like, 'Paperclip'.String
bodyThe body of the component. In markup, this is everything in the body of the tag.Aura.Component[]
classA CSS class for the outer element, in addition to the component's base classes.String
disabledSpecifies whether this button should be displayed in a disabled state. Disabled buttons can't be clicked. This value defaults to false.Booleanfalse
iconNameThe Lightning Design System name of the icon. Names are written in the format 'utility:down' where 'utility' is the category, and 'down' is the specific icon to be displayed. Note: Only utility icons can be used in this component.String
nameThe name for the button element. This value is optional and can be used to identify the button in a callback.String
onblurThe action triggered when the element releases focus.Aura.Action
onclickThe action that will be run when the button is clicked.Aura.Action
onfocusThe action triggered when the element receives focus.Aura.Action
selectedSpecifies whether button is in selected state or notBooleanfalse
sizeThe size of the buttonIcon. Options include xx-small, x-small, small, and medium. This value defaults to medium.Stringmedium
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
titleDisplays tooltip text when the mouse moves over the element.String
valueThe value for the button element. This value is optional and can be used when submitting a form.String
variantThe variant changes the appearance of buttonIcon. Accepted variants include border, border-filled, and border-inverse. This value defaults to border.Stringborder

Methods 

NameDescriptionArgument NameArgument TypeArgument Description
focusSets focus on the element.