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

@elementumjs/router

v0.0.2

Published

Very basic HTML5 History router implementation for SPA apps.

Downloads

3

Readme

CDN package_version production reference license

@elementumjs/router is a very basic HTML5 History router implementation for SPA apps.


How to use it

Route definition

Any route must have two properties at least: path and view.

  • route.path property contains the URI string definition for this route (the part of the URL that is after the hash character #).
  • route.view property contains the root route element definition, and it can be defined as a string, that represents the element tagName to be created, or as a HTMLElement, that contains the raw element.

The routes have also another two optional properties:

  • route.title that is used to update the document.title when a transition occurs.
  • route.handler property contains a function to be called when a transition occurs.

Example of routes definition:

const aboutView = document.createElement('div');
// ...
const routes = [
    {
        path: '/', 
        title: 'Home | My personal site', 
        view: 'my-homepage' // Set the route view as string tagName
    },
    {
        path: '/about', 
        title: 'About | My personal site', 
        view: aboutView, // Set the route view as HTMLElement instance
        handler: () => {
            // Get route transition data
            console.log('[About View handler]', window.history.state);
        } 
    }
];

Router initialization

To initialize the router, is required to define a root HTMLElement (target) to use as route container and a correct routes definition (check the previous section).

Example of router initialization:

import Router from '@elementumjs/router';

const routes = [ /** ... */ ];
const target = document.querySelector('container');

// Basic router initialization
const router = Router(target, routes);

To use the router instance across the app, it will be defined as global object into the window instance:

import Router from '@elementumjs/router';

const routes = [ /** ... */ ];
const target = document.querySelector('container');

// Global router initialization
window['router'] = Router(target, routes);

Navigate between routes

The navigation is possible with two ways, with HTML anchors or programmatically. The programmatically way allows to send some data to the following view.

| Method | Example | |:---|:---| | HTML anchor | <a href="#/about">About me</a> | | Programmatically | window.router.go('/home', { param1: 'value1' }); |

Full example

WebApp entry point app.js;

import Router from '@elementumjs/router';
import './my-homepage.js';

const aboutView = document.createElement('div');
aboutView.innerHTML = `
    <h1>It's me!</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam bibendum tortor id nisl mollis fermentum. Curabitur nec tellus nisl. Sed placerat aliquet auctor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Curabitur sed posuere odio.</p>
    <button>Go home</button>
`;

// Go to Home programmatically
aboutView.lastElementChild.onclick = () => window.router.go('/', { param1: 'value1' });

// Get target defined on index.html
const target = document.querySelector('container');
const routes = [
    {
        path: '/', 
        title: 'Home | My personal site', 
        view: 'my-homepage' 
    },
    {
        path: '/about', 
        title: 'About | My personal site', 
        view: aboutView, 
        handler: () => {
            // Get route transition data
            console.log('[About View handler]', window.history.state);
        } 
    }
];

// Make router instance available globally
window['router'] = Router(target, routes);

Home page component definition: my-homepage.js.

import { Component, html } from '@elementumjs/component';

Component.attach('my-homepage', class extends Component {
    template() {
        return html`<div>
            <h1>${ 'Hello to my web!' }</h1>
            <a href="#/about">Know more about me</a>
        </div>`;
    }

    created() {
        // Get route transition data
        console.log('[Home View created]', window.history.state);
    }
});

index.html definition:

<!DOCTYPE html>
<html>
	<body>
		<container></container>
		
		<script type="module" src="./app.js"></script>
	</body>
</html>

Installation

Import from CDN as ES Module

Import from jsDelivr CDN:

    import Router from "https://cdn.jsdelivr.net/gh/elementumjs/router/dist/router.esm.js";

Or install the package locally

Download the package

Install via npm:

    npm install @elementumjs/router

Import as ES Module

ES Module builds are intended for use with modern bundlers like webpack 2 or rollup. Use it with ES6 JavaScript import:

    import Router from '@elementumjs/router';

Other import methods

Checkout other import methods in dist/README.md.