Giter Site home page Giter Site logo

picodom's Introduction

Travis CI Codecov npm

Picodom is a 1 KB VDOM builder and patch function.

Try it Online

import { h, patch } from "picodom"

/** @jsx h */

let node

function render(view, state) {
  patch(document.body, node, (node = view(state)))
}

function view(state) {
  return (
    <div>
      <h1>{state}</h1>
      <input
        autofocus
        type="text"
        value={state}
        oninput={e => render(view, e.target.value)}
      />
    </div>
  )
}

render(view, "Hello!")

Picodom supports keyed updates & lifecycle events โ€” all with no dependencies. Mix it with your favorite state management library or create your own custom view framework.

Installation

Install with npm or Yarn.

npm i picodom

Then with a module bundler like Rollup or Webpack, use as you would anything else.

import { h, patch } from "picodom"

Or download directly from unpkg or jsDelivr.

<script src="https://unpkg.com/picodom"></script>

Then find it in window.picodom.

const { h, patch } = picodom

We support all ES5-compliant browsers, including Internet Explorer 10 and above.

Usage

Create virtual nodes with the built-in h() function. A virtual node is an object that describes a DOM tree.

const node = h("h1", { id: "title" }, "Hello.")

To create a component, define a function that returns a virtual node.

function AwesomeTitle(text) {
  return h("h1", { class: "awesome title" }, text)
}

To diff two virtual nodes and update the DOM, use the patch function:

const element = patch(
  parent,  // HTML container, e.g., document.body
  oldNode, // the old VNode
  newNode  // the new VNode
)

The patch function returns the patched child element.

Supported Attributes

This section describes the particular handling of certain HTML/SVG attributes, and certain special attributes reserved by Picodom.

Standard HTML and SVG Attributes

All standard HTML and SVG attributes are supported, and the following standard attributes are handled specifically:

class

Both the className-property and class-attribute are supported.

style

This attribute expects a standard object rather than a string as in HTML.

Individual style properties will be diffed and mapped against HTMLElement.style property members of the DOM element - you should therefore use the Javascript style object property names, e.g. backgroundColor rather than background-color.

Life-Cycle Attributes

Picodom supports element-level life-cycle events and keyed updates via the following reserved attributes:

key

The key attribute enables Picodom to identify and preserve DOM elements, even when the order of child elements changes during an update.

The value must be a string (or number) and it must be unique among all the siblings of a given child element.

For example, use keys to ensure that input elements don't get replaced (and lose their focus or selection state, etc.) during updates - or for table rows (or other repeated elements) to ensure that these get reused.

oncreate

Fired after the element is created and attached to the DOM.

oncreate(Element)

onupdate

Fired after the element attributes are updated. This event will fire even if the attributes have not changed.

onupdate(Element, oldProps: Attributes)

onremove

Fired before the element is removed from the DOM.

Your event handler will receive a reference to the element that is about to be removed, and a done callback function, which must be called to complete the removal, upon which the ondestroy handler will be fired.

onremove(Element, done)

You can use this event to defer the physical removal of an element from the DOM, for purposes such as animation during removal.

Note that the onremove event is only triggered for direct removals, e.g. for updates where the parent of the element still exists. For indirect removals (where the parent element was also removed) only the ondestroy event will fire.

ondestroy

Fired after the element is removed from the DOM.

ondestroy(Element)

You can use this event to clean up after your oncreate handler - this enables you to integrate third-party widgets (date-pickers, content editors, etc.) and clean up after them.

Links

License

Picodom is MIT licensed. See LICENSE.

picodom's People

Contributors

jorgebucaran avatar mindplay-dk avatar pspeter3 avatar andrewiggins avatar zaceno avatar 2hu12 avatar acstll avatar whaaaley avatar maxholman avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.