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

vanilla-ui-router

v1.2.2

Published

Simple vanilla JavaScript router

Downloads

30

Readme

npm version Build Status Coverage Status Code Climate devDependency Status Unicorn

Vanilla UI Router

Simple vanilla JavaScript router to be used inside a single page app to add routing capabilities.

The router comes with zero dependencies and can be used with any other libraries. It's based on the hashchange-Event.

Installation

$ npm install --save vanilla-ui-router

As UMD module this runs everywhere (ES6 modules, CommonJS, AMD and with good ol’ globals).

Usage

Let's assume your initial markup has the following structure:

<!DOCTYPE html>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="styles.min.css" />
</head>
<body>

	<!-- Entry point, dynamic content is rendered into this DOM element -->
	<main id="app"></main>

	<!-- Bundle where your JavaScript logic lives, even the router configuration -->
	<script src="bundle.js"></script>
</body>
</html>

Then you could configure the router with the following JavaScript:

import {createRouter} from 'vanilla-ui-router';

// Initialize the router with the dynamic DOM entry point
const router = createRouter(document.getElementById('app'));

router

	// Start route: The server side URL without a hash
	.addRoute('', () => {
		/*
			Use navigateTo(…) to make dynamic route changes, i.e. to redirect to another route
		*/
		router.navigateTo('home');
	})

	.addRoute('home', (domEntryPoint) => {
		domEntryPoint.textContent = 'I am the home route.';
	})

	.addRoute('about/:aboutId/:editable', (domEntryPoint, routeParams) => {
		console.log('I am the about route.');

		/*
			routeParams are extracted from the URL and are casted to the correct type
			(Number/Boolean/String)
		*/
		console.log(routeParams); // => { aboutId: 42, editable:false }
	})

	/*
		If routes get more complex, e.g. you need to render a template URL,
		pass a configuration object as second parameter (instead of the function)
	*/
	.addRoute('route-with-template-url', {
		templateUrl: 'path/to/template.html' // is loaded and gets rendered
	})

	.addRoute('route-with-template-string/:id', {
		templateString: '<p>Lorem ipsum dolor.</p>',
		routeHandler: (domEntryPoint, routeParams) => {
			/*
			It's called just after rendering the template, so you can add route-specific logic.
			But only if needed!
			*/
		}
	})

	/*
		You can also define a templateId, i.e. if you have a template-script inside
		your markup like:

		<script type="text/template" id="template42">
			<p>
				Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor, tenetur?
			</p>
		</script>
	*/
	.addRoute('route-with-template-id/:id', {
		templateId: 'template42'
	})

	.addRoute('route-with-dispose', {
		routeHandler: () => {},
		dispose: () => {
			// Is called before navigating to another route to do some cleanup if needed.
		}
	})

	.addRoute('inject-custom-data', {
		routeHandler: (domEntryPoint, routeParams, {customData}) => {
			// It's passed as the last parameter of the route, for instance to pass a redux store.
		},
	}, { customData: 'moep'}) // if you need to pass custom data to your routes

	.otherwise(() => {
		// If no route configuration matches, the otherwise route is invoked.
		console.log('I am the otherwise route');
		router.navigateTo('404');
	});

License

Please be aware of the licenses of the components we use in this project. Everything else that has been developed by the contributions to this project is under MIT License.