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

vanjs-routing

v1.1.3

Published

Declarative routing for the VanJS framework

Downloads

25

Readme

VanJS Routing

The cleanest, simplest declarative routing solution for the VanJS framework. If you are coming from React, vanjs-routing feels similar to react-router.

Install

yarn add vanjs-routing vanjs-core
npm i -S vanjs-routing vanjs-core

Features

  1. Declare routes with Router() using a clean, simple and concise syntax
  2. Use Link() instead of a() to navigate between pages
  3. Use navigate() in areas Link cannot be used. (E.g. In a side-effect)
  4. Access the router internal state
    • Get the current pathname with getRouterPathname()
    • Get the dynamic URL parameters with getRouterParams()
    • Get the query parameters with getRouterQuery()
  5. Supports dynamic URLs (E.g. /help/:section/:topic) with getRouterParams()
  6. Supports URL prefixing using Router.basename. (Useful for sites like Github Pages)

QuickStart

import van from "vanjs-core";
import { Router, Link, getRouterParams, navigate } from "vanjs-routing";

const { div, p, button } = van.tags;

function App() {
  return Router({
    basename: "vanjs-routing", // Optional base name (All links are now prefixed with '/vanjs-routing')
    routes: [
      { path: "/", component: HomeComponent },
      { path: "/about", component: AboutComponent },
      { path: "/help/:section", component: HelpComponent }
    ]
  });
}

function HomeComponent() {
  return div(p("Home"), Link({ href: "/about?foo=bar" }, "Goto About"), Link({ href: "/help/profile" }, "Goto Help"));
}

function AboutComponent() {
  return div(p("About"), Link({ href: "/" }, "Back to Home"));
}

function HelpComponent() {
  van.derive(() => {
    console.log(getRouterParams()); // { section: "profile" }
  });

  return div(
    p("Help"),
    Link({ href: "/" }, "Back to Home"),
    button({ onclick: () => navigate("/") }, "Back to Home (Imperative navigation)")
  );
}

van.add(document.body, App());

API Reference

Router

  • The Router component which you use to define your routes
  • Each route is an object with a path and component
    • The component is a function returning an HTMLElement
import { Router } from "vanjs-routing";

Router({
  basename?: string,
  routes: Array <{
    path: string,
    component: () => HTMLElement
  }>
});

Link

  • The Link extends the van.tags.a component to tap into the router's internal state for navigation
  • Link is a drop-in replacement for van.tags.a
  • If replace is set to true, the current route will be replaced with the Link's href when clicked
import { Link } from "vanjs-routing";

Link({
  replace?: boolean
  // Additional van.tags.a props
});

Navigate

  • The navigate function is useful in areas where Link cannot be used. For example in a function or side-effect
  • If replace is set to true, the current route will be replaced with href instead of pushing to the history stack.
import { navigate } from "vanjs-routing";

navigate(
  href,
  options ?: {
    replace?: boolean
  }
)

Router state helpers

  • getRouterPathname() returns the current pathname
  • getRouterParams() returns the parameter values in a dynamic route
  • getRouterQuery() returns the query parameters
import { getRouterPathname, getRouterParams, getRouterQuery } from "vanjs-routing";

// Route path:    /home/:section/:topic
// Current URL:   https://hello.com/home/learning/science?tab=intro

console.log(getRouterPathname()); // "/home/learning/science"
console.log(getRouterParams()); // { section: "learning", topic: "science" }
console.log(getRouterQuery()); // { tab: "intro" }

Contributors

Change Log

  • 1.1.3
    • Update package.json metadata and README documentation
  • 1.1.2
    • Update README documentation
  • 1.1.0
    • Added basename prop to Router component.