postcss-custom-selectors (6.0.3)

Published 2026-02-24 14:02:52 +00:00 by atheaadmin

Installation

registry=
npm install postcss-custom-selectors@6.0.3
"postcss-custom-selectors": "6.0.3"

About this package

PostCSS Custom Selectors PostCSS Logo

npm version CSS Standard Status Build Status Discord

PostCSS Custom Selectors lets you define @custom-selector in CSS following the Custom Selectors Specification.

@custom-selector :--heading h1, h2, h3;

article :--heading + p {
	margin-top: 0;
}

/* becomes */

article h1 + p,article h2 + p,article h3 + p {
	margin-top: 0;
}

Usage

Add PostCSS Custom Selectors to your project:

npm install postcss postcss-custom-selectors --save-dev

Use it as a PostCSS plugin:

const postcss = require('postcss');
const postcssCustomSelectors = require('postcss-custom-selectors');

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

PostCSS Custom Selectors runs in all Node environments, with special instructions for:

Node PostCSS CLI Webpack Create React App Gulp Grunt

Options

preserve

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

postcssCustomSelectors({ preserve: true })
@custom-selector :--heading h1, h2, h3;

article :--heading + p {
	margin-top: 0;
}

/* becomes */

@custom-selector :--heading h1, h2, h3;

article h1 + p,article h2 + p,article h3 + p {
	margin-top: 0;
}

article :--heading + p {
	margin-top: 0;
}

importFrom

The importFrom option specifies sources where custom selectors can be imported from, which might be CSS, JS, and JSON files, functions, and directly passed objects.

postcssCustomSelectors({
	importFrom: 'path/to/file.css' // => @custom-selector :--heading h1, h2, h3;
});
article :--heading + p {
	margin-top: 0;
}

/* becomes */

article h1 + p, article h2 + p, article h3 + p {}

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 selectors using the customProperties or custom-properties key.

postcssCustomSelectors({
	importFrom: [
		'path/to/file.css',
		'and/then/this.js',
		'and/then/that.json',
		{
			customSelectors: { ':--heading': 'h1, h2, h3' }
		},
		() => {
			const customProperties = { ':--heading': 'h1, h2, h3' };

			return { customProperties };
		}
	]
});

exportTo

The exportTo option specifies destinations where custom selectors can be exported to, which might be CSS, JS, and JSON files, functions, and directly passed objects.

postcssCustomSelectors({
	exportTo: 'path/to/file.css' // @custom-selector :--heading h1, h2, h3;
});

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 selectors using the customProperties or custom-properties key.

const cachedObject = { customSelectors: {} };

postcssCustomSelectors({
	exportTo: [
		'path/to/file.css',   // @custom-selector :--heading h1, h2, h3;
		'and/then/this.js',   // module.exports = { customSelectors: { ':--heading': 'h1, h2, h3' } }
		'and/then/this.mjs',  // export const customSelectors = { ':--heading': 'h1, h2, h3' } }
		'and/then/that.json', // { "custom-selectors": { ":--heading": "h1, h2, h3" } }
		cachedObject,
		customProperties => {
			customProperties    // { ':--heading': 'h1, h2, h3' }
		}
	]
});

Dependencies

Dependencies

ID Version
postcss-selector-parser ^6.0.4

Peer Dependencies

ID Version
postcss ^8.3

Keywords

at-rule atrule css csswg custom declarative extensions postcss postcss-plugin rule selectors specification w3c
Details
npm
2026-02-24 14:02:52 +00:00
0
MIT
latest
6.8 KiB
Assets (1)
Versions (1) View all
6.0.3 2026-02-24