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

dynamic-component

v2.0.1

Published

Minimal functional components for websites. This library is a thin wrapper around virtual-dom.

Downloads

19

Readme

dynamic-componet

Minimal functional components for websites. This library is a thin wrapper around virtual-dom.

Motivation

Dynamic Component is a module that simplifies adding dynamic behaviour to HTML pages with JavaScript. It is common for developers to use jQuery to add dynamic elements like carousels or accordions, and for those simple cases, the recommended tool is jQuery. However, for more complicated scenarios, dynamic-component aims to provide an expressive and powerful way to dynamically manipulate HTML by leveraging the virtual-dom library and stateful change propagation.

This library also uses the dom-delegator package to handle global DOM event delegation to ensure virtual-dom's EvHooks work as expected.

References:

  • https://github.com/Matt-Esch/virtual-dom/
  • https://github.com/Raynos/dom-delegator

Example usage

// a simple dynamic component that queries giphy for a random gif
// when a button is clicked

// import dynamic-componet and set up the scope
var dc = require("dynamic-component");
var $ = require("jquery");
// create the state mutator that will dynamically update the DOM
var stateMutator = dc.createState({ url: null });
// query the DOM for the element to contain the dynamic behaviour
var element = document.querySelector("#container");

// set up the render function
// this uses virtual-dom's virtual-hyperscript function, h
function render (state) {
  var h = dc.h;
  return h("div", [
    h("img", {
      attributes: {
        src: state.url
      }
    }),
    h("button", {
      "ev-click": function (event) {
         $.get("https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=cats")
          .then(function (json) {
            stateMutator.set({ url: json.data.image_url });
          });
      }
    }, ["click me!"])
  ]);
}

// bind the state and the render function to the DOM
dc.bind(stateMutator, render, element);

API

The following table describes the various functions that are part of this package. Functions prefixed by dc are part of the object exported from dynamic-component directly, while those prefixed by stateMutator represent the functions in the object returned by dc.createState.

| Function | Type Signature | Description | |---|---|---| | dc.h | See virtual-hyperscript | The virtual-hyperscript function packaged with virtual-dom. | | dc.createState | initialState :: Optional Object ->stateMutator :: Object | Creates a state mutator. | | dc.bind | stateMutator :: Object ->render :: (state :: Object -> vtree :: VTree) ->element :: HTMLElement ->undefined | Binds a state mutator and render function to an element returned from document.querySelector. | | stateMutator.get | -> state :: Object | Returns a deep copy of the state object. | | stateMutator.set | newStateValues :: Object ->silent=false :: Boolean ->shouldGet=false :: Boolean ->state :: Object OR undefined | Update the state, by performing a shallow assign of newStateValues to the original state object. Optionally operates silently without triggering a re-render, and optionally returns a deep copy of the new state. | | stateMutator.subscribe | handler :: (state :: Object -> undefined) ->unsubscribe :: (-> undefined) | Subscribe to state changes persisted with stateMutator.set. Returns a function that unsubscribes the subscription. |

Links

Author

Dhruv Dang
[email protected]
dhruv.io