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

moon-router

v0.1.3

Published

Router for Moon

Downloads

73

Readme

Moon Router

A router plugin for Moon.

Build Status

What?

Moon router lets you create basic routes that map to different components. Clicking a router-link will update the view, and updates the URL. To the user, it seems like going on a new URL, but in reality, they are on the same page.

Install

With npm:

$ npm install moon-router
var MoonRouter = require("moon-router");
Moon.use(MoonRouter)

With a CDN/Local:

<script src="https://unpkg.com/moon-router"></script>
<script>
  Moon.use(MoonRouter);
</script>

Usage

Initialize Moon router

Moon.use(MoonRouter)

Creating Routes

Before you create your Moon instance, define your routes like this:

var router = new MoonRouter({
  default: "/",
  map: {
    "/": "Root",
    "/hello": "Hello"
  }
});

This will map / to the Root component, and will map /hello to the Hello component.

The default route is /, if a URL is not found, Moon will display this route.

Dynamic Routes

Routes can also be dynamic, with support for query parameters, named parameters, and wildcards. These can be accessed via a route prop passed to the view component.

var router = new MoonRouter({
  map: {
    "/:named": "Root", // `named` can be shown with {{route.params.named}}
    "/:other/parameter/that/is/:named": "Named",
    "/*": "Wildcard" // matches any ONE path
  }
});
  • Named Parameters are in the route.params object
  • Query Parameters are in the route.query object (/?key=val)

Just remember, to access the special route variable, you must state it is a prop in the component, like:

Moon.component("Named", {
  props: ['route'],
  template: '<h1></h1>'
});

Define Components

After initializing Moon Router, define any components referenced.

Moon.component("Root", {
  template: `<div>
    <h1>Welcome to "/"</h1>
    <router-link to="/hello">To /hello</router-link>
  </div>`
});

Moon.component("Hello", {
  template: `<div>
    <h1>You have Reached "/hello"</h1>
    <router-link to="/">Back Home</router-link>
  </div>`
});

You will notice the router-link component. This is by default, rendered as an a tag, and should always be used to link to routes. A class of router-link-active will be applied to the active link by default, unless another class is provided in options.activeClass.

When clicking on this link, the user will be shown the new route at the router-view component (see below), and will not actually be going to a new page.

Installing Router to Instance

When creating your Moon instance, add the Moon Router instance as the option router

new Moon({
  el: "#app",
  router: router
});
<div id="app">
  <router-view></router-view>
</div>

This will install the Moon Router to the Moon Instance, and when you visit the page, you will notice the URL changes to /#/

The router-view is a component that will display the current mapped route.

License

Licensed under the MIT License By Kabir Shah