Skip to content

Lightning Out 2.0 dynamic content

Lightning Out 2.0 supports passing parameters to Lightning Web Components (LWCs), enabling you to configure a component dynamically based on external data. For example, an external page could provide a contact, amount and language to control how the LWC is presented. When the LWC is used as a container for a Screen Flow, you can also pass parameters further into your Flow logic.

To create dynamic content by passing parameters, you have three layers to configure:

  1. Expose parameters so that the external website can use them.
  2. Handle the parameters inside the LWC.
  3. Pass the parameters to a Screen Flow.

Before we continue, keep in mind the following:

  • Only custom parameters declared with @api can be used by the external website.
  • Custom names starting with _ are not supported by Lightning Out 2.0.
  • Parameters passed to Salesforce are external HTML attributes, so make sure values are validated or converted inside the component. For example, an amount received as a string needs to be converted before it can be used as a numeric Flow variable.

Expose parameters for dynamic content

Any value that should be configurable from outside your LWC must be exposed as a public property using the @api decorator.

First, import api:

import { LightningElement, api } from 'lwc';

Then define the properties that should be exposed. In this example, the component exposes three public parameters:

  • contactId
  • amount
  • paymentMethod
export default class FlowContainer extends LightningElement {
    @api contactId;
    @api amount;
    @api paymentMethod;
}

Lightning Out 2.0 mirrors these public LWC properties as attributes on the Lightning Out web component that runs on the external page. Without @api, the property remains internal to the LWC and cannot be set from the Lightning Out host page.

Your external page needs to use attribute names that are written differently from the property names in JavaScript. JavaScript properties use camelCase for the parameters, while the corresponding HTML uses lowercase words that separated by hyphens. For example, contactId becomes contact-id and paymentMethodbecomes payment-method.

<c-flow-container
    contact-id="003XXXXXXXXXXXX"
    payment-method="card">
</c-flow-container>

Any custom parameter name can be used by Lightning Out 2.0 unless the it starts with an underscore (_). If an unsupported attribute name is provided, Lightning Out does not throw an error. The parameter simply isn't passed to the embedded LWC.

Once the public properties exist, there are three ways to assign values. Each method services a different purpose:

  • Host page markup: suitable when the external website already knows the value when it renders the component
  • Programmatic methods: suitable when the value is dynamic, calculated at runtime, or depends on user input
  • Lightning Out 2.0 App Manager: suitable for configuration that belongs to the Lightning Out application itself and is expected to remain static.

We go through each option in the following sections.

Set values directly in the host page markup

The external website can provide values as attributes on the Lightning Out web component.

For example:

<c-flow-container
    contact-id="003XXXXXXXXXXXX"
    amount="50"
    payment-method="card">
</c-flow-container>

This is the simplest option when the values are already known when the page is rendered.

For example, a website generating a donation page may already know the donor (contact), campaign, amount and language. Each of these can be handled has parameter values that the page includes directly in the generated component markup.

Salesforce refers to this as setting the component properties declaratively on the host page.

Set values programmatically

The external page can also create or update the Lightning Out component through JavaScript and standard DOM APIs.

For example:

const component = document.querySelector('c-flow-container');

component.setAttribute('contact-id', '003XXXXXXXXXXXX');
component.setAttribute('amount', '100');

This is useful when a value is calculated or becomes available only after the page has loaded.

User selects an amount

Website JavaScript receives the selection

JavaScript updates amount on the Lightning Out component

User selects an amount

Website JavaScript receives the selection

JavaScript updates amount on the Lightning Out component

Salesforce also supports programmatically generating the Lightning Out markup using DOM methods. This way you can avoid declaring the entire component in static HTML.

Set values in Lightning Out 2.0 App Manager

Values can be configured directly when setting up the Lightning Out 2.0 app in Salesforce.

  1. Open Lightning Out 2.0 App Manager.
  2. Find the relevant Lightning Out app and select Edit App.
  3. Open the App Components tab.
  4. Find the component.
  5. Open the Custom Properties field for that component.
  6. Enter the property values in JSON format.
  7. Save the app.

