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

petit-dom

v0.6.0

Published

Minimalist virtual dom library

Downloads

74

Readme

petit-dom

A minimalist virtual DOM library.

  • Supports HTML & SVG elements.
  • Supports Render functions and Fragments.
  • Custom components allows to build your own abstraction around DOM elements.
  • Directives allows you to attach custom behavior to DOM elements.

Installation

The library is provided as a set of ES modules. You can install using npm or yarn and then import from petit-dom (see example below).

$ npm install --save petit-dom

or

$ yarn add petit-dom

Note however no transpiled build is provided. The library will work with all recent versions of Node and major browsers. If you're targeting older platforms, make sure to transpile to the desired ES version.

To run the examples, you can run a local web server (like npm http-server module) from the root folder of the project. Since all example use ES6 modules, you can simply navigate to the example you want and load the desired HTML file.

Usage

If you're using Babel you can use JSX syntax by configuring the jsx runtime

{
  "presets": [
    [
      "@babel/preset-react",
      { "runtime": "automatic", "importSource": "petit-dom" }
    ]
  ]
}
import { render } from "petit-dom";

//  assuming your HTML contains a node with "root" id
const parentNode = document.getElementById("root");

// mount
render(<h1>Hello world!</h1>, parentNode);

// patch
render(<h1>Hello again</h1>, parentNode);

Alternatively you can use the classic Babel transform via /* @jsx h */ on the top. You can also use the raw h function calls if you want, see examples folder for usage.

petit-dom also supports render functions

import { render } from "petit-dom";

function Box(props) {
  return (
    <div>
      <h1>{props.title}</h1>
      <p>{props.children}</p>
    </div>
  );
}

render(<Box title="Fancy box">Put your content here</Box>, parentNode);

render functions behave like React pure components. Patching with the same arguments will not cause any re-rendering. You can also attach a shouldUpdate function to the render function to customize the re-rendering behavior (By default props are tested for shallow equality).

Custom components

Besides HTML/SVG tag names, fragments and render fucntions, the h function also accepts any object with the following signature

{
  mount(self);
  patch(self);
  unmount(self);
}

Each of the 3 functions will be called by the library at the moment suggested by its name.

The self argument which is an aboject holding the following properties:

  • render(...): To create/update DOM content for the component
  • props: the current props passed to the JSX element (or h function)
  • oldProps: the previous props, it's value is undefined inside mount

You can also attach arbitrary properties to the object, they will persist between different invocations.

See examples folder for how to define some custom components.

Directives

You can also attach custom behaviors to DOM nodes. Directives allows you to obtain references to DOM nodes and manage their lifecycle.

A directive is an object with the current interface

{
  mount(domElement, value);
  patch(domElement, newValue, oldValue);
  unmount(element, lastValue);
}

There's an example of a simple log directive in the examples folder.

API

h(type, props, ...children)

Creates a virtual node.

  • type: a string (HTML or SVG tag name), or a custom component (see above)

  • props: in the case of HTML/SVG tags, this corresponds to the attributes/properties to be set in the real DOM node. In the case of components, { ...props, children } is passed to the appropriate component function (mount or patch).

render(vnode, parentDom, options = {})

renders a virtual node into the DOM. The function will initially create a DOM node as specified the virtual node vnode and append it to the children ofparentDOM. Subsequent calls will update the previous DOM node (or replace it if it's a different tag).

Optionally, you can use options to pass custom directives, for example:

  let log = { ... }, // defines a log directive
  render(<Something />, parent, { directives: { log } });