Sub topic 6
Cards, icons and buttons
This chapter covers three base components that appear in almost every Lightning web component: lightning-card, lightning-icon and lightning-button. It assumes you can read an LWC template and JavaScript class, know what a base component is, and have seen SLDS utility classes such as slds-var-p-around_medium.
What you will build
By the end you will be able to wrap content in a card, give it a header icon and action buttons, add standalone icons with accessible text, and choose button variants and handlers that behave correctly. The running example is a contact search panel.
lightning-card
lightning-card is a container that draws a bordered panel with an optional header and footer. It is the standard way to group related content in a Lightning page.
Attributes and slots
The card has a small API. Attributes set simple header content. Slots let you place your own markup.
| Name | Kind | Purpose |
|---|---|---|
| `title` | attribute | Plain text in the header. |
| `icon-name` | attribute | An SLDS icon shown before the title, such as `standard:contact`. |
| `actions` | slot | Content on the right side of the header, usually buttons. |
| `title` | slot | Rich header content when plain text is not enough. |
| `footer` | slot | Content below the body. |
| default slot | slot | The body of the card. |
Anything you place inside the card without a slot attribute goes into the body. Body content has no padding by default, so you add spacing yourself. The slds-var-p-around_medium class on a wrapping div does that.
Here is the card from this chapter's running example:
<template>
<lightning-card title="Contact search" icon-name="standard:contact">
<lightning-button
slot="actions"
label="Clear"
onclick={handleClear}
></lightning-button>
<div class="slds-var-p-around_medium">
<lightning-input
type="search"
label="Name"
value={searchKey}
onchange={handleChange}
></lightning-input>
</div>
</lightning-card>
</template>The button has slot="actions", so it appears in the header, aligned right. The div has no slot, so it fills the body. The input is inside the padded div, which keeps it off the card edge.
Common card mistakes
- Putting padding classes on
lightning-carditself. The class lands on the host element, not on the body, so the content still touches the border. Wrap the content instead. - Using both the
titleattribute and thetitleslot. Use one. The slot is for cases where you need markup, such as a link or a badge, in the header. - Setting
slot="actions"on a wrapper that is not a direct child of the card. Slot assignment only works for direct children of the component in your template. - Adding a footer slot and expecting it to hide when empty. The card cannot see whether your slotted markup renders anything, so a conditional footer should use
lwc:ifon the slotted element.
lightning-icon
lightning-icon renders an icon from the SLDS icon library. The icon is chosen by icon-name, which has the form category:name. The categories are standard, custom, utility, action and doctype.
<template>
<lightning-icon
icon-name="utility:warning"
alternative-text="Warning"
size="small"
variant="warning"
></lightning-icon>
</template>Attributes that matter
icon-nameselects the icon. If the name does not exist, nothing is drawn. Check the name against the SLDS icon reference when an icon is missing.sizeacceptsxx-small,x-small,small,mediumandlarge. The default ismedium.variantapplies only toutilityicons. Values includeinverse,warning,errorandsuccess. Thestandardandcustomicons already carry their own colored background, so the attribute has no visible effect on them.alternative-textis the text a screen reader announces. It is covered below.
Alternative text
An icon can mean something or it can be decoration. If it carries meaning that the nearby text does not, supply alternative-text. A warning triangle on its own needs "Warning". If an adjacent label already says the same thing, repeating it makes a screen reader read it twice. In that case leave alternative-text out so the icon is treated as decorative.
Icons also appear inside other components. lightning-card draws its icon-name for you. lightning-button accepts icon-name too, which the next section covers.
lightning-button
lightning-button renders a standard button. It exposes a label, a variant and optional icons. It fires a click event that you handle with onclick.
Variants
The variant attribute controls appearance. It does not change behavior.
| Variant | Typical use |
|---|---|
| `neutral` | Default. Secondary actions such as Cancel or Clear. |
| `brand` | The single primary action on a view. |
| `brand-outline` | A primary action with less visual weight. |
| `destructive` | Delete or other actions that lose data. |
| `destructive-text` | A destructive action shown as text. |
| `inverse` | Buttons on dark backgrounds. |
| `success` | Confirming a positive outcome. |
| `base` | Looks like a link. |
Use one brand button per area. If several buttons are brand colored, none of them reads as the main action.
Icons on buttons
Add icon-name and, optionally, icon-position set to left or right. A button with a label and an icon needs no extra alternative text, because the label already names it. If you want an icon-only button, use lightning-button-icon, which has alternative-text and variant attributes. An icon-only button without alternative text has no accessible name.
<template>
<lightning-button
label="Download"
icon-name="utility:download"
icon-position="left"
variant="brand"
onclick={handleDownload}
></lightning-button>
<lightning-button-icon
icon-name="utility:refresh"
alternative-text="Refresh results"
title="Refresh results"
onclick={handleRefresh}
></lightning-button-icon>
</template>Handling clicks
The handler is a method on the component class. The click event bubbles, so a handler on an ancestor also receives it. When you need to know which button was clicked, put a data- attribute on it and read event.target.dataset in the handler.
<template>
<lightning-button label="Approve" data-action="approve" onclick={handleAction}></lightning-button>
<lightning-button label="Reject" data-action="reject" onclick={handleAction}></lightning-button>
</template>import { LightningElement } from 'lwc';
export default class ActionButtons extends LightningElement {
handleAction(event) {
const action = event.target.dataset.action;
console.log(`Action chosen: ${action}`);
}
}event.target is the lightning-button element in your template, so its data- attributes are available. The button's inner <button> is in a shadow tree you cannot reach, and the event is retargeted to the host.
Disabled buttons and type
- Set
disabledto a boolean property to block clicks. A disabled button does not fireclick. - The
typeattribute acceptsbutton,submitandreset. The default isbutton. A button of typesubmitinside alightning-record-edit-formsubmits it. A default button does not, so the form never saves. This is a common cause of a Save button that does nothing.
Putting it together
The JavaScript for the contact search card keeps the search text in a field and lets the Clear button reset it:
import { LightningElement } from 'lwc';
export default class ContactSearch extends LightningElement {
searchKey = '';
handleChange(event) {
this.searchKey = event.target.value;
}
handleClear() {
this.searchKey = '';
}
}value={searchKey} is one-way. When handleClear sets the field to an empty string, the template rerenders and the input clears. The field is reactive because class fields in LWC are tracked when you reassign them.
The structure of this component looks like this:
Edge cases
- Icon names are case sensitive and use underscores in some categories, for example
standard:work_order. A typo produces an empty space and no error. - A card inside another card works, but the nested border adds clutter. Prefer headings and spacing inside the outer card.
titleon a button sets a tooltip. It does not replacelabeloralternative-text, because touch and keyboard users often never see tooltips.- Buttons in the
actionsslot do not wrap on narrow screens. Keep to one or two actions in the header. Put the rest in a footer or a menu.
Exercises
Exercise
A developer adds class="slds-var-p-around_medium" to a lightning-card and the input inside still touches the card border. Why, and how do they fix it?
Show answer
The class applies to the card's host element, not to the body slot, so the content gets no inner padding. Wrap the body content in a div with slds-var-p-around_medium, as in the contact search example.
Exercise
A form uses lightning-record-edit-form, and its Save lightning-button has label="Save" and variant="brand". Clicking it does nothing. What attribute is missing, and why?
Show answer
type="submit". The default type is button, which does not submit the form.
Exercise
You need a refresh control that shows only an icon. Which component do you use, and what must you provide so a screen reader can name it?
Show answer
Use lightning-button-icon with icon-name="utility:refresh" and set alternative-text (for example "Refresh results") so the control has an accessible name.