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

shadowstyles

v0.0.2

Published

Isolate CSS similar to Shadow DOM

Downloads

11

Readme

Shadow Styles

Build Status

WebComponents bring Shadow DOM with isolated CSS to some browsers. This script simulates CSS isolation for all modern browsers (IE9+, Firefox...).

Installation

CommonJS applications may need to include the mutation-observer polyfill NPM package as well if they need to support browsers that don't suppport MutationObserver natively:

npm install mutation-observer shadowstyles

Or downoad the built file and manually add tag:

<script src="build/shadowStyles.min.js"></script>

document.shadowStyles()

Pass one argument: array, string, or element. String provides a selector to match new shadow root elements. Element provides a direct reference to a root element. Array can contain a combination of both.

// Example with selector string only
document.shadowStyles('x-foo');

// Example with array containing both types
document.shadowStyles([
  'x-foo', // All x-foo elements will have shadow styled children
  document.getElementById('something') // Specific element
]);

Targeting simulated shadows

Shadowed elements are given a shadow attribute that must be present in the selector:

shadowed-element[shadow] p { color: blue; }

Detecting native support

When using in conjunction with the Platform.js Shadow DOM polyfill, it can be useful to know whether the shadow DOM is emulated. Check the boolean constant:

document.shadowStyles.nativeSupport

Note: I'm considering wrapping the polyfilled createShadowRoot() to automatically isolate the shadow CSS, also allowing createShadowRoot(false) to skip style isolation. See test/mockup/webcomponent.html for implementation details.

How it works

  1. Search for child DOM of elements to isolate.
  2. Find which rules apply to those elements and add a :not(buffer-attr*="uniqueId") pseudo-class with a unique ID to each rule. Shadowed elements gain the buffer attribute containing the unique ID of each negated rule.

Why not ::shadow?

Originally, the plan of this project was to support styles using the native ::shadow pseudo-class syntax. Until the fifteenth commit, this worked, but only if all the page's stylesheets were rewritten completely. By only using selectors that the browser already supports, the script can skip parsing all of your CSS itself and rely on the browser's interpretation, only making small changes as needed. The final result is much smoother operation.

Still to do

  • Handle nested shadow DOM correctly

Build / Run Tests

# Clone repository to current directory
$ git clone https://github.com/numtel/shadowstyles.git
$ cd shadowstyles
$ npm install
# Install Grunt Client
$ npm install -g grunt-cli

# Run default operation to build minified file
$ grunt

# Running tests locally requires Selenium jar in repo
# http://selenium-release.storage.googleapis.com/index.html
# For example, 2.45.0:
$ wget http://selenium-release.storage.googleapis.com/2.45/selenium-server-standalone-2.45.0.jar
# If not running 2.45.0, update filename in GruntFile.js

# Test using grunt-nightwatch:
$ grunt nightwatch:local
# Or use default test command alias:
$ npm test

License

MIT