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