focus-ring
Detect if a focus ring should be displayed
- Tiny: less than 500 bytes gzipped
- Flexible: let
focus-ring
add and remove a body class or write your own event handler - Unambiguous: always falls back to displaying focus rings when in doubt
- Simple API: works with the call of only one function
focus-ring
was made for the browser, but works in any JavaScript runtime if provided with a target
that implements EventTarget
. It supports IE9+.
Table of Contents
Install
This project uses node and npm. Go check them out if you don't have them locally installed.
$ npm install focus-ring
Then with a module bundler like rollup or webpack, use as you would anything else:
// using ES6 modules; // using CommonJS modulesconst focusRing = ;
The UMD build is also available on unpkg:
You can find the library on window.focusRing
.
Usage
Custom implementation
; const fr = ; // Specify a target other than `document.body`; // Listen to individual focus ring eventsfr; // Remove event listenersconst listener = fr; fr; // Listen to both eventsfr;
Default implementation
This implementation adds a class to the focus-ring
target when focus rings should be displayed and removes it if they can be hidden.
; // Adds `.focus-ring` class to `document.body`focusRing; // Adds `.custom` class to element with ID `target`focusRing; // Still supports custom event handlers so you can add additional logicfocusRing;
License
ISC License © Jonas Gierer