For example:

{
    "payment-method": "card",
    "amount": "50"
}

These values become property overrides for that component when it is used through the Lightning Out app. Salesforce specifically documents Custom Properties in App Manager as one of the supported ways to set component properties.

This option is useful when a value is a static part of the Lightning Out app configuration and doesn't need to be calculated by the external website. For example, if the same wrapper should normally launch in a particular configuration, App Manager can provide that configuration without requiring the external page to pass it every time.

Handle parameters in the LWC

Once a parameter is passed to the LWC, it is available inside the component through its @api property.

export default class FlowContainer extends LightningElement {
		@api firstName;
		@api normalizedFrequency;
		@api campaignId;
}

The values received from the external website can be used anywhere in the LWC where these properties are referenced.

The LWC can use these values in its own logic and content, or pass them to other components embedded in the LWC, including FinDock components.

Set default values

An @api property can also have a default value. The default value is used when no value is provided for that property. When the external website does provide a value, that value overrides the default. You specify the default value in the JavaScript:

@api firstName = 'Supporter';

This can be useful for creating a better fallback experience while still allowing the external website to customize it. For example, an LWC could display:

<p>Welcome, {firstName}!</p>

With the default value above, a visitor would see:

Welcome, Supporter!

If the external website knows the visitor's name, it can provide it:

<c-flow-container first-name="John"></c-flow-container>

The same LWC would then display:

Welcome, John!

Defaults can be used in the same way for other parameters whenever the component should have a predefined value that can optionally be overridden by the external website.

Pass values to embedded components

Parameters received through @api can also be passed to components used inside the LWC.

For example, if the wrapper contains another FinDock component, a received property can be supplied to that component:

<cpm-payment-method-selector
        frequency={normalizedFrequency}>
</cpm-payment-method-selector>

The same principle applies when the LWC contains a Screen Flow. However, Flow requires its input variables to be provided in a specific format. This is covered in the next step.

Pass parameters to a Screen Flow

Receiving a parameter in the LWC does not automatically make it available to the Flow. The Flow running inside the component won't know about the property until you explicitly pass the value to the Flow.

If you want to use Salesforce data based on the passed parameters, make sure the corresponding Guest User has access to all the objects and records. For example, to pull the contact info, you need to provide the object access and set up the sharing model.

  1. Create an input variable in the Flow.

    First, create a variable in Flow Builder. For example:

    API Name: contactId
    Data Type: Text
    Available for input: True

    Available for input must be enabled. This tells Flow that the variable can receive its initial value from something outside the Flow. The Flow can then use this variable in decisions, screens, record queries, assignments, or other Flow elements.

  2. Prepare the Flow variables in the LWC.

    The lightning-flow component accepts input through its flow-input-variables property.

    The wrapper therefore needs to convert its public properties into the format expected by lightning-flow.

    For example:

    import { LightningElement, api } from 'lwc';
    
    export default class FlowContainer extends LightningElement {
        @api contactId;
        @api amount;
    
        get flowInputVariables() {
            return [
                {
                    name: 'contactId',
                    type: 'String',
                    value: this.contactId
                },
                {
                    name: 'amount',
                    type: 'Number',
                    value: Number(this.amount)
                }
            ];
        }
    }

    Each Flow input contains three pieces of information:

    • name identifies the variable in the Flow. It must correspond to the variable's API name.
    • type tells lightning-flow what type of Flow value is being supplied.
    • value contains the actual value that should be passed. In this case, it comes from the public LWC property.
  3. Provide the variables to `lightning-flow`.

    The component template connects the prepared variables to the Flow:

    <template>
        <lightning-flow
            flow-api-name="Donation_Flow"
            flow-input-variables={flowInputVariables}>
        </lightning-flow>
    </template>

Always keep in mind that parameter values are only passed when the Flow starts. The flow-input-variables element provides the initial values.

Changing a public property after the Flow has already started should not be treated as a way to update the corresponding Flow variable. If parameters need to determine Flow behavior, they should normally be available before the Flow is started.

