Sub topic 8
Targets and Configurable Properties
This chapter assumes you can read a basic greetingCard bundle, know that js-meta.xml is the required configuration file, and understand that @api makes a class field public. Here we focus on two parts of the configuration file: targets, which say where a component can be placed, and targetConfigs, which say what an admin can set there.
How the pieces fit together
A component can be written and deployed without being usable in any builder. Three things have to line up before an admin sees it in Lightning App Builder:
- The JavaScript class marks a field or setter with
@api. - The
js-meta.xmlfile setsisExposedtotrueand lists at least one target. - A
targetConfigdeclares apropertywhose name matches the@apimember.
Targets
A target is a named location in Salesforce where the component can appear. You list targets inside the <targets> element, one <target> per location.
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>Common targets:
| Target | Where the component can be used |
|---|---|
| `lightning__AppPage` | Custom app pages in Lightning App Builder |
| `lightning__RecordPage` | Record pages |
| `lightning__HomePage` | The Home page |
| `lightning__Tab` | A custom tab |
| `lightning__FlowScreen` | A screen in a flow |
| `lightning__RecordAction` | A quick action on a record |
| `lightning__UtilityBar` | The utility bar of a Lightning app |
| `lightningCommunity__Page` | A page in an Experience Cloud site |
| `lightningCommunity__Default` | Exposes properties in Experience Builder |
You only list the places you want. A component with no targets can still be used inside other components, because it is imported by tag name in their markup. Targets matter only for placement by an admin or by the platform.
Exposing the component
The isExposed element is the master switch. If it is false or missing, the component is hidden from builders even when targets are listed. The component can still be used in another component's template.
<isExposed>true</isExposed>
<masterLabel>Greeting Card</masterLabel>
<description>Shows a greeting for a name.</description>masterLabel is the name shown in the builder. Without it, the builder falls back to the component's folder name. description is optional and appears as help text.
Narrowing a record page target
A broad target such as lightning__RecordPage puts the component on every record page. Use targetConfig with objects to restrict it:
<targetConfig targets="lightning__RecordPage">
<objects>
<object>Account</object>
<object>Contact</object>
</objects>
</targetConfig>The component then appears only on Account and Contact record pages. The <objects> element is only valid for record-page-style targets.
Target configs and properties
A targetConfig applies settings to one or more targets. Its targets attribute takes a comma-separated list of target names. Each <property> inside it becomes an input in the builder's property panel.
<targetConfigs>
<targetConfig targets="lightning__AppPage,lightning__HomePage">
<property name="name" type="String" label="Person's name" default="World" />
</targetConfig>
</targetConfigs>Property attributes
| Attribute | Purpose |
|---|---|
| `name` | Required. Must match the `@api` member in the class exactly, including case. |
| `type` | Required. One of `String`, `Boolean`, `Integer`, or `Color`, among others that depend on the target. |
| `label` | Text shown next to the input. Defaults to the name. |
| `default` | Initial value in the builder. |
| `description` | Help text shown on hover. |
| `required` | Marks the input as required. |
| `placeholder` | Hint text for an empty input. |
| `datasource` | A fixed list of choices, which turns the input into a picklist. |
| `min` and `max` | Bounds for an `Integer` property. |
Only target configs for targets the component declares are meaningful. Listing a property for a target that is not in <targets> has no effect.
The matching JavaScript
The property in the XML needs a public counterpart in the class.
import { LightningElement, api } from 'lwc';
export default class GreetingCard extends LightningElement {
@api name = 'World';
@api showBorder = false;
@api maxLength = 40;
}The platform sets these members after the component is created and before it first renders. The JavaScript default and the XML default are independent. If they differ, an admin sees one value in the builder panel while code uses another until the admin saves. Keep them identical.
A picklist with a datasource
Use datasource to limit an admin to fixed choices:
<targetConfig targets="lightning__AppPage">
<property name="tone" type="String" label="Tone"
datasource="formal,casual,playful" default="casual" />
</targetConfig>import { LightningElement, api } from 'lwc';
export default class GreetingCard extends LightningElement {
@api name = 'World';
@api tone = 'casual';
get message() {
const openers = { formal: 'Good day', casual: 'Hello', playful: 'Hey hey' };
return `${openers[this.tone] || 'Hello'}, ${this.name}!`;
}
}The fallback || 'Hello' protects the component if a saved value no longer appears in the list, for example after you remove a choice from datasource.
Properties per target
You can write several targetConfig elements to give different targets different properties. A target can appear in only one config.
<targetConfigs>
<targetConfig targets="lightning__AppPage">
<property name="name" type="String" default="World" />
</targetConfig>
<targetConfig targets="lightning__RecordPage">
<property name="showBorder" type="Boolean" default="false" />
<objects>
<object>Account</object>
</objects>
</targetConfig>
</targetConfigs>On app pages the admin sees name. On Account record pages the admin sees showBorder. Because the class declares both with @api, both exist on the component either way. The one the admin cannot see keeps its JavaScript default.
Common errors
- The component is missing from the builder. Check
isExposedfirst. Then check that the target matches the page type you are editing. - A property does not appear in the panel. The
targetConfigdoes not list the target you are using, or thenamedoes not match the@apimember. Matching is case-sensitive, soshowborderdoes not matchshowBorder. - Deployment fails with a property error. The class has no
@apimember with that name, or thetypeis not allowed for that target. For example, some types are valid in flows but not on pages. - Boolean values arrive as strings. In builder-set values they are real booleans, but if you set them from a parent's HTML attribute,
show-border="false"passes the string"false", which is truthy. Bind with{expression}instead. - A value changes but the component does not update. Reassigning an
@apifield inside the component that owns it is discouraged, because the parent controls it. Copy it to a private field if you need to change it.
Flow screens and other special targets
The lightning__FlowScreen target uses properties differently. Values can flow in from the flow and back out. The class marks the property with @api, and the config sets the direction.
<targetConfig targets="lightning__FlowScreen">
<property name="name" type="String" role="inputOnly" />
</targetConfig>The role attribute applies to flow screens only. Use inputOnly or outputOnly to limit the direction. Without it, a property is available in both directions. To send a value to the flow from the component, dispatch a FlowAttributeChangeEvent, which you import from lightning/flowSupport.
Verifying your configuration
- Deploy the bundle to a scratch org or sandbox.
- Open Lightning App Builder and edit a page that matches one of your targets.
- Find the component under Custom in the component list.
- Select it and confirm the property panel shows the labels, defaults, and choices you defined.
- Save, activate, and view the page to confirm the value reaches the component.
Exercises
Exercise
A component declares @api title in its class. Its js-meta.xml has isExposed set to true and a lightning__HomePage target, but the targetConfig lists a property named Title. What happens in App Builder, and how do you fix it?
Show answer
The panel shows no editable input that reaches the class, or deployment reports an error, because names are case-sensitive and Title does not match title. Change the property name in the XML to title so it matches the @api member exactly.
Exercise
You want a component to appear only on Opportunity record pages and let admins turn a border on or off. Write the targets and targetConfigs sections.
Show answer
Use this:
<targets>
<target>lightning_RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning_RecordPage">
<objects>
<object>Opportunity</object>
</objects>
<property name="showBorder" type="Boolean" default="false" />
</targetConfig>
</targetConfigs>
The class also needs @api showBorder = false;.
Exercise
Why should the JavaScript default of an @api field match the default in the XML?
Show answer
The builder shows the XML default, but the component uses its JavaScript default until a value is saved. If they differ, the admin sees one value and the page behaves with another.