spatial-navigation

Spatial Navigation Polyfill

npm stat npm version

What is this?

This is a JavaScript implementation of the Spatial Navigation Specification.

It enables users to navigate the page using the arrow keys of the keyboard (or remote control, or game pad…), instead of (or in addition to) the Tab key, the mouse cursor, or the touch screen.

Depending on the content and layout of the page, pressing one of the directional keys will either move the focus in that direction, or scroll the page, as appropriate.

For more details, see the specification or its explainer document.

Why Use the Polyfill

Eventually, we expect spatial navigation to be natively supported by browsers. However, this is not yet the case.

Until then, authors who wish to experiment with providing this feature to their users can include this polyfill in their page.

It can also be used for people interested in reviewing the specification it order to test the behavior it defines in various situations.

How to Use

Installation

npm i spatial-navigation-polyfill

We recommend only using versions of the polyfill that have been published to npm, rather than cloning the repo and using the source directly. This helps ensure the version you’re using is stable and thoroughly tested. See the changes in the polyfill so far.

If you do want to build from source, make sure you clone the latest tag!

Including the Polyfill in a page

Include the following code in your web page, and the polyfill will be included, enabling spatial navigation.

...
    <script src="/node_modules/spatial-navigation-polyfill/polyfill/spatial-navigation-polyfill.js"></script>
  </body>
</html>

Users can now user the keyboard’s arrow keys to navigate the page.

Handling Browser Events

In the polyfill, keydown event and mouseup event are used for the spatial navigation. The event handlers of those are attached to the window object.

We recommend to use it with the polyfill as below:

Using the APIs

The spatial navigation specification defines several JavaScript events and APIs. Using these is not necessary to use the polyfill, and users can start using the arrow keys as soon as the polyfill is included, but they can be convenient for authors who wish to override the default behavior in some cases. See the specification for more details.

Standard APIs

| Standard APIs | Feature | |-|-| | navigate() | Enables the author to trigger spatial navigation programatically | | spatialNavigationSearch() | Finds the element which will gain the focus within the spatial navigation container from the currently focused element | | getSpatialNavigationContainer() | Gets the spatial navigation container of an element | | focusableAreas() | Finds focusable elements within the spatial navigation container | | Navigation Events | Occurs depending on the specific contextual behavior associated with spatial navigation

Experimental APIs

NOTE: The APIs below are non-standard and experimental features of the spatial navigation.

Current Status

Browser Support

With the polyfill, the Spatial Navigation has been tested and known to work in the following browsers:

Chrome
49+
Firefox
61+
Safari
11.1+
Edge
17+
Opera
36+

Remaining Issues

The polyfill is not yet complete. It roughly matches the specification but does not yet follow it closely, and has a number of known issues.

See the list of open bugs in github.

FYI

Types of branches