PostCSS Focus Within
PostCSS Focus Within lets you use the :focus-within
pseudo-selector in
CSS, following the [Selectors Level 4 specification].
.my-form-field:focus-within label {
background-color: yellow;
}
.my-form-field[focus-within] label {
background-color: yellow;
}
PostCSS Focus Within replaces the :focus-within
pseudo-selector with a
.focus-within
class selector, the same selector used by the
focus-within polyfill. The replacement selector can be changed using the
replaceWith
option.
Usage
Add PostCSS Focus Within to your build tool:
npm install postcss-focus-within --save-dev
Node
Use PostCSS Focus Within to process your CSS:
import focusWithin from 'postcss-focus-within';
focusWithin.process(YOUR_CSS);
PostCSS
Add PostCSS to your build tool:
npm install postcss --save-dev
Use PostCSS Focus Within as a plugin:
import postcss from 'gulp-postcss';
import focusWithin from 'postcss-focus-within';
postcss([
focusWithin()
]).process(YOUR_CSS);
Webpack
Add PostCSS Loader to your build tool:
npm install postcss-loader --save-dev
Use PostCSS Focus Within in your Gulpfile:
import focusWithin from 'postcss-focus-within';
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
{ loader: 'css-loader', options: { importLoaders: 1 } },
{ loader: 'postcss-loader', options: {
ident: 'postcss',
plugins: () => [ focusWithin() ]
} }
]
}
]
}
}
Gulp
Add Gulp PostCSS to your build tool:
npm install gulp-postcss --save-dev
Use PostCSS Focus Within in your Gulpfile:
import postcss from 'gulp-postcss';
import focusWithin from 'postcss-focus-within';
gulp.task('css', () => gulp.src('./src/*.css').pipe(
postcss([
focusWithin()
])
).pipe(
gulp.dest('.')
));
Grunt
Add Grunt PostCSS to your build tool:
npm install grunt-postcss --save-dev
Use PostCSS Focus Within in your Gruntfile:
import focusWithin from 'postcss-focus-within';
grunt.loadNpmTasks('grunt-postcss');
grunt.initConfig({
postcss: {
options: {
use: [
focusWithin()
]
},
dist: {
src: '*.css'
}
}
});
Options
replaceWith
The replaceWith
option defines the selector to replace :focus-within
. By
default, the replacement selector is [focus-within]
.
focusWithin({ replaceWith: '.focus-within' });
:focus:not(:focus-within) {
outline: none;
}
:focus:not(.focus-within]) {
outline: none;
}