|
| 1 | +# Postcss-salad |
| 2 | +[![Build Status][travis-img]][travis] |
| 3 | +[![Version][version]](https://github.com/ElemeFE/postcss-salad/blob/master/CHANGELOG.md) |
| 4 | + |
| 5 | +> Postcss-salad 是一个能够帮助你更加写出更加简洁、优雅的CSS的样式解决方案。 |
| 6 | +
|
| 7 | +Postcss-salad不仅提供了下一代css语法支持,还提供了基础的sass的语法、属性顺时针简写、rem填充、基础图形绘制、可定制样式的inline-svg以及bem类名自动转化等实用的功能。 |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## Examples |
| 12 | + |
| 13 | +#### 下一代css语法 |
| 14 | + |
| 15 | +```css |
| 16 | +/* before */ |
| 17 | +:root { |
| 18 | + --mainColor: red; |
| 19 | +} |
| 20 | +@custom-media --small-viewport (max-width: 30em); |
| 21 | + |
| 22 | +@media (--small-viewport) { |
| 23 | + a { |
| 24 | + color: var(--mainColor); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/* after */ |
| 29 | +@media (max-width: 30em) { |
| 30 | + a { |
| 31 | + color: red; |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +#### 顺时针简写 |
| 37 | + |
| 38 | +```css |
| 39 | +/* before */ |
| 40 | +a { |
| 41 | + position: absolute 1em 2em 3em 4em; |
| 42 | + margin: 30px *; |
| 43 | +} |
| 44 | +b { |
| 45 | + position: absolute 1em * * 4em; |
| 46 | + padding: 30px *; |
| 47 | +} |
| 48 | + |
| 49 | +/* after */ |
| 50 | +a { |
| 51 | + top: 1em; |
| 52 | + right: 2em; |
| 53 | + bottom: 3em; |
| 54 | + left: 4em; |
| 55 | + position: absolute; |
| 56 | + margin-top: 30px; |
| 57 | + margin-bottom: 30px; |
| 58 | +} |
| 59 | +b { |
| 60 | + top: 1em; |
| 61 | + left: 4em; |
| 62 | + position: absolute; |
| 63 | + padding-top: 30px; |
| 64 | + padding-bottom: 30px; |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +#### rem填充 |
| 69 | + |
| 70 | +```css |
| 71 | +/* before */ |
| 72 | +a { |
| 73 | + width: 10rem; |
| 74 | + margin: 30px 2rem 1rem 10px; |
| 75 | +} |
| 76 | + |
| 77 | +/* after */ |
| 78 | +a { |
| 79 | + width: 160px; |
| 80 | + margin: 30px 32px 16px 10px; |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +#### 基础图形绘制 |
| 85 | + |
| 86 | +```css |
| 87 | +/* before */ |
| 88 | +.rect { |
| 89 | + rect: 10px 20px #f00; |
| 90 | +} |
| 91 | +.cirlce { |
| 92 | + circle: 50px #ff0; |
| 93 | +} |
| 94 | +.triangle { |
| 95 | + triangle: 5px left #ff0; |
| 96 | +} |
| 97 | + |
| 98 | +/* after */ |
| 99 | +.rect { |
| 100 | + width: 10px; |
| 101 | + height: 20px; |
| 102 | + background-color: #f00; |
| 103 | +} |
| 104 | +.cirlce { |
| 105 | + width: 50px; |
| 106 | + height: 50px; |
| 107 | + border-radius: 50%; |
| 108 | +} |
| 109 | +.triangle { |
| 110 | + display: inline-block; |
| 111 | + width: 0; |
| 112 | + height: 0; |
| 113 | + border: solid transparent; |
| 114 | + border-width: 5px; |
| 115 | + border-right-color: #ff0; |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +#### 可定制的inline-svg |
| 120 | + |
| 121 | +```css |
| 122 | +/* before */ |
| 123 | +@svg-load nav url(../svgs/nav.svg) { |
| 124 | + path:nth-child(2){ |
| 125 | + fill:#0ff |
| 126 | + }; |
| 127 | +} |
| 128 | +h1 { |
| 129 | + background: svg-inline(nav) |
| 130 | +} |
| 131 | + |
| 132 | +/* after */ |
| 133 | +h1 { |
| 134 | + background: url("data:image/svg+xml;charset=utf-8,<svg xmlns=\'http://www.w3.org/2000/svg\' viewBox=\'0 0 50 50\'><path d=\'M10 12h30v4H10z\'/><path d=\'M10 22h30v4H10z\' fill=\'#0ff\'/><path d=\'M10 32h30v4H10z\'/></svg>"); |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +#### 基础sass语法 |
| 139 | + |
| 140 | +```css |
| 141 | +/* before */ |
| 142 | +$pass: green !default; |
| 143 | + |
| 144 | +@define-placeholder foo { |
| 145 | + background: pink; |
| 146 | +} |
| 147 | + |
| 148 | +.bar { |
| 149 | + @extend foo; |
| 150 | + |
| 151 | + background-color: @color; |
| 152 | + color: $pass; |
| 153 | +} |
| 154 | + |
| 155 | +/* after */ |
| 156 | +.bar { |
| 157 | + background: pink; |
| 158 | +} |
| 159 | + |
| 160 | +.bar { |
| 161 | + |
| 162 | + background-color: green; |
| 163 | + color: green; |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +#### bem类名 |
| 168 | + |
| 169 | +```css |
| 170 | +/* before */ |
| 171 | +@utility utilityName { |
| 172 | + color: green; |
| 173 | +} |
| 174 | + |
| 175 | +@utility utilityName small { |
| 176 | + color: blue; |
| 177 | +} |
| 178 | + |
| 179 | +@component ComponentName { |
| 180 | + color: cyan; |
| 181 | + |
| 182 | + @modifier modifierName { |
| 183 | + color: yellow; |
| 184 | + } |
| 185 | + |
| 186 | + @descendent descendentName { |
| 187 | + color: navy; |
| 188 | + } |
| 189 | + |
| 190 | + @when stateName { |
| 191 | + color: crimson; |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +@component-namespace nmsp { |
| 196 | + @component ComponentName { |
| 197 | + color: red; |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +/* after */ |
| 202 | +.u-utilityName { |
| 203 | + color: green; |
| 204 | +} |
| 205 | + |
| 206 | +.u-sm-utilityName { |
| 207 | + color: blue; |
| 208 | +} |
| 209 | + |
| 210 | +.ComponentName { |
| 211 | + color: cyan; |
| 212 | +} |
| 213 | + |
| 214 | +.ComponentName--modifierName { |
| 215 | + color: yellow; |
| 216 | +} |
| 217 | + |
| 218 | +.ComponentName-descendentName { |
| 219 | + color: navy; |
| 220 | +} |
| 221 | + |
| 222 | +.ComponentName.is-stateName { |
| 223 | + color: crimson; |
| 224 | +} |
| 225 | + |
| 226 | +.nmsp-ComponentName { |
| 227 | + color: red; |
| 228 | +} |
| 229 | +``` |
| 230 | + |
| 231 | +## Usage |
| 232 | + |
| 233 | +Add [Postcss salad] to your build tool: |
| 234 | + |
| 235 | +```bash |
| 236 | +npm install postcss-salad --save-dev |
| 237 | +``` |
| 238 | + |
| 239 | +#### Node |
| 240 | + |
| 241 | +```js |
| 242 | +require('postcss-salad').process(YOUR_CSS, { /* options */ }); |
| 243 | +``` |
| 244 | + |
| 245 | +#### PostCSS |
| 246 | + |
| 247 | +Add [PostCSS] to your build tool: |
| 248 | + |
| 249 | +```bash |
| 250 | +npm install postcss --save-dev |
| 251 | +``` |
| 252 | + |
| 253 | +Load [Postcss salad] as a PostCSS plugin: |
| 254 | + |
| 255 | +```js |
| 256 | +postcss([ |
| 257 | + require('postcss-salad')({ /* options */ }) |
| 258 | +]).process(YOUR_CSS, /* options */); |
| 259 | +``` |
| 260 | + |
| 261 | +#### Gulp |
| 262 | + |
| 263 | +Add [Gulp PostCSS] to your build tool: |
| 264 | + |
| 265 | +```bash |
| 266 | +npm install gulp-postcss --save-dev |
| 267 | +``` |
| 268 | + |
| 269 | +Enable [Postcss salad] within your Gulpfile: |
| 270 | + |
| 271 | +```js |
| 272 | +var postcss = require('gulp-postcss'); |
| 273 | + |
| 274 | +gulp.task('css', function () { |
| 275 | + return gulp.src('./src/*.css').pipe( |
| 276 | + postcss([ |
| 277 | + require('postcss-salad')({ /* options */ }) |
| 278 | + ]) |
| 279 | + ).pipe( |
| 280 | + gulp.dest('.') |
| 281 | + ); |
| 282 | +}); |
| 283 | +``` |
| 284 | + |
| 285 | +## options |
| 286 | + |
| 287 | +***you can change the plugin's behaviour by the options*** |
| 288 | + |
| 289 | +``` |
| 290 | +var options = { |
| 291 | + browser: ['ie > 8', 'last 2 version'], |
| 292 | + features: { |
| 293 | + "bem": false, //pass boolean false can disable the plugin |
| 294 | + "inlineSvg": { |
| 295 | + "path": "src/svgs" |
| 296 | + } |
| 297 | + } |
| 298 | +} |
| 299 | +``` |
| 300 | + |
| 301 | + |
| 302 | +### Plugins |
| 303 | + |
| 304 | +Postcss-salad powered by the following plugins (in this order): |
| 305 | + |
| 306 | +- [precss]: a tool that allows you to use Sass-like markup in your CSS files |
| 307 | +- [postcss-bem]: implementing BEM as at-rules |
| 308 | +- [postcss-calc]: plugin to reduce calc() |
| 309 | +- [postcss-initial]: fallback initial keyword |
| 310 | +- [postcss-inline-svg]: reference an SVG file and control its attributes with CSS syntax. |
| 311 | +- [postcss-short-font-size]: extends the font-size property so that line-height may be set by the second value. |
| 312 | +- [postcss-short-spacing]: lets you write shorthand margin and padding properties while omitting edges in CSS. |
| 313 | +- [postcss-color-function]: W3C color methods |
| 314 | +- [postcss-short-size]: Write shorthand size properties in CSS |
| 315 | +- [postcss-short-position]: Define edges inside the position property in CSS |
| 316 | +- [postcss-salad]: draw basic salad with specified syntax in css rule |
| 317 | +- [node-pixrem]: generates pixel fallbacks for rem units. |
| 318 | +- [autoprefixer]: parse CSS and add vendor prefixes to CSS rules using values from Can I Use |
| 319 | + |
| 320 | +[travis-img]: https://travis-ci.org/ElemeFE/postcss-salad.svg |
| 321 | +[travis]: https://travis-ci.org/ElemeFE/postcss-salad |
| 322 | +[precss]: https://github.com/jonathantneal/precss |
| 323 | +[postcss-bem]: https://github.com/ileri/postcss-bem |
| 324 | +[postcss-calc]: https://github.com/postcss/postcss-calc |
| 325 | +[postcss-initial]: https://github.com/maximkoretskiy/postcss-initial |
| 326 | +[postcss-inline-svg]: https://github.com/TrySound/postcss-inline-svg |
| 327 | +[postcss-short-font-size]: https://github.com/jonathantneal/postcss-short-font-size |
| 328 | +[postcss-short-spacing]: https://github.com/jonathantneal/postcss-short-spacing |
| 329 | +[postcss-color-function]: https://github.com/postcss/postcss-color-function |
| 330 | +[postcss-short-size]: https://github.com/jonathantneal/postcss-short-size |
| 331 | +[postcss-short-position]: https://github.com/jonathantneal/postcss-short-position |
| 332 | +[node-pixrem]: https://github.com/robwierzbowski/node-pixrem |
| 333 | +[autoprefixer]: https://github.com/postcss/autoprefixer |
| 334 | +[Postcss-salad]: https://github.com/ElemeFE/postcss-salad |
| 335 | +[version]: https://img.shields.io/npm/v/postcss-salad.svg |
0 commit comments