Sub topic 4
Exposing a Component to Experience Builder
This chapter assumes you can build a basic Lightning web component (HTML template, JavaScript class, meta file) and that you have a Salesforce DX project and the Salesforce CLI. Here you will make a component appear in the Experience Builder component palette, so a site admin can drag it onto a page.
Why the meta file matters
A component you deploy does not appear in Experience Builder by default. The meta file (*.js-meta.xml) tells the platform where the component may be used. Two things must be present:
isExposedset totrue.- At least one site target in the
targetslist.
Without both, the component works in your code but never shows up in the palette.
Choosing targets
A target is a place where a component is allowed to appear. Two targets matter for sites:
| Target | Meaning |
|---|---|
| `lightningCommunity__Page` | The component can be dragged onto a page in Experience Builder. |
| `lightningCommunity__Default` | The component's properties can be edited in the builder's property panel. |
You almost always list both. lightningCommunity__Page makes the component available, and lightningCommunity__Default is where you attach targetConfigs that define editable properties.
The meta file
This meta file exposes a component labeled "Case List" and defines one editable property, maxRows.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Case List</masterLabel>
<targets>
<target>lightningCommunity__Page</target>
<target>lightningCommunity__Default</target>
</targets>
<targetConfigs>
<targetConfig targets="lightningCommunity__Default">
<property name="maxRows" type="Integer" default="5" label="Maximum rows" />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>masterLabel is the name the admin sees in the palette. The property element creates a field in the property panel. Chapter details on property types come later. For now, note that name must match a public property in the JavaScript class.
Receiving the property in JavaScript
The matching public property uses the @api decorator. The page editor sets its value, and the component reads it like any other field.
import { LightningElement, api, wire } from 'lwc';
import isGuest from '@salesforce/user/isGuest';
import getMyCases from '@salesforce/apex/CaseListController.getMyCases';
export default class CaseList extends LightningElement {
@api maxRows = 5;
isGuest = isGuest;
@wire(getMyCases, { maxRows: '$maxRows' })
cases;
get hasCases() {
return Boolean(this.cases && this.cases.data && this.cases.data.length);
}
}The $maxRows syntax makes the wire reactive. When an admin changes the value in the property panel, the wire reloads the data. The Apex method and the isGuest import are covered in later chapters. For now, treat them as given.
How the pieces connect
Common errors
- Component missing from the palette. Check
isExposedand thatlightningCommunity__Pageis listed. Redeploy, then refresh Experience Builder. - Property panel shows nothing. The
targetConfigmust namelightningCommunity__Default, andlightningCommunity__Defaultmust also appear intargets. - Property value ignored. The
namein the meta file must exactly match the@apiproperty, including case. - Deployment fails on a property. The
typemust be a supported value such asString,Integer, orBoolean, anddefaultmust fit that type.
Exercise
A developer deploys a component with isExposed set to true and the target lightningCommunity__Default only. It does not appear in the Experience Builder palette. What is the likely cause, and what is the fix?
Show answer
The lightningCommunity__Page target is missing. That target makes a component available to drag onto a page, while lightningCommunity__Default only enables the property panel. Add lightningCommunity__Page to targets, redeploy, and refresh Experience Builder.
Exercise
The meta file defines a property named maxrows, but the JavaScript class declares @api maxRows. What happens when an admin edits the value in the property panel?
Show answer
The component does not receive the admin's value, because the names do not match. Property names are case-sensitive, so the meta file name must be maxRows.