postcss-calc
PostCSS plugin to reduce calc()
Last updated 7 years ago by semigradsky .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install postcss-calc 
SYNC missed versions from official npm registry.

PostCSS Calc PostCSS

NPM Version Support Chat

PostCSS Calc lets you reduce calc() references whenever it's possible. When multiple units are mixed together in the same expression, the calc() statement is left as is, to fallback to the W3C calc() implementation.

Installation

npm install postcss-calc

Usage

// dependencies
var fs = require("fs")
var postcss = require("postcss")
var calc = require("postcss-calc")

// css to be processed
var css = fs.readFileSync("input.css", "utf8")

// process css
var output = postcss()
  .use(calc())
  .process(css)
  .css

Using this input.css:

h1 {
  font-size: calc(16px * 2);
  height: calc(100px - 2em);
  width: calc(2*var(--base-width));
  margin-bottom: calc(16px * 1.5);
}

you will get:

h1 {
  font-size: 32px;
  height: calc(100px - 2em);
  width: calc(2*var(--base-width));
  margin-bottom: 24px
}

Checkout tests for more examples.

Options

precision (default: 5)

Allow you to define the precision for decimal numbers.

var out = postcss()
  .use(calc({precision: 10}))
  .process(css)
  .css

preserve (default: false)

Allow you to preserve calc() usage in output so browsers will handle decimal precision themselves.

var out = postcss()
  .use(calc({preserve: true}))
  .process(css)
  .css

warnWhenCannotResolve (default: false)

Adds warnings when calc() are not reduced to a single value.

var out = postcss()
  .use(calc({warnWhenCannotResolve: true}))
  .process(css)
  .css

mediaQueries (default: false)

Allows calc() usage as part of media query declarations.

var out = postcss()
  .use(calc({mediaQueries: true}))
  .process(css)
  .css

selectors (default: false)

Allows calc() usage as part of selectors.

var out = postcss()
  .use(calc({selectors: true}))
  .process(css)
  .css

Example:

div[data-size="calc(3*3)"] {
  width: 100px;
}

Related PostCSS plugins

To replace the value of CSS custom properties at build time, try PostCSS Custom Properties.

Contributing

Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.

git clone git@github.com:postcss/postcss-calc.git
git checkout -b patch-1
npm install
npm test

Changelog

License

Current Tags

  • 9.0.1                                ...           latest (2 years ago)

32 Versions

  • 9.0.1                                ...           2 years ago
  • 9.0.0                                ...           2 years ago
  • 8.2.4                                ...           3 years ago
  • 8.2.3                                ...           3 years ago
  • 8.2.2                                ...           3 years ago
  • 8.2.1                                ...           3 years ago
  • 8.2.0                                ...           3 years ago
  • 8.1.0                                ...           3 years ago
  • 8.0.0                                ...           4 years ago
  • 7.0.5                                ...           4 years ago
  • 7.0.4                                ...           4 years ago
  • 7.0.3                                ...           4 years ago
  • 7.0.2                                ...           5 years ago
  • 7.0.1                                ...           6 years ago
  • 7.0.0                                ...           6 years ago
  • 6.0.2                                ...           6 years ago
  • 6.0.1                                ...           7 years ago
  • 6.0.0                                ...           8 years ago
  • 5.3.1                                ...           8 years ago
  • 5.3.0                                ...           8 years ago
  • 5.2.1                                ...           9 years ago
  • 5.2.0                                ...           9 years ago
  • 5.1.0                                ...           9 years ago
  • 5.0.0                                ...           9 years ago
  • 4.1.0                                ...           10 years ago
  • 4.0.1                                ...           10 years ago
  • 4.0.0                                ...           10 years ago
  • 3.0.0                                ...           10 years ago
  • 2.1.0                                ...           10 years ago
  • 2.0.1                                ...           10 years ago
  • 2.0.0                                ...           10 years ago
  • 1.0.0                                ...           10 years ago
Downloads
Today 0
This Week 0
This Month 11
Last Day 0
Last Week 11
Last Month 0
Dependencies (4)
Dev Dependencies (13)

Copyright 2013 - present © cnpmjs.org | Home |