loock-poster

Loock

An accessibility helper for keyboard navigation on the web.

Loock logo

Loock • A focus trap helper for keyboard navigation on the web.

NPM License


Introducing Loock

  • Organize your web page or web application by navigation areas.

  • Never lose the context while navigating the area with the TAB key.

  • Leave the context with the ESC key.

  • Use a default context.

Medium article - "How to improve keyboard navigation of your web page"

Try out the demo!

Install

$ npm install @chialab/loock
$ yarn add @chialab/loock

Use via cdn:

<script src="https://unpkg.com/@chialab/loock"></script>

Usage

import { Loock } from '@chialab/loock';

const loock = new Loock();
const mainElem = document.getElementById('main');
const navigationElem = document.getElementById('main');

// define the default context
const mainContext = loock.createDefaultContext(mainElem);

// define one context
const context = loock.createContext(navigationElem);

// listen context state
navigationElem.addEventListener('focusenter', () => {
    console.log('entered the navigation context');
    // do stuff
});

navigationElem.addEventListener('focusexit', () => {
    console.log('exited the navigation context');
    // do stuff
});

// activate the context
context.enter();
<html>
    <body>
        <nav id="navigation" aria-label="Main navigation">
            <a href="/">Home</a>
            <a href="/posts">Posts</a>
            <a href="/login">Login</a>
        </nav>
        <section id="main" aria-label="Main content">
            ...
        </section>
    </body>
</html>

On page load, the #navigation will be automatically focused and you can navigate the links using the TAB key without losing focus from the nav element. Press ESC to exit the navigation context and skip to the default context, the #main element.

For a more complete example, please see the demo source code.


Development

Build

Install the dependencies and run the build script:

$ npm install
$ npm run build

This will generate the UMD and ESM bundles in the dist folder, as well as the declaration file.

Test

Run the test script:

$ npm run test

License

Loock is released under the MIT license.