Sub topic 5
Adding configurable properties
This chapter assumes you can build a basic Lightning web component and deploy it with the Salesforce CLI. It also assumes you have already exposed a component to Experience Builder. Here you let site builders change a component's behavior from the page editor without touching code.
How properties reach your component
A site builder selects your component in Experience Builder and sees a property panel. Each field in that panel comes from a <property> element in the component's meta file. When the builder saves a value, Experience Builder passes it to the component's public property of the same name.
Two pieces must match:
- A
<property>entry inside a<targetConfig>in the.js-meta.xmlfile. - An
@apiproperty in the JavaScript class with the identical name.
Declaring the property in the meta file
<?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>The targets attribute on targetConfig matters. Properties for Experience Builder go under lightningCommunity__Default. The lightningCommunity__Page target only makes the component available on a page. It does not create a panel field.
Common attributes on <property> are:
| Attribute | Purpose |
|---|---|
| `name` | Must match the `@api` property in JavaScript |
| `type` | Data type, such as `String`, `Integer`, `Boolean`, or `Color` |
| `label` | Text shown in the property panel |
| `default` | Value used until the builder changes it |
| `description` | Help text for the builder |
| `required` | Forces the builder to supply a value |
Receiving the value in JavaScript
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 the builder changes the value, the wire provisions fresh data. Keep the JavaScript default in sync with the meta file default, so the component behaves sensibly outside Experience Builder, such as in unit tests.
Common errors
- The property panel is empty. Check that
isExposedistrue, that thetargetConfiglistslightningCommunity__Default, and that you redeployed the bundle. - The value is always the default. The
namein the XML differs from the@apiname. Names are case-sensitive. - A deploy fails with a metadata error. The
typevalue is not supported for the target, ordefaultdoes not fit the type, such as text in anInteger. - Live site ignores a new value. Property changes are part of the site draft. Publish the site to make them visible to visitors.
- Unexpected data for guests. A property only changes what the component asks for. It does not change what the user may see. Apex sharing and guest access still decide that.
Exercise
You add a Boolean property named showStatus to the meta file, but the Experience Builder panel never shows it. The JavaScript has @api showStatus = false;. Name two things in the meta file you should check.
Show answer
First, check that the property sits inside a targetConfig whose targets attribute includes lightningCommunity__Default, because properties for the page editor are read from that target. Second, check that isExposed is true and that the bundle was redeployed. A mismatch in the name attribute would affect the value but not whether the field appears in the panel.