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

@abradley2/url-change-event

v1.0.1

Published

Intercepts link clicks and url changes then fires an event

Downloads

5

Readme

Url Change Event

const initialize = require('@abradley2/url-change-event')

Convenience module for intercepting all link clicks on the body of the document that have either data-link="replaceState" or data-link="pushState" attributes, calling the corresponding history method, and then firing a urlchange event on the window when the location of the document has changed in this way.

This makes it very simple to support routing in single page applications.

Usage

const initialize = require('@abradley2/url-change-event')

// initialize the listeners and interceptors. Returns a function
// that will unsubscribe these listeners.
const stopListening = initialize()

// you can now listen to the "urlchange" event on the window
window.addEventListener('urlchange', (event) => {
  // you can handle this however. The location on the document is passed for convenience

  return document.location === event.detail // true
})

A link that triggers replaceState should look similar to this:

<a href="/path/in/application#someAnchor" data-link="replaceState"> Go somewhere! </a>

A link that triggers pushState should look similar to this:

<a href="/push/some/state?query=true" data-link="pushState"> Go somewhere! </a>

The data-link attribute is important. If the value is either pushState or replaceState, then preventDefault will be called and the event triggered. If not, then nothing happens.

Popstate events

In addition to navigation via page links, the urlchange event is also fired for the popstate event, so there's no need to manually subscribe to that as well.

State

The initialization function for this helper method takes on optional argument: a function which recieves the element of the link clicked as it's first argument, and then returns whatever data is desired to be passed as the state argument in the subsequent history change.

const initialize = require('@abradley2/url-change-event')

initialize(function getStateFromElement(el) {
  // this can be anything really. It is passed as the `stateObj` to the history method
  // (the first argument to either history.replaceState or history.pushState)
  return {
    data: el.getAttribute('data-link-state')
  }
})

Title

To have the title of the document change in response to a link being clicked, via the second argument to the invoked history method, just set the data-title attribute to the desired document title on any associated data-link element.

License

MIT