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

@abradley2/dommy

v1.0.1

Published

Tiny-to-the-point-of-almost-being-a-joke virtual DOM utility

Downloads

8

Readme

DOMMY

Custom Element rendering for dummies.

Installation npm install --save @abradley2/dommy

Motivation

I wanted a convenience function for rendering in custom elements, without importing a library like Polymer that completely redefines how I create custom elements. I just want to render stuff without losing functionality, and with the same "diff/patch" virtual DOM efficiency libraries like React gives me.

This library uses morphdom under the hood as the diffing engine, similar to yo-yo. But unlike yo-yo, uses a createElement function instead of ES6 template tags in favor of supporting JSX.

API

DOMMY has two functions. Only one of which you will use directly.

createElement
This is just for using JSX. With babel-plugin-transform-react-jsx or any other source transform that lets you use a custom jsx pragma, simply do:

/** @jsx createElement */
import { createElement } from '@abradley2/dommy'

Kewl. Now you can use JSX with DOMMY just as you would with React.

render
This is the only function of DOMMY you'll actually use.

/** @jsx createElement */
import { createElement, render } from '@abradley2/dommy'

var count = 0

const update = () => 
  render(
    this,
    <div>
      <button
        onclick={() => {
          count += 1
          update()
        }}
      >
        {`Clicked ${count} times`}
      </button>
    </div>
  )

update() // initial render

Guide: Usage With Custom Elements

This is the main motivation behind DOMMY and if it's not what you're using it for there's 100% definitely a better solution for you out there.

Let's start with a simple counter element like in the original example. Hardly anything changes here.

class MyCounter extends HTMLElement {
  connectedCallback () {
    var count = 0

    this.update = () => {
      render(
        this,
        <div>
          <button
            onclick={() => {
              count += 1
              this.update()
            }}
          >
            {`Clicked ${count} times`}
          </button>
        </div>
      )
    }

    this.update()
  }
}

Ok. Kewl. Now let's allow the application to pass in an attribute called "buttontitle" to our custom element and use it in the render function. Really, we're just doing what we normally do with custom elements: using this.getAttribute to handle data passed in from the parent.

this.update = () => {
  render(
    this,
    <div>
      <h3>{this.getAttribute("buttontitle")}</h3>
      <button
        onclick={() => {
          count += 1
          this.update()
        }}
      >
        {`Clicked ${count} times`}
      </button>
    </div>
  )
}

Awesome. But buttontitle might not always be the same as it was when the component was mounted. Once again, the solution is just doing what we normally do with web components.

class MyCounter extends HTMLElement {
  static get observedAttributes () {
    return ['buttontitle']
  }

  attributeChangedCallback() {
    if (this.update) this.update()
  }