Sub topic 6
Configuring each target with targetConfigs
This chapter assumes you can read basic XML. It also assumes you have a component with a working .js-meta.xml file, isExposed set to true, and a targets list. You should know what an @api property is. Earlier chapters covered those parts. This chapter covers the targetConfigs element, which sets how the component behaves on each target.
What targetConfigs does
The targets element says where a component can be placed. The targetConfigs element says how it behaves on each of those pages. It holds one targetConfig element for each group of targets that share settings. Each targetConfig can define:
- The properties admins can edit in the App Builder property panel.
- The objects a record page component supports (record pages only).
- The form factors the component supports (app, home, and record pages).
targetConfigs is optional. If a target has no targetConfig, the component can still be placed on that page, but the property panel shows no editable properties for it.
Anatomy of a targetConfig
Each targetConfig has a targets attribute. Its value is a comma-separated list of target names with no extra structure. Every target named there must also appear in the top-level targets element.
Here is the full metadata file for the Account Summary Card:
<?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>This file has two targetConfig elements:
- The first covers the app page and home page targets. Admins can set only
headerTextthere. - The second covers the record page target. It limits the component to Account records and exposes both
headerTextandmaxItems.
The two configurations repeat the headerText line. That repetition is intentional, and the next section explains why.
Why you split targets into groups
Each page type can need different settings. A record page has a record, so a "Maximum items" setting about related data may make sense there. A home page has no record, so the same setting would confuse admins. Splitting into groups lets you show each page type only the properties that apply.
When two page types need identical settings, list both targets in one targetConfig, as the first block above does. List each target in only one targetConfig. A target defined in two places creates conflicting configuration, and the deploy can fail.
A property is available only on the targets of the targetConfig that declares it. If maxItems is declared only in the record page block, an admin placing the component on a home page never sees it. The component still has the property, because @api maxItems = 5 is in the JavaScript, so it keeps the JavaScript default of 5 there.
Linking property elements to @api properties
Each property element configures one public property in the JavaScript class. The link is the name attribute, which must match the @api property name exactly, including case.
import { LightningElement, api } from 'lwc';
export default class AccountSummaryCard extends LightningElement {
@api recordId;
@api objectApiName;
@api headerText = 'Summary';
@api maxItems = 5;
}The table shows how the two files connect.
| XML attribute | Role | Matches |
|---|---|---|
| `name="headerText"` | Identifies the property | `@api headerText` in JavaScript |
| `type="String"` | Chooses the input control and value type | The type of value the property receives |
| `label="Header text"` | Text the admin sees in the property panel | Nothing in JavaScript |
| `default="Summary"` | Initial value shown in the panel | Should equal the JavaScript default |
If the XML names a property that has no @api counterpart, Salesforce reports an error when you deploy, or the property does nothing. A typo such as headertext is the usual cause. If you rename a property in one file, rename it in the other.
The reverse is not an error. An @api property with no property element still works, but admins cannot edit it. This is how recordId and objectApiName work. Do not declare them as property elements, because the record page fills them in automatically.
Keeping defaults consistent
The default in XML sets the value shown in the property panel when an admin first places the component. The initializer in JavaScript is the value used when nothing is passed in. These two can drift apart. For example, the XML default may say 10 while the JavaScript says 5. Admins then see one value in the panel while the component may behave differently after a refactor. Keep both values the same.
Property attributes in a targetConfig
The property element accepts several attributes. This chapter uses only the ones in the example, and a later chapter covers the full set of types and attributes.
typecan beString,Integer, orBoolean, among others.labelis shown to the admin in place of the raw property name.defaultsupplies the starting value.minandmaxbound anInteger. In the example,maxItemsis limited to 1 through 10.datasourceturns aStringinto a picklist.
This line gives admins a three-option picklist for card size:
<property name="size" type="String" label="Card size" datasource="small,medium,large" default="medium" />Place it inside a targetConfig, next to the other property elements.
The objects element
Inside a record page targetConfig, the objects element limits which objects can show the component. In the example, only an Account record page can use it:
<objects>
<object>Account</object>
</objects>Add one object element for each supported object. Use the API name of the object, so a custom object is written as Invoice__c. If you omit objects, the component is available on record pages for any object.
objects is valid only in a targetConfig for lightning__RecordPage. Put it in a block for lightning__HomePage and the deploy fails.
Supported form factors
A targetConfig for app, home, or record pages can also include supportedFormFactors. It says whether the component works on large screens (desktop), small screens (phone), or both.
<targetConfig targets="lightning__RecordPage">
<supportedFormFactors>
<supportedFormFactor type="Large" />
<supportedFormFactor type="Small" />
</supportedFormFactors>
<property name="headerText" type="String" label="Header text" default="Summary" />
</targetConfig>Use Large for desktop and Small for phone. A later chapter covers this setting in more detail.
Deploy and check
After you edit the file, deploy the bundle:
sf project deploy start --source-dir force-app/main/default/lwc/accountSummaryCardThen open a record page for an Account in Lightning App Builder. Drag Account Summary Card onto the page and select it. The property panel should show Header text and Maximum items. Open a home page in App Builder and place the same component. The panel should show only Header text.
Common errors
- A property is missing from the panel. Check that the
targetConfiglists the target you are using. Then check that thenamematches the@apiproperty exactly. - The deploy is rejected. Look for an
objectselement outside a record page block, a property with no@apicounterpart, or a target in twotargetConfigelements. - A record page component is not available for an object. Check that the object's API name appears in
objects. - The panel shows a value the component does not use. Compare the
defaultin XML against the JavaScript initializer.
Exercise
Your component has @api maxItems = 5 and should expose maxItems only on Contact record pages, but not on app pages. Write the targetConfig for the record page and say what the app page targetConfig should contain.
Show answer
Use a block such as <targetConfig targets="lightning__RecordPage"><objects><object>Contact</object></objects><property name="maxItems" type="Integer" label="Maximum items" min="1" max="10" default="5" /></targetConfig>. For app pages, either omit a targetConfig or add one for lightning__AppPage without maxItems. On app pages the component then keeps the JavaScript default of 5, and admins cannot change it.
Exercise
A deploy fails after you add <property name="Headertext" type="String" /> to a targetConfig, although the JavaScript has @api headerText. What is the cause and the fix?
Show answer
The name must match the @api property exactly, including case. Headertext does not match headerText. Change the XML name to headerText and deploy again.