web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.8 KiB

  1. # PostCSS Custom Media [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][postcss]
  2. [![NPM Version][npm-img]][npm-url]
  3. [![CSS Standard Status][css-img]][css-url]
  4. [![Build Status][cli-img]][cli-url]
  5. [![Support Chat][git-img]][git-url]
  6. [PostCSS Custom Media] lets you use Custom Media Queries in CSS, following the
  7. [CSS Media Queries] specification.
  8. ```pcss
  9. @custom-media --small-viewport (max-width: 30em);
  10. @media (--small-viewport) {
  11. /* styles for small viewport */
  12. }
  13. /* becomes */
  14. @media (max-width: 30em) {
  15. /* styles for small viewport */
  16. }
  17. ```
  18. ## Usage
  19. Add [PostCSS Custom Media] to your project:
  20. ```bash
  21. npm install postcss-custom-media --save-dev
  22. ```
  23. Use [PostCSS Custom Media] to process your CSS:
  24. ```js
  25. const postcssCustomMedia = require('postcss-custom-media');
  26. postcssCustomMedia.process(YOUR_CSS /*, processOptions, pluginOptions */);
  27. ```
  28. Or use it as a [PostCSS] plugin:
  29. ```js
  30. const postcss = require('postcss');
  31. const postcssCustomMedia = require('postcss-custom-media');
  32. postcss([
  33. postcssCustomMedia(/* pluginOptions */)
  34. ]).process(YOUR_CSS /*, processOptions */);
  35. ```
  36. [PostCSS Custom Media] runs in all Node environments, with special instructions for:
  37. | [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) |
  38. | --- | --- | --- | --- | --- | --- |
  39. ## Options
  40. ### preserve
  41. The `preserve` option determines whether custom media and atrules using custom
  42. media should be preserved in their original form.
  43. ```pcss
  44. @custom-media --small-viewport (max-width: 30em);
  45. @media (--small-viewport) {
  46. /* styles for small viewport */
  47. }
  48. /* becomes */
  49. @custom-media --small-viewport (max-width: 30em);
  50. @media (max-width: 30em) {
  51. /* styles for small viewport */
  52. }
  53. @media (--small-viewport) {
  54. /* styles for small viewport */
  55. }
  56. ```
  57. ### importFrom
  58. The `importFrom` option specifies sources where custom media can be imported
  59. from, which might be CSS, JS, and JSON files, functions, and directly passed
  60. objects.
  61. ```js
  62. postcssCustomMedia({
  63. importFrom: 'path/to/file.css' // => @custom-selector --small-viewport (max-width: 30em);
  64. });
  65. ```
  66. ```pcss
  67. @media (max-width: 30em) {
  68. /* styles for small viewport */
  69. }
  70. @media (--small-viewport) {
  71. /* styles for small viewport */
  72. }
  73. ```
  74. Multiple sources can be passed into this option, and they will be parsed in the
  75. order they are received. JavaScript files, JSON files, functions, and objects
  76. will need to namespace custom media using the `customMedia` or
  77. `custom-media` key.
  78. ```js
  79. postcssCustomMedia({
  80. importFrom: [
  81. 'path/to/file.css',
  82. 'and/then/this.js',
  83. 'and/then/that.json',
  84. {
  85. customMedia: { '--small-viewport': '(max-width: 30em)' }
  86. },
  87. () => {
  88. const customMedia = { '--small-viewport': '(max-width: 30em)' };
  89. return { customMedia };
  90. }
  91. ]
  92. });
  93. ```
  94. ### exportTo
  95. The `exportTo` option specifies destinations where custom media can be exported
  96. to, which might be CSS, JS, and JSON files, functions, and directly passed
  97. objects.
  98. ```js
  99. postcssCustomMedia({
  100. exportTo: 'path/to/file.css' // @custom-media --small-viewport (max-width: 30em);
  101. });
  102. ```
  103. Multiple destinations can be passed into this option, and they will be parsed
  104. in the order they are received. JavaScript files, JSON files, and objects will
  105. need to namespace custom media using the `customMedia` or
  106. `custom-media` key.
  107. ```js
  108. const cachedObject = { customMedia: {} };
  109. postcssCustomMedia({
  110. exportTo: [
  111. 'path/to/file.css', // @custom-media --small-viewport (max-width: 30em);
  112. 'and/then/this.js', // module.exports = { customMedia: { '--small-viewport': '(max-width: 30em)' } }
  113. 'and/then/this.mjs', // export const customMedia = { '--small-viewport': '(max-width: 30em)' } }
  114. 'and/then/that.json', // { "custom-media": { "--small-viewport": "(max-width: 30em)" } }
  115. cachedObject,
  116. customMedia => {
  117. customMedia // { '--small-viewport': '(max-width: 30em)' }
  118. }
  119. ]
  120. });
  121. ```
  122. See example exports written to [CSS](test/export-media.css),
  123. [JS](test/export-media.js), [MJS](test/export-media.mjs), and
  124. [JSON](test/export-media.json).
  125. [cli-img]: https://img.shields.io/travis/postcss/postcss-custom-media/master.svg
  126. [cli-url]: https://travis-ci.org/postcss/postcss-custom-media
  127. [css-img]: https://cssdb.org/badge/custom-media-queries.svg
  128. [css-url]: https://cssdb.org/#custom-media-queries
  129. [git-img]: https://img.shields.io/badge/support-chat-blue.svg
  130. [git-url]: https://gitter.im/postcss/postcss
  131. [npm-img]: https://img.shields.io/npm/v/postcss-custom-media.svg
  132. [npm-url]: https://www.npmjs.com/package/postcss-custom-media
  133. [CSS Media Queries]: https://drafts.csswg.org/mediaqueries-5/#custom-mq
  134. [PostCSS]: https://github.com/postcss/postcss
  135. [PostCSS Custom Media]: https://github.com/postcss/postcss-custom-media