| 1 | # PostCSS Custom Media [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
|
|---|
| 2 |
|
|---|
| 3 | [<img alt="npm version" src="https://img.shields.io/npm/v/postcss-custom-media.svg" height="20">][npm-url] [<img alt="CSS Standard Status" src="https://cssdb.org/images/badges/custom-media-queries.svg" height="20">][css-url] [<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url] [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
|
|---|
| 4 |
|
|---|
| 5 | [PostCSS Custom Media] lets you define `@custom-media` in CSS following the [Custom Media Specification].
|
|---|
| 6 |
|
|---|
| 7 | ```pcss
|
|---|
| 8 | @custom-media --small-viewport (max-width: 30em);
|
|---|
| 9 |
|
|---|
| 10 | @media (--small-viewport) {
|
|---|
| 11 | /* styles for small viewport */
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | /* becomes */
|
|---|
| 15 |
|
|---|
| 16 | @media (max-width: 30em) {
|
|---|
| 17 | /* styles for small viewport */
|
|---|
| 18 | }
|
|---|
| 19 | ```
|
|---|
| 20 |
|
|---|
| 21 | ## Usage
|
|---|
| 22 |
|
|---|
| 23 | Add [PostCSS Custom Media] to your project:
|
|---|
| 24 |
|
|---|
| 25 | ```bash
|
|---|
| 26 | npm install postcss postcss-custom-media --save-dev
|
|---|
| 27 | ```
|
|---|
| 28 |
|
|---|
| 29 | Use it as a [PostCSS] plugin:
|
|---|
| 30 |
|
|---|
| 31 | ```js
|
|---|
| 32 | const postcss = require('postcss');
|
|---|
| 33 | const postcssCustomMedia = require('postcss-custom-media');
|
|---|
| 34 |
|
|---|
| 35 | postcss([
|
|---|
| 36 | postcssCustomMedia(/* pluginOptions */)
|
|---|
| 37 | ]).process(YOUR_CSS /*, processOptions */);
|
|---|
| 38 | ```
|
|---|
| 39 |
|
|---|
| 40 | [PostCSS Custom Media] runs in all Node environments, with special
|
|---|
| 41 | instructions for:
|
|---|
| 42 |
|
|---|
| 43 | | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
|
|---|
| 44 | | --- | --- | --- | --- | --- | --- |
|
|---|
| 45 |
|
|---|
| 46 | ## Options
|
|---|
| 47 |
|
|---|
| 48 | ### preserve
|
|---|
| 49 |
|
|---|
| 50 | The `preserve` option determines whether the original notation
|
|---|
| 51 | is preserved. By default, it is not preserved.
|
|---|
| 52 |
|
|---|
| 53 | ```js
|
|---|
| 54 | postcssCustomMedia({ preserve: true })
|
|---|
| 55 | ```
|
|---|
| 56 |
|
|---|
| 57 | ```pcss
|
|---|
| 58 | @custom-media --small-viewport (max-width: 30em);
|
|---|
| 59 |
|
|---|
| 60 | @media (--small-viewport) {
|
|---|
| 61 | /* styles for small viewport */
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /* becomes */
|
|---|
| 65 |
|
|---|
| 66 | @custom-media --small-viewport (max-width: 30em);
|
|---|
| 67 |
|
|---|
| 68 | @media (max-width: 30em) {
|
|---|
| 69 | /* styles for small viewport */
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | @media (--small-viewport) {
|
|---|
| 73 | /* styles for small viewport */
|
|---|
| 74 | }
|
|---|
| 75 | ```
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 | ### importFrom
|
|---|
| 79 |
|
|---|
| 80 | The `importFrom` option specifies sources where custom media can be imported
|
|---|
| 81 | from, which might be CSS, JS, and JSON files, functions, and directly passed
|
|---|
| 82 | objects.
|
|---|
| 83 |
|
|---|
| 84 | ```js
|
|---|
| 85 | postcssCustomMedia({
|
|---|
| 86 | importFrom: 'path/to/file.css' // => @custom-selector --small-viewport (max-width: 30em);
|
|---|
| 87 | });
|
|---|
| 88 | ```
|
|---|
| 89 |
|
|---|
| 90 | ```pcss
|
|---|
| 91 | @media (max-width: 30em) {
|
|---|
| 92 | /* styles for small viewport */
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | @media (--small-viewport) {
|
|---|
| 96 | /* styles for small viewport */
|
|---|
| 97 | }
|
|---|
| 98 | ```
|
|---|
| 99 |
|
|---|
| 100 | Multiple sources can be passed into this option, and they will be parsed in the
|
|---|
| 101 | order they are received. JavaScript files, JSON files, functions, and objects
|
|---|
| 102 | will need to namespace custom media using the `customMedia` or
|
|---|
| 103 | `custom-media` key.
|
|---|
| 104 |
|
|---|
| 105 | ```js
|
|---|
| 106 | postcssCustomMedia({
|
|---|
| 107 | importFrom: [
|
|---|
| 108 | 'path/to/file.css',
|
|---|
| 109 | 'and/then/this.js',
|
|---|
| 110 | 'and/then/that.json',
|
|---|
| 111 | {
|
|---|
| 112 | customMedia: { '--small-viewport': '(max-width: 30em)' }
|
|---|
| 113 | },
|
|---|
| 114 | () => {
|
|---|
| 115 | const customMedia = { '--small-viewport': '(max-width: 30em)' };
|
|---|
| 116 |
|
|---|
| 117 | return { customMedia };
|
|---|
| 118 | }
|
|---|
| 119 | ]
|
|---|
| 120 | });
|
|---|
| 121 | ```
|
|---|
| 122 |
|
|---|
| 123 | ### exportTo
|
|---|
| 124 |
|
|---|
| 125 | The `exportTo` option specifies destinations where custom media can be exported
|
|---|
| 126 | to, which might be CSS, JS, and JSON files, functions, and directly passed
|
|---|
| 127 | objects.
|
|---|
| 128 |
|
|---|
| 129 | ```js
|
|---|
| 130 | postcssCustomMedia({
|
|---|
| 131 | exportTo: 'path/to/file.css' // @custom-media --small-viewport (max-width: 30em);
|
|---|
| 132 | });
|
|---|
| 133 | ```
|
|---|
| 134 |
|
|---|
| 135 | Multiple destinations can be passed into this option, and they will be parsed
|
|---|
| 136 | in the order they are received. JavaScript files, JSON files, and objects will
|
|---|
| 137 | need to namespace custom media using the `customMedia` or
|
|---|
| 138 | `custom-media` key.
|
|---|
| 139 |
|
|---|
| 140 | ```js
|
|---|
| 141 | const cachedObject = { customMedia: {} };
|
|---|
| 142 |
|
|---|
| 143 | postcssCustomMedia({
|
|---|
| 144 | exportTo: [
|
|---|
| 145 | 'path/to/file.css', // @custom-media --small-viewport (max-width: 30em);
|
|---|
| 146 | 'and/then/this.js', // module.exports = { customMedia: { '--small-viewport': '(max-width: 30em)' } }
|
|---|
| 147 | 'and/then/this.mjs', // export const customMedia = { '--small-viewport': '(max-width: 30em)' } }
|
|---|
| 148 | 'and/then/that.json', // { "custom-media": { "--small-viewport": "(max-width: 30em)" } }
|
|---|
| 149 | cachedObject,
|
|---|
| 150 | customMedia => {
|
|---|
| 151 | customMedia // { '--small-viewport': '(max-width: 30em)' }
|
|---|
| 152 | }
|
|---|
| 153 | ]
|
|---|
| 154 | });
|
|---|
| 155 | ```
|
|---|
| 156 |
|
|---|
| 157 | See example exports written to [CSS](test/export-media.css),
|
|---|
| 158 | [JS](test/export-media.js), [MJS](test/export-media.mjs), and
|
|---|
| 159 | [JSON](test/export-media.json).
|
|---|
| 160 |
|
|---|
| 161 | [cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
|
|---|
| 162 | [css-url]: https://cssdb.org/#custom-media-queries
|
|---|
| 163 | [discord]: https://discord.gg/bUadyRwkJS
|
|---|
| 164 | [npm-url]: https://www.npmjs.com/package/postcss-custom-media
|
|---|
| 165 |
|
|---|
| 166 | [Gulp PostCSS]: https://github.com/postcss/gulp-postcss
|
|---|
| 167 | [Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
|
|---|
| 168 | [PostCSS]: https://github.com/postcss/postcss
|
|---|
| 169 | [PostCSS Loader]: https://github.com/postcss/postcss-loader
|
|---|
| 170 | [PostCSS Custom Media]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-custom-media
|
|---|
| 171 | [Custom Media Specification]: https://www.w3.org/TR/mediaqueries-5/#at-ruledef-custom-media
|
|---|