Sub topic 2
What Lightning base components are
This chapter assumes you can read HTML, CSS and modern JavaScript (classes and modules). It also assumes you know the three parts 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 Lightning web component that Salesforce ships in the lightning namespace. You use it in a template as a custom element whose name starts with lightning-, such as lightning-button, lightning-input or lightning-card.
Base components give you three things without extra work:
- Styling that follows the Salesforce Lightning Design System (SLDS), so your component looks like the rest of the platform.
- Built-in behavior, such as input validation, keyboard handling and date pickers.
- Accessibility features, such as correct roles, labels and focus handling.
You do not import base components in JavaScript. The platform resolves any lightning-* tag in your template. You pass data in through attributes and listen for results through events.
<template>
<lightning-button
label="Save"
variant="brand"
onclick={handleSave}
></lightning-button>
</template>Here label and variant are public properties of the component. onclick attaches a handler named handleSave from your JavaScript class.
How a base component is used
Every use follows the same pattern, which the diagram shows. Your template sets attributes. The base component renders markup and applies SLDS styles. When the user acts, the component dispatches an event and your handler runs.
This one-way pattern is the core idea. Data goes down as attributes. Events go up. You do not reach into a base component and change its internals.
Attributes, slots and events
Base components expose their API in three ways.
| Part of the API | What it does | Example |
|---|---|---|
| Attribute | Sets a public property | `label="Name"` |
| Slot | Accepts child markup in a named place | `slot="actions"` |
| Event | Reports a user action | `onchange={handleChange}` |
Attributes
HTML attribute names are written in kebab-case, while the matching JavaScript property is camelCase. For example, the attribute icon-name matches the property iconName. In templates you always use the kebab-case form.
A value can be a literal string, such as label="Name". It can also be a reference to a field in your class, written in braces, such as value={searchKey}. Braces take a property or getter name. You cannot write expressions inside them.
Slots
A slot is a named place in a component where you supply your own markup. lightning-card has a default slot for the body, plus named slots such as actions and footer. You send content to a named slot with the slot attribute.
<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 goes into the header's actions area because of slot="actions". The div has no slot attribute, so it goes into the default body slot. The slds-var-p-around_medium class is an SLDS utility class that adds padding on all sides. Chapter 4 covers utility classes in detail.
Events
Base components dispatch standard DOM events or custom events. Their names are lowercase, with no hyphens or camelCase. You attach a handler in the template with the on prefix. The event object carries the data. For lightning-input, event.target.value holds the current text.
import { LightningElement } from 'lwc';
export default class ContactSearch extends LightningElement {
searchKey = '';
handleChange(event) {
this.searchKey = event.target.value;
}
handleClear() {
this.searchKey = '';
}
}The value={searchKey} binding in the template and the handleChange handler form a loop. The user types, handleChange stores the text in searchKey, and the template passes it back as value. Clicking Clear sets searchKey to an empty string, and the input shows an empty box again.
Finding the documentation
The Component Library is the reference for base components. It lives on developer.salesforce.com. Each component page has these parts:
- A description with examples.
- The list of attributes, with their types and defaults.
- The list of events and what each one carries.
- The slots, if any.
- Accessibility notes and usage considerations.
Check three details on every page before you rely on a component:
- Which attributes are required. For example,
lightning-datatableneedskey-field, andlightning-inputshould always have alabel. - Where it can be used. Some components work only in Lightning Experience or in an Experience Cloud site, or only with certain API versions. The page notes this.
- Whether it relies on Lightning Data Service. Components such as
lightning-record-edit-formread and write records for you and respect the user's field-level security.
Salesforce releases change the component set three times a year. If a component behaves differently from the docs, check the API version in your component's .js-meta.xml file.
Common base components by purpose
You do not need to learn all of them. The table groups the ones this tutorial uses.
| Purpose | Components |
|---|---|
| Containers | `lightning-card`, `lightning-tabset`, `lightning-accordion` |
| Actions | `lightning-button`, `lightning-button-icon`, `lightning-button-menu` |
| Input | `lightning-input`, `lightning-textarea`, `lightning-combobox`, `lightning-checkbox-group` |
| Record forms | `lightning-record-form`, `lightning-record-edit-form`, `lightning-record-view-form` |
| Data display | `lightning-datatable`, `lightning-formatted-text`, `lightning-formatted-number` |
| Visuals | `lightning-icon`, `lightning-avatar`, `lightning-spinner` |
Later chapters cover cards and icons, inputs, record forms and the datatable.
Why use base components
Base components save work and reduce risk. Compare building a date input yourself with using lightning-input type="date".
- The base component respects the user's locale for date format and first day of the week.
- It handles keyboard use, screen readers and focus.
- It validates values, and you can call
reportValidity()on it. - It updates with each Salesforce release, so your component follows design changes without new code.
The cost is control. You can change what a component exposes, but not its internal markup.
Limits you should know
Base components are built with shadow DOM, which hides their internal markup from your CSS. The next chapters explain how this works in detail. For now, keep these points in mind:
- A selector in your CSS file, such as
lightning-input input, does not reach the nativeinputinside the component. The rule is ignored. - To change appearance, use the attributes the component offers (like
variant), SLDS utility classes on the element or its wrapper, or SLDS styling hooks. Styling hooks are CSS custom properties that the component reads. - Hooks are the supported way to change appearance. This example changes the brand button color.
lightning-button {
--slds-c-button-brand-color-background: #0b5cab;
}The rule sets a custom property on the lightning-button element. The button reads that property for its background, and its internal styles stay untouched.
Common mistakes
- Missing label.
lightning-inputwithout alabelhas no accessible name. Add a label. If you want it hidden, setvariant="label-hidden", which hides it visually but keeps it for screen readers. - Reading
event.target.valuefrom the wrong element. In a handler on the base component,event.targetis the base component itself. This works foronchangeonlightning-input. Inside a wrapper element with many inputs, checkevent.target.nameor use adata-attribute to tell inputs apart. - Expecting two-way binding. Setting
value={searchKey}does not updatesearchKeywhen the user types. You must write the handler yourself. - Using expressions in templates.
value={a + b}is invalid. Create a getter in the class and bind to it. - Overriding internals with CSS. As described above, use hooks, not selectors that reach inside.
- Forgetting required attributes. A datatable without
key-fieldlogs an error and may render incorrectly.
Summary of the working model
A base component is a black box with a public API. You pass attributes and slot content in, and you receive events out. SLDS styling comes built in. When you need a different look, you adjust it through the documented hooks and classes rather than the internals.
Exercise
In the contact search template, the Clear button uses slot="actions" and the wrapper div has no slot attribute. Where does each one appear in the rendered lightning-card, and what would happen if you removed slot="actions" from the button?
Show answer
The button appears in the card's header actions area, next to the title. The div appears in the card body, because content with no slot attribute goes into the default slot. If you removed slot="actions" from the button, it would render in the body with the div instead of in the header.
Exercise
A teammate writes lightning-input input { border-color: red; } in the component's CSS file and sees no change. Why does this happen, and what is the supported alternative?
Show answer
The native input is inside the shadow DOM of lightning-input, so selectors in your stylesheet cannot reach it. The supported alternative is to use an attribute such as variant, an SLDS utility class, or an SLDS styling hook, which is a CSS custom property that the component reads.