Git Product home page Git Product logo

vdom's Introduction

vdom

A Clojure library for constructing virtual DOMs using virtual-dom.

Installation

[vdom "0.1.1-SNAPSHOT"]

Then require vdom.core or, if you'd like a more reactive experience, vdom.elm.

Building

After installing Node modules with npm install, build the Javascript files by running make.

Usage

vdom.core

The primary interface for vdom is the renderer function in vdom.core. Call it with a DOM element to get a function that renders a tree of Clojure data into HTML within that element.

For example,

(let [render (vdom.core/renderer js/document.body)]
  (render [:div {} "Hello, world"]))

To update the HTML, call the render function again with new data.

Rendering works well with core.async channels and goroutines. For instance, assuming you have a channel called ui on which UI data trees are passed, the following updates the HTML every time there's a new UI state.

(let [render (vdom.core/renderer js/document.body)]
  (go-loop []
    (render (<! ui))
    (recur))

UI data trees

Vdom is based on virtual-dom, with a transparent mapping between Clojure data structures and the virtual-dom functions VNode, VText, and svg.

For example, the Clojure tree

[:div {:id "root" :className "test"}
 [:span {} "Hello, "]
 [:span {} "world"]]

is equivalent to the JavaScript

new VNode('div', {id: 'root', className: 'test'}, [
  VNode('span', {}, VText('Hello, ')),
  VNode('span', {}, VText('world'))
])
new VText("Hello, world"));

Any children that are seqs are flattened, allowing, for instance,

[:ul {}
 (for [i (range 1 6)]
   [:li {} i])]

which produces the HTML

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>

Vdom handles SVG nodes transparently, so long as an svg node is part of the tree. Descendant nodes of svg are constructed with the virtual-hyperscript/svg function rather than VNode. Descendants of a foreignObject tag are constructed with VNode.

vdom.elm

The vdom.elm namespace provides some simple FRP-style functions, namely foldp and render!. They use core.async channels and goroutines instead of true FRP signals.

foldp takes a function, an initial value, and a channel of actions, and returns a new channel of values. The values are produced by applying the given function to the current value and the latest value from the channel of actions.

render! takes a channel of UI trees and a root element, and updates the DOM whenever a new UI tree comes in.

These two functions, along with core.async/map, provide the basis for a simple, Elm-style architecture.

(defn step [x action]
  (condp = action
    :inc (inc x)
    :dec (dec x)
    x))

(let [actions (core.async/chan)
      models (vdom.elm/foldp step 0 actions)]
  (vdom.elm/render! (core.async/map (fn [x] [:div {} x]) [models]) js/document.body))

Hooks

To get the actual DOM element in your UI tree, include in a node's attributes a virtual-dom hook. Provide a value by calling vdom.hooks/hook with a function that takes a DOM node. For instance,

[:input {:hookFocus (vdom.hooks/hook (fn [node] (.focus node)))}]

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.