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

@grrr/hansel

v2.1.0

Published

Runner of handlers/enhancers.

Downloads

83

Readme

Hansel

Build Status

Runner of JavaScript handlers and enhancers

  • Lightweight (less than 1kb minified and gzipped)
  • Transpile to desired browser target
  • No dependencies (except a minor utility function)

Based on the article "Progressive enhancement with handlers and enhancers" by Hidde de Vries. We've been using this model for many years with great pleasure, fine-tuning here and there. Read the article for a deeper explanation.

Built with ❤️ by GRRR.

Installation

$ npm install @grrr/hansel

Note: depending on your setup additional configuration might be needed, since this package is published with untranspiled JavaScript.

Usage

Import into your main JavaScript file:

import { enhance, handle } from '@grrr/hansel';

enhance(document.documentElement, {
  enhancer1(elm) {
    // Enhance elements with this enhancer.
  },
  enhancer2(elm) { /* */ },
  enhancerN(elm) { /* */ },
});

handle(document.documentElement, {
  handler1(elm, event) {
    // Handle clicks on elements with this handler.
  },
  handler2(elm, event) { /* */ },
  handlerN(elm, event) { /* */ },
});

In a more modular setup, this would look like:

import { enhance, handle } from '@grrr/hansel';
import { enhancer as fooEnhancer, handler as fooHandler } from './foo';
import { enhancer as barEnhancer, handler as barHandler } from './bar';

enhance(document.documentElement, {
  fooEnhancer,
  barEnhancer,
});

handle(document.documentElement, {
  fooHandler,
  barHandler,
});

Enhancers

The enhance function will look for DOM nodes containing the data-enhancer attribute. The second argument is a lookup table for enhancer functions. The value of the data-enhancer attribute will be matched with the table and if found, executed, given the element as first argument:

// Given <p data-enhancer="foo" data-message="Hello!"></p>

enhance(document.documentElement, {
  foo(elm) {
    console.log(elm.getAttribute('data-message')); //=> "Hello!"
  }
});

Multiple enhancers are possible by comma-separating them:

<div data-enhancer="foo,bar"></div>

Handlers

Handlers are called on click, using a global event listener on the document.

// Given <button data-handler="shout" data-message="Hello!">shout</button>

handle(document.documentElement, {
  shout(elm, e) {
    alert(elm.getAttribute('data-message')); //=> "Hello!"
    e.preventDefault();
  }
});

Multiple handlers are possible by comma-separating them:

<a data-handler="foo,bar" href="/">Do the thing</a>

Options

It's possible to register a handler with options. To do so, register the handler via an object with an fn and options key:

handle(document.documentElement, {
  foo(elm, e) { /* */ },
  bar(elm, e) { /* */ },
  baz: {
    fn: baz(el, e) {
      // The handler function.
    },
    options: {
      // The handler options.
    },
  },
});

The following options are available:

allowModifierKeys

By default, modifier-clicks (metaKey, ctrlKey, altKey and shiftKey) on anchors (<a>) are caught, and are not passed on to the handler. To disable this behaviour, register the handler with the allowModifierKeys option:

options: {
  allowModifierKeys: true,
},

Furthermore

Thanks to the global click listener, handlers do not have to be re-initialized to dynamically added content. The presence of a data-handler attribute is enough.

Enhancers are run immediately however, so you might want to run them again, for instance when loading new DOM nodes in response to an AJAX call. The first argument to enhance is the container element within which nodes are searched. Therefore, you can pass the parent to the newly created nodes as reference to enhance all its children:

const myContainer = document.querySelector('foo');
myContainer.innerHTML = htmlContainingEnhancedElements;

enhance(myContainer, myEnhancers);