question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

add option to disable / opt-out of Shadow DOM to support global CSS libraries

See original GitHub issue

Feature Request

Problem to solve

As great as Shadow DOM in Web Components is, for projects supporting and / or trying to migrate away from global CSS, they may need to keep global CSS in lieu of encapsulating all styles.

How this relates to this project is that the custom element provided by this library has it enabled, which means for all apps using this library but also trying to use libraries like Bootstrap or Font Awesome (like me 😅 ), this library will block out all those global styles within the route. (which is the expected behavior, naturally).

(screenshots and more details at the end 👇 )

Possible implementation

Lit provides a way to disable this from the LitElement base class by using createRenderRoot

export class MyComponent extends LitElement {
  ...
  
  createRenderRoot() {
    return this;
  }
}

Thinking of this from a user of this library’s perspective, I think it would look something like this

<lit-route
  path="/"
  component="as-route-home"
  shadow="false"
  .resolve="${() => import('/routes/home/home.js')}">
</lit-route>

Not specifically sure how that would be implemented within this project though on the fly though?

export class LitRoute extends LitElement {
  ...

  static get properties() {
    return {
      shadow: {
        type: Boolean
      }
    };
  }

  constructor() {
    super();
    this.shadow = true;
  }

  createRenderRoot() {
    return this.shadow ? this.shadowRoot : this;
  }
}

(Lit doesn’t specify what the default would be, I am assuming it is this.shadowRoot, but i can certainly ask on their issue tracker if it helps)

Alternatives considered

Technically there is one way to work around this, which is to just literally inline / include all that global CSS into the Shadow DOM for each custom element. Unfortunately, this means you’ll basically be duplicating all that CSS over and over, which is terrible for bundle size and user experience.

Additional context

You can check out my work in progress in this PR trying to to make all the CSS global, with the following observations:

  • As a test, the header includes an icon from Font Awesome, which displays when the header is NOT in a route, and is not visible when the header IS in a route. Thus <LitRoute> is acting as a Shadow “boundary” preventing FA and Bootstrap from getting through to all the child components.
  • The difference in bundle size when not needing to inline all the duplicate global CSS is pretty significant: going from 1.3 MB to 289 KB! Screen Shot 2021-12-24 at 5 41 01 PM

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
stale[bot]commented, Feb 12, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

1reaction
fernandopasikcommented, Jan 27, 2022

Analyzing this a bit further I see a complication with a use where you provide the route content through the slot , since that is not available in light dom.

I think the only way to fulfill this feature without loosing other will be with an option in the lit-route element

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove all global css pollution #6032 - facebook/docusaurus
I think the easiest solution is to provide an OOTB Sandbox component which is basically a shadow DOM? A reset utility class can...
Read more >
Style an element's shadow DOM - Polymer Project
Polymer supports DOM templating and the shadow DOM API. ... Individual elements can opt-out of the global direction by setting the dir attribute...
Read more >
Scoping CSS using Shadow DOM - Bits and Pieces
A mechanism to stop overriding your CSS styles using Shadow DOM. As a frontend developer working with a large team, one major hassle...
Read more >
Build Components in Mixed Shadow Mode (Beta)
While apps rely on global style sheets to apply styles in synthetic shadow DOM, a component's styles are added to the component bundle...
Read more >
Using shadow DOM - Web Components | MDN
The Shadow DOM API is a key part of this, providing a way to attach a hidden separated DOM to an element. This...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found