Skip to content

Latest commit

 

History

History
39 lines (27 loc) · 1.52 KB

README.npm.md

File metadata and controls

39 lines (27 loc) · 1.52 KB

Rad Event Listener

minzip size install size dependency count

Please see the full README at https://github.com/JLarky/rad-event-listener

Before:

function handler(this: Document, e: MouseEvent) {
  console.log("mouse moved to", e.x, e.y, this === e.currentTarget);
};

document.addEventListener("mousemove", handler);

const cleanup = () => {
  document.removeEventListener("mousemove", handler);
};

After:

import { on, rad, radEventListener } from "rad-event-listener";

const cleanup = radEventListener(document, "mousemove", function (e) {
  console.log("mouse moved to", e.x, e.y, this === e.currentTarget);
});

Both of examples are written in a type-safe manner that will not allow you to make mistakes. But one of them made you work much more to get types of this and e right as well as made you do more work to remove the listener.

Live examples