Coder Social home page Coder Social logo

fix-outline's Introduction

fix-outline

*:focus { outline: none; } done right.

By default, some browsers() add an outline around your shiny buttons when they are clicked:

This is great for keyboard accessibility, but most users don't use keyboard for navigating. The goal of this library is to provide best visual experience for most users, while keeping good keyboard accessibility.

fix-outline disables outline until user actually uses keyboard navigation. It works out of the box with your existing CSS.

This very small library has no dependencies and supports all modern browsers, including IE8+.

Note: Chromium's behavior to outline buttons on click is intentional. To my mind, this library is a good compromise between visual look and accessibility.

Install

NPM

npm install fix-outline

Usage

Remove all existing global outline rules like these:

*:focus {
  outline: 0;
}

Then call fixOutline() once in your JS:

var fixOutline = require('fix-outline');
fixOutline();

Now outline is disabled for all focused elements until keyboard navigation is used for the first time.

Advanced usage

fixOutline() automatically adds new CSS rules to your page. You can disable this behavior by setting autoCSS option to false:

var fixOutline = require('fix-outline');
fixOutline({
    autoCss: false
});

That means you need to add some CSS yourself. For example this rule is added when autoCss is enabled:

body:not(.kb-nav-used) *:focus {
  outline: none;
}

How it works

This is shortly how fix-outline works:

  • Add new CSS rule which disables outline on elements when .kb-nav-used is not defined for body
  • Setup hook, which adds .kb-nav-used class to body when TAB key is pressed

In other words, outline for elements is enabled after user uses keyboard navigation for the first time.

Reasoning behind the implementation:

  • Enables accessibility only for the ones using it.
  • Fast. Does CSS stylesheet modifications only once and removes event listener. Other implementations keep listening for all mousedown and keydown events.

Usually developers fix this by setting a global *:focus { outline: none; } rule, but they shouldn't.

Resources and other implementations

License

MIT

fix-outline's People

Contributors

artisin avatar kimmobrunfeldt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

fix-outline's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.