GithubHelp home page GithubHelp logo

luwes / disco Goto Github PK

View Code? Open in Web Editor NEW
34.0 4.0 0.0 1.16 MB

Easy and universal way to react to elements being disconnected and connected from the DOM aka lifecycle methods.

JavaScript 100.00%
dom disconnected connected universal lifecycle

disco's Introduction

Disco ๐Ÿ•บ

Version Badge size

npm: npm i disco
cdn: https://unpkg.com/disco

Easy and universal way to react to elements being disconnected and connected via native DOM events.

Observe via a direct node reference, an array of nodes or a string selector that internally makes use of Element.matches(selectorString). It's recommended to keep the observed scope as small as possible for the best performance.

Example

import { observe } from 'disco';

/**
 * Observe one specific div element
 */
const div = document.createElement('div');
observe(div);

div.addEventListener('connected', () => console.log('connected!'));
document.body.append(div);

/******************************************************************/

/**
 * Observe all (future) h1 elements.
 */
observe('h1');
const firstH1 = document.createElement('h1');
const lastH1 = document.createElement('h1');

firstH1.addEventListener('connected', () => console.log('connected!'));
lastH1.addEventListener('connected', () => console.log('connected!'));

document.body.append(firstH1, lastH1);

/******************************************************************/

/**
 * Observe all section elements in the document for removal.
 */
const sections = document.querySelectorAll('section');
observe(...sections);
[...sections].forEach(section =>
  section.addEventListener('disconnected', () => console.log('๐Ÿ•บ')))

API

observe(...nodesOrSelectors)

Observe a node, array of nodes or an element selector for dis-connected events.

unobserve([...nodesOrSelectors])

Unobserve for dis-connected events.

observe(...nodesOrSelectors)

Observe a node, array of nodes or an element selector for dis-connected events.

Kind: global function

Param Type
...nodesOrSelectors Node | String

unobserve([...nodesOrSelectors])

Unobserve for dis-connected events.

Kind: global function

Param Type
[...nodesOrSelectors] Node | String

disco's People

Contributors

luwes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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

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.