Giter Site home page Giter Site logo

sachinchoolur / tiny-events.js Goto Github PK

View Code? Open in Web Editor NEW
22.0 4.0 1.0 353 KB

TinyEvents is a tiny event utility library for modern browsers(IE 11+). Supports jQuery-like syntax. Just 1 kb gzipped.

License: MIT License

JavaScript 3.63% TypeScript 96.37%
events jquery-syntax dom-events jquery-alternative

tiny-events.js's Introduction

npm tree-shaking types license

Test coverage

Statements Functions Lines
Statements Functions Lines

TinyEvents

TinyEvents is a tiny event utility library for modern browsers(IE 11+). Supports jQuery like syntax. Just 1 kb gzipped.

  • Event namespace support - Easily remove event handlers
  • Works with collection of elements
  • Custom events support
  • Add or remove multiple events at a time.
  • Easy event delegation
  • Attach event listeners to dynamically added element
  • And more. Just 1 kb

Installation

TinyEvents is available on NPM, Yarn, and GitHub. You can use any of the following method to download tinyEvents

  • NPM - NPM is a package manager for the JavaScript programming language. You can install tiny-events.js using the following command

    npm install tiny-events.js
  • YARN - Yarn is another popular package manager for the JavaScript programming language. If you prefer you can use Yarn instead of NPM

    yarn add tiny-events.js
  • GitHub - You can also directly download tinyevents from GitHub

Usage example

<button class="change-bg">Change background color</button>
<button class="change-color">Change color</button>
<button class="remove-btn">Remove click</button>
<span id="color-code">#FFF</span>
import tinyEvents from 'tinyevents';

tinyEvents(".change-bg").on("click.sample mouseover.sample", () => {
  const color = getRandomColor();
  document.body.style.backgroundColor = color;
  triggerColorChange(color);
});

tinyEvents(".change-color").on("click.sample", () => {
  const color = getRandomColor();
  document.body.style.color = color;
  triggerColorChange(color);
});

tinyEvents(".remove-btn").on("click", () => {
  tinyEvents(".change-bg, .change-color").off(".sample");
});

tinyEvents("#color-code").on("color-change", (event) => {
  const { color } = event.detail;
  document.getElementById("color-code").innerHTML = color;
});

function getRandomColor() {
  return "#" + Math.floor(Math.random() * 16777215).toString(16);
}

function triggerColorChange(color) {
  // Custom events demo
  tinyEvents("#color-code").trigger("color-change", {
    color
  });
}

API

  • on - Attach an event handler function for one or more events to the selected elements.
  • off - Remove an event handlers.
  • one - Attach a handler to an event for the elements. The handler is executed at most once per element per event type.
  • trigger - Execute all handlers and behaviors attached to the matched elements for the given event type.

On

Attach an event handler function for one or more events to the selected elements.

import $ from 'tinyevents';

$('.btn').on('click.bg mouseover.bg', () => {
    document.body.style.backgroundColor = 'red';
});

$('.btn').on('my-custom-event', (e) => {
    console.log(e.detail)
});
Delegation

Event delegation is an event handling technique where, instead of attaching event handlers directly to every element you want to listen to events on, you attach a single event handler to a parent element of those elements to listen for events occurring on it’s descendant elements

By passing a selector as the second argument to on() method, the event handler will fire only if the event is triggered on the selector

import tinyEvents from 'tinyevents';

tinyEvents('body').on('mousemove', 'span', () => {
    console.log('Hovering over span');
});

Off

Remove event handlers.

tinyEvents('.btn').on('click.bg hover.bg', () => {
    document.body.style.backgroundColor = 'red';
});

// Removes both click and hover event handlers
tinyEvents('.btn').off('click.bg hover.bg');

// Removes event handlers by namespace
// Removes both click and hover event handlers
tinyEvents('.btn').off('.bg');

// Removes all click events handlers that were attached with .on()
tinyEvents('.btn').off('click');

// Another example with multiple namespaces
tinyEvents('.btn').on('click.abc', () => {
    document.body.style.color = 'red';
});
tinyEvents('.btn').on('click.xyz', () => {
    document.body.style.backgroundColor = 'yellow';
});

// Removes both event handlers
tinyEvents('.btn').off('.abc .xyz');

One

Attach a handler to an event for the elements. The handler is executed at most once per element per event type.

tinyEvents('.btn').one('click', () => {
    alert('This will be displayed only once.');
});

// or

tinyEvents('ul').one('click', 'li', () => {
    alert('This will be displayed only once.');
});

Trigger

Execute all handlers and behaviors attached to the matched elements for the given event type.

tinyEvents('.btn').trigger('click');

tinyEvents('.btn').on('my-custom-event', (event) => {
    // prints 'custom-event'
    console.log(event.detail.type)
});

tinyEvents('.btn').trigger('my-custom-event', {
  type: 'custom-event',
  purpose: 'some text'
});

Story

I built the initial version of tinyEvents during the development of lightGallery. lightGallery 2.0 is completely re-written from scratch in vanilla JavaScript which was dependent on jQuery earlier. It was very difficult to keep track of all event listeners, making use of event delegation, and using custom events in vanilla JS while it is very easy with jQuery. So I wrote this tiny library which supports jQuery-like syntax and functionalities and is still less than 1 kb.

P.S. I'm working on an other tool that automatically finds and replaces jQuery methods with vanilla javascript methods. Watch the repo to get notified

tiny-events.js's People

Contributors

sachinchoolur avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

geojacobm6

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.