Sub topic 2
What Lightning base components are
This chapter assumes you know HTML, CSS and modern JavaScript (classes and modules). It also assumes you know the three files of a Lightning web component: an HTML template, a JavaScript class and an optional CSS file. You need a Salesforce org or scratch org to try the examples.
What a base component is
A Lightning base component is a prebuilt web component that Salesforce ships in the lightning namespace. You use it in a template as a lightning-* element, such as lightning-card, lightning-button or lightning-input.
Base components give you three things without extra work:
- Markup and styling that follow the Salesforce Lightning Design System (SLDS).
- Built-in behavior, such as input validation, keyboard handling and focus management.
- Accessibility features, such as correct roles and label associations.
You configure a base component through its attributes, listen to it through events, and place content in it through slots. You do not edit its internal markup.
How a base component is used
In a template, the namespace and the component name are joined by a hyphen. A component named lightning/button in JavaScript becomes lightning-button in HTML.
<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>This template uses the three ways to talk to a base component:
- Attributes set its state.
title,icon-name,label,typeandvalueare all attributes. Multi-word JavaScript properties become kebab-case in HTML, soiconNameis writtenicon-name. - Slots place your content inside it. The
slot="actions"attribute puts the button in the card's actions area. Content with noslotattribute goes in the default body. - Events report what happened.
onclickandonchangebind to methods in your class.
The matching JavaScript class keeps the search text in a field:
import { LightningElement } from 'lwc';
export default class ContactSearch extends LightningElement {
searchKey = '';
handleChange(event) {
this.searchKey = event.target.value;
}
handleClear() {
this.searchKey = '';
}
}Because value={searchKey} is bound in the template, setting searchKey to an empty string in handleClear also clears the visible input.
Finding attributes and events
The Component Library at developer.salesforce.com/docs/component-library lists every base component. Each page shows these sections:
- Attributes, with type, default value and whether it is required.
- Events, with the event name and the detail it carries.
- Slots, with the names you can use.
- Examples and usage notes, including which Salesforce contexts support the component.
Check this page before you write custom markup. Many needs, such as a date picker or a table, already have a component.
Common errors
- Writing
iconNamein HTML. Useicon-name. Template attributes are kebab-case. - Forgetting braces.
value={searchKey}reads a property.value="searchKey"passes the literal text. - Reading the wrong value. In a
changehandler onlightning-input, readevent.target.value. For a checkbox, readevent.target.checked. - Styling internals. The base component's inner elements are hidden by the shadow DOM, so a selector from your CSS cannot reach them. Later chapters cover styling hooks for this.
- Using a slot name that does not exist. An unknown slot name renders nothing. Check the Slots section of the component's page.
Exercise
A template contains <lightning-input label="Name" value="searchKey">, but the input shows the text "searchKey" instead of the field's content. What is wrong, and how do you fix it?
Show answer
The attribute value is a quoted string, so the component receives the literal text "searchKey". Use braces to bind the class property: value={searchKey}.
Exercise
You want to put a Clear button in the header area of a lightning-card. Which attribute do you put on the button, and where do you find the valid name?
Show answer
Put slot="actions" on the button. The valid slot names are listed in the Slots section of the lightning-card page in the Component Library.