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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@gilchrist/spacnav

v1.0.1

Published

HTMLElement bindings for spatial navigation

Downloads

19

Readme

Spatnav — HTMLElement bindings for generic spatial navigation

Spatial navigation is a means of navigating focusable elements in an interface based on their positions, relative to one another. Pressing up should move you upward, right should move you rightward, and so on.

Spacnav, short for Spatial Navigation, is a series of function bindings for HTMLElements which provide spatial navigation functionality. It was initially written for my web-based roguelike game, but I split off the code into its own project. Currently, this library's development is coupled to that game project, but I am intending to isolate the code more over time.

There is a W3C spec draft the for the feature, but no official spatial navigation functionality exists in browsers. This implementation does not follow the W3C draft, and currently, it does not implement any CSS properties to control spatial navigation. It also does not set or define any event handling functions. The developer must call the spatial navigation methods and implement event handling themselves. Over time, it may get reworked and move closer to that specification.

Initialization

The library does not automatically bind functions to HTMLElement. Instead, call the default export, initSpacnavElementMethods(). This function is idempotent.

<script>
  import initSpacnavElementMethods from "spatnav.js";
  initSpacnavElementMethods();
</script>

Using spatnav methods

This module adds several methods to HTMLElement.

isSelectable()

Returns true if the element can be focused via spatial navigation. By default, this includes <button> elements, or any element with the class button or selectable, or the attributes selectable or button.

note: At a later point, these classes and attributes may be replaced with CSS properties, similar to the W3C specification draft.

findSpacnavElement(direction_or_theta)

Finds the lowest-scoring candidate for spatial navigation in a given direction.

  • direction_or_theta: Can be a numeric angle in radians or a directional vector in the form of a 2-element Array: [dx, dy].
  • Returns the HTMLElement with the lowest score, or null if no suitable candidate is found.

handleSelect(previous_selection)

Standard handler for selecting an element. It calls handleDeselect() on the previous_selection (if provided), focuses the current element, and scrolls it into view.

handleDeselect()

Standard handler for deselecting an element. By default, it calls this.blur().

isSpacnavContainer()

Returns true if the element is marked as a spatial navigation container (has class spacnav-container, spacnav-context, or menu). Containers help organize and limit the scope of spatial navigation.

note: The menu class will be removed from this module, as it is specific to the game code this module is based on.

getSpacnavContainer()

Returns the closest parent element that is a spatial navigation container.

iterSpacnavCandidates()

A generator that yields all selectable elements or nested containers within the current container. It does not recurse into nested containers but yields the containers themselves if they are selectable.

getSpacnavDistanceTo(direction, candidate)

Calculates a "distance" score to a candidate element in a specific direction. This is used internally by findSpacnavElement to determine the best next element. Lower scores are better. It returns null if the candidate is not in the specified direction or exceeds the angular threshold.

Configuration

spacnav-threshold attribute

You can control how "wide" the search beam is by setting the spacnav-threshold attribute on an element or any of its parents.

  • Value can be in radians (e.g., 1.57) or degrees (e.g., 90deg).
  • Default is 2π/3 (approx 120 degrees).

Tests

Automated testing is done with vitest and playwright in a headless Chrome instance. To run tests, install NPM packages and run the NPM test script:

npm i
npm test

Credits

spacnav was developed by Gilchrist Pitts, initially for a web-based game project.

Originally, it was made as an attempt to modify the algorithm in the CSS Spatial Navigation Module Level 1 specification draft:

License

MIT