|
| 1 | +# postcss-assign-layer <!-- omit in toc --> |
| 2 | + |
| 3 | +[PostCSS] plugin to assign a css cascade layer to files based on a glob pattern |
| 4 | + |
| 5 | +[postcss]: https://github.com/postcss/postcss |
| 6 | + |
| 7 | +## Table of Contents <!-- omit in toc --> |
| 8 | + |
| 9 | +- [Why?](#why) |
| 10 | +- [Usage](#usage) |
| 11 | + - [**Step 1:** Install plugin](#step-1-install-plugin) |
| 12 | + - [**Step 2:** Check your project for existing PostCSS config:](#step-2-check-your-project-for-existing-postcss-config) |
| 13 | + - [**Step 3:** Add the plugin to plugins list (at the end)](#step-3-add-the-plugin-to-plugins-list-at-the-end) |
| 14 | + - [**Step 4:** Configure the plugin](#step-4-configure-the-plugin) |
| 15 | + - [`include`](#include) |
| 16 | + - [`layerName`](#layername) |
| 17 | + |
| 18 | +## Why? |
| 19 | + |
| 20 | +[CSS Cascade Layers] are a powerful new tool to give us more control over the cascade. [A Complete Guide to CSS Cascade Layers] is an excellent introduction that goes in depth into the details of how it works and the reasons you might want to use it. |
| 21 | + |
| 22 | +One useful approach is to create different layers for defaults, components, and utilities, with increasing order of priority. So, components will always override a selector with the same specificity in the defaults layer, but utilities will override components. Defaults and utilities can be easily assigned up-front, in your global stylesheets imported at the root of your applicaiton. But you might have separate css files spread over your app for your components, especially if you're using an approach like [css modules]. |
| 23 | + |
| 24 | +That's where this plugin comes in. By default, it will wrap the contents of each `*.module.css` file into a `components` layer (both the glob and the name of the layer can be changed). So, for example, given a css module file like: |
| 25 | + |
| 26 | +```css |
| 27 | +/* component.module.css */ |
| 28 | + |
| 29 | +.foo { |
| 30 | + color: rebeccapurple; |
| 31 | +} |
| 32 | + |
| 33 | +.bar { |
| 34 | + color: blanchedalmond; |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +The end result will be: |
| 39 | + |
| 40 | +```css |
| 41 | +/* component.module.css */ |
| 42 | + |
| 43 | +@layer components { |
| 44 | + .foo { |
| 45 | + color: rebeccapurple; |
| 46 | + } |
| 47 | + |
| 48 | + .bar { |
| 49 | + color: blanchedalmond; |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +Now, you can be assured that your component css will be added to the correct layer! |
| 55 | + |
| 56 | +## Usage |
| 57 | + |
| 58 | +### **Step 1:** Install plugin |
| 59 | + |
| 60 | +```sh |
| 61 | +npm install --save-dev postcss postcss-assign-layer |
| 62 | +``` |
| 63 | + |
| 64 | +### **Step 2:** Check your project for existing PostCSS config: |
| 65 | + |
| 66 | +`postcss.config.js` in the project root, `"postcss"` section in `package.json` or `postcss` in bundle config. |
| 67 | + |
| 68 | +If you do not use PostCSS, add it according to [official docs] and set this plugin in settings. |
| 69 | + |
| 70 | +### **Step 3:** Add the plugin to plugins list (at the end) |
| 71 | + |
| 72 | +```diff |
| 73 | +module.exports = { |
| 74 | + plugins: [ |
| 75 | + require('autoprefixer'), |
| 76 | ++ require('postcss-assign-layer')({ |
| 77 | ++ include: '**/*.module.css', |
| 78 | ++ layerName: 'components', |
| 79 | ++ }), |
| 80 | + ], |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +### **Step 4:** Configure the plugin |
| 85 | + |
| 86 | +It's possible to configure the include glob, as well as the layer name. |
| 87 | + |
| 88 | +#### `include` |
| 89 | + |
| 90 | +Default: `"**/*.module.css"` |
| 91 | + |
| 92 | +A valid [picomatch] pattern, or array of patterns. Note that `picomatch` patterns are very similar to [minimatch] patterns, and in most use cases, they are interchangeable. If you have more specific pattern matching needs, you can view [this comparison table] to learn more about where the libraries differ. |
| 93 | + |
| 94 | +#### `layerName` |
| 95 | + |
| 96 | +Default: `'components'` |
| 97 | + |
| 98 | +The [layer name] that will be used. |
| 99 | + |
| 100 | +[css cascade layers]: https://www.w3.org/TR/css-cascade-5/#layering |
| 101 | +[a complete guide to css cascade layers]: https://css-tricks.com/css-cascade-layers/ |
| 102 | +[css modules]: https://github.com/css-modules/css-modules |
| 103 | +[official docs]: https://github.com/postcss/postcss#usage |
| 104 | +[picomatch]: https://github.com/micromatch/picomatch#globbing-features |
| 105 | +[minimatch]: https://github.com/isaacs/minimatch#readme |
| 106 | +[this comparison table]: https://github.com/micromatch/picomatch#library-comparisons |
| 107 | +[layer name]: https://www.w3.org/TR/css-cascade-5/#typedef-layer-name |
0 commit comments