-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02d7e93
commit a3682fb
Showing
4 changed files
with
94 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,52 @@ | ||
const hyperHTML = require('hyperhtml') | ||
const onload = require('on-load') | ||
const slice = Array.prototype.slice | ||
|
||
const WIRE_OR_BOUND_NODE = /(update|hyperHTML)$/ | ||
const ONLOAD_ATTR = /^data-onloadid/ | ||
|
||
module.exports = function hypercomponent (component) { | ||
const render = hyperHTML.wire() | ||
const renderer = typeof component === 'function' | ||
? component | ||
: component.render | ||
|
||
return function wireComponent () { | ||
const onloadHandler = component.onload | ||
const onunloadHandler = component.onunload | ||
const args = slice.call(arguments) | ||
|
||
if (typeof args[0] === 'function' && WIRE_OR_BOUND_NODE.test(args[0].name)) { | ||
return component.apply(component, args) | ||
let isMounted = false | ||
let el = null | ||
|
||
if ( | ||
typeof args[0] === 'function' && | ||
WIRE_OR_BOUND_NODE.test(args[0].name) | ||
) { | ||
el = renderer.apply(renderer, args) | ||
} else { | ||
args.unshift(render) // asign default renderer | ||
el = renderer.apply(renderer, args) | ||
} | ||
|
||
if (!onloadHandler && !onunloadHandler) return el | ||
|
||
if (Array.isArray(el)) { | ||
return el // A root elelemnt is required if you want to use mount/unmmount | ||
} | ||
|
||
let len = (el.attributes && el.attributes.length) || 0 | ||
while (len--) { | ||
if (ONLOAD_ATTR.test(el.attributes[len].name)) { | ||
isMounted = true | ||
break | ||
} | ||
} | ||
|
||
if (!isMounted) { | ||
return onload(el, onloadHandler, onunloadHandler) | ||
} | ||
|
||
args.unshift(render) | ||
return component.apply(component, args) | ||
return el | ||
} | ||
} |
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
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