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.

166 lines
4.5 KiB

  1. # PostCSS Custom Selectors [<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 Selectors] lets you use Custom Selectors in CSS, following the
  7. [CSS Extensions] specification.
  8. ```pcss
  9. @custom-selector :--heading h1, h2, h3;
  10. article :--heading + p {
  11. margin-top: 0;
  12. }
  13. /* becomes */
  14. article h1 + p, article h2 + p, article h3 + p {}
  15. ```
  16. ## Usage
  17. Add [PostCSS Custom Selectors] to your project:
  18. ```bash
  19. npm install postcss-custom-selectors --save-dev
  20. ```
  21. Use [PostCSS Custom Selectors] to process your CSS:
  22. ```js
  23. const postcssCustomSelectors = require('postcss-custom-selectors');
  24. postcssCustomSelectors.process(YOUR_CSS /*, processOptions, pluginOptions */);
  25. ```
  26. Or use it as a [PostCSS] plugin:
  27. ```js
  28. const postcss = require('postcss');
  29. const postcssCustomSelectors = require('postcss-custom-selectors');
  30. postcss([
  31. postcssCustomSelectors(/* pluginOptions */)
  32. ]).process(YOUR_CSS /*, processOptions */);
  33. ```
  34. [PostCSS Custom Selectors] runs in all Node environments, with special instructions for:
  35. | [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) |
  36. | --- | --- | --- | --- | --- | --- |
  37. ## Options
  38. ### preserve
  39. The `preserve` option determines whether custom selectors and rules using
  40. custom selectors should be preserved in their original form.
  41. ```pcss
  42. @custom-selector :--heading h1, h2, h3;
  43. article :--heading + p {
  44. margin-top: 0;
  45. }
  46. /* becomes */
  47. article h1 + p, article h2 + p, article h3 + p {}
  48. article :--heading + p {}
  49. ```
  50. ### importFrom
  51. The `importFrom` option specifies sources where custom selectors can be
  52. imported from, which might be CSS, JS, and JSON files, functions, and directly
  53. passed objects.
  54. ```js
  55. postcssCustomSelectors({
  56. importFrom: 'path/to/file.css' // => @custom-selector :--heading h1, h2, h3;
  57. });
  58. ```
  59. ```pcss
  60. article :--heading + p {
  61. margin-top: 0;
  62. }
  63. /* becomes */
  64. article h1 + p, article h2 + p, article h3 + p {}
  65. ```
  66. Multiple sources can be passed into this option, and they will be parsed in the
  67. order they are received. JavaScript files, JSON files, functions, and objects
  68. will need to namespace custom selectors using the `customProperties` or
  69. `custom-properties` key.
  70. ```js
  71. postcssCustomSelectors({
  72. importFrom: [
  73. 'path/to/file.css',
  74. 'and/then/this.js',
  75. 'and/then/that.json',
  76. {
  77. customSelectors: { ':--heading': 'h1, h2, h3' }
  78. },
  79. () => {
  80. const customProperties = { ':--heading': 'h1, h2, h3' };
  81. return { customProperties };
  82. }
  83. ]
  84. });
  85. ```
  86. ### exportTo
  87. The `exportTo` option specifies destinations where custom selectors can be
  88. exported to, which might be CSS, JS, and JSON files, functions, and directly
  89. passed objects.
  90. ```js
  91. postcssCustomSelectors({
  92. exportTo: 'path/to/file.css' // @custom-selector :--heading h1, h2, h3;
  93. });
  94. ```
  95. Multiple destinations can be passed into this option, and they will be parsed
  96. in the order they are received. JavaScript files, JSON files, and objects will
  97. need to namespace custom selectors using the `customProperties` or
  98. `custom-properties` key.
  99. ```js
  100. const cachedObject = { customSelectors: {} };
  101. postcssCustomSelectors({
  102. exportTo: [
  103. 'path/to/file.css', // @custom-selector :--heading h1, h2, h3;
  104. 'and/then/this.js', // module.exports = { customSelectors: { ':--heading': 'h1, h2, h3' } }
  105. 'and/then/this.mjs', // export const customSelectors = { ':--heading': 'h1, h2, h3' } }
  106. 'and/then/that.json', // { "custom-selectors": { ":--heading": "h1, h2, h3" } }
  107. cachedObject,
  108. customProperties => {
  109. customProperties // { ':--heading': 'h1, h2, h3' }
  110. }
  111. ]
  112. });
  113. ```
  114. [cli-img]: https://img.shields.io/travis/postcss/postcss-custom-selectors.svg
  115. [cli-url]: https://travis-ci.org/postcss/postcss-custom-selectors
  116. [css-img]: https://cssdb.org/badge/custom-selectors.svg
  117. [css-url]: https://cssdb.org/#custom-selectors
  118. [git-img]: https://img.shields.io/badge/support-chat-blue.svg
  119. [git-url]: https://gitter.im/postcss/postcss
  120. [npm-img]: https://img.shields.io/npm/v/postcss-custom-selectors.svg
  121. [npm-url]: https://www.npmjs.com/package/postcss-custom-selectors
  122. [CSS Extensions]: https://drafts.csswg.org/css-extensions/#custom-selectors
  123. [PostCSS]: https://github.com/postcss/postcss
  124. [PostCSS Custom Selectors]: https://github.com/postcss/postcss-custom-selectors