|
| 1 | +#CSS Modules |
| 2 | + |
| 3 | +A**CSS Module** is a CSS file in which all class names and animation names are scoped locally by default. All URLs (`url(...)`) and`@imports` are in module request format (`./xxx` and`../xxx` means relative,`xxx` and`xxx/yyy` means in modules folder, i. e. in`node_modules`). |
| 4 | + |
| 5 | +```css |
| 6 | +/* style.css*/ |
| 7 | +.className { |
| 8 | +color:green; |
| 9 | +} |
| 10 | +``` |
| 11 | + |
| 12 | +When importing the**CSS Module** from an JS Module, it exports global names for all local names. As default export an object with all mappings is exported. |
| 13 | + |
| 14 | +```js |
| 15 | +importstylesfrom"./style.css"; |
| 16 | +// import { className } from "./style.css"; |
| 17 | + |
| 18 | +return'<div class="'+styles.className+'">'; |
| 19 | +``` |
| 20 | + |
| 21 | +##Naming |
| 22 | + |
| 23 | +For local class names camelCase naming is recommended (but not enforced). |
| 24 | + |
| 25 | +##Exceptions |
| 26 | + |
| 27 | +`:global` switches to global scope for the current selector resp. identifier.`:global(.xxx)` resp.`@keyframes :global(xxx)` resp.`animation: :global(xxx);` declares a the stuff in brackets in the global scope. |
| 28 | + |
| 29 | +Similar`:local` and`:local(...)` for local scope. |
| 30 | + |
| 31 | +If the selector is switched into global mode, global mode is also activated for the rules. |
| 32 | + |
| 33 | +Example:`.localA :global .global-b .global-c :local(.localD.localE) .global-d` |
| 34 | + |
| 35 | +##Extends |
| 36 | + |
| 37 | +It's possible to extend a selector. |
| 38 | + |
| 39 | +```css |
| 40 | +.className { |
| 41 | +color:green; |
| 42 | +background:red; |
| 43 | +} |
| 44 | + |
| 45 | +.otherClassName { |
| 46 | +extends: className; |
| 47 | +color:yellow; |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +There can be multiple`extends` rules, but`extends` rules must be before other rules. Extending works only for local-scoped selectors and only if the selector is a single class name. When a class name extends from another class name, the CSS Module exports both class names for the local class. This can add up to multiple class names. |
| 52 | + |
| 53 | +It's possible to extend from multiple classes with`extends: classNameA classNameB;`. |
| 54 | + |
| 55 | +##Dependencies |
| 56 | + |
| 57 | +It's possible to extend from class names from other**CSS Modules**. |
| 58 | + |
| 59 | +```css |
| 60 | +.otherClassName { |
| 61 | +extends: className from"./style.css"; |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +##Usage with preprocessors |
| 66 | + |
| 67 | +Preprocessors can make it easy to define a block global or local. |
| 68 | + |
| 69 | +i. e. with less.js |
| 70 | + |
| 71 | +```less |
| 72 | +:global { |
| 73 | +.global-class-name { |
| 74 | +color:green; |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +##Why? |
| 80 | + |
| 81 | +**modular** and**reusable** CSS! |
| 82 | + |
| 83 | +* No more conflicts. |
| 84 | +* Explicit dependencies. |
| 85 | +* No global scope. |
| 86 | + |
| 87 | +##Examples |
| 88 | + |
| 89 | +*[markdalgleish/css-modules-example](https://github.com/markdalgleish/css-modules-example); |
| 90 | +*[Theming](https://github.com/webpack/css-modules/blob/master/examples/theming.md) |
| 91 | + |
| 92 | +#webpack implementation |
| 93 | + |
| 94 | +The css-loader replaces every local-scoped identifier with a global unique name (hashed from module name and local identifier by default) and exports the used identifer. |
| 95 | + |
| 96 | +Extending adds the source class name(s) to the exports. |
| 97 | + |
| 98 | +Extending from other modules first imports the other module and than adds the class name(s) to the exports. |