made with esnextbin
Last active
February 3, 2016 15:52
-
-
Save developit/ba28702c94e22d26e3bc to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains hidden or 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 --> | |
| </body> | |
| </html> |
This file contains hidden or 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
| import { h, render } from 'preact'; | |
| /** @jsx h */ | |
| /** Demo: functional-reactive paradigm, using Preact (git.io/preact) */ | |
| // data store, updated via mutation() | |
| const data = { | |
| value: 0 | |
| }; | |
| // Just a function that takes ({ mutation, ...data }) and returns DOM. | |
| const App = ({ value, mutation }) => ( | |
| <div style="padding:20px;"> | |
| <button onClick={ mutation('value', v => v+1) }>Increment</button> | |
| <p>Value: { value }</p> | |
| </div> | |
| ); | |
| // kick it off: | |
| preactCycle(App, data); | |
| /** A little helper library to do FR. | |
| * @param {Function} render | |
| */ | |
| function preactCycle(Renderable, data={}) { | |
| let root; | |
| // optional key-specific mutation of data | |
| // eg: mutate('value', v => v*2 ) | |
| function mutate(key, fn) { | |
| if (typeof key==='function') [fn, key] = [key, null]; | |
| let p = key ? data[key] : data; | |
| if (fn) p = fn(p); | |
| if (key) data[key] = p; | |
| else data = p; | |
| root = render(<Renderable {...data} {...{mutate,mutation}} />, document.body, root); | |
| } | |
| // mutation future/thunk | |
| // eg: let double = mutation('value', v => v*2 ) | |
| let mutation = (...args) => () => mutate(...args); | |
| mutate(); | |
| } |
This file contains hidden or 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", | |
| "version": "0.0.0", | |
| "dependencies": { | |
| "preact": "2.8.3", | |
| "babel-runtime": "6.3.19" | |
| } | |
| } |
This file contains hidden or 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 _preact = require('preact'); | |
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| /** @jsx h */ | |
| /** Demo: functional-reactive paradigm, using Preact (git.io/preact) */ | |
| // data store, updated via mutation() | |
| var data = { | |
| value: 0 | |
| }; | |
| // Just a function that takes ({ mutation, ...data }) and returns DOM. | |
| var App = function App(_ref) { | |
| var value = _ref.value; | |
| var mutation = _ref.mutation; | |
| return (0, _preact.h)( | |
| 'div', | |
| { style: 'padding:20px;' }, | |
| (0, _preact.h)( | |
| 'button', | |
| { onClick: mutation('value', function (v) { | |
| return v + 1; | |
| }) }, | |
| 'Increment' | |
| ), | |
| (0, _preact.h)( | |
| 'p', | |
| null, | |
| 'Value: ', | |
| value | |
| ) | |
| ); | |
| }; | |
| // kick it off: | |
| preactCycle(App, data); | |
| /** A little helper library to do FR. | |
| * @param {Function} render | |
| */ | |
| function preactCycle(Renderable) { | |
| var data = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | |
| var root = undefined; | |
| // optional key-specific mutation of data | |
| // eg: mutate('value', v => v*2 ) | |
| function mutate(key, fn) { | |
| if (typeof key === 'function') { | |
| ; | |
| var _ref2 = [key, null]; | |
| fn = _ref2[0]; | |
| key = _ref2[1]; | |
| }var p = key ? data[key] : data; | |
| if (fn) p = fn(p); | |
| if (key) data[key] = p;else data = p; | |
| root = (0, _preact.render)((0, _preact.h)(Renderable, (0, _extends3.default)({}, data, { mutate: mutate, mutation: mutation })), document.body, root); | |
| } | |
| // mutation future/thunk | |
| // eg: let double = mutation('value', v => v*2 ) | |
| var mutation = function mutation() { | |
| for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | |
| args[_key] = arguments[_key]; | |
| } | |
| return function () { | |
| return mutate.apply(undefined, args); | |
| }; | |
| }; | |
| mutate(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment