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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nadi.js/router

v0.1.0-alpha.1

Published

Lightweight client-side router for Nadi

Readme

@nadi/router

Lightweight client-side router for Nadi framework (~1.3KB gzipped).

Features

  • 🎯 Path pattern matching with parameters (:id)
  • 🌳 Nested routes support
  • 🔒 Navigation guards for route protection
  • 🎨 Active link styling
  • History API integration
  • 📦 Type-safe route definitions
  • 🪶 Tiny bundle size (~1.3KB gzipped)

Installation

pnpm add @nadi/router

Quick Start

1. Define Your Routes

import { createRouter } from '@nadi/router';

const router = createRouter({
  routes: [
    {
      path: '/',
      component: Home,
      name: 'home',
    },
    {
      path: '/about',
      component: About,
      name: 'about',
    },
    {
      path: '/users/:id',
      component: UserProfile,
      name: 'user-profile',
    },
    {
      path: '/admin',
      component: AdminLayout,
      beforeEnter: (to, from) => {
        // Check authentication
        if (!isAuthenticated()) {
          return '/login'; // Redirect
        }
        return true; // Allow navigation
      },
      children: [
        {
          path: '/dashboard',
          component: Dashboard,
        },
      ],
    },
  ],
  scrollBehavior: (to, from) => {
    return { top: 0, behavior: 'smooth' };
  },
});

2. Use Router in Your App

import { useRoute } from '@nadi/router';

function App() {
  const route = useRoute();

  return (
    <div>
      <nav>
        <Link to="/" activeClass="active">
          Home
        </Link>
        <Link to="/about" activeClass="active">
          About
        </Link>
      </nav>

      <main>
        {route.path === '/' && <Home />}
        {route.path === '/about' && <About />}
      </main>
    </div>
  );
}

3. Navigate Programmatically

import { useNavigate, useParams } from '@nadi/router';

function UserProfile() {
  const navigate = useNavigate();
  const params = useParams();

  const goToSettings = () => {
    navigate.push(`/users/${params.id}/settings`);
  };

  return (
    <div>
      <h1>User {params.id}</h1>
      <button onClick={goToSettings}>Settings</button>
    </div>
  );
}

API Reference

createRouter(config)

Creates a router instance.

Config options:

  • routes: Array of route definitions
  • base: Base URL for the app (default: '')
  • scrollBehavior: Function to control scroll position on navigation
  • beforeEach: Global navigation guard
  • afterEach: Global hook called after navigation

useRouter()

Returns the router instance.

const router = useRouter();
router.push('/about'); // Navigate to /about
router.replace('/home'); // Replace current route
router.back(); // Go back
router.forward(); // Go forward
router.go(-2); // Go back 2 steps

useRoute()

Returns the current route match.

const route = useRoute();
console.log(route.path); // '/users/123'
console.log(route.params); // { id: '123' }
console.log(route.query); // { tab: 'profile' }
console.log(route.hash); // '#section'

useParams()

Returns route parameters.

const params = useParams();
console.log(params.id); // '123'

useNavigate()

Returns navigation functions.

const navigate = useNavigate();
navigate.push('/about');
navigate.replace('/home');
navigate.back();
navigate.forward();
navigate.go(-2);

<Link> Component

Link component for client-side navigation.

<Link
  to="/about"
  class="nav-link"
  activeClass="active"
  replace={false}
  onClick={(e) => console.log('clicked')}
>
  About
</Link>

Props:

  • to: Target path
  • class: CSS class
  • activeClass: CSS class when route is active
  • replace: Use replace instead of push (default: false)
  • onClick: Click handler

<Route> Component

Conditional rendering based on current route.

<Route path="/about" component={About} />

Navigation Guards

Global Guards

const router = createRouter({
  routes: [...],
  beforeEach: (to, from) => {
    // Return false to cancel navigation
    if (to.path === '/admin' && !isAdmin()) {
      return false;
    }

    // Return string to redirect
    if (!isAuthenticated()) {
      return '/login';
    }

    // Return true to allow navigation
    return true;
  },
  afterEach: (to, from) => {
    // Track page views
    analytics.track(to.path);
  }
});

Route-Specific Guards

{
  path: '/admin',
  component: Admin,
  beforeEnter: (to, from) => {
    if (!isAdmin()) {
      return '/'; // Redirect to home
    }
    return true;
  }
}

Nested Routes

{
  path: '/dashboard',
  component: DashboardLayout,
  children: [
    {
      path: '/overview',
      component: Overview
    },
    {
      path: '/settings',
      component: Settings
    }
  ]
}

Route Parameters

// Route definition
{
  path: '/users/:userId/posts/:postId',
  component: Post
}

// Access parameters
const params = useParams();
console.log(params.userId); // '123'
console.log(params.postId); // '456'

Query Parameters

// URL: /search?q=nadi&page=2
const route = useRoute();
console.log(route.query.q); // 'nadi'
console.log(route.query.page); // '2'

Scroll Behavior

const router = createRouter({
  routes: [...],
  scrollBehavior: (to, from) => {
    // Scroll to top
    return { top: 0, behavior: 'smooth' };

    // Scroll to anchor
    if (to.hash) {
      return { top: 0, behavior: 'smooth' };
    }
  }
});

TypeScript Support

Full TypeScript support with type-safe route definitions and parameters.

import type { RouteDefinition, RouterConfig } from '@nadi/router';

const routes: RouteDefinition[] = [
  {
    path: '/',
    component: Home,
  },
];

const config: RouterConfig = {
  routes,
};

License

MIT