Sub topic 16
Using Lightning base components
This chapter assumes you know HTML, JavaScript classes, and the earlier chapters on component bundles, @api properties, and CustomEvent. Base components are the ready-made components that ship with Salesforce. You use them the same way you use your own components.
What base components are
Base components live in the lightning namespace. In JavaScript the namespace is lightning. In a template, the tag name replaces the dot with a hyphen and the camelCase name with kebab-case. The component lightning/button is written as <lightning-button>.
Each base component is a custom element built with LWC. It has public @api properties you set and events you listen for. It also uses shadow DOM, so your CSS does not reach inside it. Using one is the same as using the c-counter-button component from the earlier chapter on composition. Only the namespace differs: your components use c, and base components use lightning.
Base components bring Salesforce styling, accessibility, and platform behavior with them. Examples are lightning-button, lightning-input, lightning-card, and lightning-datatable. They work only on the Salesforce platform, because the lightning namespace is not part of the open-source LWC project.
A worked example
This template replaces the plain <input> from the greeting component with base components.
<!-- baseGreeting.html -->
<template>
<lightning-card title="Greeting" icon-name="standard:contact">
<div class="slds-var-p-around_medium">
<lightning-input
label="Your name"
value={name}
onchange={handleChange}
></lightning-input>
<p>Hello, {name}!</p>
<lightning-button
label="Reset"
variant="neutral"
onclick={handleReset}
></lightning-button>
</div>
</lightning-card>
</template>// baseGreeting.js
import { LightningElement } from 'lwc';
export default class BaseGreeting extends LightningElement {
name = 'World';
handleChange(event) {
this.name = event.detail.value;
}
handleReset() {
this.name = 'World';
}
}Four points connect to earlier chapters:
label,value,title, andvariantare public@apiproperties. Attributes in the template set them.lightning-cardrenders yourdivthrough a slot. The card supplies the frame, and you supply the body.onchangelistens for the event the base component dispatches. Readevent.detail.value. A native input usesevent.target.value, butlightning-inputretargets its events and puts the data indetail.- Multi-word properties such as
icon-nameuse kebab-case in templates. In JavaScript they are camelCase (iconName).
Common errors
- Styling internals. Your CSS cannot reach the shadow DOM inside a base component. Use its documented properties, such as
variant, or styling hooks. Selectors that target inner elements will not work. - Wrong event data. Check the documentation for what each event puts in
detail. - Guessing property names. The Component Library lists each component's properties, events, and slots. Check it before you rely on a name.
- Using them outside Salesforce. Base components are unavailable in plain open-source LWC.
Exercise
In a template, how do you write the base component lightning/formattedNumber, and how do you read the new value from lightning-input's change event?
Show answer
Write <lightning-formatted-number>: the lightning namespace, a hyphen, and the camelCase name in kebab-case. Read the value from event.detail.value, because lightning-input puts the data in the event's detail instead of event.target.value.