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

lski-events

v1.1.1

Published

Micro cross-browser event api

Downloads

2

Readme

Lski-Events

A micro events handler (only 2.3kb - 936bytes gzipped) that behaves like the jQuery event API, where you can add, remove, fire events.

Providing features such as:

  • Single run event handlers
  • Multiple event attachments in single call
  • Delegated events via a selector
  • Flexible target element arguments: either an Element, Elements or a query selector
  • The ability to trigger events
  • Custom events
  • Normalisation of behaviour between browsers
  • IE8 support (With the use an extra plugin supplied see here)

Install

lski-events supports for globals (lski.events), CommonJS and AMD. It is also available via bower or npm using one of the commands below, or can be added as a script using the files in the dist folder.

npm install lski-events
or
bower install lski-events

Usage

There a several functions provided, each has an alias to a jquery method that it matches:

  • add - Adds event listeners (alias 'on')
  • remove - Removes event listeners (alias 'off')
  • once - Adds an event listener that will only fire once (alias 'one')
  • fire - Dispatches an event, so that any listeners will run their handlers (alias 'trigger')

Examples

Add Listeners

The following examples show how to add an event listeners to the following html:

<div class="buttons">
	<button id="mybutton" type="button">Click Me!</button>
	<button id="otherbutton" type="button">Click Me Too!</button>
	<img src="some-image.js"/>
<div>
// With selector
events.add('#mybutton', 'click', function(event) {
	// this = element that event is attached too
});

As well as a selector you can pass in an Element or any 'array like' (e.g. an array or a NodeList) list of Element objects:

// With an element:
events.add(document.getElementById('mybutton'), 'click', function(event) { });

// With an array like list of Element objects:
events.add(document.querySelectorAll('button'), 'click', function(event) { });

You can also add a handler to multiple events at the same time, using a space separated list between event names or an array of event names:

events.add('#mybutton', 'click dblclick', function(event) { });
//or 
events.add('#mybutton', ['click', 'dblclick'], function(event) { });

You can add a "single run" listener too, that will only fire the once then remove itself from the element.

events.once('#mybutton', 'click', function(event) { });

Add Delegated Listeners

You can also handle events that bubble from child elements, these are called delegated events.

The advantage of this is if the child elements are dynamic, they dont have to exist when adding the listener, also it means only adding one listener to one element, rather than the overhead of adding it too all child elements. See addEventListener at MDN for more detailed explanation.

This API gives you two ways of doing this. The first uses the same syntax as above, where a listener is added to a parent element and any child element that fires an event that bubbles is caught by that listener.

// Same behaviour as addEventListener
events.add('.buttons', 'click', function(event) {
	
	/* Catches click events from ANY child element of the div buttons
	 * this = element that the listener is attached too
	 * event.currentTarget = element that the listener is attached too
	 * event.delegateTarget = element that the listener is attached too
	 * event.target = the element clicked e.g. the button or image
	 */
});

The second method does the same thing, but accepts an additional 'selector' as the 3rd parameter. If a child element raises an event that bubbles to the parent, the selector is checked against the child element, if it matches it will run the handler, otherwise does nothing. Still works with child elements that havent been created yet.

// Filters any bubbling events so only the if the button is clicked will it fire
events.add('.buttons', 'click', 'button', function(event) {
	
	/* Catches click events only from child elements of the div that are buttons
	 * this = the element clicked e.g. the button
	 * event.target = the element clicked i.e. only the button
	 * event.currentTarget = element that the listener is attached too
	 * event.delegateTarget = element that the listener is attached too
	 */
});

Remove Listeners

// A handler would normally be added first
function myHandler() { }

events.add('#mybutton', 'click', myHandler);

// Later on you can remove the handler
events.remove('#mybutton', 'click', myHandler);

NB: Removing a handler that hasnt been added yet does NOT throw an error

Like 'add' you can provide a string selector, an Element object or an array like list of Element objects. Plus you can detach a handler from multiple events in a single call like 'add' too:

// Remove handler from multiple events
events.remove('#mybutton', 'click, dblclick', function(event) { });

Trigger Events

Triggering an event means fire against any matching events that have been added to the element

// With a string name for the event
events.trigger('button', 'click');

// Passing data to the listener
events.trigger('button', 'click', { myData: data });

NB: This example creates a cross browser 'CustomEvent' behind the scenes for you, so that you dont have to worry about the different ways of creating an Event object in browsers yourself.

// Using a pre-created Event object 
var evt = new CustomEvent('click', { 'bubbles': true, 'cancelable': true });
events.trigger('button', evt);

// Using a pre-created Event object with data to pass to the listener
var evt = new CustomEvent('click', { 'bubbles': true, 'cancelable': true, detail: { myData: data } });
events.trigger('button', evt);

NB: This example of creating a new event object would be different on different enviroments but the trigger function would work the same regardless.

Build

To build the distribution files, at the command line follow run the following commands. The first command installs required node modules if not already loaded and then run the build.

npm install
npm run build

Test

To run the tests in the browser, at the command line follow run the following commands. The first command installs required node modules if not already loaded and then open a browser and run the tests.

npm install
npm test

jQuery

Although not a complete drop in replacement for jQuerys event methods, it matches most of its footprint and behaviour of the on/off/one/trigger methods, even having alias's for those methods.

Atm the only real omission is passing data to the on function. I am hoping to add that functionality. Please raise an issue if something is missing that I have overlooked.

IE8

Although this API does have good support for IE8, it requires a polyfill that implements addEventListener/removeEventListener/dispatchEvent, that can be added via conditional comments.

I have included a package addeventlistener-with-dispatch that I have created and is added to the dist folder on creating the distribution. This API has been tested against IE8 using that polyfill, I can not guarantee it will work with other polyfills.

To test against IE8 simply run the test in IE8, the polyfill has already been added.