# PostCSS Focus Within

[![NPM Version](https://img.shields.io/npm/v/postcss-focus-within.svg)](https://www.npmjs.com/package/postcss-focus-within) [![CSS Standard Status](https://cssdb.org/badge/focus-within-pseudo-class.svg)](https://cssdb.org/#focus-within-pseudo-class) [![Build Status](https://img.shields.io/travis/jonathantneal/postcss-focus-within.svg)](https://travis-ci.org/jonathantneal/postcss-focus-within) [![Gitter Chat](https://img.shields.io/badge/support-chat-blue.svg)](https://gitter.im/postcss/postcss)

[PostCSS Focus Within](https://github.com/jonathantneal/postcss-focus-within) lets you use the `:focus-within` pseudo-class in CSS, following the [Selectors Level 4 specification](https://www.w3.org/TR/selectors-4/#the-focus-within-pseudo).

It is the companion to the [focus-within polyfill](https://github.com/jonathantneal/focus-within).

```css
.my-form-field:focus-within label {
  background-color: yellow;
}

/* becomes */

.my-form-field[focus-within] label {
  background-color: yellow;
}

.my-form-field:focus-within label {
  background-color: yellow;
}
```

[PostCSS Focus Within](https://github.com/jonathantneal/postcss-focus-within) duplicates rules using the `:focus-within` pseudo-class with a `[focus-within]` attribute selector, the same selector used by the [focus-within polyfill](https://github.com/jonathantneal/focus-within). This replacement selector can be changed using the `replaceWith` option. Also, the preservation of the original `:focus-within` rule can be disabled using the `preserve` option.

## Usage

Add [PostCSS Focus Within](https://github.com/jonathantneal/postcss-focus-within) to your project:

```bash
npm install postcss-focus-within --save-dev
```

Use [PostCSS Focus Within](https://github.com/jonathantneal/postcss-focus-within) to process your CSS:

```js
const postcssFocusWithin = require('postcss-focus-within');

postcssFocusWithin.process(YOUR_CSS /*, processOptions, pluginOptions */);
```

Or use it as a [PostCSS](https://github.com/postcss/postcss) plugin:

```js
const postcss = require('postcss');
const postcssFocusWithin = require('postcss-focus-within');

postcss([
  postcssFocusWithin(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);
```

[PostCSS Focus Within](https://github.com/jonathantneal/postcss-focus-within) runs in all Node environments, with special instructions for:

| [Node](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-focus-within/INSTALL.md#node) | [PostCSS CLI](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-focus-within/INSTALL.md#postcss-cli) | [Webpack](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-focus-within/INSTALL.md#webpack) | [Create React App](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-focus-within/INSTALL.md#create-react-app) | [Gulp](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-focus-within/INSTALL.md#gulp) | [Grunt](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-focus-within/INSTALL.md#grunt) |
| ---------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |

## Options

### preserve

The `preserve` option defines whether the original selector should remain. By default, the original selector is preserved.

```js
focusWithin({ preserve: false });
```

```css
.my-form-field:focus-within label {
  background-color: yellow;
}

/* becomes */

.my-form-field[focus-within] label {
  background-color: yellow;
}
```

### replaceWith

The `replaceWith` option defines the selector to replace `:focus-within`. By default, the replacement selector is `[focus-within]`.

```js
focusWithin({ replaceWith: '.focus-within' });
```

```css
.my-form-field:focus-within label {
  background-color: yellow;
}

/* becomes */

.my-form-field.focus-within label {
  background-color: yellow;
}

.my-form-field:focus-within label {
  background-color: yellow;
}
```
