Homepage

Spatial keyboard navigation

Danilo Woznica, Oct 10, 2021 2 min read

Check out the result of this article in the GitHub or see the preview.

Motivation

The current way to navigate through selectable elements in a page hasn’t changed much, and we are used to considering it the right way to navigate in a DOM document, but I’d like to bring a fresh perspective on this approach.

Human beings generally use visualization methods to memorize and understand the space they are surrounded by. Still, for some reason, the browsers don’t use these methods in favor of user experience. The TAB key has been great for a long time and it solves many problems, so what’s wrong with using it to navigate in a DOM document?

Spatial sensitive keyboard navigation

Graphical User Interface has already been using analogies from the real world on the computer universe (e.g., floppy disk for saving files). Bringing a spatial navigation approach might also improve the navigation in a DOM document.

That said, putting all selectable elements in a visual hierarchy structure and using the arrow keys to navigate through them might be the easiest way to introduce this new concept, which would work just like a spreadsheet.

Example: Use the arrow keys to navigate

This approach makes it easier and faster to navigate through the selectable elements, plus it doesn’t introduce any new aspects and it doesn’t require any new learnings from the users.

The memory journey

However, to make navigation even faster and make it even easier to memorize the “paths” the users take on the page, we need to picture that these selectable items belong to a group of items, or that there are “paths” to get to them. These issues lead to the following concepts:

Usage

To get started, install the package and wrap the whole application using the following Provider component:

import { Provider } from "spatial-keyboard-navigation";

const App = ({ children }) => <Provider>{children}</Provider>;
Prop nameDescriptionTypeDefault value
areaClassNameClassName when the area is selectedstringarea-selected
strictAreaIt doesn’t allow the user navigate beyond the area boundaries.booleanfalse

Components

Spatial navigation introduces two main components to make the items selectable: Anchor and Area

import { Area, Anchor } from "spatial-keyboard-navigation";

const Sidebar = () => {
  return (
    <Area>
      <section>
        <Anchor>
          <a href="">Item</a>
        </Anchor>
        <Anchor>
          <a href="">Item</a>
        </Anchor>
        <Anchor>
          <button>Item</button>
        </Anchor>
      </section>
    </Area>
  );
};

Check out the result of this article in the GitHub or see the preview.


Thanks to Zeh Fernandes for all support.