Socket
Socket
Sign inDemoInstall

postcss-focus-visible

Package Overview
Dependencies
7
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    postcss-focus-visible

Use the :focus-visible pseudo-selector in CSS


Version published
Weekly downloads
4.7M
decreased by-22%
Maintainers
2
Install size
220 kB
Created
Weekly downloads
 

Package description

What is postcss-focus-visible?

The postcss-focus-visible npm package is a PostCSS plugin that polyfills the :focus-visible pseudo-class. It allows developers to style elements that have focus only when they are focused via keyboard input, providing better accessibility and user experience. This is particularly useful for avoiding focus styles when clicking on elements with a mouse, which can sometimes be visually disruptive.

What are postcss-focus-visible's main functionalities?

Polyfill :focus-visible

This feature allows developers to use the :focus-visible pseudo-class in their CSS, which the plugin then compiles into a backward-compatible format. The output CSS works in conjunction with a JavaScript detection script to apply focus styles only when an element is focused via the keyboard.

/* Input CSS */
:focus-visible { outline: 1px solid black; }

/* Output CSS */
.js-focus-visible :focus:not(.focus-visible) { outline: none; }
.js-focus-visible .focus-visible { outline: 1px solid black; }

Other packages similar to postcss-focus-visible

Readme

Source

PostCSS Focus Visible PostCSS Logo

npm version CSS Standard Status Build Status Discord

PostCSS Focus Visible lets you use the :focus-visible pseudo-class in CSS, following the Selectors Level 4 specification.

It is the companion to the focus-visible polyfill.

'Can I use' table

:focus:not(:focus-visible) {
  outline: none;
}

/* becomes */

:focus:not(.focus-visible) {
  outline: none;
}

:focus:not(:focus-visible) {
  outline: none;
}

PostCSS Focus Visible duplicates rules using the :focus-visible pseudo-class with a .focus-visible class selector, the same selector used by the focus-visible polyfill. This replacement selector can be changed using the replaceWith option. Also, the preservation of the original :focus-visible rule can be disabled using the preserve option.

Usage

Add PostCSS Focus Visible to your project:

npm install postcss-focus-visible --save-dev

Use PostCSS Focus Visible to process your CSS:

const postcssFocusVisible = require('postcss-focus-visible');

postcssFocusVisible.process(YOUR_CSS /*, processOptions, pluginOptions */);

Or use it as a PostCSS plugin:

const postcss = require('postcss');
const postcssFocusVisible = require('postcss-focus-visible');

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

PostCSS Focus Visible runs in all Node environments, with special instructions for:

NodePostCSS CLIWebpackCreate React AppGulpGrunt

Options

preserve

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

focusVisible({ preserve: false });
:focus:not(:focus-visible) {
  outline: none;
}

/* becomes */

:focus:not(.focus-visible) {
  outline: none;
}

replaceWith

The replaceWith option defines the selector to replace :focus-visible. By default, the replacement selector is .focus-visible.

focusVisible({ replaceWith: '[focus-visible]' });
:focus:not(:focus-visible) {
  outline: none;
}

/* becomes */

:focus:not([focus-visible]) {
  outline: none;
}

:focus:not(:focus-visible) {
  outline: none;
}

Keywords

FAQs

Last updated on 05 Feb 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc