npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

roving-grid-ux

v1.0.4

Published

roving focus grid for web ui

Downloads

1

Readme

roving-grid-ux

npm downloads npm latest npm license npm bundle size

Adds keyboard grid-based navigation to a DOM node

Based on the work done by Adam Argyle on roving ux
Try it at Roving Grid Playground and Glyph Browser

Features

  • Rtl support
  • Customizable key-bindings (W, A, S, D ? 👀)
  • Configurable wrapping behavior between rows
  • Keeps focus when DOM changes (add or remove nodes)
  • Updates when children are programmatically focused by an external component
  • Children are focused by pointerdown

Use Cases

  • Add keyboard navigation to custom components
  • Prototype a game or custom interaction pattern

Installation

npm i roving-grid-ux

CDN: https://cdn.skypack.dev/roving-grid-ux

Importing

// import from CDN
import { rovingGrid } from "https://cdn.skypack.dev/roving-grid-ux";

// import roving grid from npm
import { rovingGrid } from "roving-grid-ux";

// import roving grid and updater from npm
import { rovingGrid, updateRover } from "roving-grid-ux";

API

rovingGrid({
  element: node,    // container that manages focus
  target: ".cell",  // query selector for focusable children
  wrap: false,      // ? whether focus wraps from row to row
  VKMap: {}         // ? custom keybindings for navigating the grid
})

updateRover(
  element: node,    // roving container to be updated
  {
    wrap: true      // ? updated wrap property
    VKMap: {}       // ? updated keybindings
  }
)

Custom key bindings interface

/**
 * @typedef {string} KeyCombo - A `+` delimited key combination (e.g. `Ctrl+Home`)
 */

/**
 * @typedef {Object} VKMap - A map between virtual keys and key combinations
 * @property {KeyCombo[]} [TOP_LEFT] - The top left key combination
 * @property {KeyCombo[]} [TOP] - The top key combination
 * @property {KeyCombo[]} [TOP_RIGHT] - The top right key combination
 * @property {KeyCombo[]} [UP] - The up key combination
 * @property {KeyCombo[]} [DOWN] - The down key combination
 * @property {KeyCombo[]} [HOME] - The home key combination
 * @property {KeyCombo[]} [END] - The end key combination
 * @property {KeyCombo[]} [LEFT] - The left key combination
 * @property {KeyCombo[]} [RIGHT] - The right key combination
 * @property {KeyCombo[]} [BOTTOM_LEFT] - The bottom left key combination
 * @property {KeyCombo[]} [BOTTOM] - The bottom key combination
 * @property {KeyCombo[]} [BOTTOM_RIGHT] - The bottom right key combination
 *
 */
  • Directional keys for horizontal movements are swapped for Rtl (Home and End keys excepted)
  • Multiple keys can be bound to a virtual key. Use JavaScript Event.key names.
  • Key combinations should be in order: Ctrl (^), Alt (⌥), Shift (⇧), Meta (⌘).

Virtual Key Positions

Any of the below positions in a grid can be mapped to a virtual key.
7x7 grid denoting virtual key positions Top Left, Top, Top Right, Up, Home, Left, Right, End, Down, Bottom Left, Bottom, Bottom Right, relative to the current position

If no custom keybindings are provided, the following defaults are used:

| Virtual Key | Key Combinations | | ----------- | ------------------------ | | TOP | Ctrl+Home, Meta+ArrowUp | | BOTTOM | Ctrl+End, Meta+ArrowDown | | UP | ArrowUp | | DOWN | ArrowDown | | HOME | Home, Meta+ArrowLeft | | END | End, Meta+ArrowRight | | LEFT | ArrowLeft | | RIGHT | ArrowRight |

Programmatic Focus

A custom location can be focused by calling its focus method. The grid will update its internal representation of the focused element.

Example Usage

import { rovingGrid } from "roving-grid-ux";

rovingGrid({
  element: document.querySelector("#grid"),
  target: ".cell",
});
import { rovingGrid, updateRover } from "roving-grid-ux";

document.querySelectorAll(".glyph-grid").forEach((grid) => {
  rovingGrid({
    element: grid,
    target: ".glyph",
  });
});

Limitations

Does not support virtual focus (e.g. aria-activedescendant), so not suitable for composite widgets where focus must remain on one element while keeping the grid navigable via keyboard (a ComboBox with a grid popup... 🤔).

Does not handle selection, you will have to bring your own selection logic.

Tab moves focus out of the container; this is intentional so that keyboard navigation is efficient. However, spreadsheets generally mix tabbed navigation with ←, →, ↑, ↓ navigation, so this may not be good for prototyping that type of component.

PageUp and PageDown do not have a default virtual key. If you need these to move focus you will need to implement it yourself.

Grid items must be sequential in the DOM (i.e. no voids). For inaccessible items, use disabled or inert items.

Caution

Keyboard grid navigation is not common outside of default controls or data tables. Consider your use case, users, and authoring practices when setting up a keyboard navigable grid.

Learn

ARIA grid interaction pattern