@csstools/postcss-random-function
Use the random function in CSS
Last updated 2 months ago by romainmenke .
MIT-0 · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @csstools/postcss-random-function 
SYNC missed versions from official npm registry.

PostCSS Random Function PostCSS Logo

npm install @csstools/postcss-random-function --save-dev

PostCSS Random Function lets you use the random function, following the CSS Values 5 specification.

div {
	color: oklch(0.7 0.2 random(120deg, 240deg));
}

div {
	color: oklch(0.7 0.2 random(120deg, 240deg, 7deg));
}

div {
	margin: random(10px, 100px) random(10px, 100px);
	padding: random(--padding, 10px, 100px) random(--padding, 10px, 100px);
}

/* becomes */

div {
	color: oklch(0.7 0.2 calc(166.80407deg));
}

div {
	color: oklch(0.7 0.2 calc(169deg));
}

div {
	margin: calc(45.10305px) calc(45.10305px);
	padding: calc(73.36206px) calc(73.36206px);
}

[!NOTE] Generated values are deterministic pseudo random numbers. Generating values twice with the same input will give the same result. The input length of the CSS source file is used as a random seed.

Usage

Add PostCSS Random Function to your project:

npm install postcss @csstools/postcss-random-function --save-dev

Use it as a PostCSS plugin:

const postcss = require('postcss');
const postcssRandomFunction = require('@csstools/postcss-random-function');

postcss([
	postcssRandomFunction(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);

⚠️ About custom properties

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.

Because of that, any usage that contains a var is skipped.

Options

preserve

The preserve option determines whether the original notation is preserved. By default, it is not preserved.

postcssRandomFunction({ preserve: true })
div {
	color: oklch(0.7 0.2 random(120deg, 240deg));
}

div {
	color: oklch(0.7 0.2 random(120deg, 240deg, 7deg));
}

div {
	margin: random(10px, 100px) random(10px, 100px);
	padding: random(--padding, 10px, 100px) random(--padding, 10px, 100px);
}

/* becomes */

div {
	color: oklch(0.7 0.2 calc(166.80407deg));
	color: oklch(0.7 0.2 random(120deg, 240deg));
}

div {
	color: oklch(0.7 0.2 calc(169deg));
	color: oklch(0.7 0.2 random(120deg, 240deg, 7deg));
}

div {
	margin: calc(45.10305px) calc(45.10305px);
	margin: random(10px, 100px) random(10px, 100px);
	padding: calc(73.36206px) calc(73.36206px);
	padding: random(--padding, 10px, 100px) random(--padding, 10px, 100px);
}

Current Tags

  • 4.0.6                                ...           latest (10 days ago)

17 Versions

  • 4.0.6                                ...           10 days ago
  • 4.0.5                                ...           2 months ago
  • 4.0.4                                ...           2 months ago
  • 4.0.3                                ...           2 months ago
  • 4.0.2                                ...           2 months ago
  • 4.0.1                                ...           3 months ago
  • 4.0.0                                ...           4 months ago
  • 3.0.3                                ...           6 months ago
  • 3.0.2                                ...           7 months ago
  • 3.0.1                                ...           9 months ago
  • 3.0.0                                ...           10 months ago
  • 2.0.1                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Downloads
Total 37
Today 0
This Week 0
This Month 30
Last Day 0
Last Week 11
Last Month 7
Dev Dependencies (0)
None
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE