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

oxidizer-router

v2.1.2

Published

A router component & state management library for use with the oxidizer library

Downloads

15

Readme

oxidizer-router

A router component & state management library for use with the oxidizer library.

Installation

npm install oxidizer-router

Usage

Router

  • This package provides a default export Router which is a function used to map routes to elements.
import Router from "oxidizer-router";
import { DIV } from "oxidizer/intrinsics";

export default function App(){
  return (
    Router({
      '/': () => (
        DIV('Home Page')
      ),
      '/pages': {
        'index' : () => DIV('List of Pages'),
        '/1' : () => DIV('Page One')
      },
      '*': () => (
        DIV('404: Invalid URL')
      )
    })
  )
}
  • As seen in the example above, routes can map to either a function that returns some instance of an HTMLElement, or another Routes object

Dynamic Routes

  • Dynamic Routes can be created by using a : instead of a / at the beginning of a route.
    • The values of these dynamic routes can be acquired via the getParams method.
import Router, {getParams} from "oxidizer-router";
import { DIV } from "oxidizer/intrinsics";

const User = () => {
  const {userId} = getParams();
  return DIV('User ID: '+userId);
}

export default function App(){
  return (
    Router({
      '/': () => DIV('Home Page'),
      '/user': {
        'index' : () => DIV('List of Users'),
        ':userId' : User
      }
    })
  )
}

Catch All (star) Routes

  • A route which is simply a star * will be rendered in the event that all other routes failed to match the specified path.
import Router, {getParams} from "oxidizer-router";
import { DIV } from "oxidizer/intrinsics";

export default function App(){
  return (
    Router({
      '/': () => DIV('Home Page'),
      '*': () => DIV('404: Not Found'),
      '/pages': {
        'index' : () => DIV('List of Pages'),
        '/1' : () => DIV('Page One'),
        '*' : () => DIV('404: Page Does Not Exist')
      },
    })
  )
}
  • In this example, the route /goose will render a div with content "404: Not Found"
  • The route /pages/goose on the other hand will render a div with content "404: Page Does Not Exist"

Index Routes

  • Index routes can be used when a certain path should render an element, however, it may also have child paths.
import Router, {getParams} from "oxidizer-router";
import { DIV } from "oxidizer/intrinsics";

export default function App(){
  return (
    Router({
      '/': () => DIV('Home Page'),
      '/pages': {
        'index' : () => DIV('List of Pages'),
        '/1' : () => DIV('Page One'),
      },
    })
  )
}
  • In this example, /pages will render a div with content "List of Pages"
  • The route /pages/1 on the other hand will render a div with content "Page One"

Utility Functions

navigate

  • used to update the path state. This function should be used to traverse routes.
  • Beginning the desired route with a slash / indicates an absolute path, while not using a slash indicates a relative path.

navigate('/pages') // path --> '/pages'
navigate('one')    // path --> '/pages/one'

/* Setting Search and Hash */
navigate('/pages/one', {search: {name:"John"}, hash:"example"})
// path --> /pages/one?name=John#example

getParams

  • used to get the current values of dynamic routes.

getSearch

  • used to get the search params for a current route.
    • returns a string,string object.

getHash

  • used to get the current hash of a url

getPathname

  • used to get the current pathname

setSearch

  • used to set the current search parameters. Takes 1 argument, either a string,string object, or a string.

setHash

  • used to set the hash

Example

  • The following example was built off an template app created with the create-oxidizer-app package.

1. Create the Views

// ./src/views/HomePage.ts

import { H1, MAIN } from "oxidizer";

export default function HomePage () {
  return (
    MAIN(
      H1('Hello World')
    )
  )
}
// src/views/Users.ts

import { BR, DIV, LI, UL } from "oxidizer"

export const users = <const>{
  '10001': 'Jane Doe',
  '10002': 'John Doe',
  '10003': 'Horse Fish'
}

export default function Users() {
  return (
    DIV({ id: 'users' },
      UL(
        Object.entries(users).map(([id, name]) => (
          LI(
            `ID: ${id}`, 
            BR(),
            `Name: ${name}`
          )          
        ))
      )
    )
  )
}
// src/views/User.ts

import { DIV, H1, H2, HR } from "oxidizer";
import { getParams } from "oxidizer-router";
import { users } from "./Users";

export default function User() {
  const { userId } = getParams();

  if (!(userId in users)) return (
    DIV(
      H1('User Does Not Exist :(')
    )
  )

  return (
    DIV(
      H1(`User #${userId}`),
      HR(),
      H2(`Name: ${users[userId]}`)
    )
  )
}

2. Create a Navbar

// ./src/components/Navbar.ts
import { DIV, BUTTON, INPUT } from "oxidizer/intrinsics";
import { navigate } from "oxidizer-router";

export default function Navbar() {
  const NavInput = INPUT({
    placeholder: 'Type your Route Here' 
  });

  return (
    DIV({ id: 'navbar' },
      DIV({ style: { display: 'flex' } },
        NavInput,
        BUTTON({
          onclick: () => navigate(NavInput.value)
        }, 'Go')
      )
    )
  )
}

3. Create your App and Router

// ./src/App.ts

import { DIV } from "oxidizer/intrinsics";
import Router from "oxidizer-router";
import HomePage from "./views/HomePage";
import Users from "./views/Users";
import User from "./views/User";
import Navbar from "./components/Navbar";

export default function App() {
  return (
    DIV({ id: 'app' },
      Navbar(),
      Router({
        '/': HomePage,
        '/users': {
          'index': Users,
          ':userId': User,
        },
        '*': () => (
          DIV('404: Invalid URL')
        )
      })
    )
  )
}

4. Connect your App to the DOM

// ./src/index.ts

import App from "./App";

const app = App();

document.body.append(app);

And you're all set!