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

better-hash-router

v3.2.2

Published

A Router Library for Hash links on an html page

Downloads

124

Readme

Better-Hash-Router

A better routing using hashed anchor tags on your html pages.

Usage

include the script from CDN https://unpkg.com/better-hash-router@latest

index.html

<script src="./script.js" type="module"></script>

script.js

import { Hash } from "https://unpkg.com/better-hash-router@latest";

see HTML example on Github here

or if you're using npm

Install npm i better-hash-router

// ES6
import { Hash } from "better-hash-router";

// CommonJS
const { Hash } = require("better-hash-router");

Note: The <body> </body> must be empty. all the content on each route will be parsed and injected to the <body></body> at runtime.

How TO

  1. First you have to initialize the library, else it'll remind you to doing so by throwing an error.
Hash.initialize();
  1. Now you have to create a new instance of Hash
const myRouter = new Hash("router-name");
  1. Now you can add new Routes to your Router instance.

    route() method accepts 2 parameters.

  • path: a String representing the hashed path without the #. eg. if path is "#about" then remove the #. which is "about"
  • data: String | HTMLElement | Object | Function data can be passed in as multiple formats. see the examples below.
  • route() method returns the router instance. therefore the method is chainable.

Both Arguments are Mandatory.

"/" path determines the root path, which is your default path. so the contents assigned to the "/" route will be displayed when you open your page.

Adding new routes
  • data: string
Hash.initialize();
const myRouter = new Hash("router-name");
myRouter.route("/", "this will be displayed on your root");
  • data: HTMLElement
Hash.initialize();
const myRouter = new Hash("router-name");

const aboutDiv = document.createElement("div");
aboutDiv.innerHTML = "Hey this is about me";

myRouter.route("about", aboutDiv);
  • data: { template, selector }
   { 
    template: "absolute path to file", 
    selector: "css selector" 
    }

assuming that you have an html file on /pages/contact.html and there is an element with id contact-form

NOTE: Here the selector is optional. if selector is not provided, then all the contents of the contact.html's body will be copied to the route.

Hash.initialize();
const myRouter = new Hash("router-name");
myRouter.route("contact", { 
 template: "/pages/contact.html", 
 selector: "#contact-form" 
});

data: Function

Hash.initialize();
const myRouter = new Hash("router-name");
myRouter.route("/", () => {
 return "this data comes from a function";
});

Chaining route() methods

instance.route() can be chained since it returns the instance itself.

Hash.initialize();
const myRouter = new Hash("router-name");
myRouter
.route("/", "this page shows info about me")
.route("contact", "my contact info")
.route("any_other_route", "some interesting stuff"); 

// goto yourdomain:port/#any_other_route to see the content

Manually open a page

instance.open() can be used to open a path manually.

  • accepts an argument Path as a String
const button = document.getElementById("contact-button");
button.addEventListener("click", event => {
myRouter.open("contact");
});

Events

Hash.EVENTS // all supported events can be accessed via this static property.
  1. open - fires when you open a page using instance.open(path).
  2. doneparsing - fires when a intance.route(path, data) parse a template.
  3. close - fires when the close() method is called.
  4. error - fires when a parsing error occures on a route.

Event Handlers

  1. addEventListener(event, callback)
myRouter.addEventListener("open",function (event){
 // your code goes here..
});
  1. onPageLoad(path, callback)
myRouter.onPageLoad("/", function (event) {
 // your code goes here..
});
  1. onReady(path, callback)
myRouter.onReady("/", function (event) {
 // your code goes here..
});
  1. onError(callback)
myRouter.onError(function(){
 // your code goes here..
});

Instance Methods

1. route(path, data)
2. open(path)
3. fetchTemplate({template, selector})
4. onPageLoad(path, callback)
5. onReady(path, callback)
6. onError(callback)
7. addEventListener(event, callback)

Instance Properties

1. availableRoutes (getter)

Static Methods

1. initialize()
2. isRouteDefined(path)
3. close()
4. router(name)

Static Properties

1. availableRouters
2. isInitialized
3. availableTemplates
4. EVENTS

Examples

  1. using a function as the data argument.

function must have a return statement of a String or an HTMLElement

Hash.initialize();

const navigationRouter = new Hash("navigation");

navigationRouter.route("/", () => {
  const tiles = ["skills", "projects", "about", "resume", "contact"];
  const wrapper = document.createElement("div");

  tiles.forEach(tile => {
    const link = document.createElement("a");
    a.innerHTML = tile;
    wrapper.append(link)
  });

  return wrapper;
});
  1. using templates from a single html file.

assuming that you have a template.html file in your root directory. and they have elements with appropriate id

Hash.initialize();
 
const myRouter = Hash.router("my router");

myRouter
 .route("/", {template: "/template.html", selector: "#home"})
 .route("about", {template: "/template.html", selector: "#about"})
 .route("contact", {template: "/template.html", selector: "#contact"});

DOM Manipulation

If you want to manipulate the content of your routes, please note that they don't exist at the window.onload event. because the routes and their contents are fetched and rendered after the onload event of window. so, instead of using window.onload, you should use the onPageLoad eventListener on your router instance.

assuming that you already have a contact route defined on your router

myRouter.onPageLoad("contact", (event) => {
 console.log("contact page has loaded");
 // now you can Manipulate your DOM elements her...
});

CONTRIBUTOR

Amalu Sajeev

Feel free to reach out to me..