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

mopac-router

v0.2.0

Published

A small, zero-dependency client-side router

Downloads

10

Readme

Mopac Router

Description

mopac-router is a small, zero-dependency client-side router.

Installing

mopac-router can be installed via npm or used through unpkg:

via npm:

npm install mopac-router

via unpkg:

import { HistoryRouter } from "https://unpkg.com/mopac-router?module";

Usage

mopac-router is completely framework agnostic and can be dropped into any client-side project.

This package currently does not ship with any framework specific bindings, but some are on the roadmap for future inclusion.

Mopac is Typescript native so typings are included out of the box.

Instantiating a router instance

A new Router instance can be created by declaring a new HistoryRouter.

import { HistoryRouter } from "mopac-router";

const router = new HistoryRouter();

Declaring routes

Routes are declared though the router.route method and accepts two arguments: The route's path and a handler callback. The handler will fire on a successful routeChange event:

router.route("/about", () => {
  console.log("I'm on the about page!");
});

Dynamic segments

Dynamic segmenting is also supported. The segments are passed to the handler as an object.

router.route("/user/:userId", ({ userId }) => console.log(`hello, ${user}`));

Search params can be accessed through the search property on the browser's Location object.

Starting the router

The router will not start listening for routeChange events until router.start() has been called. This needs to be done after the routes have been declared. This ensures that if the browser’s current path is a declared route, it’s captured by the router, matched, and fires the corresponding handler.

The start method will also setup the routeChange and popstate .

All this is done through simply calling the router.start() method.

Navigating routes

A route can be changed through firing a routeChange custom event. mopac-router ships with a utility function, changeRoute that wraps a routeChange dispatcher.

import { changeRoute } from "mopac-router";

// If using React/Preact...
const Link = ({ to, title, children }) => (
  <a
    href={to}
    title={title}
    onClick={(e) => {
      e.preventDefault();
      changeRoute(to);
    }}
  >
    {children}
  </a>
);

Example

import Preact from "preact";
import { useCallback, useState } from "preact/hooks";
import { HistoryRouter, changeRoute } from "mopac-router";

const router = new Router();

const Link = ({ to, title, children }) => (
  <a
    href={to}
    title={title}
    onClick={(e) => {
      e.preventDefault();
      changeRoute(to);
    }}
  >
    {children}
  </a>
);

const Home = () => <h1>Home</h1>;

const About = () => <h1>About</h1>;

const App = () => {
  const [route, setRoute] = useState(null);

  router.route("/", () => setRoute(Home));
  route.route("/about", () => setRoute(About));

  useEffect(() => {
    router.start();
  }, [route, router]);

  return (
    <>
      <header>
        <nav>
          <Link to="/" title="Home page">
            Home
          </Link>
          <Link to="/about" title="About page">
            About
          </Link>
        </nav>
      </header>
      <main>{route}</main>
    </>
  );
};

Building & Testing

Building

The package can be built though the build script contained in package.json:

npm run build (or yarn build)

The build will be outputted to dist/ directory.

Testing

Unit tests can be ran via the test script:

npm run test (or yarn test)