• Stars
    star
    409
  • Rank 101,884 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PostCSS plugin for Sass-like variables

PostCSS Simple Variables

PostCSS plugin for Sass-like variables.

You can use variables inside values, selectors and at-rule parameters.

$dir:    top;
$blue:   #056ef0;
$column: 200px;

.menu_link {
  background: $blue;
  width: $column;
}
.menu {
  width: calc(4 * $column);
  margin-$(dir): 10px;
}
.menu_link {
  background: #056ef0;
  width: 200px;
}
.menu {
  width: calc(4 * 200px);
  margin-top: 10px;
}

If you want be closer to W3C spec, you should use postcss-custom-properties and postcss-at-rules-variables plugins.

Look at postcss-map for big complicated configs.

Sponsored by Evil Martians

Interpolation

There is special syntax for using variables inside CSS words:

$prefix: my-company-widget

$prefix { }
$(prefix)_button { }

Comments

You can use variables in comments too (for example, to generate special mdcss comments). Syntax for comment variables is different to separate them from PreCSS code examples:

$width: 100px;
/* $width: <<$(width)>> */

compiles to:

/* $width: 100px */

Escaping

If you want to escape $ in the content property, use Unicode escape syntax.

.foo::before {
  content: "\0024x";
}

If you want to use e.g. : in a selector variable, use the corresponding unicode escape sequence:

/* Given */
$selector: .my-component[data-emoji="\U0001f389"]\u003Adisabled;

$selector {
  width: 1px;
}

/* Generates */
.my-component[data-emoji="๐ŸŽ‰"]:disabled {
  width: 1px;
}

Note: Use \u for 4-digit sequences and \U for 8-digit sequences.

Usage

Step 1: Install plugin:

npm install --save-dev postcss postcss-simple-vars

Step 2: Check your project for existing PostCSS config: postcss.config.js in the project root, "postcss" section in package.json or postcss in bundle config.

If you do not use PostCSS, add it according to official docs and set this plugin in settings.

Step 3: Add the plugin to plugins list:

module.exports = {
  plugins: [
+   require('postcss-simple-vars'),
    require('autoprefixer')
  ]
}

Options

Call plugin function to set options:

    require('postcss-simple-vars')({ silent: true })

variables

Set default variables. It is useful to store colors or other constants in a common file:

// config/colors.js

module.exports = {
  blue: '#056ef0'
}

// postcss.config.js

const colors = require('./config/colors')
const vars   = require('postcss-simple-vars')

module.exports = {
  plugins: [
    require('postcss-simple-vars')({ variables: colors })
  ]
}

You can use a function return an object, if you want to update default variables in webpack hot reload:

    require('postcss-simple-vars')({
      variables: function () {
        return require('./config/colors');
      }
    })

onVariables

Callback invoked once all variables in css are known. The callback receives an object representing the known variables, including those explicitly declared by the variables option.

    require('postcss-simple-vars')({
      onVariables (variables) {
        console.log('CSS Variables');
        console.log(JSON.stringify(variables, null, 2));
      }
    })

unknown

Callback on unknown variable name. It receives the node instance, variable name and PostCSS Result object.

    require('postcss-simple-vars')({
      unknown (node, name, result) {
        node.warn(result, 'Unknown variable ' + name);
      }
    })
])

silent

Leave unknown variables in CSS and do not throw an error. Default is false.

only

Set value only for variables from this object. Other variables will not be changed. Itย isย usefulย forย PostCSS plugin developers.

keep

Keep variables as is and not delete them. Default is false.

Messages

This plugin passes result.messages for each variable:

const result = await postcss([vars]).process('$one: 1; $two: 2')
console.log(result.messages)

will output:

[
  {
    plugin: 'postcss-simple-vars',
    type: 'variable',
    name: 'one'
    value: '1'
  },
  {
    plugin: 'postcss-simple-vars',
    type: 'variable',
    name: 'two'
    value: '2'
  }
]

You can access this in result.messages and in any plugin that included after postcss-simple-vars.

More Repositories

1

postcss

Transforming styles with JS plugins
TypeScript
27,948
star
2

autoprefixer

Parse CSS and add vendor prefixes to rules by Can I Use
JavaScript
21,460
star
3

postcss-import

PostCSS plugin to inline at-import rules content
JavaScript
1,352
star
4

postcss-nested

PostCSS plugin to unwrap nested rules like how Sass does it.
JavaScript
1,108
star
5

postcss-100vh-fix

PostCSS plugin to fix height/min-height: 100vh on iOS
JavaScript
911
star
6

postcss-cli

CLI for postcss
JavaScript
808
star
7

gulp-postcss

Pipe CSS through PostCSS processors with a single parse
JavaScript
771
star
8

sugarss

Indent-based CSS syntax for PostCSS
JavaScript
698
star
9

