UNPKG

2.55 kBMarkdownView Raw
1arrow-key-navigation [![Build Status](https://travis-ci.org/nolanlawson/arrow-key-navigation.svg)](https://travis-ci.org/nolanlawson/arrow-key-navigation)
2=====
3
4## Overview
5
6`arrow-key-navigation` is a simple utility to add left/right focus navigation to a web app. It's
7designed for KaiOS apps but also available for any browser.
8
9The basic idea is to make the <kbd></kbd> and <kbd></kbd> keys act similar to
10<kbd>Tab</kbd> and <kbd>Shift</kbd>+<kbd>Tab</kbd>, i.e. to change focus between focusable elements in the DOM.
11Since the <kbd></kbd> and <kbd></kbd> keys typically scroll the page in KaiOS, this is usually all you need
12to add basic KaiOS accessibility to an existing web app.
13
14It will also listen for the <kbd>Enter</kbd> key for certain special cases like checkbox/radio buttons. `contenteditable` and Shadow DOM are also supported.
15
16## Install
17
18 npm install --save arrow-key-navigation
19
20Or [browse unpkg.com](https://unpkg.com/browse/arrow-key-navigation/) for a list of build files.
21
22## Usage
23
24```js
25import * as arrowKeyNavigation from 'arrow-key-navigation'
26
27arrowKeyNavigation.register() // start listening for key inputs
28arrowKeyNavigation.unregister() // stop listening
29```
30
31## Focus traps
32
33To build [an accessible dialog](https://www.w3.org/TR/wai-aria-practices-1.1/#dialog_modal), you need to
34"trap" focus inside of the dialog, i.e. make it so focus cannot escape the dialog while it is active. To
35accomplish this, you can set a "focus trap test" which takes an element as input and returns truthy/falsy
36to indicate that the element is a focus trap (e.g. the modal dialog root):
37
38```js
39arrowKeyNavigation.setFocusTrapTest(element => {
40 return element.classList.contains('my-dialog-class')
41})
42```
43
44If you don't call `setFocusTrapTest()`, then `arrow-key-navigation` will assume that there are no focus traps
45in your app.
46
47## Conditional or lazy loading
48
49You can choose to install this module only in KaiOS environments using logic like the following:
50
51```js
52if (/KAIOS/.test(navigator.userAgent)) {
53 import('arrow-key-navigation').then(arrowKeyNavigation => {
54 arrowKeyNavigation.register()
55 })
56}
57```
58
59## Contributing
60
61### Build
62
63 npm run build
64
65### Lint
66
67 npm run lint
68
69### Fix most lint issues
70
71 npm run lint:fix
72
73### Test
74
75 npm test
76
77### Code coverage
78
79 npm run cover
80
81### Manual KaiOS app test
82
83The `index.html` and `manifest.webapp` files are designed for a quick-and-dirty KaiOS app test.
84
85Run `npm run build` and then install the root directory as a packaged KaiOS app to test it.