Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
postcss-custom-properties
Advanced tools
The postcss-custom-properties npm package is a PostCSS plugin that allows you to use CSS Custom Properties (also known as CSS variables) in environments that do not support them natively. It transforms CSS variables into static values based on your configurations, making it easier to maintain themes and styles dynamically across your project.
Transform CSS Custom Properties
This feature allows the transformation of CSS custom properties into their corresponding static values. It is useful for supporting older browsers that do not understand CSS variables.
/* Input CSS */
:root {
--main-color: red;
}
a {
color: var(--main-color);
}
/* Output CSS */
a {
color: red;
}
Preserve option
With the preserve option set to true, the plugin outputs both the transformed static value and the original variable. This is useful for progressive enhancement.
/* Input CSS */
:root {
--main-color: red;
}
a {
color: var(--main-color);
}
/* Output CSS with preserve: true */
a {
color: red;
color: var(--main-color);
}
cssnext is a PostCSS plugin that allows you to use future CSS features today. It includes support for CSS custom properties among other features. Compared to postcss-custom-properties, cssnext offers a broader range of CSS features but might be heavier due to its comprehensive nature.
postcss-preset-env lets you convert modern CSS into something most browsers can understand, determining the polyfills you need based on your targeted browsers or runtime environments. It includes handling of CSS custom properties as part of its feature set, similar to postcss-custom-properties, but is more configurable and includes various stages of CSS specifications.
PostCSS Custom Properties lets you use Custom Properties in CSS, following the CSS Custom Properties specification.
:root {
--color: red;
}
h1 {
color: var(--color);
}
/* becomes */
:root {
--color: red;
}
h1 {
color: red;
color: var(--color);
}
Add PostCSS Custom Properties to your project:
npm install postcss-custom-properties --save-dev
Use PostCSS Custom Properties to process your CSS:
const postcssCustomProperties = require('postcss-custom-properties');
postcssCustomProperties.process(YOUR_CSS /*, processOptions, pluginOptions */);
Or use it as a PostCSS plugin:
const postcss = require('postcss');
const postcssCustomProperties = require('postcss-custom-properties');
postcss([
postcssCustomProperties(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);
PostCSS Custom Properties runs in all Node environments, with special instructions for:
Node | PostCSS CLI | Webpack | Create React App | Gulp | Grunt |
---|
The preserve
option determines whether Custom Properties and properties using
custom properties should be preserved in their original form. By default, both
of these are preserved.
postcssCustomProperties({
preserve: false
});
:root {
--color: red;
}
h1 {
color: var(--color);
}
/* becomes */
h1 {
color: red;
}
The importFrom
option specifies sources where Custom Properties can be imported
from, which might be CSS, JS, and JSON files, functions, and directly passed
objects.
postcssCustomProperties({
importFrom: 'path/to/file.css' // => :root { --color: red }
});
h1 {
color: var(--color);
}
/* becomes */
h1 {
color: red;
}
Multiple sources can be passed into this option, and they will be parsed in the
order they are received. JavaScript files, JSON files, functions, and objects
will need to namespace Custom Properties using the customProperties
or
custom-properties
key.
postcssCustomProperties({
importFrom: [
'path/to/file.css', // :root { --color: red; }
'and/then/this.js', // module.exports = { customProperties: { '--color': 'red' } }
'and/then/that.json', // { "custom-properties": { "--color": "red" } }
{
customProperties: { '--color': 'red' }
},
() => {
const customProperties = { '--color': 'red' };
return { customProperties };
}
]
});
See example imports written in CSS, JS, and JSON.
The exportTo
option specifies destinations where Custom Properties can be exported
to, which might be CSS, JS, and JSON files, functions, and directly passed
objects.
postcssCustomProperties({
exportTo: 'path/to/file.css' // :root { --color: red; }
});
Multiple destinations can be passed into this option, and they will be parsed
in the order they are received. JavaScript files, JSON files, and objects will
need to namespace Custom Properties using the customProperties
or
custom-properties
key.
const cachedObject = { customProperties: {} };
postcssCustomProperties({
exportTo: [
'path/to/file.css', // :root { --color: red; }
'and/then/this.js', // module.exports = { customProperties: { '--color': 'red' } }
'and/then/this.mjs', // export const customProperties = { '--color': 'red' } }
'and/then/that.json', // { "custom-properties": { "--color": "red" } }
cachedObject,
customProperties => {
customProperties // { '--color': 'red' }
}
]
});
FAQs
Use Custom Properties Queries in CSS
The npm package postcss-custom-properties receives a total of 2,185,558 weekly downloads. As such, postcss-custom-properties popularity was classified as popular.
We found that postcss-custom-properties demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.