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

html-dom-navigator

v0.0.2

Published

Building navigation Tree to navigate by html DOM

Downloads

1

Readme

html-dom-navigator

Build Status Coverage Status

The Navigator allows you to navigate with keyboard arrows and mouse click by the html navigation elements which have data-nav attribute. You can see an interactive example here.

Install

npm install html-dom-navigator

How to use

Example:

import Navigator from "html-dom-navigator";

const html = document.createElement('div')
html.innerHTML = `
  <div id="nav-panel" data-nav="row">
    <div data-nav="column">
      <div data-nav="item" data-nav-label="it will be activated by default">column-1</div>
      <div data-nav="item">column-2</div>
    </div>
    <div data-nav="row">
      <div id="uuid" data-nav="item" data-nav-label="test-label">row-1</div>
      <div data-nav="item" data-nav-uuid="test-uuid">row-2</div>
      </div>
  </div>
`

const navPanel = document.getElementById('nav-panel') as HTMLElement
const navigator = new Navigator()

navigator.subscribe(navPanel)

Navigation tree handle next events:

  • Changing DOM in order to rebuild navigation tree.
  • Keyboard arrows, tab keypress in order to activate next navigation node.
  • Clicking on navigation node with attribute data-nav="item" to activate navigation node.

After changing DOM, Navigation tree rebuild and activate previous active navigation node otherwise it activates first navigation node which was found.

If you want to navigate manually, you can use one of the possible ways:

  1. Navigate by UUID. You just need to get it from html element. UUID generates on each node automatically. Don't set uuid manually. This may affect the correct operation.

Example:

const elemWithUUID = document.getElementById('uuid') as HTMLElement
const uuid = elemWithUUID.dataset.navUuid

navigator.activateNavNodeByUuid(uuid)
  1. Navigate by Label. You can set a label via data-nav-label attribute.

Example:

navigator.activateNavNodeByLabel('test-label')

If you activate block navigation node, then first navigation child with attribute data-nav="item" will be activated.

You can also deactivate current active node.

Example:

navigator.deactivateNavNode()

Remember to unsubscribe from the navigator before deleting the item to prevent memory leaks.

Example:

navigator.unsubscribe()

Attributes

The following attributes can be set on the navigation node:

  • data-nav: "row" | "column" | "item" - set by user

    "row" and "column" is block type. Elements with this type can contain other navigation items.

    "item" is element that can be activated. It cannot contain other navigation elements.

    <div data-nav="row">
      <div data-nav="item">1<div>
      <div data-nav="item">2<div>
      <div data-nav="item">3<div>
    <div>
    
    <div data-nav="column">
      <div data-nav="item">1<div>
      <div data-nav="item">2<div>
      <div data-nav="item">3<div>
    <div>
  • data-label: string - set by user

    Label can be set on any navigation element.

    <div data-nav="row" data-nav-label="nav-block">
     <div data-nav="item" data-nav-label="home">home<div>
    <div>
  • data-uuid: string - set automatically

    UUID generates on each node automatically and has view template 8-4-4-4-12.

    <div data-nav="row" data-nav-uuid="9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d">
     <div data-nav="item" data-nav-uuid="1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed">home<div>
    <div>
  • data-active-nav-item: "true" | nothing - set automatically if element is active

    This attribute set only on active element.

    <!-- active   -->
    <div data-nav="item" data-active-nav-item="true">home<div>
    
    <!-- not active   -->
    <div data-nav="item">home<div>
  • data-nav-focus: string - set by user

    This attribute set only on item navigation element. You can use any selector as for Document.querySelector() method. In most cases it needs for custom inputs.

    <div data-nav="item" data-nav-focus="#input">
     <input id="input" type="text" placeholder="enter text" />
    </div>