You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.0 KiB

3 months ago
  1. # PostCSS Calc [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][PostCSS]
  2. [![NPM Version][npm-img]][npm-url]
  3. [![Support Chat][git-img]][git-url]
  4. [PostCSS Calc] lets you reduce `calc()` references whenever it's possible.
  5. When multiple units are mixed together in the same expression, the `calc()`
  6. statement is left as is, to fallback to the [W3C calc() implementation].
  7. ## Installation
  8. ```bash
  9. npm install postcss-calc
  10. ```
  11. ## Usage
  12. ```js
  13. // dependencies
  14. var fs = require("fs")
  15. var postcss = require("postcss")
  16. var calc = require("postcss-calc")
  17. // css to be processed
  18. var css = fs.readFileSync("input.css", "utf8")
  19. // process css
  20. var output = postcss()
  21. .use(calc())
  22. .process(css)
  23. .css
  24. ```
  25. Using this `input.css`:
  26. ```css
  27. h1 {
  28. font-size: calc(16px * 2);
  29. height: calc(100px - 2em);
  30. width: calc(2*var(--base-width));
  31. margin-bottom: calc(16px * 1.5);
  32. }
  33. ```
  34. you will get:
  35. ```css
  36. h1 {
  37. font-size: 32px;
  38. height: calc(100px - 2em);
  39. width: calc(2*var(--base-width));
  40. margin-bottom: 24px
  41. }
  42. ```
  43. Checkout [tests] for more examples.
  44. ### Options
  45. #### `precision` (default: `5`)
  46. Allow you to define the precision for decimal numbers.
  47. ```js
  48. var out = postcss()
  49. .use(calc({precision: 10}))
  50. .process(css)
  51. .css
  52. ```
  53. #### `preserve` (default: `false`)
  54. Allow you to preserve calc() usage in output so browsers will handle decimal
  55. precision themselves.
  56. ```js
  57. var out = postcss()
  58. .use(calc({preserve: true}))
  59. .process(css)
  60. .css
  61. ```
  62. #### `warnWhenCannotResolve` (default: `false`)
  63. Adds warnings when calc() are not reduced to a single value.
  64. ```js
  65. var out = postcss()
  66. .use(calc({warnWhenCannotResolve: true}))
  67. .process(css)
  68. .css
  69. ```
  70. #### `mediaQueries` (default: `false`)
  71. Allows calc() usage as part of media query declarations.
  72. ```js
  73. var out = postcss()
  74. .use(calc({mediaQueries: true}))
  75. .process(css)
  76. .css
  77. ```
  78. #### `selectors` (default: `false`)
  79. Allows calc() usage as part of selectors.
  80. ```js
  81. var out = postcss()
  82. .use(calc({selectors: true}))
  83. .process(css)
  84. .css
  85. ```
  86. Example:
  87. ```css
  88. div[data-size="calc(3*3)"] {
  89. width: 100px;
  90. }
  91. ```
  92. ---
  93. ## Related PostCSS plugins
  94. To replace the value of CSS custom properties at build time, try [PostCSS Custom Properties].
  95. ## Contributing
  96. Work on a branch, install dev-dependencies, respect coding style & run tests
  97. before submitting a bug fix or a feature.
  98. ```bash
  99. git clone git@github.com:postcss/postcss-calc.git
  100. git checkout -b patch-1
  101. npm install
  102. npm test
  103. ```
  104. ## [Changelog](CHANGELOG.md)
  105. ## [License](LICENSE)
  106. [git-img]: https://img.shields.io/badge/support-chat-blue.svg
  107. [git-url]: https://gitter.im/postcss/postcss
  108. [npm-img]: https://img.shields.io/npm/v/postcss-calc.svg
  109. [npm-url]: https://www.npmjs.com/package/postcss-calc
  110. [PostCSS]: https://github.com/postcss
  111. [PostCSS Calc]: https://github.com/postcss/postcss-calc
  112. [PostCSS Custom Properties]: https://github.com/postcss/postcss-custom-properties
  113. [tests]: src/__tests__/index.js
  114. [W3C calc() implementation]: https://www.w3.org/TR/css3-values/#calc-notation