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 🙏

© 2025 – Pkg Stats / Ryan Hefner

on-off-listener

v1.0.0

Published

Tiny type-safe helper for addEventListener with built-in cleanup.

Readme

on-off-listener

A tiny helper that adds event listeners with full TypeScript safety and hands you a cleanup function so you never forget to unsubscribe.

Why people reach for it

  • Intellisense everywhere – Event names autocomplete per target, and handler arguments are strongly typed.
  • One-line cleanup – The function you get back removes the listener with the exact same options.
  • No dependencies – Ships as plain TypeScript. Works in any modern build step.
  • DOM-friendly extras – Includes a closest helper for event delegation in the browser.

Installation

npm install on-off-listener
pnpm add on-off-listener
bun add on-off-listener

Quick start

import { on } from "on-off-listener";

const button = document.querySelector("button")!;
const off = on(button, "click", (event) => {
  console.log("Button clicked at X:", event.clientX); // `event` is a MouseEvent
});

off(); // Removes the listener

Need to pass AddEventListenerOptions (like capture or once)? Provide a small object with a type property:

on(window, { type: "resize", passive: true }, () => {
  console.log("Resized!");
});

Custom events still work—just type the handler yourself and enjoy the shared cleanup story.

Browser closest helper

If you prefer event delegation, wrap your listener with closest so you only attach one handler:

import { on, closest } from "on-off-listener";

const off = on(
  document.body,
  "click",
  closest("button", (event, button) => {
    console.log("Clicked button text:", button.textContent);
  }),
);

The filtered listener receives the original event plus the closest matching element, passed as the second argument.

API highlights

  • on(target, typeOrOptions, listener)

    • target: Any EventTarget
    • typeOrOptions: Either the event name ("click") or { type, ...addEventListener options }
    • listener: Function or EventListenerObject
    • Returns: () => void to remove the listener
  • closest(selector, listener)
    Filters a listener so it only fires when the event target or one of its ancestors matches the selector. Your listener receives the original event and the matched element, with HTML, SVG, and MathML tags typed out of the box.

How typing works

During development you can run bun run gen-types:browser to regenerate the DOM event map shim. The library attaches a hidden eventMap symbol to each DOM interface so TypeScript can infer the right event types for every EventTarget.


That’s it—import on, attach listeners with confidence, and tear them down just as easily.