| 1 | # PostCSS Stepped Value Functions [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
|
|---|
| 2 |
|
|---|
| 3 | [<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/postcss-stepped-value-functions.svg" height="20">][npm-url] [<img alt="CSS Standard Status" src="https://cssdb.org/images/badges/stepped-value-functions.svg" height="20">][css-url] [<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url] [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
|
|---|
| 4 |
|
|---|
| 5 | [PostCSS Stepped Value Functions] lets you use `round`, `rem` and `mod` stepped value functions, following the [CSS Values 4].
|
|---|
| 6 |
|
|---|
| 7 | ```pcss
|
|---|
| 8 | .test-functions {
|
|---|
| 9 | padding: 8px mod(18px, 5px) 1px calc(rem(15px, 6px) + 50%);
|
|---|
| 10 | transform: rotate(mod(-140deg, -90deg));
|
|---|
| 11 | top: round(15px, 4px);
|
|---|
| 12 | right: round(nearest, 15px, 4px);
|
|---|
| 13 | bottom: round(up, 15px, 7px);
|
|---|
| 14 | left: round(down, 15px, 4px);
|
|---|
| 15 | width: round(to-zero, 15px, 4px);
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | /* becomes */
|
|---|
| 19 |
|
|---|
| 20 | .test-functions {
|
|---|
| 21 | padding: 8px 3px 1px calc(3px + 50%);
|
|---|
| 22 | transform: rotate(-50deg);
|
|---|
| 23 | top: 16px;
|
|---|
| 24 | right: 16px;
|
|---|
| 25 | bottom: 21px;
|
|---|
| 26 | left: 12px;
|
|---|
| 27 | width: 12px;
|
|---|
| 28 | }
|
|---|
| 29 | ```
|
|---|
| 30 |
|
|---|
| 31 | ## Usage
|
|---|
| 32 |
|
|---|
| 33 | Add [PostCSS Stepped Value Functions] to your project:
|
|---|
| 34 |
|
|---|
| 35 | ```bash
|
|---|
| 36 | npm install postcss @csstools/postcss-stepped-value-functions --save-dev
|
|---|
| 37 | ```
|
|---|
| 38 |
|
|---|
| 39 | Use it as a [PostCSS] plugin:
|
|---|
| 40 |
|
|---|
| 41 | ```js
|
|---|
| 42 | const postcss = require('postcss');
|
|---|
| 43 | const postcssSteppedValueFunctions = require('@csstools/postcss-stepped-value-functions');
|
|---|
| 44 |
|
|---|
| 45 | postcss([
|
|---|
| 46 | postcssSteppedValueFunctions(/* pluginOptions */)
|
|---|
| 47 | ]).process(YOUR_CSS /*, processOptions */);
|
|---|
| 48 | ```
|
|---|
| 49 |
|
|---|
| 50 | [PostCSS Stepped Value Functions] runs in all Node environments, with special
|
|---|
| 51 | instructions for:
|
|---|
| 52 |
|
|---|
| 53 | | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
|
|---|
| 54 | | --- | --- | --- | --- | --- | --- |
|
|---|
| 55 |
|
|---|
| 56 | ## ⚠️ About custom properties
|
|---|
| 57 |
|
|---|
| 58 | Given the dynamic nature of custom properties it's impossible to know what the variable value is, which means the plugin can't compute a final value for the stylesheet.
|
|---|
| 59 |
|
|---|
| 60 | Because of that, any usage that contains a `var` is skipped.
|
|---|
| 61 |
|
|---|
| 62 | ## Options
|
|---|
| 63 |
|
|---|
| 64 | ### preserve
|
|---|
| 65 |
|
|---|
| 66 | The `preserve` option determines whether the original notation
|
|---|
| 67 | is preserved. By default, it is not preserved.
|
|---|
| 68 |
|
|---|
| 69 | ```js
|
|---|
| 70 | postcssSteppedValueFunctions({ preserve: true })
|
|---|
| 71 | ```
|
|---|
| 72 |
|
|---|
| 73 | ```pcss
|
|---|
| 74 | .test-functions {
|
|---|
| 75 | padding: 8px mod(18px, 5px) 1px calc(rem(15px, 6px) + 50%);
|
|---|
| 76 | transform: rotate(mod(-140deg, -90deg));
|
|---|
| 77 | top: round(15px, 4px);
|
|---|
| 78 | right: round(nearest, 15px, 4px);
|
|---|
| 79 | bottom: round(up, 15px, 7px);
|
|---|
| 80 | left: round(down, 15px, 4px);
|
|---|
| 81 | width: round(to-zero, 15px, 4px);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /* becomes */
|
|---|
| 85 |
|
|---|
| 86 | .test-functions {
|
|---|
| 87 | padding: 8px 3px 1px calc(3px + 50%);
|
|---|
| 88 | padding: 8px mod(18px, 5px) 1px calc(rem(15px, 6px) + 50%);
|
|---|
| 89 | transform: rotate(-50deg);
|
|---|
| 90 | transform: rotate(mod(-140deg, -90deg));
|
|---|
| 91 | top: 16px;
|
|---|
| 92 | top: round(15px, 4px);
|
|---|
| 93 | right: 16px;
|
|---|
| 94 | right: round(nearest, 15px, 4px);
|
|---|
| 95 | bottom: 21px;
|
|---|
| 96 | bottom: round(up, 15px, 7px);
|
|---|
| 97 | left: 12px;
|
|---|
| 98 | left: round(down, 15px, 4px);
|
|---|
| 99 | width: 12px;
|
|---|
| 100 | width: round(to-zero, 15px, 4px);
|
|---|
| 101 | }
|
|---|
| 102 | ```
|
|---|
| 103 |
|
|---|
| 104 | ### onInvalid
|
|---|
| 105 |
|
|---|
| 106 | `onInvalid` option allows you to assign the `warn` value so you can get warnings when the usage of the functions is wrong.
|
|---|
| 107 |
|
|---|
| 108 | ```js
|
|---|
| 109 | postcssSteppedValueFunctions({ onInvalid: 'warn' })
|
|---|
| 110 | ```
|
|---|
| 111 |
|
|---|
| 112 | ```pcss
|
|---|
| 113 | .invalid {
|
|---|
| 114 | font-size: mod(18px, 5rem);
|
|---|
| 115 | }
|
|---|
| 116 | ```
|
|---|
| 117 |
|
|---|
| 118 | Will produce on the terminal:
|
|---|
| 119 |
|
|---|
| 120 | ```bash
|
|---|
| 121 | [postcss-stepped-value-functions]: Failed to transform mod(18px, 5rem) as the units don't match
|
|---|
| 122 | ```
|
|---|
| 123 |
|
|---|
| 124 | [cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
|
|---|
| 125 | [css-url]: https://cssdb.org/#stepped-value-functions
|
|---|
| 126 | [discord]: https://discord.gg/bUadyRwkJS
|
|---|
| 127 | [npm-url]: https://www.npmjs.com/package/@csstools/postcss-stepped-value-functions
|
|---|
| 128 |
|
|---|
| 129 | [Gulp PostCSS]: https://github.com/postcss/gulp-postcss
|
|---|
| 130 | [Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
|
|---|
| 131 | [PostCSS]: https://github.com/postcss/postcss
|
|---|
| 132 | [PostCSS Loader]: https://github.com/postcss/postcss-loader
|
|---|
| 133 | [PostCSS Stepped Value Functions]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-stepped-value-functions
|
|---|
| 134 | [CSS Values 4]: https://www.w3.org/TR/css-values-4/#round-func
|
|---|