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

es6-delegate

v1.2.0

Published

Delegate function built with ES6

Downloads

58

Readme

ES6 Delegate

Simple, flexible function used for delegating events in JavaScript.

Installation

Install with npm/yarn:

yarn add es6-delegate

Then import into a javascript file you want to use it in:

import delegate from 'es6-delegate';

Preface

This plugin is built using ES6, transpiled into es6 export, and doesnt include any polyfills out of the box. If you plan to support legacy browsers, you'll need to add the appropriate polyfills.

Usage

ES6 delegate has 4 parameters that work around what you pass to it. The 4 parameters are:

  • elements {String|Node|NodeList} - String, Node, or NodeList to bind the actual event to
    • e.g. '.container', document, document.querySelectorAll('.form')
  • eventListeners {String} - Space separated list of events
    • e.g. 'click', 'click focus'
  • selector {String|Node|NodeList} - String, Node, or NodeList used to determine if the event should be triggered
    • e.g. '.btn', document.getElementById('btn1'), document.querySelectorAll('.btn')
  • callback {Function} - Function called when event is triggered
    • e.g (event, element) => { console.log(event, element) }

Defaulting element to document

delegate('click', '.btn', (e, ele) => {
    // Event object
    const eventObj = e;
    // Element that triggered event - '.btn'
    const targetElement = ele;
    // Array of Elements that event is bound to - [document]
    const boundElement = e.relatedElements;
});

Passing string as element

delegate('.container', 'click', '.btn', (e, ele) => {
    // ele is NodeList of '.btn'
    // e.relatedElements is Array of '.container' elements
});

Passing node as element

const body = document.body;
delegate(body, 'click', '.btn', (e, ele) => {
    // e.relatedElements is Array of 'body' elements
});

Passing nodelist as element

const forms = document.querySelectorAll('.form');
delegate(forms, 'click', '.btn', (e, ele) => {
    // e.relatedElements is array of forms
});

Passing multiple events

delegate('click focus', '.btn', () => {
    // Function will fire when a '.btn' is clicked or focused
});

Passing string as selector

delegate('click', '.btn', (e, ele) => {
    // ele will be the '.btn' that was clicked
});

Passing node as selector

const btn1 = document.getElementById('btn1');
delegate('click', btn1, (e, ele) => {
    // ele is btn1
});

Passing nodelist as selector

const allBtns = document.querySelectorAll('.btn');
delegate('click', allBtns, (e, ele) => {
    // ele is button clicked from allBtns
});

Unbinding Events

The delegate function returns the method it bound the event with for ease of use when unbinding events.

// Bind method
const method = delegate('click', '.btn', (e, ele) => {});

// Unbind method
document.removeEventListener('click', method, true);