Sub topic 11
Supported form factors for desktop and phone
This chapter assumes you can already write a basic Lightning web component and its js-meta.xml file. You should know isExposed, targets and targetConfigs from the earlier chapters. You should also be comfortable opening a Lightning page in App Builder and deploying with the Salesforce CLI.
What a form factor is
A form factor is the kind of device screen a page is drawn on. Lightning pages can be viewed in a browser on a desktop or laptop. They can also be viewed in the Salesforce mobile app on a phone. The same page can have different layouts on each.
App Builder needs to know which of those screens your component was built for. If a component only works in a wide layout, an admin should not be able to drop it on a phone page. The supportedFormFactors element in the metadata file declares this.
Two values are available:
| Value | Meaning |
|---|---|
| `Large` | Desktop. Larger screens such as laptops and, in Lightning Experience, tablets. |
| `Small` | Phone, in the Salesforce mobile app. |
Declaring form factors in the metadata file
supportedFormFactors is a child of a targetConfig. It applies to the targets listed in that targetConfig's targets attribute. Each supportedFormFactor child names one value in its type attribute.
The next file extends the Account Summary Card from earlier chapters. The record page version supports both desktop and phone. The app and home page versions support desktop only.
<?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>
<description>Shows a titled card with the current record context.</description>
<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" />
<supportedFormFactors>
<supportedFormFactor type="Large" />
</supportedFormFactors>
</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" />
<supportedFormFactors>
<supportedFormFactor type="Large" />
<supportedFormFactor type="Small" />
</supportedFormFactors>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>Some points about this file:
supportedFormFactorsgoes inside atargetConfig. It does not go at the top level of the bundle next totargets.- One
supportedFormFactorselement holds one or moresupportedFormFactorchildren. Listing both types makes the component available on both screen sizes. - The element comes after
propertyandobjectsin these examples. Keep that order, because the metadata schema can reject elements placed out of order. - A
targetConfigcovers every target in itstargetslist. The app and home targets above share one form factor setting.
How App Builder uses the setting
App Builder lets an admin choose which form factor to preview or edit. A page opened for phone only offers components that declare Small. A component that supports only Large does not appear in the component list for that view.
The supportedFormFactors element only controls availability. It does not change how the component renders. A component that declares Small still has to work well on a narrow screen. That part is your job, and the later sections cover it.
What happens when you omit the element
You can leave out supportedFormFactors entirely. The platform then applies a default set of form factors for the target. Defaults have differed between page types and API versions. Do not rely on memory or on this chapter for the exact default. Instead, state your intent explicitly in every targetConfig that matters. That makes the behavior visible in source control and removes any doubt when a component is missing on one device.
Reacting to the form factor in code
Declaring Small makes the component available on phone pages. It does not make it adapt. For component logic that depends on the device, LWC provides the @salesforce/client/formFactor module. It exports a string with one of three values: Large, Medium or Small. Treat it as a constant for the session. It is set when the page loads.
This version of the component limits the item count on phones. It also switches a style class.
import { LightningElement, api } from 'lwc';
import formFactor from '@salesforce/client/formFactor';
const PHONE_MAX_ITEMS = 3;
export default class AccountSummaryCard extends LightningElement {
@api recordId;
@api objectApiName;
@api headerText = 'Summary';
@api maxItems = 5;
get isPhone() {
return formFactor === 'Small';
}
get effectiveMaxItems() {
return this.isPhone
? Math.min(this.maxItems, PHONE_MAX_ITEMS)
: this.maxItems;
}
get bodyClass() {
return this.isPhone
? 'slds-var-p-around_x-small'
: 'slds-var-p-around_medium';
}
}The template uses those getters. The lwc:if and lwc:else directives choose between a compact and a full layout.
<template>
<lightning-card title={headerText} icon-name="standard:account">
<div class={bodyClass}>
<p>Object: {objectApiName}</p>
<p>Showing up to {effectiveMaxItems} items</p>
<template lwc:if={isPhone}>
<p>Compact layout for phone</p>
</template>
<template lwc:else>
<p>Record Id: {recordId}</p>
<p>Full layout for desktop</p>
</template>
</div>
</lightning-card>
</template>Deploy it with the same command as before:
sf project deploy start --source-dir force-app/main/default/lwc/accountSummaryCardChoosing between code and CSS for responsiveness
Reading formFactor is one option. CSS is often better for pure layout changes, because it reacts to the real width of the space rather than to the device type. Consider these trade-offs.
| Approach | Good for | Limitation |
|---|---|---|
| `formFactor` in JavaScript | Changing behavior, such as fewer items or skipping a heavy feature | Fixed at page load. Does not react to a resized window or a narrow region. |
| SLDS grid and utility classes | Stacking or resizing columns | Cannot change logic. |
| CSS media or container queries | Layout that follows the width of the screen or region | Cannot change logic. |
A desktop page can place your component in a narrow sidebar. There, formFactor still says Large even though the space is small. For layout, prefer CSS that reacts to the available width. Use formFactor when the behavior itself must differ.
Testing each form factor
Test both views before you hand the component to an admin.
- Deploy the bundle to your org.
- Open a record page in App Builder and add the component. Confirm it appears under Custom components.
- Switch the page's form factor to phone in the App Builder preview, then check that the component is still offered. Preview is limited, so also open a record in the Salesforce mobile app or in the mobile browser view.
- Resize the browser window on desktop to check the layout and spot overflowing content.
- Change a property in the property panel and confirm the phone layout still reads well. Long header text can wrap awkwardly on narrow screens.
Common mistakes
- Putting
supportedFormFactorsoutside atargetConfig. The deployment can fail, or the setting is ignored. Place it inside thetargetConfigthat covers the target. - Expecting different properties per form factor. A target can appear in only one
targetConfig. You cannot give phone and desktop different property sets on the same page type. If the phone view needs a different value, handle it in code, aseffectiveMaxItemsdoes. - Declaring a form factor and forgetting a target. Form factors only matter for targets that have a
targetConfig. A target without one uses the default behavior. - Using
formFactoras a layout switch. It reflects the device, not the space the component occupies. Use CSS for width-based layout. - Forgetting to redeploy. App Builder reads the deployed metadata. Edit the file, deploy again, then reload App Builder.
If the component is missing from a phone page, check these in order. First, isExposed is true. Second, the page's target is listed in targets. Third, the matching targetConfig includes <supportedFormFactor type="Small" />. Fourth, for record pages, the objects restriction includes the object of the page.
Exercise
Your Account Summary Card is deployed with a record page targetConfig that lists only <supportedFormFactor type="Large" />. An admin reports that the component does not appear in the list when editing the phone version of an Account record page. What change fixes this, and what should you check before deploying it?
Show answer
Add <supportedFormFactor type="Small" /> inside the supportedFormFactors element of the lightning__RecordPage targetConfig, then redeploy. Before deploying, check that the component renders well on a narrow screen, for example by limiting items with formFactor and using responsive CSS. After deploying, reload App Builder and confirm the component now appears for the phone view.
Exercise
The component sits in a narrow sidebar region of a desktop record page. The content looks cramped, but formFactor still returns Large. Why, and what is the better way to adapt the layout?
Show answer
formFactor describes the device type, not the width of the region the component occupies, and it is read once when the page loads. A desktop page therefore reports Large even for a narrow sidebar. Use CSS that responds to the available width, such as SLDS grid classes or container queries. Keep formFactor for behavior changes such as showing fewer items.