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

@icelab/defo

v0.0.3

Published

A dynamic DOM binding library

Downloads

6,765

Readme

Defo

A tiny library (2KB) that simplifies the process of attaching JavaScript behaviour to the DOM. It does a couple of things:

  • Creates a pattern for attaching and scoping behaviour to specific parts of the DOM, and for passing data from the DOM to your JavaScript functions.
  • Defines an API for managing the life-cycle of DOM-attached behaviour
  • Magically handles that life-cycle by reacting to changes to the DOM

Example usage

Add an element with a data-defo-* to your HTML:

<input data-defo-gday-mate="World" />

Define a matching view function and pass it to defo:

import defo from "defo";
// Create an object to hold our views
const views = {
  // Create a camelCased key matching the dasherized HTML attribute:
  //   data-defo-gday-mate -> gdayMate
  gdayMate: (el, name) => {
    // Attach some behaviour! Each view function receives the bound DOM node
    // and the value of its `defo` attribute as arguments
    el.value = `G’day, {name}!`;
    // Return methods for managing the life-cycle of your view
    return {
      // Called whenever the value of the `defo` attribute changes
      update: (newName, oldName) => {
        el.value = `G’day, {newName}! Bye, ${oldName}`;
      },
      // Caled when the element (or its defo attribute) is removed from the DOM
      destroy: () => {
        el.value = "";
      }
    };
  }
};

// Create a defo instance
const instance = defo({ views });

When initialized defo will call the view functions of any matching elements that are in the DOM, as well as for any and all future changes to the DOM.

Options

The defo function takes three options arguments:

  • scope — a DOM scope for defo to use. Defaults to document.documentElement.
  • prefix — The prefix used for namespacing data attributes. Defaults to defo.
  • views — an object for mapping view functions to known attribute names

View functions API

View functions are at the core of defo’s functionality. They have a simple shape:

function viewName(el, props) {
  return {
    update: (nextProps, prevProps) = {},
    destroy: () = {},
  }
}

The view function itself will be called on initialization for a given DOM node, and will receive two agruments:

  • el — the DOM node that triggered the instance of the function call
  • props — JSON-parsed version of the relevent els matching data-attribute value

Life-cycle methods

The return value of each view function is expected to be an object defining update and destroy methods for managing the full life-cycle of the instance of a view.

Update

update will be called whenever the value of a nodes matching data-attribute is changed. If defined, that method will be called with two arguments:

  • nextProps — JSON-parsed version of the current attribute value
  • prevProps — JSON-parsed version of the previous attribute value

Destroy

destroy will be called whenever a node:

  • Is removed from the DOM
  • Has its relevant data-attribute removed

Async views functions

View functions can return a Promise. This will be automatically unwrapped to retrive any returned life-cycle methods:

async function asyncViewName(el, props) {
  const dependency = await import("dependency");
  // Do something with dependency
  return {
    update: (nextProps, prevProps) = {},
    destroy: () = {},
  }
}

Browser support

Defo should work in any browser that support MutationObserver.

Prior art