Created
October 22, 2019 02:17
-
-
Save developit/17fdda2e2236d0c390e8a9bd947f044d to your computer and use it in GitHub Desktop.
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
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