made with esnextbin
Last active
September 6, 2016 19:10
-
-
Save tusharmath/3d01659f63f914882340e4173d5d273d to your computer and use it in GitHub Desktop.
RWC - maquette
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
<x-counter> | |
</x-counter> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// write ES2015 code and import modules from npm | |
// and then press "Execute" to run your program | |
import rwc from 'rwc' | |
import {h, createProjector} from 'maquette' | |
/* | |
projector.append(document.body, renderMaquette); | |
*/ | |
function maquettePatcher (root) { | |
const projector = createProjector() | |
let __vNode | |
function render () { | |
projector.append(root, () => __vNode) | |
} | |
return function (vNode) { | |
if(!__vNode) { | |
__vNode = vNode | |
render() | |
} | |
__vNode = vNode | |
} | |
} | |
const CounterComponent = { | |
init () { | |
return {count: 0} | |
}, | |
update (state, {type, params}) { | |
const {count} = state | |
switch(type) { | |
case 'INC': return {count: count + 1} | |
case 'DEC': return {count: count - 1} | |
default: return state | |
} | |
}, | |
view ({count}, d) { | |
return h('div', [ | |
h('h1', [count]), | |
h('button',{onclick: d('INC')}, ['INCREMENT']), | |
h('button',{onclick: d('DEC')}, ['DECREMENT']) | |
]) | |
} | |
} | |
// create prototype object | |
const proto = rwc.createWCProto(maquettePatcher, CounterComponent) | |
// create an HTMLElement instance | |
const html = Object.create(HTMLElement.prototype) | |
// extend html element with the created prototype | |
const prototype = Object.assign(html, proto) | |
document.registerElement('x-counter', {prototype}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "esnextbin-sketch", | |
"dependencies": { | |
"rwc": "1.1.1", | |
"maquette": "2.3.5", | |
"babel-runtime": "6.11.6" | |
}, | |
"version": "0.0.0" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _extends2 = require('babel-runtime/helpers/extends'); | |
var _extends3 = _interopRequireDefault(_extends2); | |
var _create = require('babel-runtime/core-js/object/create'); | |
var _create2 = _interopRequireDefault(_create); | |
var _rwc = require('rwc'); | |
var _rwc2 = _interopRequireDefault(_rwc); | |
var _maquette = require('maquette'); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
/* | |
projector.append(document.body, renderMaquette); | |
*/ | |
// write ES2015 code and import modules from npm | |
// and then press "Execute" to run your program | |
function maquettePatcher(root) { | |
var projector = (0, _maquette.createProjector)(); | |
var __vNode = void 0; | |
function render() { | |
projector.append(root, function () { | |
return __vNode; | |
}); | |
} | |
return function (vNode) { | |
if (!__vNode) { | |
__vNode = vNode; | |
render(); | |
} | |
__vNode = vNode; | |
}; | |
} | |
var CounterComponent = { | |
init: function init() { | |
return { count: 0 }; | |
}, | |
update: function update(state, _ref) { | |
var type = _ref.type; | |
var params = _ref.params; | |
var count = state.count; | |
switch (type) { | |
case 'INC': | |
return { count: count + 1 }; | |
case 'DEC': | |
return { count: count - 1 }; | |
default: | |
return state; | |
} | |
}, | |
view: function view(_ref2, d) { | |
var count = _ref2.count; | |
return (0, _maquette.h)('div', [(0, _maquette.h)('h1', [count]), (0, _maquette.h)('button', { onclick: d('INC') }, ['INCREMENT']), (0, _maquette.h)('button', { onclick: d('DEC') }, ['DECREMENT'])]); | |
} | |
}; | |
// create prototype object | |
var proto = _rwc2.default.createWCProto(maquettePatcher, CounterComponent); | |
// create an HTMLElement instance | |
var html = (0, _create2.default)(HTMLElement.prototype); | |
// extend html element with the created prototype | |
var prototype = (0, _extends3.default)(html, proto); | |
document.registerElement('x-counter', { prototype: prototype }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment