UNPKG

4.9 kBMarkdownView Raw
1
2# virtex
3
4[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
5
6Small, focused virtual dom library that allows you to re-interpret all side effects via an action dispatcher. Those side effects include rendering 'thunks' (the primitive from which components are built), and DOM rendering. This allows you to do interesting things, like thread state from a single atom into your components in a pure way.
7
8## Installation
9
10 $ npm install virtex
11
12## Usage
13
14virtex consists of two functions `create` and `update`, curried with an effect processor. You initialize them like this:
15
16`const {create, update} = virtex(effect)`
17
18 * `create(tree)` - takes a virtual node tree and returns a DOM element (if you are using [virtex-dom](https://github.com/ashaffer/virtex-dom))
19 * `update(oldTree, newTree)` - takes the previous vnode tree and the new vnode tree and renders them into the DOM over the old nodes
20 * `effect(action)` - handle all effectful actions (DOM manipulation and thunk rendering, for the time being)
21
22## Example
23
24```javascript
25import {createStore, applyMiddleware} from 'redux'
26import dom from 'virtex-dom'
27import virtex from 'virtex'
28import app from './app'
29
30const store = applyMiddleware(dom(document))(createStore)(() => {}, {})
31const {create, update} = virtex(store.dispatch)
32
33let tree = app(store.getState())
34const node = create(tree)
35
36document.body.appendChild(node)
37
38store.subscribe(() => {
39 const state = store.getState()
40 const newTree = app(state)
41
42 update(tree, newTree, node)
43 tree = newTree
44})
45```
46
47## JSX Pragma
48
49Virtex itself exports a very minimal jsx/hyperscript function for building elements. It doesn't provide any syntactic sugar or register event handlers or anything like that - if you want those things, you should use [virtex-element](https://github.com/ashaffer/virtex-element). If you want to build your own JSX pragma, you should build it on top of virtex's element, however:
50
51```javascript
52import {element} from 'virtex'
53
54function myElement (type, attrs, ...children) {
55 if (attrs.focused) {
56 attrs.focused = node => node.focus()
57 }
58}
59```
60
61Would allow you to pass a `focused` bool that would focus the element in question when it is rendered.
62
63## Processing effects
64
65Your effect processor receives an action object, with at least one field: `type`. The other object properties are type specific. The available actions (and actions creators) are exported on the main virtex object, you can grab them like this:
66
67```javascript
68import {actions} from 'virtex'
69
70const {CREATE_ELEMENT} = actions.types
71```
72
73The available actions are:
74
75```
76CREATE_ELEMENT
77SET_ATTRIBUTE
78REMOVE_ATTRIBUTE
79APPEND_CHILD
80REPLACE_NODE
81REMOVE_NODE
82INSERT_BEFORE
83CREATE_THUNK
84UPDATE_THUNK
85DESTROY_THUNK
86```
87
88Refer to [actions.js](https://github.com/ashaffer/virtex/tree/master/src/actions.js) and the existing middleware for more details on their structure and interpretation.
89
90## Performance
91
92Virtex is not the fastest, but it's pretty fast. 6-7x faster than React, and about on par (ok, just a little slower) with [snabbdom](https://github.com/paldepind/snabbdom)/[deku](https://github.com/dekujs/deku). Here's the [vdom-benchmark](https://github.com/ashaffer/vdom-benchmark-virtex).
93
94## Ecosystem
95
96 * [vdux](https://github.com/ashaffer/vdux) - High-level micro-framework that creates your update cycle for you.
97 * [virtex-dom](https://github.com/ashaffer/virtex-dom) - DOM rendering effect processor
98 * [virtex-component](https://github.com/ashaffer/virtex-component) - Enables react/deku-like components
99 * [virtex-local](https://github.com/ashaffer/virtex-local) - Adds local state and refs
100 * [virtex-string](https://github.com/ashaffer/virtex-string) - String rendering backend
101
102## License
103
104The MIT License
105
106Copyright © 2015, Weo.io <info@weo.io>
107
108Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
109
110The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
111
112THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.