Sub topic 3
Targets and Configurable Properties
Assume you know basic HTML, JavaScript classes and modules, CSS selectors, and the Salesforce DX project layout. This chapter covers optional bundle files and how to decide which ones to include.
Targets and configurable properties
The *.js-meta.xml file controls where Salesforce can use a component. A target is a Salesforce location, such as an app page or record page. A property is a value an administrator can edit in Lightning App Builder.
<targetConfigs>
<targetConfig targets="lightning__RecordPage">
<property name="name" type="String" default="World" />
</targetConfig>
</targetConfigs>The property name must match a public JavaScript property:
import { LightningElement, api } from 'lwc';
export default class GreetingCard extends LightningElement {
@api name = 'World';
}The component receives the configured value when Salesforce places it on the selected page.
Optional files in a bundle
| File or folder | Purpose |
|---|---|
| `component.svg` | Icon shown for the component in App Builder |
| `helper.js` | Reusable JavaScript functions |
| Extra HTML files | Alternative templates |
| CSS-only bundle | Shared styles imported by other components |
| `__tests__` | Jest tests |
SVG icon
An SVG file with the component's name can provide its builder icon. It is optional and affects authoring tools, not runtime behavior.
Helper JavaScript modules
A helper module keeps reusable logic outside the component class.
// formatters.js
export function formatGreeting(name) {
return `Hello, ${name}!`;
}Import it into the component:
import { LightningElement } from 'lwc';
import { formatGreeting } from './formatters';
export default class GreetingCard extends LightningElement {
name = 'World';
get greeting() {
return formatGreeting(this.name);
}
}Multiple templates with `render()`
A bundle can contain multiple HTML templates. The render() method selects one based on component state. Each template still needs a single root <template> element.
render() {
return this.isSignedIn ? signedIn : signedOut;
}Shared CSS modules
A CSS-only bundle can be imported by another component:
@import 'c/sharedStyles';
.card-body {
padding: 1rem;
}Regular component CSS remains scoped to its own template. Shared CSS is useful for deliberately reused design rules.
Tests with Jest
Place tests in __tests__, commonly in a file such as greetingCard.test.js. Jest tests create the component, set public properties, and inspect the rendered shadow DOM. Tests catch broken bindings and behavior before deployment.
Choosing files
Start with the HTML template, JavaScript class, and metadata file. Add CSS only when the component needs local styling. Add helper modules when logic is reused or becomes difficult to read. Add extra templates only when the component has distinct rendering modes. Add an SVG for a better builder experience and Jest tests for behavior you need to protect.
Exercise
Which file controls whether an LWC appears in Lightning App Builder, and where do you declare an editable property?
Show answer
The component.js-meta.xml file controls exposure. Declare the property inside a targetConfig element with a matching public JavaScript property.