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.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # PostCSS Dir Pseudo Class [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" 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 Dir Pseudo Class] lets you style by directionality using the `:dir()`
  7. pseudo-class in CSS, following the [Selectors] specification.
  8. ```pcss
  9. article h3:dir(rtl) {
  10. margin-right: 10px;
  11. }
  12. article h3:dir(ltr) {
  13. margin-left: 10px;
  14. }
  15. /* becomes */
  16. [dir="rtl"] article h3 {
  17. margin-right: 10px;
  18. }
  19. [dir="ltr"] article h3 {
  20. margin-left: 10px;
  21. }
  22. ```
  23. ### Maintaining Specificity
  24. Using [PostCSS Dir Pseudo Class] will not impact selector weight, but it will
  25. require having at least one `[dir]` attribute in your HTML. If you don’t have
  26. _any_ `[dir]` attributes, consider using the following JavaScript:
  27. ```js
  28. // force at least one dir attribute (this can run at any time)
  29. document.documentElement.dir=document.documentElement.dir||'ltr';
  30. ```
  31. If you absolutely cannot add a `[dir]` attribute in your HTML or even force one
  32. via JavaScript, you can still work around this by presuming a direction in your
  33. CSS using the [`dir` option](#dir-option), but understand that this will
  34. sometimes increase selector weight by one element (`html`).
  35. ## Usage
  36. Add [PostCSS Dir Pseudo Class] to your project:
  37. ```bash
  38. npm install postcss-dir-pseudo-class --save-dev
  39. ```
  40. Use [PostCSS Dir Pseudo Class] to process your CSS:
  41. ```js
  42. const postcssDirPseudoClass = require('postcss-dir-pseudo-class');
  43. postcssDirPseudoClass.process(YOUR_CSS /*, processOptions, pluginOptions */);
  44. ```
  45. Or use it as a [PostCSS] plugin:
  46. ```js
  47. const postcss = require('postcss');
  48. const postcssDirPseudoClass = require('postcss-dir-pseudo-class');
  49. postcss([
  50. postcssDirPseudoClass(/* pluginOptions */)
  51. ]).process(YOUR_CSS /*, processOptions */);
  52. ```
  53. [PostCSS Dir Pseudo Class] runs in all Node environments, with special instructions for:
  54. | [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) |
  55. | --- | --- | --- | --- | --- | --- |
  56. ## Options
  57. ### dir
  58. The `dir` option allows you presume a direction in your CSS. By default, this
  59. is not specified and you are required to include a direction `[dir]` attribute
  60. somewhere in your HTML, preferably on the `html` element.
  61. Here’s an example of using the `dir` option to presume a left-to-right
  62. direction:
  63. ```js
  64. postcssDirPseudoClass({ dir: 'ltr' });
  65. ```
  66. ```pcss
  67. .example:dir(ltr) {
  68. margin-left: 10px;
  69. }
  70. .example:dir(rtl) {
  71. margin-right: 10px;
  72. }
  73. /* becomes */
  74. html:not([dir="rtl"]) .example {
  75. margin-left: 10px;
  76. }
  77. [dir="rtl"] .example {
  78. margin-right: 10px;
  79. }
  80. ```
  81. ### preserve
  82. The `preserve` option determines whether the original `:dir()` rule should
  83. remain in the CSS. By default, the original rule is not preserved.
  84. ```js
  85. postcssDirPseudoClass({ preserve: true });
  86. ```
  87. ```pcss
  88. article h3:dir(rtl) {
  89. margin-right: 10px;
  90. }
  91. article h3:dir(ltr) {
  92. margin-left: 10px;
  93. }
  94. /* becomes */
  95. [dir="rtl"] article h3 {
  96. margin-right: 10px;
  97. }
  98. article h3:dir(rtl) {
  99. margin-right: 10px;
  100. }
  101. [dir="ltr"] article h3 {
  102. margin-left: 10px;
  103. }
  104. article h3:dir(ltr) {
  105. margin-left: 10px;
  106. }
  107. ```
  108. [cli-img]: https://img.shields.io/travis/jonathantneal/postcss-dir-pseudo-class.svg
  109. [cli-url]: https://travis-ci.org/jonathantneal/postcss-dir-pseudo-class
  110. [css-img]: https://cssdb.org/badge/dir-pseudo-class.svg
  111. [css-url]: https://cssdb.org/#dir-pseudo-class
  112. [git-img]: https://img.shields.io/badge/support-chat-blue.svg
  113. [git-url]: https://gitter.im/postcss/postcss
  114. [npm-img]: https://img.shields.io/npm/v/postcss-dir-pseudo-class.svg
  115. [npm-url]: https://www.npmjs.com/package/postcss-dir-pseudo-class
  116. [win-img]: https://img.shields.io/appveyor/ci/jonathantneal/postcss-dir-pseudo-class.svg
  117. [win-url]: https://ci.appveyor.com/project/jonathantneal/postcss-dir-pseudo-class
  118. [Gulp PostCSS]: https://github.com/postcss/gulp-postcss
  119. [Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
  120. [PostCSS]: https://github.com/postcss/postcss
  121. [PostCSS Loader]: https://github.com/postcss/postcss-loader
  122. [PostCSS Dir Pseudo Class]: https://github.com/jonathantneal/postcss-dir-pseudo-class
  123. [Selectors]: https://www.w3.org/TR/selectors-4/#the-dir-pseudo