All Projects → samthor → scoped

samthor / scoped

Licence: Apache-2.0 License
Scoped CSS polyfill

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to scoped

scoper
A polyfill for scoped HTML styles elements
Stars: ✭ 79 (+17.91%)
Mutual labels:  polyfill, scoped
Eqcss
EQCSS is a CSS Reprocessor that introduces Element Queries, Scoped CSS, a Parent selector, and responsive JavaScript to all browsers IE8 and up
Stars: ✭ 1,513 (+2158.21%)
Mutual labels:  polyfill, scoped-css
iframe-worker
A tiny WebWorker polyfill for the file:// protocol
Stars: ✭ 23 (-65.67%)
Mutual labels:  polyfill
o9n
🖥 A screen.orientation ponyfill
Stars: ✭ 55 (-17.91%)
Mutual labels:  polyfill
react-native-wasm
A polyfill to use WebAssembly in React Native.
Stars: ✭ 39 (-41.79%)
Mutual labels:  polyfill
weakmap-polyfill
ECMAScript6 WeakMap polyfill
Stars: ✭ 25 (-62.69%)
Mutual labels:  polyfill
github-wc-polyfill
Ensure that all GitHub and GitLab scripts required for UXP and SeaMonkey are loaded correctly
Stars: ✭ 87 (+29.85%)
Mutual labels:  polyfill
parcel-plugin-goodie-bag
provides the Promise and fetch goodies needed for IE(11) support w/ parcel bundle loading
Stars: ✭ 15 (-77.61%)
Mutual labels:  polyfill
audioworklet-polyfill
strictly unofficial polyfill for Web Audio API AudioWorklet
Stars: ✭ 49 (-26.87%)
Mutual labels:  polyfill
webpack2-polyfill-plugin
Insert polyfills (such as Promise) for Webpack 2
Stars: ✭ 18 (-73.13%)
Mutual labels:  polyfill
appHistory
A polyfill for the AppHistory proposal
Stars: ✭ 21 (-68.66%)
Mutual labels:  polyfill
require-polyfill
Make `require` work in browsers, at runtime. No code bundling needed!
Stars: ✭ 37 (-44.78%)
Mutual labels:  polyfill
symfony-tools
Collection of polyfill (backport) and incubator features for Symfony 3
Stars: ✭ 19 (-71.64%)
Mutual labels:  polyfill
NetStandardPolyfills
Type and Reflection polyfill extension methods. .NET 3.5+ and .NET Standard 1.0+.
Stars: ✭ 22 (-67.16%)
Mutual labels:  polyfill
polyfill-php81
This component provides functions unavailable in releases prior to PHP 8.1.
Stars: ✭ 618 (+822.39%)
Mutual labels:  polyfill
async generator
Making it easy to write async iterators in Python 3.5
Stars: ✭ 87 (+29.85%)
Mutual labels:  polyfill
fetch
A fetch API polyfill for React Native with text streaming support.
Stars: ✭ 27 (-59.7%)
Mutual labels:  polyfill
Javascript-Interview-Preparation
A curated collection of javascript interview questions & solutions.
Stars: ✭ 163 (+143.28%)
Mutual labels:  polyfill
object-keys
Object.keys shim
Stars: ✭ 41 (-38.81%)
Mutual labels:  polyfill
mini-create-react-context
(A smaller) polyfill for the react context API
Stars: ✭ 34 (-49.25%)
Mutual labels:  polyfill

This is a polyfill for scoped CSS, also known as <style scoped>, a feature that is useful for building simple components (but which was removed from the HTML specification around 2016). Check out a small demo.

Usage

Include the polyfill (via your favourite package manager as style-scoped) and then include a <style scoped> tag:

<script src="node_modules/style-scoped/scoped.min.js"></script>
<div>
  <style scoped>
    h1 { color: red; }
  </style>
  <h1>I'm red</h1>
</div>
<h1>I'm not</h1>

This can help you provide CSS encapsulation for your projects. The library is about ~1.9k minified and gzipped. You need to include it on your own domain, as it does odd changes that are seen as security problems if loaded cross-origin.

Supports

The polyfill supports Firefox, Safari and Chromium-based browsers (and should support IE11 and above). (It requires browsers with WeakMap and MutationObserver).

The code works by applying a custom attribute name, e.g. __scoped_123, to the parent element of the <style scoped> tag, and modifying the style rules to have this as a prefix. If the scoped CSS changes (via .textContent), or its node is moved etc, the style rules are reevaluated.

Config

If window.scopedCSS is defined before the polyfill is loaded, it accepts two options:

  • scopedCSS.applyToClass (default false): if true, the polyfill will use a class, not an attribute
  • scopedCSS.prefix: a custom prefix for the attribute or class name set on the scope

For example:

<script>
  window.scopedCSS = {applyToClass: true, prefix: '_some_custom_name'};
</script>
<script type="module">
import './node_modules/style-scoped/scoped.min.js';
// or maybe
import 'style-scoped';
</script>

Advanced

As well as scoping regular CSS rules, the polyfill also rewrite :scope. This refers to the parent of the <style scoped> tag, e.g.:

<div><!-- just this div will have a blue background -->
  <style scoped>
    :scope {
      background: blue;
    }
  </style>
</div>

Note that :scope is supported by most modern browsers—but without <style scoped> support, it will match the HTML element and is the same as :root.

⚠️ Rules which use :scope inside another selector (e.g. :is(div:scope)) are not currently supported and will be cleared. If this is actually something you need, I will eat my hat. 🎩

Notes

  • The polyfill doesn't operate on all CSS rules: e.g., @keyframes, @font are ignored
  • If you depend on cross-domain CSS via @import, this is loaded dynamically with an XHR: so it may take a little while to arrive (see background)

Release

Compile code with npm run build.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].