10.0.4 • Published 15 days ago

postcss-custom-media v10.0.4

Weekly downloads
3,967,510
License
MIT
Repository
github
Last release
15 days ago

PostCSS Custom Media

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

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 */
}

true and false

With @custom-media you can use the constants true and false. These are especially handy when debugging.

If you are unsure how your page is affected when a certain media query matches or not you can use these, to quickly toggle the results. This plugin downgrades these queries to something that works in all browsers.

Quickly check the result as if the query matches:

@custom-media --small-viewport true;

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

/* becomes */

@media (max-color:2147477350) {
	/* styles for small viewport */
}

Quickly check the result as if the query does not match:

@custom-media --small-viewport false;

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

/* becomes */

@media (color:2147477350) {
	/* styles for small viewport */
}

logical evaluation of complex media queries

It is impossible to accurately and correctly resolve complex @custom-media queries as these depend on the browser the queries will eventually run in.

Some of these queries will have only one possible outcome but we have to account for all possible queries in this plugin.

!NOTE When handling complex media queries you will see that your CSS is doubled for each level of complexity. GZIP works great to de-dupe this but having a lot of complex media queries will have a performance impact.

An example of a very complex (and artificial) use-case :

@custom-media --a-complex-query tty and (min-width: 300px);

@media not screen and ((not (--a-complex-query)) or (color)) {
	/* Your CSS */
}

/* becomes */


@media tty and (min-width: 300px) {
@media not screen and ((not (max-color:2147477350)) or (color)) {
	/* Your CSS */
}
}
@media not  tty and (min-width: 300px) {
@media not screen and ((not (color:2147477350)) or (color)) {
	/* Your CSS */
}
}

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:

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 */
}

Modular CSS Processing

If you're using Modular CSS such as, CSS Modules, postcss-loader or vanilla-extract to name a few, you'll probably notice that custom media queries are not being resolved. This happens because each file is processed separately so unless you import the custom media query definitions in each file, they won't be resolved.

To overcome this, we recommend using the PostCSS Global Data plugin which allows you to pass a list of files that will be globally available. The plugin won't inject any extra code in the output but will provide the context needed to resolve custom media queries.

For it to run it needs to be placed before the PostCSS Custom Media plugin.

const postcss = require('postcss');
const postcssCustomMedia = require('postcss-custom-media');
const postcssGlobalData = require('@csstools/postcss-global-data');

postcss([
	postcssGlobalData({
		files: [
			'path/to/your/custom-media-queries.css'
		]
	}),
	postcssCustomMedia(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);
postcss-preset-envarcade-ui@commercetools-frontend/mc-scriptslipemat-postcss-boilerplatelipemat-js-boilerplatepostcss-config-packce-webpack-tempdstack-reactconnexstyleguide@dnb-hugo/postcss-configtuum@infinitebrahmanuniverse/nolb-postcss-c@d-zero/dz-env-cssparottasklif-ui-kitsklif-apiparticle41-reshaped-ui-lib@everything-registry/sub-chunk-2460sklif-uilevel4letterhousehiqhover-media-queryhelp-widgetkirei-cssgulp-config-devgulp-dev-kitlearning_locker@jetshop/react-scriptsnathanjosiah-sc5-styleguidenartex.concise.cssnka-gantt-task-reactmicroend-componentmsops-cssjulien-easy-modalmvf-patternsmy-library-buttonmetalsmith-bundlenpm-dev-boilerplatemavectralsv-componentsedge-postcssedgecsseditor.js-custom-multiple-selectededitorjs_forkedelectron-react-app-scriptsecstacyember-mappghost-spiritgridssgulp-postcss-nimedevfeds-clifrack-postcssf-of-sgenerator-react-staoberd-oui-webpack-config@paralect/pdf-service-clientomni-common-uiprotobuilderplastticpostcss-basscsspostcss-blue-pluginspostcss-cssnextpostcss-csspluspostcss-preset-ccbpostcss-preset-signalsciencespostcss-mixpostcss-niduscsspostcss-mythpolygulpportal_kitreact-lightbox-pack-18supportparis-nord-estreact-solid-gradient-pickerpineglade-apppgridpickupbiz-npm-packagepm-galleryreshapedxgency-dialer@gravityforms/gulp-tasks@helpfulhuman/postcss-preset@jameskolce/novavc-webpack-vendorsvirtual-tour-guide@mayuriachewad/pickupbiz-npm-package@kanopi/pack@knooks/use-title@miracle-box/flint@mizchi/postcss-config@kkt/postcss-preset-env@kolcelabs/deltawebrenderycsswildplatewm-cli@lonelyplanet/travel-agent@nativeweb/build@lipemat/js-boilerplate@lipemat/postcss-boilerplate
10.0.4

15 days ago

10.0.3

1 month ago

10.0.2

6 months ago

10.0.1

6 months ago

9.1.5

9 months ago

9.1.4

10 months ago

10.0.0

9 months ago

9.1.3

12 months ago

9.1.2

1 year ago

9.1.1

1 year ago

9.1.0

1 year ago

9.0.1

1 year ago

9.0.0

1 year ago

8.0.1

2 years ago

8.0.2

2 years ago

8.0.0

3 years ago

7.0.8

5 years ago

7.0.7

5 years ago

7.0.6

5 years ago

7.0.5

5 years ago

7.0.4

6 years ago

7.0.3

6 years ago

7.0.2

6 years ago

7.0.1

6 years ago

7.0.0

6 years ago

6.0.0

7 years ago

5.0.1

8 years ago

5.0.0

9 years ago

4.1.0

9 years ago

4.0.0

9 years ago

3.0.0

9 years ago

1.3.0

9 years ago

1.2.1

9 years ago

1.2.0

9 years ago

1.1.0

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

2.0.0

10 years ago