MicrosoftEdge / MSEdgeExplainers

Home for explainer documents originated by the Microsoft Edge team
Creative Commons Attribution 4.0 International
1.31k stars 213 forks source link

[Declarative Shadow DOM Style Sharing] locator instead of specifier? #887

Open jsebrech opened 1 month ago

jsebrech commented 1 month ago

The proposal uses CSS module specifiers as a mechanism to identify CSS modules which are inlined into the page and which can then be referred to from the adoptedstylesheets attribute. This mechanism only works by using both attributes in combination.

Instead of exporting the module to the page as a CSS module specifier, what if it was exported using a locator (URL)?

<script type="css-module" locator="/foo.css"> 
  #content { 
    color: red; 
  } 
</script>

This would then make this CSS module exist as if it was loaded from the locator (URL). In other words, it inlines a networked resource and announces the inlined resource to the page by its locator. The resource would behave as if it was loaded with a GET with response header Cache-Control: no-store, not affecting the browser's cache, and would only exist for the lifetime of the document. Any reference to that URL would load the CSS module that was provided inline:

<my-element>  
  <template shadowrootmode="open">  
     <link rel="stylesheet" href="/foo.css" />
     ...
  </template>  
</my-element>

Benefits:

Downsides:

niutech commented 1 month ago

I think putting bare CSS rules inside <script> is very confusing for developers as well as IDEs providing syntax highlighting. I'd suggest instead putting inline CSS styles inside <style id="xxx"> and referencing them using <link rel="stylesheet" href="#xxx"> inside <template shadowrootmode="open">.