| 1 | # PostCSS Cascade Layers [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
|
|---|
| 2 |
|
|---|
| 3 | [<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/postcss-cascade-layers.svg" height="20">][npm-url] [<img alt="CSS Standard Status" src="https://cssdb.org/images/badges/cascade-layers.svg" height="20">][css-url] [<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url] [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
|
|---|
| 4 |
|
|---|
| 5 | [PostCSS Cascade Layers] lets you use `@layer` following the [Cascade Layers Specification]. For more information on layers, checkout [A Complete Guide to CSS Cascade Layers] by Miriam Suzanne.
|
|---|
| 6 |
|
|---|
| 7 | ```pcss
|
|---|
| 8 |
|
|---|
| 9 | target {
|
|---|
| 10 | color: purple;
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | @layer {
|
|---|
| 14 | target {
|
|---|
| 15 | color: green;
|
|---|
| 16 | }
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | /* becomes */
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | target:not(#\#) {
|
|---|
| 24 | color: purple;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | target {
|
|---|
| 28 | color: green;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | ```
|
|---|
| 32 |
|
|---|
| 33 | ## How it works
|
|---|
| 34 |
|
|---|
| 35 | [PostCSS Cascade Layers] creates "layers" of specificity.
|
|---|
| 36 |
|
|---|
| 37 | It applies extra specificity on all your styles based on :
|
|---|
| 38 | - the most specific selector found
|
|---|
| 39 | - the order in which layers are defined
|
|---|
| 40 |
|
|---|
| 41 | ```css
|
|---|
| 42 | @layer A, B;
|
|---|
| 43 |
|
|---|
| 44 | @layer B {
|
|---|
| 45 | .a-less-specific-selector {
|
|---|
| 46 | /* styles */
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | @layer A {
|
|---|
| 51 | #something #very-specific {
|
|---|
| 52 | /* styles */
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | @layer C {
|
|---|
| 57 | .a-less-specific-selector {
|
|---|
| 58 | /* styles */
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 | ```
|
|---|
| 62 |
|
|---|
| 63 | most specific selector :
|
|---|
| 64 | - `#something #very-specific`
|
|---|
| 65 | - `[2, 0, 0]`
|
|---|
| 66 | - `2 + 1` -> `3` to ensure there is no overlap
|
|---|
| 67 |
|
|---|
| 68 | the order in which layers are defined :
|
|---|
| 69 | - `A`
|
|---|
| 70 | - `B`
|
|---|
| 71 | - `C`
|
|---|
| 72 |
|
|---|
| 73 | | layer | previous adjustment | specificity adjustment | selector |
|
|---|
| 74 | | ------ | ------ | ----------- | --- |
|
|---|
| 75 | | `A` | `0` | `0 + 0 = 0` | N/A |
|
|---|
| 76 | | `B` | `0` | `0 + 3 = 3` | `:not(#/#):not(#/#):not(#/#)` |
|
|---|
| 77 | | `C` | `3` | `3 + 3 = 6` | `:not(#/#):not(#/#):not(#/#):not(#/#):not(#/#):not(#/#)` |
|
|---|
| 78 |
|
|---|
| 79 | This approach lets more important (later) layers always override less important (earlier) layers.<br>
|
|---|
| 80 | And layers have enough room internally so that each selector works and overrides as expected.
|
|---|
| 81 |
|
|---|
| 82 | More layers with more specificity will cause longer `:not(...)` selectors to be generated.
|
|---|
| 83 |
|
|---|
| 84 | ⚠️ For this to work the plugin needs to analyze your entire stylesheet at once.<br>
|
|---|
| 85 | If you have different assets that are unaware of each other it will not work correctly as the analysis will be incorrect.
|
|---|
| 86 |
|
|---|
| 87 | ## Usage
|
|---|
| 88 |
|
|---|
| 89 | Add [PostCSS Cascade Layers] to your project:
|
|---|
| 90 |
|
|---|
| 91 | ```bash
|
|---|
| 92 | npm install postcss @csstools/postcss-cascade-layers --save-dev
|
|---|
| 93 | ```
|
|---|
| 94 |
|
|---|
| 95 | Use it as a [PostCSS] plugin:
|
|---|
| 96 |
|
|---|
| 97 | ```js
|
|---|
| 98 | const postcss = require('postcss');
|
|---|
| 99 | const postcssCascadeLayers = require('@csstools/postcss-cascade-layers');
|
|---|
| 100 |
|
|---|
| 101 | postcss([
|
|---|
| 102 | postcssCascadeLayers(/* pluginOptions */)
|
|---|
| 103 | ]).process(YOUR_CSS /*, processOptions */);
|
|---|
| 104 | ```
|
|---|
| 105 |
|
|---|
| 106 | [PostCSS Cascade Layers] runs in all Node environments, with special
|
|---|
| 107 | instructions for:
|
|---|
| 108 |
|
|---|
| 109 | | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
|
|---|
| 110 | | --- | --- | --- | --- | --- | --- |
|
|---|
| 111 |
|
|---|
| 112 | ## Options
|
|---|
| 113 |
|
|---|
| 114 | ### onRevertLayerKeyword
|
|---|
| 115 |
|
|---|
| 116 | The `onRevertLayerKeyword` option enables warnings if `revert-layer` is used.
|
|---|
| 117 | Transforming `revert-layer` for older browsers is not possible in this plugin.
|
|---|
| 118 |
|
|---|
| 119 | Defaults to `warn`
|
|---|
| 120 |
|
|---|
| 121 | ```js
|
|---|
| 122 | postcssCascadeLayers({ onRevertLayerKeyword: 'warn' }) // 'warn' | false
|
|---|
| 123 | ```
|
|---|
| 124 |
|
|---|
| 125 | ```pcss
|
|---|
| 126 | /* [postcss-cascade-layers]: handling "revert-layer" is unsupported by this plugin and will cause style differences between browser versions. */
|
|---|
| 127 | @layer {
|
|---|
| 128 | .foo {
|
|---|
| 129 | color: revert-layer;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | ```
|
|---|
| 133 |
|
|---|
| 134 | ### onConditionalRulesChangingLayerOrder
|
|---|
| 135 |
|
|---|
| 136 | The `onConditionalRulesChangingLayerOrder` option enables warnings if layers are declared in multiple different orders in conditional rules.
|
|---|
| 137 | Transforming these layers correctly for older browsers is not possible in this plugin.
|
|---|
| 138 |
|
|---|
| 139 | Defaults to `warn`
|
|---|
| 140 |
|
|---|
| 141 | ```js
|
|---|
| 142 | postcssCascadeLayers({ onConditionalRulesChangingLayerOrder: 'warn' }) // 'warn' | false
|
|---|
| 143 | ```
|
|---|
| 144 |
|
|---|
| 145 | ```pcss
|
|---|
| 146 | /* [postcss-cascade-layers]: handling different layer orders in conditional rules is unsupported by this plugin and will cause style differences between browser versions. */
|
|---|
| 147 | @media (min-width: 10px) {
|
|---|
| 148 | @layer B {
|
|---|
| 149 | .foo {
|
|---|
| 150 | color: red;
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | @layer A {
|
|---|
| 156 | .foo {
|
|---|
| 157 | color: pink;
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | @layer B {
|
|---|
| 162 | .foo {
|
|---|
| 163 | color: red;
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 | ```
|
|---|
| 167 |
|
|---|
| 168 | ### onImportLayerRule
|
|---|
| 169 |
|
|---|
| 170 | The `@import` at-rule can also be used with cascade layers, specifically to create a new layer like so:
|
|---|
| 171 | ```css
|
|---|
| 172 | @import 'theme.css' layer(utilities);
|
|---|
| 173 | ```
|
|---|
| 174 | If your CSS uses `@import` with layers, you will also need the [postcss-import] plugin. This plugin alone will not handle the `@import` at-rule.
|
|---|
| 175 |
|
|---|
| 176 | This plugin will warn you when it detects that [postcss-import] did not transform`@import` at-rules.
|
|---|
| 177 |
|
|---|
| 178 | ```js
|
|---|
| 179 | postcssCascadeLayers({ onImportLayerRule: 'warn' }) // 'warn' | false
|
|---|
| 180 | ```
|
|---|
| 181 |
|
|---|
| 182 | ### Contributors
|
|---|
| 183 | The contributors to this plugin were [Olu Niyi-Awosusi] and [Sana Javed] from [Oddbird] and Romain Menke.
|
|---|
| 184 |
|
|---|
| 185 | [cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
|
|---|
| 186 | [css-url]: https://cssdb.org/#cascade-layers
|
|---|
| 187 | [discord]: https://discord.gg/bUadyRwkJS
|
|---|
| 188 | [npm-url]: https://www.npmjs.com/package/@csstools/postcss-cascade-layers
|
|---|
| 189 |
|
|---|
| 190 | [Gulp PostCSS]: https://github.com/postcss/gulp-postcss
|
|---|
| 191 | [Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
|
|---|
| 192 | [PostCSS]: https://github.com/postcss/postcss
|
|---|
| 193 | [PostCSS Loader]: https://github.com/postcss/postcss-loader
|
|---|
| 194 | [PostCSS Cascade Layers]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-cascade-layers
|
|---|
| 195 | [Cascade Layers Specification]: https://www.w3.org/TR/css-cascade-5/#layering
|
|---|
| 196 | [A Complete Guide to CSS Cascade Layers]: https://css-tricks.com/css-cascade-layers/
|
|---|
| 197 | [Olu Niyi-Awosusi]: https://github.com/oluoluoxenfree
|
|---|
| 198 | [Sana Javed]: https://github.com/sanajaved7
|
|---|
| 199 | [Oddbird]: https://github.com/oddbird
|
|---|
| 200 | [postcss-import]: https://github.com/postcss/postcss-import
|
|---|