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

dom-listener

v0.1.2

Published

A listener for delegated DOM events.

Downloads

7

Readme

dom-listener

This library simplifies the event delegation pattern for DOM events. When you build a DOMListener with a DOM node, you can associate event handles with any of its descendant nodes via CSS selectors.

Say you have the following DOM structure.

<div class="parent">
  <div class="child">
    <div class="grandchild"></div>
    <div class="grandchild"></div>
  </div>
</div>

Now you can associate a click event with all .grandchild nodes as follows:

DOMListener = require 'dom-listener'

listener = new DOMListener(document.querySelector('.parent'))
listener.add '.grandchild', 'click', (event) -> # handle event...

Selector-Based Handlers

To create a selector-based handler, call DOMListener::add with a selector, and event name, and a callback. Handlers with selectors matching a given element will be invoked in order of selector specificity, just like CSS. In the event of a specificity tie, more recently added handlers will be invoked first.

listener.add '.child.foo', 'click', (event) -> # handler 1
listener.add '.child', 'click', (event) -> # handler 2
listener.add '.child', 'click', (event) -> # handler 3

In the example above, all handlers match an event on .child.foo, but handler 1 is the most specific, so it will be invoked first. Handlers 2 and 3 are tied in specificity, so handler 3 is invoked first since it is more recent.

Inline Handlers

To create event handlers for specific DOM nodes, pass the node rather than a selector as the first argument to DOMListener::add.

childNode = document.querySelector('.child')
listener.add childNode, 'click', (event) -> # handle inline event...

This is a bit different than adding the event handler directly via the native .addEventListener method, because only inline handlers registered via DOMListener::add will correctly interleave with selector-based handlers. Interleaving selector-based handlers with native event listeners isn't possible without monkey-patching DOM APIs because you can't ask an element what event handlers are registered.

Disposing of Handlers

If you want to remove an event handler, call .dispose() on the Disposable returned from DOMListener::add:

disposable = listener.add 'child', 'click', (event) -> # handle event
disposable.dispose() # remove event handler

Destroying the Listener

If you want to remove all event handlers associated with the listener and remove its native event listeners, call DOMListener::destroy().

listener.destroy() # All handlers are removed

You can add new event handlers and call .destroy() again at a later point.