Sub topic 5
Adding configurable properties
Experience Builder lets an admin edit a component's settings in the property panel without touching code. This chapter covers how to declare those settings, how they reach your JavaScript, and what to watch for. It assumes you have already exposed a component with the lightningCommunity__Page target, as in the previous chapter, and that you know @api, @wire, and basic Apex.
How a property travels from the meta file to the component
A configurable property has two halves that must agree. The meta file declares it, which makes it appear in the Builder property panel. The component class declares a matching @api property, which receives the value at runtime.
The value is stored with the page, not with the component. Every instance of the component on every page has its own value. Visitors cannot change it.
Declaring properties in the meta file
Properties go inside a targetConfig element. For Experience Builder, the targets attribute of that element must include lightningCommunity__Default. The lightningCommunity__Page target makes the component appear in the component palette. The lightningCommunity__Default target is what makes the property panel show your fields. If you list only the first, the component can be dropped on a page but has nothing to edit.
<?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="heading" type="String" default="My cases"
label="Heading" description="Text shown above the list." />
<property name="maxRows" type="Integer" default="5" min="1" max="25"
label="Maximum rows" required="true" />
<property name="showStatus" type="Boolean" default="true"
label="Show status" />
<property name="accentColor" type="Color" default="#0176d3"
label="Accent color" />
<property name="layout" type="String" default="list"
datasource="list,compact" label="Layout" />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>Property attributes
| Attribute | Purpose |
|---|---|
| `name` | Must match the `@api` property name exactly, including case. |
| `type` | `String`, `Integer`, `Boolean`, or `Color`. Each type gets a matching editor control. |
| `label` | Text shown next to the control in the panel. |
| `description` | Help text shown with the control. |
| `default` | Initial value when an admin drops the component on a page. |
| `required` | Blocks an empty value in the panel. |
| `min`, `max` | Bounds for an `Integer` control. |
| `datasource` | A comma-separated list of values on a `String`. The panel shows a picklist instead of a text box. |
Receiving the values in JavaScript
Each property needs an @api field with the same name. The component below uses all five properties from the meta file.
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 heading = 'My cases';
@api maxRows = 5;
@api showStatus = false;
@api accentColor = '#0176d3';
@api layout = 'list';
isGuest = isGuest;
@wire(getMyCases, { maxRows: '$maxRows' })
cases;
get hasCases() {
return Boolean(this.cases && this.cases.data && this.cases.data.length);
}
get containerClass() {
return this.layout === 'compact' ? 'case-list compact' : 'case-list';
}
get containerStyle() {
return `--case-accent: ${this.accentColor};`;
}
}A few details in this class matter:
- Boolean defaults. The
@api showStatusfield starts asfalse, even though the meta file default istrue. LWC requires public Boolean properties to default tofalsein the class, because a component cannot be set tofalsefrom markup if it starts astrue. Put the "on by default" behavior in the meta filedefaultattribute. Builder passes the value explicitly. - Types arrive typed. An
Integerproperty reaches the component as a JavaScript number, and aBooleanas a Boolean. You do not need to parse them. - Do not assign to
@apifields. A component must not modify its own public properties. If you need a changed copy, such as a clamped value, compute it in a getter or a private field. - Reactivity. Because
maxRowsis referenced as'$maxRows'in the wire config, changing the value in Builder preview reloads the data.
The template applies the layout class, the color through a CSS custom property, and the status toggle:
<template>
<div class={containerClass} style={containerStyle}>
<h2 class="heading">{heading}</h2>
<template lwc:if={isGuest}>
<p>Log in to see your cases.</p>
</template>
<template lwc:elseif={hasCases}>
<ul>
<template for:each={cases.data} for:item="c">
<li key={c.Id}>
{c.CaseNumber}: {c.Subject}
<template lwc:if={showStatus}>
<span class="status">({c.Status})</span>
</template>
</li>
</template>
</ul>
</template>
<template lwc:else>
<p>No cases found.</p>
</template>
</div>
</template>.heading {
color: var(--case-accent);
}
.compact li {
padding: 0.25rem 0;
}Passing a color through a custom property, instead of writing it into many rules, keeps the stylesheet in one place. A later chapter covers how this fits with site branding.
Property values are untrusted input
A property is editable only by someone with Builder access. That does not make it safe on the server. The @AuraEnabled method is callable by anyone who can reach the component, and a caller can pass any value for maxRows. The min and max attributes only constrain the panel control. Enforce limits in Apex too:
public with sharing class CaseListController {
@AuraEnabled(cacheable=true)
public static List<Case> getMyCases(Integer maxRows) {
Integer safeLimit = Math.min(Math.max(maxRows == null ? 5 : maxRows, 1), 25);
return [
SELECT Id, CaseNumber, Subject, Status
FROM Case
WITH USER_MODE
ORDER BY CreatedDate DESC
LIMIT :safeLimit
];
}
}Never use a property to carry a secret, such as an API key. Page definitions are delivered to the browser, so anyone viewing the page can read the value.
Common problems
- The component shows in the palette but the panel is empty. The
targetConfigis missing, or itstargetsattribute does not includelightningCommunity__Default. - The control appears but the component ignores it. The
namein the meta file and the@apifield differ, for examplemaxrowsandmaxRows. - A saved value disappears after a deploy. Renaming or retyping a property orphans the value stored on existing pages. Treat property names as a public contract. Add a new property and retire the old one gradually.
- A change in the meta file does not show in Builder. Deploy the bundle and reopen Builder. A page that is already open keeps the old definition.
- An invalid
default. A default that violates thetype, such as text for anInteger, makes the deployment fail. - Properties on both runtimes. Aura and LWR sites both read
lightningCommunity__Default. Some property types and editors are limited to one runtime. Check the metadata reference for the runtime you target before relying on a less common type.
Exercises
Exercise
You add a Boolean property showStatus to the meta file with default="true". In the component class you write @api showStatus = true;. Why is this a problem, and what should the class contain?
Show answer
LWC requires a public Boolean property to start as false, because a component initialized to true cannot be turned off from markup. The class should declare @api showStatus = false;. The "on by default" behavior belongs in the meta file default="true", which Builder applies when the component is added to a page.
Exercise
An admin sets Maximum rows to 10 in Builder, yet the component still loads five cases. The meta file has <property name="maxrows" type="Integer" default="5" /> and the class has @api maxRows = 5;. What is wrong?
Show answer
The property name is maxrows, but the @api field is maxRows. Names are case-sensitive and must match exactly, so Builder never sets the class field and it keeps its default of 5. Change the meta file to name="maxRows" and redeploy.
Exercise
The max="25" attribute is set on maxRows. Why does the Apex method still clamp the value?
Show answer
max only limits the control in the Builder property panel. The Apex method is callable with any value by any user who can reach the component, so the server must enforce its own limit.