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