postcss-at-scope
v1.2.3
Published
PostCSS plugin @scope polyfill
Downloads
468
Maintainers
Readme
PostCSS @scope polyfill
PostCSS plugin to partially polyfill the experimental @scope at-rule.
Installation
npm install --save-dev postcss-at-scopepostcss (>= 8.4.27) is a peer dependency, so make sure it is installed too.
Usage
Add the plugin to your PostCSS configuration:
// postcss.config.js
module.exports = {
plugins: [
require("postcss-at-scope")({ depth: 10 }),
],
};Then author scoped styles with the native @scope
syntax and the plugin rewrites them into plain, widely-supported selectors:
@scope (.card) to (.card-embed) {
p {
color: rebeccapurple;
}
}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.
Advanced & and :scope selectors
The subject of a scoped selector must be a descendant-or-self of the scope root, and every &/:scope refers to the single scoping-root element. Selectors that violate this are unsatisfiable and correctly resolve to nothing, matching native behavior:
- Referencing the scope root in more than one compound —
& &,:scope :scope,& .middle :scope span— would require one element to be its own ancestor. - Pushing the subject out of the scope root's subtree with a sibling combinator right after the root —
& ~ div,:scope + div— makes the subject a sibling of the root rather than a descendant.
An unsupported or empty @scope prelude (for example a bare @scope { … }, which has no explicit scope root) is left unchanged and reported as a PostCSS warning.
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;
}
}
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 */
}),
],
};depthnumber: maximum DOM tree depth to support scoping. Default value:10. Values are clamped to a positive integer.
