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.

198 lines
3.8 KiB

2 years ago
  1. # PostCSS Nested
  2. <img align="right" width="135" height="95"
  3. title="Philosopher’s stone, logo of PostCSS"
  4. src="https://postcss.org/logo-leftp.svg">
  5. [PostCSS] plugin to unwrap nested rules like how Sass does it.
  6. ```css
  7. .phone {
  8. &_title {
  9. width: 500px;
  10. @media (max-width: 500px) {
  11. width: auto;
  12. }
  13. body.is_dark & {
  14. color: white;
  15. }
  16. }
  17. img {
  18. display: block;
  19. }
  20. }
  21. .title {
  22. font-size: var(--font);
  23. @at-root html {
  24. --font: 16px
  25. }
  26. }
  27. ```
  28. will be processed to:
  29. ```css
  30. .phone_title {
  31. width: 500px;
  32. }
  33. @media (max-width: 500px) {
  34. .phone_title {
  35. width: auto;
  36. }
  37. }
  38. body.is_dark .phone_title {
  39. color: white;
  40. }
  41. .phone img {
  42. display: block;
  43. }
  44. .title {
  45. font-size: var(--font);
  46. }
  47. html {
  48. --font: 16px
  49. }
  50. ```
  51. Related plugins:
  52. * Use [`postcss-atroot`] for `@at-root` at-rule to move nested child
  53. to the CSS root.
  54. * Use [`postcss-current-selector`] **after** this plugin if you want
  55. to use current selector in properties or variables values.
  56. * Use [`postcss-nested-ancestors`] **before** this plugin if you want
  57. to reference any ancestor element directly in your selectors with `^&`.
  58. Alternatives:
  59. * See also [`postcss-nesting`], which implements [CSSWG draft]
  60. (requires the `&` and introduces `@nest`).
  61. * [`postcss-nested-props`] for nested properties like `font-size`.
  62. <a href="https://evilmartians.com/?utm_source=postcss-nested">
  63. <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
  64. alt="Sponsored by Evil Martians" width="236" height="54">
  65. </a>
  66. [`postcss-atroot`]: https://github.com/OEvgeny/postcss-atroot
  67. [`postcss-current-selector`]: https://github.com/komlev/postcss-current-selector
  68. [`postcss-nested-ancestors`]: https://github.com/toomuchdesign/postcss-nested-ancestors
  69. [`postcss-nested-props`]: https://github.com/jedmao/postcss-nested-props
  70. [`postcss-nesting`]: https://github.com/jonathantneal/postcss-nesting
  71. [CSSWG draft]: https://drafts.csswg.org/css-nesting-1/
  72. [PostCSS]: https://github.com/postcss/postcss
  73. ## Usage
  74. **Step 1:** Install plugin:
  75. ```sh
  76. npm install --save-dev postcss postcss-nested
  77. ```
  78. **Step 2:** Check your project for existing PostCSS config: `postcss.config.js`
  79. in the project root, `"postcss"` section in `package.json`
  80. or `postcss` in bundle config.
  81. If you do not use PostCSS, add it according to [official docs]
  82. and set this plugin in settings.
  83. **Step 3:** Add the plugin to plugins list:
  84. ```diff
  85. module.exports = {
  86. plugins: [
  87. + require('postcss-nested'),
  88. require('autoprefixer')
  89. ]
  90. }
  91. ```
  92. [official docs]: https://github.com/postcss/postcss#usage
  93. ## Options
  94. ### `bubble`
  95. By default, plugin will bubble only `@media` and `@supports` at-rules.
  96. You can add your custom at-rules to this list by `bubble` option:
  97. ```js
  98. postcss([ require('postcss-nested')({ bubble: ['phone'] }) ])
  99. ```
  100. ```css
  101. /* input */
  102. a {
  103. color: white;
  104. @phone {
  105. color: black;
  106. }
  107. }
  108. /* output */
  109. a {
  110. color: white;
  111. }
  112. @phone {
  113. a {
  114. color: black;
  115. }
  116. }
  117. ```
  118. ### `unwrap`
  119. By default, plugin will unwrap only `@font-face`, `@keyframes` and `@document`
  120. at-rules. You can add your custom at-rules to this list by `unwrap` option:
  121. ```js
  122. postcss([ require('postcss-nested')({ unwrap: ['phone'] }) ])
  123. ```
  124. ```css
  125. /* input */
  126. a {
  127. color: white;
  128. @phone {
  129. color: black;
  130. }
  131. }
  132. /* output */
  133. a {
  134. color: white;
  135. }
  136. @phone {
  137. color: black;
  138. }
  139. ```
  140. ### `preserveEmpty`
  141. By default, plugin will strip out any empty selector generated by intermediate
  142. nesting levels. You can set `preserveEmpty` to `true` to preserve them.
  143. ```css
  144. .a {
  145. .b {
  146. color: black;
  147. }
  148. }
  149. ```
  150. Will be compiled to:
  151. ```css
  152. .a { }
  153. .a .b {
  154. color: black;
  155. }
  156. ```
  157. This is especially useful if you want to export the empty classes with `postcss-modules`.