Target elements if they are focused or contain a focused element
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.appveyor.yml 1.0.0 Feb 17, 2018
.editorconfig 1.0.0 Feb 17, 2018
.gitignore 1.0.1 Feb 21, 2018
.rollup.js Update project configuration Jun 4, 2018
.travis.yml 1.0.0 Feb 17, 2018
CHANGELOG.md 2.0.0 Jun 4, 2018
CONTRIBUTING.md 1.0.0 Feb 17, 2018
LICENSE.md 1.0.0 Feb 17, 2018
README.md Update documentation Jun 4, 2018
index.js Default polyfill to only execute in unsupported browsers Jun 4, 2018
package.json 2.0.0 Jun 4, 2018

README.md

Focus Within dom logo

NPM Version Build Status

Focus Within lets you target elements based on whether they are focused or contain a focused element, following the Selectors Level 4 specification.

.form-field label {
  /* style a label */
}

.form-field:focus-within label {
  /* style a label differently when .form-field contains a focused input */
}

Focus Within is the companion to PostCSS Focus Within, which is also included PostCSS Preset Env.

In unsupported browsers, the Focus Within polyfill adds a focus-within attribute to elements otherwise matching :focus-within natively. PostCSS Focus Within will duplicate these rules for you. Otherwise, be sure to either duplicate the entire rule, or use the force option.

.form-field label {
  /* style a label */
}

.form-field[focus-within] label {
  /* style a label differently when .form-field contains a focused input */
}

.form-field:focus-within label {
  /* style a label differently when .form-field contains a focused input */
}

The script, including the test and polyfill minify to 434 bytes gzipped or less.

Usage

Add Focus Within to your build tool:

npm install focus-within

Activate Focus Within on the document:

import focusWithin from 'focus-within';

focusWithin(document);

Options

Focus Within accepts a secondary paramater to configure the attribute or class name added to elements matching focused elements or containing focused elements.

focusWithin(document, {
  attr: false,
  className: '.focus-within'
});

Falsey values on either attr or className will disable setting the attribute or class name on elements matching :focus-within.

Focus Within also accepts a secondary paramater to configure whether the polyfill is loaded regardless of support. If force is given a truthy value, then the polyfill will always execute.

focusWithin(document, {
  force: true
});