theKashey / used-styles

πŸ“All the critical styles you've used to render a page.
MIT License
138 stars 10 forks source link

CSS Cascade Layers support #50

Closed AlexandrHoroshih closed 1 year ago

AlexandrHoroshih commented 1 year ago

Relates to #26

Hello and thank you for the awesome library! πŸ‘‹

Since CSS Cascade Layers support is gettng better in the browsers, i was wondering if there any plans to support this feature?

Right now it seems to be not supported: I wrote some snapshot tests and current version of used-styles just adds all @layer-related stuff into the critiical styles without considering, if any of styles in that layer are actually used

So it looks like that some special handling, like it is done for @media rules, is needed πŸ€”

There is a polyfill for CSS Layers and it does work much better with used-styles, but since this feature is getting wider support in the browsers (90% according to caniuse), i think, it is a good time to introduce a native support

CSS Layers are pretty cool, since by using it

Authors can create layers to represent element defaults, third-party libraries, themes, components, overrides, and other styling concerns – and are able to re-order the cascade of layers in an explicit way, without altering selectors or specificity within each layer, or relying on source-order to resolve conflicts across layers

which is basically a native CSS solution to issue #26

What do you think?

AlexandrHoroshih commented 1 year ago

I see a possible way to support CSS Layers as follows:

  1. layer order definitions are treated the same way as tag-based selectors (html, body, etc.), so things like @layer theme, layout, utilities; are always included in critical css
  2. "Layered" styles ( @layer theme { ... }) are handled the same way @media is handled now

If PR with this feature would be welcome, I'd be happy to work on it πŸ™‚

theKashey commented 1 year ago

Absolutely correct

If PR with this feature would be welcome, I'd be happy to work on it πŸ™‚

Yes please, my capacity is quite limited now. I even answering to issues a couple of days late 😿