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

basic-interactions

v0.2.1

Published

Simple lib to add basic interactivity for your common front-end project

Downloads

2

Readme

This is README for basic-interactions

Simple interactions library are willing to unify and simplify the approach of developing routine interaction tasks as toggle classes for HTML DOM elements.

The main approach is to use one global Class that can obtain and process all actions based on the custom data attributes provided for each element.

The main goal is not develop each time on the new project the common JS code and use instead only HTML markup to add basic interactivity.

Installation

Install basic-interactions package:

npm install basic-interactions

Initialize basic event listeners to use Class functionality

import BasicInteractions from 'basic-interactions';

window.addEventListener('load', () => {
  // create class instance
  const bi = new BasicInteractions();
  // init default event listeners
  bi.init();
});

In order to init global event listeners with some additional functionality, you can manually add all needed event listeners.

init() method has the following code, that you can replicate with desired modifications:

//interact on load
bi.handleAutoInteraction();

// add global listeners for you app
document.addEventListener('click', ({ target }) => {
  bi.handleInteraction(target);
});

window.addEventListener('resize', () => {
  bi.debounce(() => {
    if (bi.resets?.length > 0) {
      bi.handleResets('resize');
    }
  });
});

window.addEventListener('keydown', (e) => {
  if (e.key === 'Escape') bi.handleResets('escape');
});

How to use it?

After the installation process and setup of needed EventListeners, all you need is just use prepared data attributes to add interactivity into your project

Data Attributes

| Attribute Name | Description | | :------------------------ | --------------------------------------------------------------------------------------------------------------------------------: | | data-toggle | main attribute / contains string of classes to toggle on current target | | data-toggle-action | contains string of toggle action ('add' / 'remove'). By default can be not included or included without value to work as a toggle | | data-toggle-target | contains target selector - used querySelectorAll | | data-toggle-target-class | contains string of classes to toggle on targets | | data-toggle-target-action | same as data-toggle-action but used for found targets | | data-reset | contains string of reset actions (resize escape) | | data-group-reset | used as plain attribute (w/o value) for parent HTMLElement to mark group of items where only one item can be active | | data-toggle-scroll | to mark ('disable', 'enable' or undefined) body scroll behavior. | | data-attr | attribute name to attach | | data-attr-value | attribute value to attach | | data-attr-target | attribute target (single target - used querySelector) | | data-attr-action | attribute actions (same behavior as classes toggle - 'add' / 'remove') | | data-on-load | to indicate targets that should be interacted on page load |

Common usage

Toggle classes on click:

<button data-toggle="some classes goes here"></button>

Toggle classes on click and load:

<button data-toggle="some classes goes here" data-on-load></button>

Toggle classes with reset options (escape and resize):

<button data-toggle="some classes goes here" data-rest="escape resize"></button>

Toggle classes for all specific targets:

<button
  data-toggle-target-class="some classes goes here"
  data-toggle-target=".target"
></button>

Add or Remove classes for specific target or targets:

<button
  data-toggle-target-class="some classes goes here"
  data-toggle-target-action="add"
  data-toggle-target="#target-id"
></button>
<button
  data-toggle-target-class="some classes goes here"
  data-toggle-target-action="remove"
  data-toggle-target=".another-targets"
></button>

Toggle classes for specific target and event target:

<button
  data-toggle="some classes goes here"
  data-toggle-target-class="other classes"
  data-toggle-target="#target-id"
></button>

Toggle classes and body scroll:

<button
  data-toggle="some classes goes here"
  data-toggle-scroll
  data-rest="escape resize"
></button>

Toggle classes for group of element where only one element should be active (with toggled classes):

<div data-group-reset>
  <button data-toggle="some classes goes here"></button>
  <button data-toggle="some classes goes here"></button>
  <button data-toggle="some classes goes here"></button>
</div>

Toggle/Add/Remove data attributes to HTML markup

<button
  data-toggle="className"
  data-attr="data-test"
  data-attr-value="test value"
></button>
<button
  data-toggle="className"
  data-attr="data-test"
  data-attr-value="test value"
  data-attr-action="add"
></button>
<button
  data-toggle="className"
  data-attr="data-test"
  data-attr-action="remove"
></button>

Advanced Usage / adding extra functionality

Below is an example on how to track DOM mutations for adding additional functionality.

Example based on adding and removing custom data attribute: 'data-src'.

window.addEventListener('load', () => {
  const observer = new MutationObserver((mutationList) => {
    mutationList.forEach((mutation) => {
      if (mutation.attributeName === 'data-src') {
        if (mutation.target.hasAttribute('data-src')) {
          // mutation on add atr
          // your code goes here...
        } else {
          // mutation on remove atr
          // your code goes here...
        }
      }
    });
  });
  observer.observe(document.querySelector('#modal'), {
    attributes: true,
    attributeFilter: ['data-src'],
  });
});

More info about usage of Mutation Observer you can find on MDN.