Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Declarative Shadow DOM Style Sharing] consider compatibility between declarative and imperative use #890

Open
sorvell opened this issue Oct 10, 2024 · 2 comments

Comments

@sorvell
Copy link

sorvell commented Oct 10, 2024

It's common for users to create Shadow DOM imperatively in custom elements. Libraries can then server side render SSR the result into declarative Shadow DOM that later upgrades to a loaded custom element. The solution here should integrate well with both approaches.

Using the module graph for this is a good start, so I want to confirm that the following would work:

<script type="css-module" indentifier="/styles.css">
  :host { display: block; background: green; }
</script>
<div>
  <template shadowrootmode="open" adoptedstylesheets="./styles.css">Hi</template>
</div>

<x-foo></x-foo>
<script type="module">
  import sheet from './styles.css' with { type: 'css' };
  import otherSheet from './load-styles.css' with { type: 'css' };
  customElements.define('x-foo', class extends HTMLElement {
    constructor() {
      super();
      this.attachShadow({mode: 'open'}).innerHTML = 'hi';
      this.shadowRoot.adoptedStyleSheets.push(sheet, otherSheet);
    }
  });
</script>

<div>
  <template shadowrootmode="open" adoptedstylesheets="./load-styles.css">Hi</template>
</div>
@KurtCattiSchmidt
Copy link
Contributor

Yes, based on our current proposal, this should work. One of the goals of using the module graph is that imperative and declarative should be able to interact like this.

@jpzwarte
Copy link

jpzwarte commented Oct 19, 2024

Some questions to the above example:

  • So the relative path in the example is relative to what? The current URL? What if you have a SPA and you reload the page on a child route?
  • What if you have a hash-based SPA?
  • What if you have a custom element that is used on different pages/URLs?
  • What if you have a combination of compiled custom elements and build-less CEs and both use the same stylesheet?

Do import maps perhaps mitigate some of these issues by mapping different URLs to the same stylesheet?

Edited: Is it possible to use import maps with this? Since there isn't an actual CSS file that can be downloaded by the browser?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants