Skip to content

Instantly share code, notes, and snippets.

View developit's full-sized avatar
🦊
write, the codes

Jason Miller developit

🦊
write, the codes
View GitHub Profile
@developit
developit / hoc-ey.js
Created September 14, 2016 01:37
HoC stuff
// HoC
// localize({ a:'b' })(MyFoo)
const localize = dict => Child => class Connected extends Component {
getChildContext() {
return { dict }
}
render(props) {
return <Child {...props} />
}
}
@developit
developit / _delegated-component.md
Last active October 20, 2016 21:39
DelegatedComponent

Experiment: Backbone-style events in Preact

What if we wanted to use backbone-style event delegation within the component paradigm afforded by react/preact/etc? Would that be useful?

Example

Basic usage:

@developit
developit / preact-vdom-viewer.js
Last active July 11, 2017 15:42
preact vdom viewer
/** 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();

DraftJS Preact Demo

Start

npm install

npm start

Go to localhost:8080

@developit
developit / async-examples.js
Last active February 19, 2020 00:43
Async Array utilities in async/await. Now available as an npm package: https://github.com/developit/asyncro
/** 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;
@developit
developit / views.js
Created October 25, 2016 20:09
preact-views
import { h, cloneElement, Component } from 'preact';
/**
* <Views view="home">
* <Home name="home" />
* <Other name="other" />
* </Views>
*/
class Views extends Component {
state = {
@developit
developit / split-point.js
Last active December 5, 2017 16:35
A couple of variants for preact split points
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);
};
@developit
developit / show-priorities.js
Created December 12, 2016 19:54
paste this into any preact app
[].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;
}
}
});
@developit
developit / esnextbin.md
Created December 15, 2016 23:48
esnextbin sketch