Skip to main content
Group

LWC Patterns

A place to share best practices and patterns for developing LWC components and applications.

Context with inheritance of static components

Let's suppose we have a parent LWC component with .js, .html and .css parts.

Let's also support we have a child LWC component which extends the parent component.

 

To display the child component :

  • if the child only has a .js part, then the HTML and CSS of the parent are used,
  • if the child also has an HTML part, it is used instead of the parent HTML part,
  • if the child also has a CSS part, it is used instead of the parent CSS part.

Context extended to the inheritance of dynamic components

Let's now suppose that the parent component is generated dynamically.

So the HTML part of the parent is simply :

<template>  <div lwc:dom="manual"></div></template>

This is OK and works well : the CSS of the parent decorates the component generated dynamically in the div.

 

If the child wants to resuse the dynamic generation of the parent, it might look like this (in my case) :

<!-- child.html --><template>  <lightning-card title={title} icon-name="custom:custom14">    <div lwc:dom="manual">      <!-- This is populated by the parent -->    </div>  </lightning-card></template>

So, once again, this works well : when displaying the child component, the parent's HTML is not used, but the parent JS generates the dynamic component in the child's HTML's div above. Once again, the parent's CSS decorates the dynamically generated component displayed in the child.

 

Problem

If I now add a child.css to decorate the lightning-card of the child, my child now has a CSS part, and the parent's CSS part is ignored : unfortunately, the dynamically generated component in the child's HTML is no longer decorated by the parent's CSS which is ignored.

 

Questions

  1. How can I resolve this problem without breaking encapsulation, which supposes that the developer of the child component does not know anything about the way the parent component is implemented (especially its CSS part, in our case) ?
  2. Is there a solution to this problem which also works if the parent component is part of a managed 2GP package, and the child is unpackaged ?

cc @Greg Whitworth

cc @LWC

4 answers
  1. Mar 14, 2024, 4:06 PM

    The child is not dynamically generated.

    The child is a static class, which inherits from the parent. The parent generates a dynamic LWC component. The parent has its own CSS. If I add a child.css to the child, the parent.css is no longer used.

    NB : this is about inheritance. Child is inherited from parent.

0/9000
0/9000
0/9000
0/9000

I have a set of small packages using some shared LWC headless components (Javascript-only). I cannot find the right syntax to get my parent LWC component in package A to refer to child component in package B : it will not compile and not deploy to my scratch org, although package B is installed.

 

Questions :

  1. I have found no way of declaring child LWC as "public" in package B; I suppose all is public since the package is unlocked.
  2. I have found nothing about how to adapt jsconfig.json in package A to refer to child in package B
  3. Is it possible to share LWC components between (unlocked) packages ? If so, how ?
  4. Do the answers to these questions differ when the unlocked package has a namespace or when it does not have one ?

#LWC Component

 

cc @LWC @LWC Patterns @Lightning Web Components

7 answers
0/9000

Hello everyone,  I've created a custom rest service (APEX class), which works as expected.  Until our client application, which will use this custom rest service in future, is completed, I'm asked  to provide a LWC button inside our Salesforce instance to use this rest service and copies the rest service json to the clipboard as temporarely solution.  Has anyone of this group did this in the past and can give me an advice how to do call the rest service from LWCs?  Thank you in advance.  Best regards, Michael @LWC@lwc developer group@LWC Patterns

4 answers
  1. Ines Garcia (get: Agile) Forum Ambassador
    Oct 14, 2022, 11:11 AM

    Fab Michael, thansk 

    Let us know how you get on

0/9000

Dynamic Interaction Between Two Lightning Web Components  

Many times we have requirements to update or show detail on other components based on the action of the first component. Like we can have one component which will show a list of accounts and on selection, we will show the address of that account on a map. This post will explain to create a dynamic interaction between two  Lightning Web Components.

 

Check out post Dynamic Interaction Between Two Lightning Web Components to get step-by-step detail.

 

Dynamic Interaction Between Two Lightning Web Components Many times we have requirements to update or show detail on other components based on the action of the first component.

 

@Lightning Web Components @LWC @LWC Patterns

#Salesforce Developer #LWC #Custom LWC #Salesforce

0/9000
0/9000