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.

Adopted stylesheets support

See original GitHub issue

It would be useful if we could target a constructed stylesheet which we can then use via shadowRoot.adoptedStyleSheets = [twindSheet].

This would allow us to create the stylesheet once and re-use it across multiple web components without having the side effect of polluting the root document’s styles.

I’ve had a read through the source and it seems the current “sheet” implementation is using CSSOM to add rules to a stylesheet already. However, its default target is a style tag appended to the DOM.

So maybe its as simple as having a different target in the existing cssom sheet? A reference to a sheet rather than one we have in DOM.

Though then the question is, how do we get hold of that per web component?

class SomeElement extends HTMLElement {
  // ...

  connectedCallback() {
    // how do we get hold of this sheet?
    this.shadowRoot.adoptedStyleSheets = [REF_TO_TWIND_SHEET];
  }

  // Assume this just ends up appending to this.shadowRoot
  render() {
    return `<div class="${tw`bg-black`}">Foo</div>`;
  }
}

As you can see, we’d probably need to create the stylesheet ourselves and tell twind to append to it. Like during setup() of twind.

Though that leaves us with a choice to make too:

  • a monolithic global twind sheet (i.e. we can only setup() once, which means we can only have one tailwind stylesheet to contain everything)
  • somehow per-module tell twind which sheet to use (like a setup() per es module)

Could do with some christmas coding so would be happy to have a go at this once i get my head around it

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
sastancommented, Dec 24, 2020

Thanks. That means a lot. Closing it now.

Feel free to reopen if you something is not working as expected.

1reaction
43081jcommented, Dec 24, 2020

I guess it means in your example, a stylesheet would exist per component. Whereas if we reused the same one across modules, we could have one monolithic TW stylesheet many components can adopt.

Not sure if either way is better than the other. This way we keep each component contained at least, so each one has its own tw stylesheet. Which can mean some duplication but less risk of side effects etc

Guess I’m saying i like your way better 😂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Document.adoptedStyleSheets - Web APIs | MDN
The adoptedStyleSheets property of the Document interface is used for setting an array of constructed stylesheets to be used by the document ...
Read more >
Constructable Stylesheets - web.dev
Constructable Stylesheets are a way to create and distribute reusable styles when using Shadow DOM. Browser support: chrome 73, Supported 73 firefox 101, ......
Read more >
"adoptedStyleSheets" | Can I use... Support tables for HTML5 ...
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Read more >
Why Would Anyone Use Constructible Stylesheets, Anyways?
DISCLAIMER: The following article contains examples that rely on APIs that are no longer so new and are no1 supported in both Chrome...
Read more >
Constructable Stylesheets - Smart UI Components
Constructable StyleSheets are very useful, when we use Shadow DOM. There is a new property called adoptedStyleSheets, which is available on Shadow Roots...
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