PostCSS plugin to add :focus-visible
selector to every :hover
for keyboard accessibility.
See also postcss-pseudo-class-enter for more explicit way.
*:focus-visible {
outline: 0;
}
.button:hover {
background: red;
}
*:focus-visible {
outline: 0;
}
.button:hover {
background: red;
}
.button:focus-visible {
background: red;
}
If there is a :focus-visible
selector, it will be excluded
from the processing.
.a:hover, .b:hover {
outline: 0;
}
.b:focus-visible {
background: red;
}
.a:hover, .b:hover, .a:focus-visible {
outline: 0;
}
.b:focus-visible {
background: red;
}
Step 1: Install plugin:
npm install --save-dev postcss postcss-focus
Step 2: Check your project for existing PostCSS config: postcss.config.js
in the project root, "postcss"
section in package.json
or postcss
in bundle config.
If you do not use PostCSS, add it according to official docs and set this plugin in settings.
Step 3: Add the plugin to plugins list:
module.exports = {
plugins: [
+ require('postcss-focus'),
require('autoprefixer')
]
}
module.exports = {
plugins: [
require('postcss-focus')({
oldFocus: true,
splitRules: false,
})
]
}
Type: boolean
. Default: false
.
Enable if you need to add the old :focus
instead of :focus-visible
selector
to every :hover
.
Type: boolean
. Default: true
.
Disable if you need to append the selector. If you don't need to support browsers, which doesn't support :focus-visible
, you can safely disable this option.