Sub topic 7
Exposing properties in the App Builder property panel
This chapter assumes you can build a basic Lightning web component and read XML. It also assumes you have used the Setup UI and Lightning pages, and can deploy to an org with Salesforce CLI. You will connect @api properties in JavaScript to the fields an admin sees in the App Builder property panel.
What the property panel is
The property panel is the sidebar in Lightning App Builder. It appears when an admin selects a component on the canvas. It lists the settings you chose to expose, such as a header text box or a size picklist. An admin changes a value, and App Builder passes it to your component when the page renders.
Two things must line up for a setting to appear:
- A public property in the component's JavaScript class, marked with
@api. - A matching
<property>element inside atargetConfigin the.js-meta.xmlfile.
The @api decorator alone does not create a field in the panel. The property is public, so a parent component can set it, but App Builder shows only what the metadata file lists.
Step 1: Declare the public property
Start with the JavaScript class. Each property you want an admin to configure needs @api.
import { LightningElement, api } from 'lwc';
export default class AccountSummaryCard extends LightningElement {
@api recordId;
@api objectApiName;
@api headerText = 'Summary';
@api maxItems = 5;
}headerText and maxItems are the properties an admin will configure. recordId and objectApiName are different. App Builder fills them in on record pages, so you normally do not list them in the property panel.
The default values in JavaScript ('Summary' and 5) apply when nothing else sets the property. The default attribute in the XML sets the starting value the admin sees in the panel. Keep the two values the same, so the component behaves identically whether or not the admin touches the field.
Step 2: Add property elements to a targetConfig
Next, describe each configurable property in the metadata file. Each <targetConfig> applies to one or more targets and lists the properties for those targets.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Account Summary Card</masterLabel>
<targets>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__AppPage,lightning__HomePage">
<property name="headerText" type="String" label="Header text" default="Summary" />
</targetConfig>
<targetConfig targets="lightning__RecordPage">
<objects>
<object>Account</object>
</objects>
<property name="headerText" type="String" label="Header text" default="Summary" />
<property name="maxItems" type="Integer" label="Maximum items" min="1" max="10" default="5" />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>Read this file in three parts:
- The first
targetConfigcovers app pages and home pages. It exposes onlyheaderText. - The second covers record pages. It exposes
headerTextandmaxItems. - The
objectselement limits the record page version to Account records. A later chapter covers it in detail.
The name attribute must match the JavaScript property name exactly. The match is case-sensitive, so headerText and headertext are different names.
A property you list for one target does not carry over to another. If you want maxItems on the home page too, add it to that targetConfig.
The attributes of a property element
Each <property> element accepts a set of attributes. The common ones are below.
| Attribute | Purpose | Notes |
|---|---|---|
| `name` | Links the field to the `@api` property | Required. Must match the JavaScript name exactly. |
| `type` | The data type of the value | Required. Controls which input control appears. |
| `label` | The text shown beside the field | If omitted, App Builder shows the property name. |
| `description` | Help text for the field | Appears as an info icon next to the label. |
| `default` | The starting value | Must be valid for the `type`. |
| `required` | Forces the admin to supply a value | Set to `true` to block saving a page with an empty field. |
| `min` and `max` | Numeric limits | Apply to `Integer` properties. |
| `datasource` | A fixed list of choices | Turns a `String` property into a picklist. |
Property types
The type attribute decides what kind of input the admin gets. The types you will use most often are:
String: a text box, or a picklist when you adddatasource.Integer: a number box that accepts whole numbers. Combine it withminandmax.Boolean: a checkbox.
Your JavaScript property receives a value of the matching type. An Integer field arrives as a number, and a Boolean field arrives as true or false. A String field arrives as text, so convert it yourself if you need another type.
Creating a picklist with datasource
Add datasource to a String property to give the admin a list of choices. Separate the choices with commas and no extra spaces.
<property name="size" type="String" label="Card size" datasource="small,medium,large" default="medium" />The matching JavaScript property reads the chosen value as plain text.
import { LightningElement, api } from 'lwc';
export default class AccountSummaryCard extends LightningElement {
@api size = 'medium';
get cardClass() {
return `slds-card card-${this.size}`;
}
}The default value must be one of the listed choices. If it is not, the picklist starts without a valid selection.
Using required, min, and max
required="true" stops an admin from saving the page while the field is empty. Use it sparingly. A sensible default is usually friendlier than a forced choice.
min and max apply to Integer properties. In the example above, an admin cannot enter a value below 1 or above 10 for maxItems. App Builder enforces these limits in the panel. Your component can still receive an out-of-range value if another code path sets the property, so clamp the value in JavaScript if it matters.
get visibleItems() {
const limit = Math.min(Math.max(this.maxItems, 1), 10);
return this.items.slice(0, limit);
}Step 3: Show the values in the template
The template reads the properties like any other. App Builder only changes where the values come from.
<template>
<lightning-card title={headerText} icon-name="standard:account">
<div class="slds-var-p-around_medium">
<p>Record Id: {recordId}</p>
<p>Object: {objectApiName}</p>
<p>Max items: {maxItems}</p>
</div>
</lightning-card>
</template>On a record page, recordId and objectApiName show the current record. On a home page, they are empty, because no record exists there.
Step 4: Deploy and test
Deploy the bundle to your default org:
sf project deploy start --source-dir force-app/main/default/lwc/accountSummaryCardThen open a Lightning page in App Builder. Find Account Summary Card under Custom components, drag it onto the canvas, and select it. The property panel shows Header text on every page type, and Maximum items on Account record pages. Change a value, and the canvas updates.
Common mistakes
- A property is missing from the panel. Check that its
namematches the JavaScript property, that it sits in thetargetConfigfor the page type you are editing, and that you redeployed after changing the XML. - The panel shows the property name instead of a friendly label. Add a
labelattribute. - The picklist has no selection. Make sure
defaultis one of thedatasourcevalues. - A value is not the type you expected. A
Stringproperty always arrives as text. Convert it in JavaScript. - The wrong properties appear on one page type. Each
targetConfiglists its own properties. Check thetargetsattribute on each one. - Deployment fails with a metadata error. Property elements go inside a
targetConfig, andtargetConfigsgoes aftertargets. Validate the XML structure.
Summary
- App Builder shows only the properties you list in a
targetConfig, not every@apiproperty. - The
namein the XML must match the JavaScript property exactly. typepicks the input control, anddatasourceturns aStringinto a picklist.default,required,min, andmaxcontrol the starting value and limits.- Each
targetConfighas its own property list, so repeat a property for every target that needs it.
Exercise
A component has @api maxItems = 5; in JavaScript, and the admin sees no "Maximum items" field on a home page in App Builder. The XML has a property element for maxItems inside the targetConfig for lightning__RecordPage only. What is the cause, and how do you fix it?
Show answer
Property lists are per targetConfig. The home page target has no maxItems property element, so no field appears. Add <property name="maxItems" type="Integer" label="Maximum items" min="1" max="10" default="5" /> to the targetConfig that includes lightning__HomePage, then redeploy the bundle.
Exercise
Write a property element named size that gives admins a picklist of small, medium, and large, with medium selected at the start.
Show answer
<property name="size" type="String" label="Card size" datasource="small,medium,large" default="medium" />