[6a3a178] | 1 | # PostCSS Focus Within [<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 | [![Gitter Chat][git-img]][git-url]
|
---|
| 7 |
|
---|
| 8 | [PostCSS Focus Within] lets you use the `:focus-within` pseudo-class in CSS,
|
---|
| 9 | following the [Selectors Level 4 specification].
|
---|
| 10 |
|
---|
| 11 | It is the companion to the [focus-within polyfill].
|
---|
| 12 |
|
---|
| 13 | ```css
|
---|
| 14 | .my-form-field:focus-within label {
|
---|
| 15 | background-color: yellow;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | /* becomes */
|
---|
| 19 |
|
---|
| 20 | .my-form-field[focus-within] label {
|
---|
| 21 | background-color: yellow;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | .my-form-field:focus-within label {
|
---|
| 25 | background-color: yellow;
|
---|
| 26 | }
|
---|
| 27 | ```
|
---|
| 28 |
|
---|
| 29 | [PostCSS Focus Within] duplicates rules using the `:focus-within` pseudo-class
|
---|
| 30 | with a `[focus-within]` attribute selector, the same selector used by the
|
---|
| 31 | [focus-within polyfill]. This replacement selector can be changed using the
|
---|
| 32 | `replaceWith` option. Also, the preservation of the original `:focus-within`
|
---|
| 33 | rule can be disabled using the `preserve` option.
|
---|
| 34 |
|
---|
| 35 | ## Usage
|
---|
| 36 |
|
---|
| 37 | Add [PostCSS Focus Within] to your project:
|
---|
| 38 |
|
---|
| 39 | ```bash
|
---|
| 40 | npm install postcss-focus-within --save-dev
|
---|
| 41 | ```
|
---|
| 42 |
|
---|
| 43 | Use [PostCSS Focus Within] to process your CSS:
|
---|
| 44 |
|
---|
| 45 | ```js
|
---|
| 46 | const postcssFocusWithin = require('postcss-focus-within');
|
---|
| 47 |
|
---|
| 48 | postcssFocusWithin.process(YOUR_CSS /*, processOptions, pluginOptions */);
|
---|
| 49 | ```
|
---|
| 50 |
|
---|
| 51 | Or use it as a [PostCSS] plugin:
|
---|
| 52 |
|
---|
| 53 | ```js
|
---|
| 54 | const postcss = require('postcss');
|
---|
| 55 | const postcssFocusWithin = require('postcss-focus-within');
|
---|
| 56 |
|
---|
| 57 | postcss([
|
---|
| 58 | postcssFocusWithin(/* pluginOptions */)
|
---|
| 59 | ]).process(YOUR_CSS /*, processOptions */);
|
---|
| 60 | ```
|
---|
| 61 |
|
---|
| 62 | [PostCSS Focus Within] runs in all Node environments, with special
|
---|
| 63 | instructions for:
|
---|
| 64 |
|
---|
| 65 | | [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) |
|
---|
| 66 | | --- | --- | --- | --- | --- | --- |
|
---|
| 67 |
|
---|
| 68 | ## Options
|
---|
| 69 |
|
---|
| 70 | ### preserve
|
---|
| 71 |
|
---|
| 72 | The `preserve` option defines whether the original selector should remain. By
|
---|
| 73 | default, the original selector is preserved.
|
---|
| 74 |
|
---|
| 75 | ```js
|
---|
| 76 | focusWithin({ preserve: false });
|
---|
| 77 | ```
|
---|
| 78 |
|
---|
| 79 | ```css
|
---|
| 80 | .my-form-field:focus-within label {
|
---|
| 81 | background-color: yellow;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /* becomes */
|
---|
| 85 |
|
---|
| 86 | .my-form-field[focus-within] label {
|
---|
| 87 | background-color: yellow;
|
---|
| 88 | }
|
---|
| 89 | ```
|
---|
| 90 |
|
---|
| 91 | ### replaceWith
|
---|
| 92 |
|
---|
| 93 | The `replaceWith` option defines the selector to replace `:focus-within`. By
|
---|
| 94 | default, the replacement selector is `[focus-within]`.
|
---|
| 95 |
|
---|
| 96 | ```js
|
---|
| 97 | focusWithin({ replaceWith: '.focus-within' });
|
---|
| 98 | ```
|
---|
| 99 |
|
---|
| 100 | ```css
|
---|
| 101 | .my-form-field:focus-within label {
|
---|
| 102 | background-color: yellow;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /* becomes */
|
---|
| 106 |
|
---|
| 107 | .my-form-field.focus-within label {
|
---|
| 108 | background-color: yellow;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | .my-form-field:focus-within label {
|
---|
| 112 | background-color: yellow;
|
---|
| 113 | }
|
---|
| 114 | ```
|
---|
| 115 |
|
---|
| 116 | [css-img]: https://cssdb.org/badge/focus-within-pseudo-class.svg
|
---|
| 117 | [css-url]: https://cssdb.org/#focus-within-pseudo-class
|
---|
| 118 | [cli-img]: https://img.shields.io/travis/jonathantneal/postcss-focus-within.svg
|
---|
| 119 | [cli-url]: https://travis-ci.org/jonathantneal/postcss-focus-within
|
---|
| 120 | [git-img]: https://img.shields.io/badge/support-chat-blue.svg
|
---|
| 121 | [git-url]: https://gitter.im/postcss/postcss
|
---|
| 122 | [npm-img]: https://img.shields.io/npm/v/postcss-focus-within.svg
|
---|
| 123 | [npm-url]: https://www.npmjs.com/package/postcss-focus-within
|
---|
| 124 |
|
---|
| 125 | [focus-within polyfill]: https://github.com/jonathantneal/focus-within
|
---|
| 126 | [Gulp PostCSS]: https://github.com/postcss/gulp-postcss
|
---|
| 127 | [Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
|
---|
| 128 | [PostCSS]: https://github.com/postcss/postcss
|
---|
| 129 | [PostCSS Focus Within]: https://github.com/jonathantneal/postcss-focus-within
|
---|
| 130 | [PostCSS Loader]: https://github.com/postcss/postcss-loader
|
---|
| 131 | [Selectors Level 4 specification]: https://www.w3.org/TR/selectors-4/#the-focus-within-pseudo
|
---|