What if we wanted to use backbone-style event delegation within the component paradigm afforded by react/preact/etc? Would that be useful?
Basic usage:
| // HoC | |
| // localize({ a:'b' })(MyFoo) | |
| const localize = dict => Child => class Connected extends Component { | |
| getChildContext() { | |
| return { dict } | |
| } | |
| render(props) { | |
| return <Child {...props} /> | |
| } | |
| } |
| /** Use the built-in document viewer in devtools to inspect your VDOM. | |
| * Usage in console: | |
| * viewComponentTree(document.body) | |
| */ | |
| function viewComponentTree(node) { | |
| let doc = new DOMParser().parseFromString('', 'application/xml'); | |
| function visitor(dom) { | |
| let el, c; | |
| if (dom instanceof Node) { | |
| el = dom.cloneNode(); |
| /** Async version of Array.prototype.reduce() | |
| * await reduce(['/foo', '/bar', '/baz'], async (acc, v) => { | |
| * acc[v] = await (await fetch(v)).json(); | |
| * return acc; | |
| * }, {}); | |
| */ | |
| export async function reduce(arr, fn, val, pure) { | |
| for (let i=0; i<arr.length; i++) { | |
| let v = await fn(val, arr[i], i, arr); | |
| if (pure!==false) val = v; |
| import { h, cloneElement, Component } from 'preact'; | |
| /** | |
| * <Views view="home"> | |
| * <Home name="home" /> | |
| * <Other name="other" /> | |
| * </Views> | |
| */ | |
| class Views extends Component { | |
| state = { |
| import { h, Component } from 'preact'; | |
| /** | |
| * <SplitPoint load={require('bundle?lazy!./foo')} fallbackContent={( | |
| * <div class="loading" /> | |
| * )} /> | |
| */ | |
| class SplitPoint extends Component { | |
| componentWillMount() { | |
| let cb = this.linkState('child'), |
| import { options } from 'preact'; | |
| let old = options.vnode; | |
| options.vnode = vnode => { | |
| if (!vnode.nodeName) throw Error('Undefined component'); | |
| old(vnode); | |
| }; |
| [].filter.call(document.querySelectorAll('*'), el => { | |
| if (el._component) { | |
| var p = el._priority = el.offsetHeight ? (1 - el.getBoundingClientRect().top/window.innerHeight) : -1; | |
| if (p>0) { | |
| el.setAttribute('data-priority', p); | |
| return true; | |
| } | |
| } | |
| }); |
made with esnextbin