A pill displays a label that can contain links and can be removed from view.
For Aura components only. For LWC development, use lightning-pill.
For Use In
Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline
A lightning:pill component represents an item, such as an account name or case number. By default, pills are rendered with a remove button. They are useful for displaying read-only text that can be added and removed on demand, for example, a list of email addresses or a list of keywords.
This component implements styling from pills in the Lightning Design System.
Use the class attribute to apply additional styling.
URLs without a protocol use the host domain’s protocol. For example, www.example.com is prefixed with https:// if the host domain’s protocol is https://.
Inserting an Icon or Avatar
A pill can contain an icon or avatar to represent the type of object. To insert an icon or avatar in the pill, use the media attribute to include your lightning:icon or lightning:avatar component.
Insert an icon on the pill using lightning:icon. For more information, see lightning:icon.
All three elements trigger the onclick handler. If you provide an href value, clicking the text label triggers the onclick handler and then takes you to the provided path.
Clicking the remove button (X) on the pill fires the remove event and then the click event. Use the optional onremove and onclick handlers to remove the pill and perform an action. You must hide or remove the pill on your own using the onremove handler, as shown in the example below. If you don’t specify a pill removal behavior, clicking the remove button doesn’t have a visible effect.
When an href value is present, clicking the text label navigates to a link, but clicking the icon or avatar doesn’t. To prevent your browser from navigating to a link when you click a pill’s label, call event.preventDefault() in the onclick handler.
This example hides a pill when the remove button is clicked.
To prevent navigating to the link when the remove button is clicked, call event.preventDefault() in the onremove handler. The click event is still fired when you click the text label. However, calling event.preventDefault() in the onremove handler prevents the click event from firing when you click the remove button.
1({2 handleRemoveOnly: function(cmp, event){3 event.preventDefault();4 alert("Remove button was clicked!");5},6 handleClick: function(cmp, event){7 // this won't run when you click the remove button8 alert("The pill was clicked!");9},10});
Displaying an Error
Display a pill in an error state when the containing text doesn’t match a pre-defined collection of items, such as when an email address is invalid or a case number doesn’t exist.
Use the hasError attribute to denote an error on a pill. Setting hasError to true inserts an error icon next to the label and changes the label text to red. Providing an icon or avatar in this context has no effect on the pill; the error icon is still displayed.
Icons are not available in Lightning Out, but they are available in Lightning Components for Visualforce and other experiences.
To create more than one pill, use the lightning:pillContainer component, which gives you access to the pill array via the itemremove event.
Specifying a target to change where the link should open is not supported. The link opens on the same tab or window. To create a URL that navigates to another page in Salesforce, use lightning:navigation.
Accessibility
When using lightning:avatar, use the alternativeText attribute to describe the avatar, such as a user’s initials or name. This description provides the value for the alt attribute in the img HTML tag.
When using lightning:icon, use the alternativeText attribute to describe the icon. For example, specify “Account” instead of “Pill icon”. This description is used as assistive text on the pill.
Press the Tab key to move focus to a pill with a link, and tab again to move focus to the remove button. To remove a pill, press the Enter key when you tab to the remove button. You can define your own behavior to remove the pill from view, such as using the onremove handler shown in Interacting with Pills. To navigate to the target of a link on a pill, press the Enter key when the focus is on the pill.
Attributes
Name
Description
Type
Default
Required
body
The body of the component. In markup, this is everything in the body of the tag.
Aura.Component[]
class
A CSS class for the outer element, in addition to the component's base classes.
String
hasError
Specifies whether the pill has errors. The default is false.
Boolean
false
href
The URL of the page that the link goes to.
String
label
The text label that displays in the pill.
String
media
The icon or figure that's displayed next to the textual information.
Aura.Component[]
name
The name for the pill. This value is optional and can be used to identify the pill in a callback.
String
onclick
The action triggered when the button is clicked.
Aura.Action
onremove
The action triggered when the pill is removed.
Aura.Action
title
Displays tooltip text when the mouse moves over the element.