decca
Version:
Render interfaces using pure functions and virtual DOM, kinda
60 lines (44 loc) • 2.11 kB
JavaScript
;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var _h = require('virtual-dom/h');
var _h2 = _interopRequireDefault(_h);
var _fix_props = require('./fix_props');
var _fix_props2 = _interopRequireDefault(_fix_props);
var _widget = require('./widget');
var _widget2 = _interopRequireDefault(_widget);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
module.exports = buildPass;
/*
* A rendering pass.
* This closure is responsible for:
*
* - keeping aware of `context` to be passed down to Components
*
* build = buildPass(...)
* build(el) // render a component/node
*/
function buildPass(context, dispatch) {
/*
* Builds from a vnode (`element()` output) to a virtual hyperscript element.
* The `context` and `dispatch` is passed down recursively.
* https://github.com/Matt-Esch/virtual-dom/blob/master/virtual-hyperscript/README.md
*/
return function build(el) {
if (typeof el === 'string') return el;
if (typeof el === 'number') return '' + el;
if (typeof el === 'undefined' || el === null) return;
if (Array.isArray(el)) return el.map(build);
var tag = el.tag;
var props = el.props;
var children = el.children;
if ((typeof tag === 'undefined' ? 'undefined' : _typeof(tag)) === 'object') {
// Defer to Widget if it's a component
if (!tag.render) throw new Error('no render() in component');
return new _widget2.default({ component: tag, props: props, children: children }, { context: context, dispatch: dispatch }, build);
} else if (typeof tag === 'function') {
// Pure components
return new _widget2.default({ component: { render: tag }, props: props, children: children }, { context: context, dispatch: dispatch }, build);
}
return (0, _h2.default)(tag, (0, _fix_props2.default)(props), children.map(build));
};
}