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

css-scopulation-enterprise-max-ai

v0.1.2

Published

Bring back the CSS scope property with a tiny esm module

Downloads

7

Readme

CSS Scopulation® Enterprise Max AI™

TL;DR

A tiny ES module with a ridiculously self-aggrandizing name for emulating the tasty but now unsupported scoped attribute for scoping embedded stylesheets to a local parent element.

Usage

Install CSS Scopulation® Enterprise Max AI™:

npm install --save css-scopulation-enterprise-max-ai

Import and execute scopeSheets.min.js:

import { scopeSheets } from 'css-scopulation-enterprise-max-ai';

scopeSheets({ revert: true });

Why?? How??

The CSS cascade is a thing and it is marvelous. But it is a shame that scoped CSS never took hold because it would have meant you could do this:

<div class="example">
  <style scoped>
    p {
      color: red;
    }
  </style>
  <p>I am well red innit.</p>
</div>
<p>I am not even red, like, at all.</p>

Since scoped isn’t supported in any browsers (any more) you have to use JavaScript to emulate this. The trouble is, most solutions are environment/library/framework specific, meaning you can’t use them unless you are using, say, React or Vue.

CSS Scopulation® Enterprise Max AI™ (Jesus Christ) is not only framework independent but, by foregoing CSS parsing in favor of simple token/keyword replacement, is ridiculously small (less than half a K). Honestly it’s just the most simple/naive/basic/little (apply whichever quasi-pejorative term you like) script, but it works.

And here’s how. In your page, you write something similar to the code example above:

<div class="example">
  <style scoped>
    § p {
      color: red;
    }
  </style>
  <p>I am well red innit.</p>
</div>
<p>I am not even red, like, at all.</p>

The only difference from that first example, as you may have noticed, is the § part which gets replaced in the output. The output looks like this:

<div class="example" data-scoped="0">
  <style scoped="">
    [data-scoped="0"] p {
      color: red;
    }
  </style>
  <p>I am well red innit.</p>
</div>
<p>I am not even red, like, at all.</p>

The § placeholder can be literally anything you like in the options but it has to be unique or the replacement part of the script will match unwanted parts of the styles and it will all go fucky.

Bleeding and seeping PREVENTED

Since lots of people also want that Shadow DOM kind of effect wherein styles are not just prevented from bleeding out but also seeping in, you can also switch reversion on. This just applies all: revert to the scoping element (class="example" above) and all its descendants.

<div class="example" data-scoped="0">
  <style scoped="">
    [data-scoped="0"], [data-scoped="0"] > * { 
      all: revert 
    } 
    [data-scoped="0"] p {
      color: red;
    }
  </style>
  <p>I am well red innit.</p>
</div>
<p>I am not even red, like, at all.</p>

Note that this will revert any styles applied directly to the elements in the scope but inheritance is not prevented (which is the same as how Shadow DOM works, see Why is my Web Component inheriting styles).

Options

These are passed to the scopeSheets function as an object (see Usage).

  • placeholder: the keyword/token representing the scoping element that is replaced with the generated attribute selector (String) (default: '§')
  • revert: Whether to revert styles with all: revert (Boolean) (default: false)
  • context: Where the script looks for <style scoped> (HTMLElement) (default: document)
  • dataName: What to call the data attribute (String) (default: 'scoped')

If you want my advice

If you want my advice, avoid running this on the client at all. Scope you styles during static compilation on the server, inside a JSDOM instance or something like that.

Notes

  1. I have been advised to point out that this ES module does not constitute a true form of Artificial Intelligence in any way and that my inclusion of the suffix “AI” should be treated with the same suspicion and contempt as any other product that conflates “artificial intelligence” with some kind of trivial code execution.
  2. If you take the 'S' away from 'Scopulation' you get copulation, which is nice.