Sub topic 13
Accessibility considerations
This chapter assumes you can build a basic Lightning web component and have used lightning-input, lightning-button, and the SLDS utility classes from earlier chapters. It covers what base components do for accessibility on their own and what you still have to provide.
What base components give you
Base components ship with keyboard support, focus handling, and ARIA roles built in. A lightning-button renders a real <button>. A lightning-input links its label to the input element. You get this without extra code, which is the main accessibility argument for choosing base components over custom markup.
The built-in work covers the component's internals. It cannot supply content that only you know, such as a label's text or an icon's meaning. Most accessibility errors in base-component code come from leaving those attributes out.
Labels on form inputs
Every input needs a label. lightning-input requires the label attribute. If the design has no visible label, keep the label and hide it with variant="label-hidden". Screen readers still announce it.
<template>
<lightning-input
type="search"
label="Search contacts"
variant="label-hidden"
value={searchKey}
onchange={handleChange}
></lightning-input>
</template>Leaving out label, or filling it with a placeholder-style value such as "Enter text", leaves screen reader users without a useful name for the field. The placeholder attribute is not a substitute, because it disappears when the user types.
For required fields, set the required attribute. The component then marks the field visually and exposes the state to assistive technology. Use field-level-help for hints that apply to the field.
Alternative text for icons and buttons
Icons carry meaning that screen readers cannot infer. Use alternative-text on lightning-icon and lightning-button-icon.
<template>
<lightning-icon
icon-name="utility:warning"
alternative-text="Warning"
title="Warning"
></lightning-icon>
<lightning-button-icon
icon-name="utility:delete"
alternative-text="Delete contact"
onclick={handleDelete}
></lightning-button-icon>
</template>An icon-only button with no alternative-text has no accessible name. A screen reader announces just "button". Describe the action ("Delete contact"), not the picture ("Trash can").
If an icon is purely decorative and sits next to text that already says the same thing, you can leave out the alternative text so it is not announced twice.
Do not rely on color alone
SLDS color utility classes and styling hooks make it easy to mark an error in red. Color alone is not enough for users with color blindness or low vision. Pair it with text or an icon, and keep contrast between text and background at a ratio of at least 4.5:1 when you override colors with styling hooks.
Headings, structure, and focus
- Use headings in order, without skipping levels.
lightning-cardtitles are not a substitute for page headings. - Keep a logical tab order. Do not set positive
tabindexvalues. Usetabindex="0"or-1only when needed. - Make sure custom clickable elements, such as a
divwith anonclick, work from the keyboard. Alightning-buttonalready does. Prefer it over a clickablediv. - After an action changes the screen, move focus deliberately. Call
focus()on the first invalid field or on a dialog's heading.
import { LightningElement } from 'lwc';
export default class ContactForm extends LightningElement {
handleSubmit() {
const input = this.template.querySelector('lightning-input');
if (!input.checkValidity()) {
input.reportValidity();
input.focus();
}
}
}reportValidity() shows the error message on the field, and focus() moves keyboard and screen reader users to it.
Datatables and live updates
lightning-datatable exposes table semantics, so give each column a meaningful label. For updates the user did not trigger, such as a saved confirmation, show the message in an element with role="status" or role="alert" so assistive technology announces it.
<template>
<p role="status" if:true={message}>{message}</p>
</template>Exercise
A design shows a trash-can icon button with no visible text. Which attribute makes it usable with a screen reader, and what value is better: "Trash can" or "Delete contact"?
Show answer
Set alternative-text on lightning-button-icon. "Delete contact" is better because it names the action the button performs, not the image.