Complete Screen Flow example

Using the procedure we laid out above, let's look at an end-to-end example.

The external website provides:

<c-flow-container
    contact-id="003XXXXXXXXXXXX"
    amount="50">
</c-flow-container>

The LWC JavaScript contains:

import { LightningElement, api } from 'lwc';

export default class FlowContainer extends LightningElement {
    @api contactId;
    @api amount;

    get flowInputVariables() {
        return [
            {
                name: 'contactId',
                type: 'String',
                value: this.contactId
            },
            {
                name: 'amount',
                type: 'Number',
                value: Number(this.amount)
            }
        ];
    }
}

The LWC template contains:

<template>
    <lightning-flow
        flow-api-name="Donation_Flow"
        flow-input-variables={flowInputVariables}>
    </lightning-flow>
</template>

Finally, Donation_Flow contains two variables: contactId(Next) and amount(Number).

The external website can now determine the values that the Screen Flow receives when it starts.

Standard HTML and ARIA attributes

Lightning Out 2.0 supports a specific subset of standard HTML and ARIA attributes. Support for an attribute means that Lightning Out allows the attribute on the component proxy. It doesn't necessarily mean that the attribute is propagated to or changes the behavior of controls rendered inside an embedded Screen Flow.

Supported standard HTML attributes are not handled as custom Salesforce parameters, so they do not need to be declared using @api. They provide standard browser information or behavior for the Lightning Out component.

HTML AttributeDescription
langDefines the language of the component's content, for example lang="en" or lang="nl". It can be used by browsers and assistive technologies (like screen reader) to correctly interpret the content.
dirDefines text direction. Typical values are ltr for languages such as English and Dutch and rtl for right-to-left languages such as Arabic.
titleProvides additional descriptive information about the element. Browsers may display it as a tooltip, but it shouldn't be used as the primary accessibility label.
spellcheckIndicates whether the browser should perform spelling and grammar checking for editable text. For example, spellcheck="false" can be useful around inputs containing identifiers or references rather than normal text.
autocapitalizeProvides a hint to mobile browsers about automatic capitalization during text entry. For example, autocapitalize="words" can be useful for names.
autocorrectControls automatic text correction on browsers and devices that support it. Setting autocorrect="off" can help prevent correction of identifiers, references, and similar values.
inputmodeSuggests which virtual keyboard a mobile device should display. For example, numeric, decimal, email, and tel can provide keyboards better suited to the expected input.
enterkeyhintSuggests how the Enter key should be presented on a virtual keyboard. Values such as next, done, search, and send help communicate the expected action.
translateIndicates whether browser translation tools should translate the element's content. translate="no" can be useful for product names, identifiers, and other text that should remain unchanged.

Lightning Out 2.0 also supports a subset of standard ARIA attributes. ARIA attributes provide accessibility information to assistive technologies such as screen readers.

ARIA AttributeDescription
aria-labelProvides an accessible name for the component when an appropriate visible label isn't available. For example, aria-label="Donation payment form" can help a screen reader identify the purpose of the component.
aria-hiddenIndicates that the component should be ignored by assistive technologies when set to true. It doesn't visually hide the component.
aria-disabledCommunicates to assistive technologies that the component is currently disabled or unavailable. It doesn't disable the component or prevent user interaction by itself.
aria-liveControls whether assistive technologies should announce changes to dynamically updated content. Values such as polite and assertive determine how urgently updates should be announced.
aria-modalIndicates that the component represents a modal interface. This only communicates the modal state to assistive technologies; it doesn't implement modal behavior.
aria-pressedSpecial-purpose attribute for toggle controls. Communicates whether a toggle-style button or control is currently pressed (true) or not pressed (false). It isn't intended for normal buttons or general component state.
aria-valuemin, aria-valuemax, aria-valuenowSpecial-purpose attributes for range-based controls, such as custom sliders, progress indicators, or spin buttons. They describe the minimum, maximum, and current numeric value to assistive technologies. They don't define, restrict, or validate the actual value and aren't intended for normal text or number inputs.