Skip to content

Instantly share code, notes, and snippets.

@developit
Created October 22, 2019 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developit/17fdda2e2236d0c390e8a9bd947f044d to your computer and use it in GitHub Desktop.
Save developit/17fdda2e2236d0c390e8a9bd947f044d to your computer and use it in GitHub Desktop.
import { h, options } from 'preact';
const X = 'props' in h('a');
export const getProps = X ? vnode => vnode.props : vnode => vnode.attributes;
export const setProps = X
? (vnode, props) => { vnode.props = props; }
: (vnode, props) => { vnode.attributes = props; };
export const getType = X ? vnode => vnode.type : vnode => vnode.nodeName;
export const getChild = X ? vnode => vnode.props.children : vnode => vnode.children[0];
/**
* Install a plugin hook into Preact.
* @example
* hook('vnode', (old, vnode) => {
* const props = getProps(vnode);
* if (props) props.foo = 'bar';
* old(vnode)
* });
*/
export const hook = (type, fn) => { options[hook] = fn.bind(null, options[hook]); };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment