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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@oy3o/css-scope-polyfill

v1.2.0

Published

A lightweight, zero-dependency polyfill for the CSS @scope at-rule with MutationObserver support.

Readme

CSS @scope Polyfill

Bring the future of CSS encapsulation to today's browsers.
A lightweight, zero-dependency runtime polyfill for the CSS @scope at-rule.

npm version license size

Why this?

The CSS @scope rule is a game-changer for component architecture, allowing you to select elements specifically within a DOM subtree. However, browser support is still catching up.

This polyfill bridges the gap. It observes your DOM, parses your CSS, and applies the scoping logic dynamically—preserving your ability to write modern, clean CSS today.

Features

  • Native Syntax Support: Writes exactly like native CSS. No custom classes required.
  • Live DOM Monitoring: Automatically detects new <style> tags or lazy-loaded components via MutationObserver.
  • Intelligent Scoping:
    • Supports :scope pseudo-class.
    • Supports & nesting selectors.
    • Handles complex selectors like :is(), :where(), and :not() correctly.
  • Dual-Mode Architecture:
    • Zero-Config: Drop it in via CDN, and it just works.
    • Framework-Ready: Pure ESM export with Dependency Injection support for advanced integration.

Installation

npm install @oy3o/css-scope-polyfill

Or use your favorite package manager (pnpm, yarn).


Usage

1. The "Drop-in" Mode (CDN)

Best for static sites or quick prototypes.

Simply add the script to your <head>. It will automatically scan for @scope rules and watch for changes.

<!-- Via unpkg -->
<script src="https://unpkg.com/@oy3o/css-scope-polyfill"></script>

<!-- OR via jsdelivr -->
<script src="https://cdn.jsdelivr.net/npm/@oy3o/css-scope-polyfill"></script>

Manual Control (Optional)

If you want to load the script but prevent it from running automatically (e.g., to configure it later), add the data-manual attribute:

<script src="..." data-manual></script>
<script>
  // Start it manually when you are ready
  window.CSSScopePolyfill();
</script>

2. The "Architect" Mode (ESM / Frameworks)

Best for React, Vue, Vite, Webpack, or internal frameworks.

Import the pure logic. It does not start automatically, giving you full control over the lifecycle.

import ScopePolyfill from '@oy3o/css-scope-polyfill';

// Basic usage: Start with default MutationObserver
ScopePolyfill();

Advanced: Dependency Injection (BYO Observer)

If your framework already has an event system (like a global EventBus or a virtual DOM patcher), you can inject a custom watcher to avoid running a duplicate MutationObserver.

import ScopePolyfill from '@oy3o/css-scope-polyfill';
import { events } from './my-framework';

/**
 * @typedef {object} EventSystem
 * @property {(callback: (mutations: {added: Set<Element>, removed: Set<Element>}) => void) => void} onMutation
 */

ScopePolyfill(events);

Supported Syntax

Basic Scoping

/* Input */
@scope (.card) {
    :scope {
        border: 1px solid red; /* Applies to .card itself */
    }
    
    img {
        border-radius: 50%; /* Only applies to img INSIDE .card */
    }
}

Nesting & Pseudo-classes

/* Input */
@scope (.sidebar) {
    .tab {
        background: #ccc;
        
        /* Nesting support */
        &:hover {
            background: #fff;
        }
    }
    
    /* Complex selectors work too */
    :scope > .content :is(h1, h2) {
        color: blue;
    }
}

Limitations & Trade-offs (Red Team Analysis)

  1. CORS (Cross-Origin Resource Sharing): Since the polyfill must fetch external CSS files to parse them, <link href="..."> pointing to a different domain must serve correct CORS headers (Access-Control-Allow-Origin: *).
  2. Flash of Unstyled Content (FOUC): As a runtime polyfill, there is a slight delay between the CSS loading and the polyfill rewriting the rules. For critical path CSS, consider server-side transformation if possible.
  3. Specific Syntax: Currently supports the "Root Scoping" syntax (@scope (.root) { ... }). Support for the "Donut Scoping" (lower boundary to (...)) is experimental.

License

MIT © oy3o