|
1 | | -# CSS Modules: Scope Locals & Extend |
| 1 | +# postcss-icss-composes [![Build Status][travis-img]][travis] |
2 | 2 |
|
3 | | -[](https://travis-ci.org/css-modules/postcss-modules-scope) |
| 3 | +[PostCSS]: https://github.com/postcss/postcss |
| 4 | +[travis-img]: https://travis-ci.org/css-modules/postcss-icss-composes.svg |
| 5 | +[travis]: https://travis-ci.org/css-modules/postcss-icss-composes |
4 | 6 |
|
5 | | -Transforms: |
| 7 | +PostCSS plugin for css modules to compose local-scope classes |
6 | 8 |
|
7 | | -```css |
8 | | -:local(.continueButton) { |
9 | | - color: green; |
10 | | -} |
11 | | -``` |
12 | | - |
13 | | -into: |
14 | | - |
15 | | -```css |
16 | | -:export { |
17 | | - continueButton: __buttons_continueButton_djd347adcxz9; |
18 | | -} |
19 | | -.__buttons_continueButton_djd347adcxz9 { |
20 | | - color: green; |
21 | | -} |
22 | | -``` |
23 | | - |
24 | | -so it doesn't pollute CSS global scope and can be simply used in JS like so: |
| 9 | +## Usage |
25 | 10 |
|
26 | 11 | ```js |
27 | | -import styles from './buttons.css' |
28 | | -elem.innerHTML = `<button class="${styles.continueButton}">Continue</button>` |
| 12 | +postcss([require('postcss-icss-composes')]) |
29 | 13 | ``` |
30 | 14 |
|
31 | | -## Composition |
| 15 | +See [PostCSS] docs for examples for your environment. |
| 16 | + |
| 17 | +### Local class composition |
32 | 18 |
|
33 | | -Since we're exporting class names, there's no reason to export only one. This can give us some really useful reuse of styles: |
| 19 | +`composes` and `compose-with` combines specified class name with rule class name. |
34 | 20 |
|
35 | 21 | ```css |
36 | | -.globalButtonStyle { |
37 | | - background: white; |
38 | | - border: 1px solid; |
39 | | - border-radius: 0.25rem; |
| 22 | +.buttonStyle { |
| 23 | + background: #fff; |
40 | 24 | } |
41 | | -.globalButtonStyle:hover { |
| 25 | +.buttonStyle:hover { |
42 | 26 | box-shadow: 0 0 4px -2px; |
43 | 27 | } |
44 | | -:local(.continueButton) { |
45 | | - compose-with: globalButtonStyle; |
| 28 | +.cellStyle { |
| 29 | + margin: 10px; |
| 30 | +} |
| 31 | +.addButton { |
| 32 | + composes: buttonStyle cellStyle; |
46 | 33 | color: green; |
47 | 34 | } |
48 | | -``` |
49 | 35 |
|
50 | | -becomes: |
| 36 | +/* becomes */ |
51 | 37 |
|
52 | | -```css |
53 | 38 | :export { |
54 | | - continueButton: ___buttons_continueButton_djd347adcxz9 globalButtonStyle; |
| 39 | + buttonStyle: buttonStyle; |
| 40 | + cellStyle: cellStyle; |
| 41 | + addButton: addButton buttonStyle cellStyle |
55 | 42 | } |
56 | | -.globalButtonStyle { |
57 | | - background: white; |
58 | | - border: 1px solid; |
59 | | - border-radius: 0.25rem; |
| 43 | +.buttonStyle { |
| 44 | + background: #fff; |
60 | 45 | } |
61 | | -.globalButtonStyle:hover { |
| 46 | +.buttonStyle:hover { |
62 | 47 | box-shadow: 0 0 4px -2px; |
63 | 48 | } |
64 | | -.___buttons_continueButton_djd347adcxz9 { |
| 49 | +.cellStyle { |
| 50 | + margin: 10px; |
| 51 | +} |
| 52 | +.addButton { |
65 | 53 | color: green; |
66 | 54 | } |
67 | 55 | ``` |
68 | 56 |
|
69 | | -**Note:** you can also use `composes` as a shorthand for `compose-with` |
| 57 | +### Global class composition |
70 | 58 |
|
71 | | -## Local-by-default & reuse across files |
| 59 | +You may use any identifier for composition |
72 | 60 |
|
73 | | -You're looking for [CSS Modules](https://github.com/css-modules/css-modules). It uses this plugin as well as a few others, and it's amazing. |
74 | | - |
75 | | -## Building |
| 61 | +```css |
| 62 | +.addButton { |
| 63 | + composes: globalButtonStyle; |
| 64 | + background: #000; |
| 65 | +} |
76 | 66 |
|
77 | | -``` |
78 | | -npm install |
79 | | -npm test |
| 67 | +/* becomes */ |
| 68 | +:export { |
| 69 | + addButton: addButton globalButtonStyle |
| 70 | +} |
| 71 | +.addButton { |
| 72 | + background: #000; |
| 73 | +} |
80 | 74 | ``` |
81 | 75 |
|
82 | | -[](https://travis-ci.org/css-modules/postcss-modules-scope) |
| 76 | +### Scoping class names |
83 | 77 |
|
84 | | -* Lines: [](https://coveralls.io/r/css-modules/postcss-modules-scope?branch=master) |
85 | | -* Statements: [](http://codecov.io/github/css-modules/postcss-modules-scope?branch=master) |
| 78 | +[postcss-icss-selectors](https://github.com/css-modules/postcss-icss-selectors) plugin allows to local-scope classes. |
86 | 79 |
|
87 | | -## Development |
| 80 | +```css |
| 81 | +.buttonStyle { |
| 82 | + background: #fff; |
| 83 | +} |
| 84 | +.addButton { |
| 85 | + composes: buttonStyle; |
| 86 | + border: 1px solid #000; |
| 87 | +} |
88 | 88 |
|
89 | | -- `npm autotest` will watch `src` and `test` for changes and run the tests, and transpile the ES6 to ES5 on success |
| 89 | +/* becomes */ |
90 | 90 |
|
91 | | -## License |
| 91 | +:export { |
| 92 | + buttonStyle: __scope__buttonStyle; |
| 93 | + addButton: __scope__addButton __scope__buttonStyle |
| 94 | +} |
| 95 | +.__scope__buttonStyle { |
| 96 | + background: #fff; |
| 97 | +} |
| 98 | +.__scope__addButton { |
| 99 | + border: 1px solid #000; |
| 100 | +} |
| 101 | +``` |
92 | 102 |
|
93 | | -ISC |
| 103 | +### External composition |
94 | 104 |
|
95 | | -## With thanks |
| 105 | +```css |
| 106 | +/* compositions.css */ |
| 107 | +.button { |
| 108 | + background: #fff; |
| 109 | + border: 1px solid #000; |
| 110 | +} |
| 111 | +.cell { |
| 112 | + margin: 10px; |
| 113 | +} |
| 114 | + |
| 115 | +/* main.css */ |
| 116 | +.addButton { |
| 117 | + composes: button cell from './composition.css'; |
| 118 | + font-size: 20px; |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### Messages |
| 123 | + |
| 124 | +postcss-icss-composes passes result.messages for each composed class name |
96 | 125 |
|
97 | | -- Mark Dalgleish |
98 | | -- Tobias Koppers |
99 | | -- Guy Bedford |
| 126 | +```js |
| 127 | +{ |
| 128 | + plugin: 'postcss-icss-composes', |
| 129 | + type: 'icss-composed', |
| 130 | + name: string, // rule class name |
| 131 | + value: string // composed class name |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +## License |
100 | 136 |
|
101 | | ---- |
102 | | -Glen Maddern, 2015. |
| 137 | +MIT © Glen Maddern and Bogdan Chadkin, 2015 |
0 commit comments