npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

postcss-at-scope

v1.2.0

Published

PostCSS plugin @scope polyfill

Downloads

79

Readme

PostCSS @scope polyfill

PostCSS plugin to partially polyfill experimental @scope at-rule.

The idea

If we consider limited depth of DOM tree then we can polyfill some parts of styles scoping specification. For example this selector in scoping rule

@scope (.scoping-root) to (.scoping-limit) {
  a {
    color: rebeccapurple;
  }
}

can be rewritten to list of selectors

.scoping-root > a:not(.scoping-limit),
.scoping-root > :not(.scoping-limit) > a:not(.scoping-limit),
.scoping-root
  > :not(.scoping-limit)
  > :not(.scoping-limit)
  > a:not(.scoping-limit) {
  color: rebeccapurple;
}

And to preserve original specificity of selector we cen wrap it in :where() pseudo class:

a:where(
    .scoping-root > a:not(.scoping-limit),
    .scoping-root > :not(.scoping-limit) > a:not(.scoping-limit),
    .scoping-root
      > :not(.scoping-limit)
      > :not(.scoping-limit)
      > a:not(.scoping-limit)
  ) {
  color: rebeccapurple;
}

Limitations

DOM depth

Main limitation of this polyfill is that it will not work for unlimited depth of DOM tree (or to be more specific the distance from scoping root to matched element). For example above result will look like this:

<div class="scoping-root">
  <a>in the scope</a>
  <a class="scoping-limit">not in the scope</a>
  <div>
    <div>
      <div>
        <a>in the scope but not matched because of depth limitation</a>
      </div>
    </div>
  </div>
</div>

This should't be a major problem for application based on components. Usually we would limit scoping so that it wouldn't match html elements inside inner components. For example

function SomeComponent(props) {
  return (
    <div className="scoping-root">
      <a href="#">in the scope</a>
      <div className="scoping-limit">
        <SomeOtherComponent />
      </div>
    </div>
  );
}

Scope proximity

Scope Proximity could probably be polyfilled using Style Queries. The problem is that browser support for style queries is even more limited the support for native scoping so for now this is not supported by this plugin.

Performance

Size of output CSS

Size of output css can grow really high with increasing value of depth option so this should be taken into consideration. On the other hand beacause of repetitiveness in the output code it compresses extremely well. Here are some examples for .css file conting simple example from this README:

@scope (.scoping-root) to (.scoping-limit) {
  a {
    color: rebeccapurple;
  }
}

Impact of depth option on size of output file and level of compression

Note: This requires more "real life" examples to test

Style calculation

Using this polyfill instead of native scoping can increase style recalculation time. This may differ between browser. Polyfilled code extensively uses child combinator (>) and this should help browsers to "fast reject" during element matching but nevertheless this problem requires more testing and deep diving into it.

PostCSS Options

module.exports = {
  plugins: [
    require("postcss-at-scope")({
      /* options */
    }),
  ],
};
  • depth number: maximum DOM tree depth to support scoping. Default value: 10.