postcss-js

PostCSS for React Inline Styles, Free Style and other CSS-in-JS
JavaScript
643
star
10

postcss-scss

SCSS parser for PostCSS.
JavaScript
635
star
11

postcss-load-config

Autoload Config for PostCSS
JavaScript
618
star
12

postcss-custom-properties

Use Custom Properties in CSS
JavaScript
598
star
13

postcss-bem-linter

A BEM linter for postcss
JavaScript
561
star
14

postcss-mixins

PostCSS plugin for mixins
JavaScript
446
star
15

postcss-url

PostCSS plugin to rebase url(), inline or copy asset.
JavaScript
373
star
16

postcss-color-function

PostCSS plugin to transform W3C CSS color function to more compatible CSS
JavaScript
323
star
17

postcss-media-minmax

Writing simple and graceful Media Queries!
JavaScript
290
star
18

postcss-plugin-boilerplate

PostCSS Plugin Boilerplate
JavaScript
220
star
19

postcss-calc

PostCSS plugin to reduce calc()
JavaScript
208
star
20

postcss-selector-parser

A CSS selector parser, integrates with postcss but does not require it.
JavaScript
196
star
21

postcss-reporter

Log PostCSS messages in the console
JavaScript
154
star
22

postcss-use

Enable PostCSS plugins directly in your stylesheet.
JavaScript
148
star
23

postcss-dark-theme-class

PostCSS plugin to make dark/light theme switcher by copying styles from media query to special class
JavaScript
147
star
24

postcss-easings

PostCSS plugin to replace easing names to cubic-bezier()
JavaScript
144
star
25

postcss-focus

PostCSS plugin to add :focus selector to every :hover for keyboard accessibility
JavaScript
116
star
26

postcss-safe-parser

Fault tolerant CSS parser for PostCSS
JavaScript
115
star
27

benchmark

PostCSS benchmarks
JavaScript
114
star
28

postcss-devtools

Log execution time for each plugin in a PostCSS instance.
JavaScript
92
star
29

postcss.org

Official website for PostCSS
JavaScript
81
star
30

postcss-browser-reporter

Plugin to display warning messages right in your browser
JavaScript
75
star
31

postcss-color-rebeccapurple

PostCSS plugin to transform rebeccapurple color to rgb()
JavaScript
62
star
32

postcss-deno

Postcss for Deno
JavaScript
56
star
33

postcss-brand-colors

PostCSS plugin to insert branding colors of all the major companies
JavaScript
54
star
34

postcss-size

PostCSS plugin for size shortcut
JavaScript
52
star
35

postcss-will-change

PostCSS plugin to insert 3D hack before will-change property
JavaScript
51
star
36

postcss-color-rgba-fallback

PostCSS plugin to transform rgba() to hexadecimal.
JavaScript
50
star
37

postcss-selector-matches

PostCSS plugin to transform :matches() W3C CSS pseudo class to more compatible CSS (simpler selectors)
JavaScript
44
star
38

postcss-plugin-context

Limit a PostCSS processor to a local stylesheet context.
JavaScript
33
star
39

postcss-color-hex-alpha

Use 4 & 8 character hex color notation in CSS
JavaScript
28
star
40

postcss-color-gray

Use the gray() color function in CSS
JavaScript
27
star
41

postcss-font-variant

PostCSS plugin to transform W3C CSS font variant properties to more compatible CSS (font-feature-settings)
JavaScript
25
star
42

brand

PostCSS branding files
JavaScript
24
star
43

postcss-filter-plugins

Exclude/warn on duplicated PostCSS plugins.
JavaScript
23
star
44

postcss-parser-tests

Base tests for every PostCSS CSS parser
JavaScript
21
star
45

postcss-color-hwb

PostCSS plugin to transform W3C CSS hwb() function to more compatible CSS (rgb() or rgba()).
JavaScript
21
star
46

eslint-config-postcss

An ESLint shareable config for postcss and plugins
JavaScript
18
star
47

postcss-plugin-suggestion-box

Suggestion box for PostCSS plugins
14
star
48

postcss-relative-opacity

PostCSS plugin to add opacity to any colors with Relative Color Syntax
JavaScript
11
star
49

fly-postcss

UNMAINTAINED
10
star
50

postcss-fail-on-warn

PostCSS plugin throws a error on any warning from previous PostCSS plugins.
JavaScript
8
star
51

postcss-sharec-config

Best parctices and configs from PostCSS plugin
7
star
52

postcss-at-rule-parser

A modern CSS at rule parser in PostCSS.
TypeScript
5
star
53

postcss-color

DEPRECATED - PostCSS plugin to transform latest W3C CSS color module syntax to more compatible CSS
JavaScript
5
star
54

postcss-deno-import

postcss-import plugin for Deno
JavaScript
4
star