Button Icon Stateful

lightning-button-icon-stateful

An icon-only button that retains state.

For Use In

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

A lightning-button-icon-stateful 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 handler set for onclick. You can change the state of the icon by using the selected 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.

Selecting the Dislike button also toggles the state on the Like button and deselects it.

1<template>
2    <lightning-button-icon-stateful
3        icon-name="utility:like"
4        selected={liked}
5        alternative-text="Like"
6        onclick={handleToggle}
7    >
8    </lightning-button-icon-stateful>
9</template>

Handle the click event in your JavaScript code.

1import { LightningElement } from "lwc";
2
3export default class MyComponentName extends LightningElement {
4  liked = true;
5
6  handleToggle() {
7    this.liked = !this.liked;
8  }
9}

Design 

lightning-button-icon-stateful implements the button icons blueprint in the Salesforce Lightning Design System (SLDS). The button icons adapt to SLDS 1 or SLDS 2 styling based on the org’s theme or the container app that you use.

SLDS 1SLDS 2
DesignButton Icons (Stateful)Button Icons
For Use InLightning Experience, Experience Builder sites, Salesforce mobile app, Lightning Out (Beta), Standalone Lightning app, Mobile OfflineLightning Experience

Component Styling 

Use a combination of icon-name, variant, size, and class attributes to customize the button and icon styles.

Icons 

Use the icon-name attribute to add a utility icon to the button.

The SLDS utility icon category offers nearly 200 utility icons that can be used in lightning-button-icon-stateful. Although the 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.

Note

Variants 

Use the variant attribute with one of these values to apply styling.

  • border is the default variant, an icon in a transparent container with a border
  • border-filled shows an icon in a filled container with a border
  • border-inverse shows an icon in a transparent container with a border, useful for dark backgrounds

Sizes 

Adjust the button and icon sizes by using the size attribute with one of these values.

  • medium is the default size, which creates a 32px by 32px button enclosing a 14px by 14px icon.
  • small creates a 24px by 24px button enclosing a 14px by 14px icon
  • x-small creates a 20px by 20px button enclosing a 12px by 12px icon
  • xx-small creates a 16px by 16px button enclosing a 8px by 8px icon

Utility Classes 

To apply additional styling, use the SLDS utility classes with the class attribute.

This example adds a margin to the left of the second button by using an SLDS class.

1<lightning-button-icon-stateful
2    icon-name="utility:like"
3    selected={likeState}
4    onclick={handleLikeButtonClick}
5    alternative-text="Like"
6></lightning-button-icon-stateful>
7<lightning-button-icon-stateful
8    icon-name="utility:answer"
9    selected={answerState}
10    onclick={handleAnswerButtonClick}
11    alternative-text="Answer"
12    class="slds-m-left_xx-small"
13></lightning-button-icon-stateful>

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.

For more information, see Style Components Using Lightning Design System Styling Hooks in the Lightning Web Components Developer Guide.

CSS custom properties for stateful button icons work only with specific lightning-button-icon-stateful variants.

CSS Custom Propertylightning-button-icon-stateful Variants
--slds-c-button-color-backgroundborder (default) and border-inverse
--slds-c-button-color-borderN/A
--slds-c-button-text-colorN/A
--slds-c-button-text-color-*border (default) and border-filled
--slds-c-button-radius-borderall
--slds-c-button-sizing-borderN/A

For more information, see Style Components Using Lightning Design System Styling Hooks in the Lightning Web Components Developer Guide.

Usage Considerations 

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

Accessibility 

lightning-button-icon-stateful contains an informational icon, which conveys information that surrounding text doesn’t. Each icon should be accompanied by either assistive text. When selected, the button renders with aria-pressed="true". When not selected, the button renders with aria-pressed="false". The aria-pressed attribute enables screen readers to announce whether a button is pressed or not.

Use the alternative-text attribute to describe the icon. The description should indicate what happens when you click the button, for example ‘Upload File’, not what the icon looks like, ‘Paperclip’.

The text is available to users in two ways.

  • On the tooltip when you hover over the button
  • As text for assistive technologies

Buttons that only show an icon to represent do not have an accessible name. For lightning-button-icon-stateful, provide the accessible name using the aria-label attribute.

Use the following accessibility and aria attributes on lightning-button-icon-stateful.

AttributeTypeDescription
accesskeystringA shortcut key to activate or place focus on the button
aria-atomicbooleanSpecifies whether the screen reader should always present the live region as a whole, even if only part of the region changes. The default is false.
aria-controlsID reference listAn element ID or a space-separated list of element IDs whose presence or content is controlled by this button.
aria-describedbyID reference listAn element ID or a space-separated list of element IDs that provide a descriptive label or description for the button.
aria-expandedbooleanIndicates whether the state of an element, controlled by the button, is expanded or collapsed. To reference the controlled element, use aria-controls.
aria-labelstringProvides an assistive label where a visible label cannot be used.
aria-livestringIndicates whether the button will be updated, and describes the types of updates assistive technologies will make. Possible values include off, polite, and assertive. The default is off. For the screen reader to announce changes when the user is idle, use polite. For immediate notifications, use assertive.

For more information, see the WAI-ARIA Specification.

Attributes 

NameDescriptionTypeDefaultRequired
alternative-textThe 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
icon-nameThe 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. 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
selectedSpecifies whether the button is in a selected state. This value defaults to false.boolean
sizeThe size of the button-icon component. Options include xx-small, x-small, small, and medium. This value defaults to medium.stringmedium
tooltipText to display when the user mouses over or focuses on the button. The tooltip is auto-positioned relative to the button and screen space.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 button-icon. Accepted variants include border, border-filled, and border-inverse. This value defaults to border.stringborder

Methods 

NameDescriptionArgument NameArgument TypeArgument Description
focusSets focus on the button.