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

route-event

v6.0.5

Published

Simple client side route event

Downloads

1,180

Readme

route event

tests types module semantic versioning license

Simple route event for the browser. This will handle URL changes client-side, so that navigating will not cause a page reload.

Call a function with a path whenever someone clicks a link that is local to the server. Also, use the history API to handle back/forward button clicks.

install

npm i route-event

CJS

var Route = require('route-event').default

ESM

import Route from 'route-event'

example

Listen for click events on document.body. If the event is triggered by using the browser's back/forward button, then { popstate } will be true.

import { Route } from 'route-event'
const routeEvent = Route()  // by default listen on document.body

// listen for click events on docuement.body. If the href is local to the
// server, call `onRoute`
var stopListening = routeEvent(function onRoute (path, data) {
  console.log(path)
  // => '/example/path'
  console.log(data)
  // => { scrollX: 0, scrollY: 0, popstate: false }

  // handle scroll state like a web browser
  // (restore scroll position on back/forward)
  if (data.popstate) {
      return window.scrollTo(data.scrollX, data.scrollY)
  }

  // if this was a link click (not back button), then scroll to top
  window.scrollTo(0, 0)
})

// programmatically change the location and call the onRoute cb
routeEvent.setRoute('/some/path')

// ...sometime in the future...
// unsubscribe from route events
stopListening()

Pass in an element to listen to, and handle events with a router:

import Route from 'route-event'
import Router from '@bicycle-codes/routes'

const router = Router()
const routeEvent = Route({
  el: document.getElementById('example')
})

router.addRoute('/', function () {
  console.log('root')
})

routeEvent(function onChange (path, ev) {
  var m = router.match(path)
  m.action()

  // handle scroll state like a web browser
  if (ev.popstate) {
      return window.scrollTo(ev.scrollX, ev.scrollY)
  }
  window.scrollTo(0, 0)
})

API

Listener

Event listeners are functions that take an href and an object with previous scroll position and popstate -- a boolean indicating if this was a link click or back / forward button (true means it was back/forward button).

interface Listener {
  (href:string, data:{
    scrollX:number,
    scrollY:number,
    popstate:boolean
  }):void;
}

Route

Create an instance of the event listener. Optionally take an element to listen to. Return a function that takes a callback that will receive route events. The returned function also has a property setRoute that will prgrammatically change the URL and call any route listeners.

import Route from 'route-event'
function Route (opts:{ el?:HTMLElement } = {}):{
    (cb:Listener):void;
    setRoute:ReturnType<typeof singlePage>
}

setRoute

A property on the returned function so you can programmatically set the URL.

function setRoute (href:string):void
const routeEvent = Route()
routeEvent.setRoute('/example')