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

hookahjs

v0.0.6

Published

JS library that adds empty/dirty/touched CSS hooks to input and textarea elements

Downloads

141

Readme

HookahJS

HookahJS is a tiny JavaScript library that monitors all input and textarea elements on your page and adds empty/dirty/touched CSS hooks to each element automatically.

Introduction

HookahJS is a tiny JavaScript library that monitors all input and textarea elements on your page and adds the following CSS classes in response to user interactions with each element:

  • hkjs--empty - control element is empty
  • hkjs--not-empty - control element is not empty
  • hkjs--pristine - control element has not seen an input or change event
  • hkjs--dirty - control element has seen an input or change event
  • hkjs--untouched - control element has not seen a blur event
  • hkjs--touched - control element has seen a blur event

HookahJS uses CSS @keyframes to detect new DOM elements so once the library is loaded, it will automatically add CSS hooks to new input and textarea elements. HookahJS is 1056 bytes (minified + gzipped).

Quickstart

To use HookahJS you only need to add hookah.js to your page and the library will automatically add event listeners to all current and future input and textarea elements. The following example will draw a red box around an invalid input box after the user has touched the element:

<!doctype html>
<html>
  <head>
    <script src="//cdn.rawgit.com/muicss/hookahjs/0.0.6/dist/hookah.min.js"></script>
    <style>
      .hkjs--touched:not(:valid) {
        border: 1px solid red;
      }
    </style>
  </head>
  <body>
    <input type="text" required>
    <input type="email" required>
  </body>
</html>

View Demo »

HookahJS uses CSS @keyframes to detect new DOM elements so once the library is loaded, it will automatically add CSS hooks to new input and textarea elements.

Browser Support

  • IE10+
  • Opera 12+
  • Safari 5+
  • Chrome
  • Firefox
  • iOS 6+
  • Android 4.4+

Note: HookahJS uses CSS @keyframes to detect new DOM elements automatically. To use HookahJS in IE9 you can initialize DOM elements explicitly with hkjs.init().

Documentation

How to load HookahJS

For production systems we recommend that you host the library file yourself which you can download from the dist/ directory in this repository:

For tighter integration with your code you can also use the HookahJS NPM package:

$ npm install --save hookahjs
var hkjs = require('hookahjs');

document.addEventListener('DOMContentLoaded', function() {
  hkjs.init();
});

HookahJS can be loaded asynchronously but keep in mind that if HookahJS is initialized after the DOM content has been displayed to the user, there may be a flash of unstyled content. To avoid this you can seed your page with .hkjs--empty/.hkjs--not-empty classes as necessary.

How to initialize elements selectively

By default, HookahJS will add event listeners to all current and future input and textarea elements. To prevent this behavior you can listen to the hkjs-init event and call the preventDefault() method on the event object. You can also use the hkjs global object to add hooks to individual elements:

window.addEventListener('hkjs-init', function(ev) {
  // prevent HookahJS from adding hooks to all <input> and <textarea> elements
  ev.preventDefault();

  // use the `hkjs` global object to add hooks to elements manually
  var inputEl = document.getElementById('my-input-element');
  hkjs.init(inputEl);
});

How to handle programmatic updates

HookahJS can detect all change and input events triggered by user interactions but it can't detect programmatic changes to control elements. To update the HookahJS CSS classes after making a programmatic change to a control element, you can trigger a change or input event on the element:

// modify element
var inputEl = document.getElementById('my-input-element');
inputEl.value = 'Programmatic input';

// initialize event object (with bubbles = false)
var ev = document.createEvent('HTMLEvents');
ev.initEvent('change', false);

// trigger event
inputEl.dispatchEvent(ev);