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

dom-router

v5.1.3

Published

URL hash DOM router

Downloads

29

Readme

dom-router

Imagine you didn't have to write a bunch of JavaScript to get a slick, progressively enhanced interface! dom-router is a URL hash to DOM router which automatically, & intelligently toggles visibility of Elements based on popstate events.

This provides a clean separation of concerns, and progressive enhancement in a simple library. You can write clean HTML, and dom-router will progressively enhance the interface with CSS classes (not supplied). DOM updates happen on an animation frame to minimize impacting your application. An optional callback allows you to handle application state changes the way you want.

Example

This example is meant to demonstrate multi-tier routing in a single page application. When the HTML is "clean", it is functional for screen readers & text based browsers like lynx, and with progressive enhancement, developers can add new behaviour without impacting the experience of other consumers.

Minimal coding required

import {router} from "./dom-router.js";
window.appRouter = router({callback: arg => console.log(`${arg.element.id} is visible`)});

Before routing is enabled

<nav>
  <ul>
    <li><a href="#main">Main</a></li>
    <li><a href="#settings/billing" class="settings">Billing</a></li>
    <li><a href="#settings/password" class="settings">Password</a></li>
    <li><a href="#settings/avatar" class="settings">Avatar</a></li>
  </ul>
</nav>
...
<article>
  <section id="main">...</section>
  <section id="settings">
    <section id="billing">...</section>
    <section id="password">...</section>
    <section id="avatar">...</section>
  </section>
</article>

After routing is enabled

This would be the result if a user visited #settings/billing:

<nav>
  <ul>
    <li><a href="#main">Main</a></li>
    <li><a href="#settings/billing" class="settings dr-current">Billing</a></li>
    <li><a href="#settings/password" class="settings">Password</a></li>
    <li><a href="#settings/avatar" class="settings">Avatar</a></li>
  </ul>
</nav>
...
<article>
  <section id="main" class="dr-hidden">...</section>
  <section id="settings">
    <section id="billing">...</section>
    <section id="password" class="dr-hidden">...</section>
    <section id="avatar" class="dr-hidden">...</section>
  </section>
</article>

How can I load dom-router?

When loaded with a script tag, window.domRouter.router() will be created.

Configuration

active

Boolean which enables/disables routing

callback

Function to execute after route has changed, takes arg which describes the event

css

Object with current, & hidden keys which have corresponding CSS class values, defaults to "dr-current", & "dr-hidden"

ctx

Context for DOM selector, defaults to body if not specified

delimiter

Multi-tier routing delimiter, defaults to /, e.g. #settings/billing; each tier should map to a nested id

logging

Boolean which logs routing to router.history[] if true, defaults to false; could be a memory leak if logging is enabled and target Elements are removed from DOM

start

[Optional] The starting route to display if one is not specified, or an invalid route is specified

stickyPos

Boolean which enables/disables remaining at Y position when the route changes, i.e. no scrolling, defaults to true.

stickyRoute

Boolean which enables/disables sticky routing, defaults to true.

stickySearchParams

Boolean which enables/disables sticky searchParams of location, defaults to false. When it disabled history.replaceState() is executed on location before callback().

storage

String Storage used for stickyRoute, defaults to session; valid options are session or local.

storageKey

String Key for persistent storage for stickyRoute.

API

current()

Returns the current Route; if logging is enabled the trigger Element will be present

popstate()

Event handler

scan(default)

Scans ctx for routes & resets default which is an optional argument, otherwise it defaults to the first route

select(query)

Context specific DOM selector

Requirements

  • Element.classList API, or shim
  • popstate Event

License

Copyright (c) 2022 Jason Mulligan Licensed under the BSD-3 license