| 1 |
# SugarSS |
| 2 |
|
| 3 |
<img align="right" width="120" height="155" |
| 4 |
title="SugarSS logo by Maria Keller" |
| 5 |
src="http://postcss.github.io/sugarss/logo.svg"> |
| 6 |
|
| 7 |
Indent-based CSS syntax for [PostCSS]. |
| 8 |
|
| 9 |
```sass |
| 10 |
a |
| 11 |
color: blue |
| 12 |
|
| 13 |
.multiline, |
| 14 |
.selector |
| 15 |
box-shadow: 1px 0 9px rgba(0, 0, 0, .4), |
| 16 |
1px 0 3px rgba(0, 0, 0, .6) |
| 17 |
|
| 18 |
// Mobile |
| 19 |
@media (max-width: 400px) |
| 20 |
.body |
| 21 |
padding: 0 10px |
| 22 |
``` |
| 23 |
|
| 24 |
As any PostCSS custom syntax, SugarSS has source map, [stylelint] |
| 25 |
and [postcss-sorting] support out-of-box. |
| 26 |
|
| 27 |
It was designed to be used with [postcss-simple-vars] and [postcss-nested]. |
| 28 |
But you can use it with any PostCSS plugins |
| 29 |
or use it without any PostCSS plugins. |
| 30 |
With [postcss-mixins] you can use `@mixin` syntax as in Sass. |
| 31 |
|
| 32 |
<a href="https://evilmartians.com/?utm_source=sugarss"> |
| 33 |
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" |
| 34 |
alt="Sponsored by Evil Martians" width="236" height="54"> |
| 35 |
</a> |
| 36 |
|
| 37 |
[postcss-mixins]: https://github.com/postcss/postcss-mixins |
| 38 |
[postcss-nested]: https://github.com/postcss/postcss-nested |
| 39 |
[postcss-simple-vars]: https://github.com/postcss/postcss-simple-vars |
| 40 |
[postcss-sorting]: https://github.com/hudochenkov/postcss-sorting |
| 41 |
[stylelint]: http://stylelint.io/ |
| 42 |
[PostCSS]: https://github.com/postcss/postcss |
| 43 |
|
| 44 |
|
| 45 |
## Syntax |
| 46 |
|
| 47 |
SugarSS MIME-type is `text/x-sugarss` with `.sss` file extension. |
| 48 |
|
| 49 |
|
| 50 |
### Indent |
| 51 |
|
| 52 |
We recommend 2 spaces indent. However, SugarSS autodetects indent |
| 53 |
and can be used with tabs or spaces. |
| 54 |
|
| 55 |
But it is prohibited to mix spaces and tabs in SugarSS sources. |
| 56 |
|
| 57 |
|
| 58 |
### Multiline |
| 59 |
|
| 60 |
SugarSS was designed to have intuitively multiline selectors and declaration |
| 61 |
values. |
| 62 |
|
| 63 |
There are 3 rules for any types of nodes: |
| 64 |
|
| 65 |
```sass |
| 66 |
// 1. New line inside brackets will be ignored |
| 67 |
@supports ( (display: flex) and |
| 68 |
(display: grid) ) |
| 69 |
|
| 70 |
// 2. Comma at the end of the line |
| 71 |
@media (max-width: 400px), |
| 72 |
(max-height: 800px) |
| 73 |
|
| 74 |
// 3. Backslash before new line |
| 75 |
@media screen and \ |
| 76 |
(min-width: 600px) |
| 77 |
``` |
| 78 |
|
| 79 |
In a selector you can put a new line anywhere. Just keep same indent |
| 80 |
for every line of selector: |
| 81 |
|
| 82 |
```sass |
| 83 |
.parent > |
| 84 |
.child |
| 85 |
color: black |
| 86 |
``` |
| 87 |
|
| 88 |
In a declaration value you can put a new line anywhere. Just keep a bigger indent |
| 89 |
for the value: |
| 90 |
|
| 91 |
```sass |
| 92 |
.one |
| 93 |
background: linear-gradient(rgba(0, 0, 0, 0), black) |
| 94 |
linear-gradient(red, rgba(255, 0, 0, 0)) |
| 95 |
|
| 96 |
.two |
| 97 |
background: |
| 98 |
linear-gradient(rgba(0, 0, 0, 0), black) |
| 99 |
linear-gradient(red, rgba(255, 0, 0, 0)) |
| 100 |
``` |
| 101 |
|
| 102 |
|
| 103 |
### Comments |
| 104 |
|
| 105 |
SugarSS supports two types of comments: |
| 106 |
|
| 107 |
```sass |
| 108 |
/* |
| 109 |
Multiline comments |
| 110 |
*/ |
| 111 |
|
| 112 |
// Inline comments |
| 113 |
``` |
| 114 |
|
| 115 |
There is no “silent” comment in SugarSS. Output CSS will contain all comments |
| 116 |
from `.sss` source. But you can use [postcss-discard-comments] |
| 117 |
for Sass’s silent/loud comments behaviour. |
| 118 |
|
| 119 |
[postcss-discard-comments]: https://www.npmjs.com/package/postcss-discard-comments |
| 120 |
|
| 121 |
|
| 122 |
### Rule and Declarations |
| 123 |
|
| 124 |
SugarSS separates selectors and declarations by `:\s` or `:\n` token. |
| 125 |
|
| 126 |
So you must write a space after the property name: `color: black` is good, |
| 127 |
`color:black` is prohibited. |
| 128 |
|
| 129 |
|
| 130 |
### Other |
| 131 |
|
| 132 |
SugarSS is just a syntax, it change the way how you write CSS, |
| 133 |
but do not add preprocessor features build-in. |
| 134 |
|
| 135 |
Here are PostCSS plugins which could add you preprocessor features: |
| 136 |
|
| 137 |
* **[postcss-simple-vars]** adds variables. |
| 138 |
* **[postcss-nested]** adds nested rules. |
| 139 |
* **[postcss-import]** adds `@import` directive support. |
| 140 |
* **[postcss-import-ext-glob]** extends [postcss-import] path resolver to allow glob usage as a path. |
| 141 |
* **[postcss-mixins]** add `@mixin` support. |
| 142 |
* **[postcss-functions]** allows you to define own CSS functions in JS. |
| 143 |
|
| 144 |
[postcss-functions]: https://github.com/andyjansson/postcss-functions |
| 145 |
[postcss-mixins]: https://github.com/postcss/postcss-mixins |
| 146 |
[postcss-import-ext-glob]: https://github.com/dimitrinicolas/postcss-import-ext-glob |
| 147 |
[postcss-import]: https://github.com/postcss/postcss-import |
| 148 |
[postcss-nested]: https://github.com/postcss/postcss-nested |
| 149 |
[postcss-simple-vars]: https://github.com/postcss/postcss-simple-vars |
| 150 |
|
| 151 |
|
| 152 |
## Text Editors |
| 153 |
|
| 154 |
* SublimeText: [Syntax Highlighting for .SSS SugarSS] |
| 155 |
* Atom: [language-postcss], [source-preview-postcss] and [build-sugarss] |
| 156 |
* Vim: [vim-sugarss] |
| 157 |
* VSCode: [vetur](https://vuejs.github.io/vetur/), and [postcss-sugarss-language](https://github.com/MhMadHamster/vscode-postcss-language) |
| 158 |
|
| 159 |
We are working on syntax highlight support in text editors. |
| 160 |
|
| 161 |
Right now, you can set `Sass` or `Stylus` syntax highlight for `.sss` files. |
| 162 |
|
| 163 |
[Syntax Highlighting for .SSS SugarSS]: https://packagecontrol.io/packages/Syntax%20Highlighting%20for%20SSS%20SugarSS |
| 164 |
[source-preview-postcss]: https://atom.io/packages/source-preview-postcss |
| 165 |
[language-postcss]: https://atom.io/packages/language-postcss |
| 166 |
[build-sugarss]: https://atom.io/packages/build-sugarss |
| 167 |
[vim-sugarss]: https://github.com/hhsnopek/vim-sugarss |
| 168 |
|
| 169 |
|
| 170 |
## Usage |
| 171 |
|
| 172 |
SugarSS needs PostCSS compiler. Install [`postcss-loader`] for webpack, |
| 173 |
[`gulp-postcss`] for Gulp, [`postcss-cli`] for npm scripts. |
| 174 |
[Parcel] has build-in support for PostCSS. |
| 175 |
|
| 176 |
Then install SugarSS: `npm install --save-dev postcss sugarss` if you use npm |
| 177 |
and `yarn add --dev postcss sugarss` if you use Yarn. |
| 178 |
|
| 179 |
Then create `.postcssrc` file: |
| 180 |
|
| 181 |
```json |
| 182 |
{ |
| 183 |
"parser": "sugarss", |
| 184 |
"plugins": { |
| 185 |
"postcss-simple-vars": {} |
| 186 |
} |
| 187 |
} |
| 188 |
``` |
| 189 |
|
| 190 |
[`postcss-loader`]: https://github.com/postcss/postcss-loader |
| 191 |
[`gulp-postcss`]: https://github.com/postcss/gulp-postcss |
| 192 |
[`postcss-cli`]: https://github.com/postcss/postcss-cli |
| 193 |
[Parcel]: https://parceljs.org/transforms.html |
| 194 |
|
| 195 |
|
| 196 |
### Imports |
| 197 |
|
| 198 |
If you doesn’t use Webpack or Parcel, you need some PostCSS plugin |
| 199 |
to process `@import` directives. |
| 200 |
|
| 201 |
If you want `@import`, install [postcss-import] and add it to `.postcssrc` file: |
| 202 |
|
| 203 |
```diff js |
| 204 |
{ |
| 205 |
"parser": "sugarss", |
| 206 |
"plugins": { |
| 207 |
+ "postcss-import": {}, |
| 208 |
"postcss-simple-vars": {} |
| 209 |
} |
| 210 |
} |
| 211 |
``` |
| 212 |
|
| 213 |
[postcss-import]: https://github.com/postcss/postcss-import |
| 214 |
|
| 215 |
|
| 216 |
### Mixins |
| 217 |
|
| 218 |
For mixins support, install [postcss-mixins] and add it to `.postcssrc` file: |
| 219 |
|
| 220 |
```diff js |
| 221 |
{ |
| 222 |
"parser": "sugarss", |
| 223 |
"plugins": { |
| 224 |
+ "postcss-mixins": { |
| 225 |
+ "mixinsDir": "./mixins" |
| 226 |
+ }, |
| 227 |
"postcss-simple-vars": {} |
| 228 |
} |
| 229 |
} |
| 230 |
``` |
| 231 |
|
| 232 |
Now you can define your mixins in `mixins/` dir. |
| 233 |
For example create `mixins/circle.sss` with: |
| 234 |
|
| 235 |
```sss |
| 236 |
@define-mixin circle $size |
| 237 |
border-radius: 50% |
| 238 |
width: $size |
| 239 |
height: $size |
| 240 |
``` |
| 241 |
|
| 242 |
|
| 243 |
### Functions |
| 244 |
|
| 245 |
To define custom functions you need to install [postcss-functions] |
| 246 |
and add it to `.postcssrc` file: |
| 247 |
|
| 248 |
```diff js |
| 249 |
{ |
| 250 |
"parser": "sugarss", |
| 251 |
"plugins": { |
| 252 |
+ "postcss-functions": { |
| 253 |
+ "glob": "./functions" |
| 254 |
+ }, |
| 255 |
"postcss-simple-vars": {} |
| 256 |
} |
| 257 |
} |
| 258 |
``` |
| 259 |
|
| 260 |
Then you can define functions in `functions/` dir. For example, |
| 261 |
`functions/foo.js` will define `foo()` function in CSS: |
| 262 |
|
| 263 |
```js |
| 264 |
module.exports = function (args) { |
| 265 |
return 'foo' |
| 266 |
} |
| 267 |
``` |
| 268 |
|
| 269 |
|
| 270 |
### SugarSS to SugarSS |
| 271 |
|
| 272 |
Sometimes we use PostCSS not to build CSS, but to fix source files. |
| 273 |
For example, to sort properties by [postcss-sorting]. |
| 274 |
|
| 275 |
For this cases use the `syntax` option, instead of `parser`: |
| 276 |
|
| 277 |
```js |
| 278 |
gulp.task('sort', function () { |
| 279 |
return gulp.src('src/**/*.sss') |
| 280 |
.pipe(postcss([sorting], { syntax: sugarss })) |
| 281 |
.pipe(gulp.dest('src')); |
| 282 |
}); |
| 283 |
``` |
| 284 |
|
| 285 |
[postcss-sorting]: https://github.com/hudochenkov/postcss-sorting |
| 286 |
|
| 287 |
|
| 288 |
### CSS to SugarSS |
| 289 |
|
| 290 |
You can even compile existing CSS sources to SugarSS syntax. |
| 291 |
Just use `stringifier` option instead of `parser`: |
| 292 |
|
| 293 |
```js |
| 294 |
postcss().process(css, { stringifier: sugarss }).then(function (result) { |
| 295 |
result.content // Converted SugarSS content |
| 296 |
}); |
| 297 |
``` |
| 298 |
|
| 299 |
|
| 300 |
## Thanks |
| 301 |
|
| 302 |
Cute project logo was made by [Maria Keller](http://www.mariakellerac.com/). |