Sub topic 6
Cards, icons and buttons
This chapter assumes you can read a Lightning web component's HTML template and JavaScript class, and that you know how to add a base component to a template. You should also have seen basic SLDS utility classes such as slds-var-p-around_medium.
The card as a container
lightning-card is the standard container for a block of related content. It draws a bordered, padded panel with a header and a body. You control three parts of it:
- The
titleattribute sets the header text. - The
icon-nameattribute adds an icon next to the title. - Named slots add markup in specific places. The
actionsslot puts content on the right side of the header, and thefooterslot puts content at the bottom. Anything without aslotattribute goes in the body.
<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">
<p>Card body content goes here.</p>
</div>
<p slot="footer">Results are limited to 50 contacts.</p>
</lightning-card>
</template>The card adds no padding to its body. If your content touches the card edge, wrap it in a div with a spacing class, as above.
Icons
An icon name has the form category:name. The category is one of the SLDS icon sets: standard, utility, action, custom or doctype. For example, standard:contact and utility:search are valid names. lightning-card accepts these names through icon-name. To place an icon anywhere else, use lightning-icon.
<template>
<lightning-icon
icon-name="utility:warning"
size="small"
variant="warning"
alternative-text="Warning"
title="Warning"
></lightning-icon>
</template>The alternative-text attribute gives screen readers a description of the icon. The variant attribute changes the colour, but it only applies to utility icons. Use an alternative text that describes meaning ("Warning"), not appearance ("Yellow triangle").
Buttons
lightning-button renders a text button, with an optional icon. Its main attributes are:
| Attribute | Purpose |
|---|---|
| `label` | The visible text |
| `variant` | The style: `neutral` (default), `brand`, `destructive`, `success`, `inverse`, `base` |
| `icon-name` | An icon shown beside the label |
| `icon-position` | `left` (default) or `right` |
| `type` | `button` (default), `submit` or `reset` |
| `disabled` | Prevents clicks |
Handle clicks with onclick. The button is a custom element, so you do not style it with plain <button> rules.
<template>
<lightning-button
label="Save"
variant="brand"
icon-name="utility:save"
onclick={handleSave}
></lightning-button>
</template>import { LightningElement } from 'lwc';
export default class SaveButton extends LightningElement {
handleSave() {
console.log('Save clicked');
}
}For a button that shows only an icon, use lightning-button-icon. It has no visible label, so alternative-text is required for accessibility.
<template>
<lightning-button-icon
icon-name="utility:refresh"
alternative-text="Refresh"
onclick={handleRefresh}
></lightning-button-icon>
</template>Common errors
- Icon does not appear. The name is misspelled or missing its category. Write
utility:refresh, notrefresh. - Colour variant has no effect on a standard icon.
variantworks only withutilityicons. - Content touches the card edge. Add an SLDS padding class to a wrapper element.
- Button inside a form does nothing, or submits unexpectedly. The default
typeisbutton, which never submits. Settype="submit"where you want submission.
Exercise
You add <lightning-icon icon-name="standard:account" variant="error"> and the icon stays the same colour. Why, and how would you fix it?
Show answer
The variant attribute only changes the colour of utility icons. Standard icons keep their own background colour. Switch to a utility icon such as utility:error, or accept the standard icon's colour.
Exercise
Which attribute must you set on lightning-button-icon so that screen reader users understand what it does?
Show answer
alternative-text, for example alternative-text="Refresh".