Sunday, February 20, 2022

How to use design attribute in LWC?

 In this blog post we will learn how we can use design attribute in LWC.

In aura component we use to have the design resource in the same folder as our .cmp resource.
Design resource controls which attributes are exposed to builder tools like the Lightning App Builder, Experience Builder, or Flow Builder. 

Now, the question is what if we want to use this with LWC? Is that possible?

Yes it is possible to we can use design attribute in LWC.

As we know LWC bundle contains an HTML file, a JavaScript file, and a metadata configuration file as shown in the below image and these files are created once we create a Lightning web component.

How to use design attribute in LWC?

A sample metadata configuration file is shown below. The configuration file defines the metadata values for the components including the design configuration which we will understand later with an example.

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="">




        <target>lightning__AppPage</target>  // Component available for app page.
        <target>lightning__RecordPage</target>  // Component available for record detail page.
        <target>lightning__HomePage</target> // Component available for app home page.


So now let's understand this with an example.

We need to define the property in component javascript class using @api decorator.



    <lightning-card title="Design Attribute Example"> 

        Card Name: {cardName}


        Card Description: {cardDescription}




import { LightningElement, api } from 'lwc';


export default class DesignAttributeExampleLwc extends LightningElement {

    @api cardName='I am card 1';

    @api cardDescription ='This is card 1 description';



<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="">










        <targetConfig targets="lightning__RecordPage,lightning__HomePage">

            <property label="Card Name" name="cardName" type="String"/>

            <property label="Card Name" name="cardDescription" type="String"/>





Now, Let's add this component to the account record detail page as shown below.

How to use design attribute in LWC?

No comments:

Post a Comment