Socket
Socket
Sign inDemoInstall

postcss-custom-media

Package Overview
Dependencies
4
Maintainers
4
Versions
39
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    postcss-custom-media

Use Custom Media Queries in CSS


Version published
Weekly downloads
5.9M
decreased by-2.09%
Maintainers
4
Install size
24.2 kB
Created
Weekly downloads
 

Readme

Source

PostCSS Custom Media PostCSS Logo

npm version CSS Standard Status Build Status Discord

PostCSS Custom Media lets you define @custom-media in CSS following the Custom Media Specification.

@custom-media --small-viewport (max-width: 30em);

@media (--small-viewport) {
	/* styles for small viewport */
}

/* becomes */

@media (max-width: 30em) {
	/* styles for small viewport */
}

Usage

Add PostCSS Custom Media to your project:

npm install postcss postcss-custom-media --save-dev

Use it as a PostCSS plugin:

const postcss = require('postcss');
const postcssCustomMedia = require('postcss-custom-media');

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

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

NodePostCSS CLIWebpackCreate React AppGulpGrunt

Options

preserve

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

postcssCustomMedia({ preserve: true })
@custom-media --small-viewport (max-width: 30em);

@media (--small-viewport) {
	/* styles for small viewport */
}

/* becomes */

@custom-media --small-viewport (max-width: 30em);

@media (max-width: 30em) {
	/* styles for small viewport */
}

@media (--small-viewport) {
	/* styles for small viewport */
}

importFrom

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

postcssCustomMedia({
	importFrom: 'path/to/file.css' // => @custom-selector --small-viewport (max-width: 30em);
});
@media (max-width: 30em) {
	/* styles for small viewport */
}

@media (--small-viewport) {
	/* styles for small viewport */
}

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 media using the customMedia or custom-media key.

postcssCustomMedia({
	importFrom: [
		'path/to/file.css',
		'and/then/this.js',
		'and/then/that.json',
		{
			customMedia: { '--small-viewport': '(max-width: 30em)' }
		},
		() => {
			const customMedia = { '--small-viewport': '(max-width: 30em)' };

			return { customMedia };
		}
	]
});

exportTo

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

postcssCustomMedia({
	exportTo: 'path/to/file.css' // @custom-media --small-viewport (max-width: 30em);
});

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 media using the customMedia or custom-media key.

const cachedObject = { customMedia: {} };

postcssCustomMedia({
	exportTo: [
		'path/to/file.css',   // @custom-media --small-viewport (max-width: 30em);
		'and/then/this.js',   // module.exports = { customMedia: { '--small-viewport': '(max-width: 30em)' } }
		'and/then/this.mjs',  // export const customMedia = { '--small-viewport': '(max-width: 30em)' } }
		'and/then/that.json', // { "custom-media": { "--small-viewport": "(max-width: 30em)" } }
		cachedObject,
		customMedia => {
			customMedia    // { '--small-viewport': '(max-width: 30em)' }
		}
	]
});

See example exports written to CSS, JS, MJS, and JSON.

Keywords

FAQs

Last updated on 03 Jun 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc