Skip to content

Commit aba0d61

Browse files
authored
Merge pull request #1 from joshgillies/lifecycle-events
lifecycle events
2 parents 02d7e93 + a3682fb commit aba0d61

File tree

4 files changed

+94
-5
lines changed

4 files changed

+94
-5
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,33 @@ app`${[
4242
]}`
4343
```
4444

45+
### Subscribe to lifecycle events
46+
47+
```js
48+
const hyperHTML = require('hyperhtml')
49+
const component = require('hypercomponent')
50+
const app = hyperHTML.bind(document.body)
51+
52+
const Button = component({
53+
onload: (e) => {
54+
console.log(e, 'loaded')
55+
},
56+
onunload: (e) => {
57+
console.log(e, 'unloaded')
58+
},
59+
render: (render, text) => render`
60+
<button onclick="${(e) => e.target.parentNode.removeChild(e.target)}">
61+
${text}
62+
</button>
63+
`
64+
})
65+
66+
app`${[
67+
Button(hyperHTML.wire(), 'Hello world!'),
68+
Button(hyperHTML.wire(), 'Hello again!')
69+
]}`
70+
```
71+
4572
## See also:
4673

4774
- [yoshuawuyts/nanocomponent][nano]

index.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,52 @@
11
const hyperHTML = require('hyperhtml')
2+
const onload = require('on-load')
23
const slice = Array.prototype.slice
4+
35
const WIRE_OR_BOUND_NODE = /(update|hyperHTML)$/
6+
const ONLOAD_ATTR = /^data-onloadid/
47

58
module.exports = function hypercomponent (component) {
69
const render = hyperHTML.wire()
10+
const renderer = typeof component === 'function'
11+
? component
12+
: component.render
713

814
return function wireComponent () {
15+
const onloadHandler = component.onload
16+
const onunloadHandler = component.onunload
917
const args = slice.call(arguments)
1018

11-
if (typeof args[0] === 'function' && WIRE_OR_BOUND_NODE.test(args[0].name)) {
12-
return component.apply(component, args)
19+
let isMounted = false
20+
let el = null
21+
22+
if (
23+
typeof args[0] === 'function' &&
24+
WIRE_OR_BOUND_NODE.test(args[0].name)
25+
) {
26+
el = renderer.apply(renderer, args)
27+
} else {
28+
args.unshift(render) // asign default renderer
29+
el = renderer.apply(renderer, args)
30+
}
31+
32+
if (!onloadHandler && !onunloadHandler) return el
33+
34+
if (Array.isArray(el)) {
35+
return el // A root elelemnt is required if you want to use mount/unmmount
36+
}
37+
38+
let len = (el.attributes && el.attributes.length) || 0
39+
while (len--) {
40+
if (ONLOAD_ATTR.test(el.attributes[len].name)) {
41+
isMounted = true
42+
break
43+
}
44+
}
45+
46+
if (!isMounted) {
47+
return onload(el, onloadHandler, onunloadHandler)
1348
}
1449

15-
args.unshift(render)
16-
return component.apply(component, args)
50+
return el
1751
}
1852
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"test": "standard"
2323
},
2424
"dependencies": {
25-
"hyperhtml": "^0.8.6"
25+
"hyperhtml": "^0.8.6",
26+
"on-load": "^3.2.0"
2627
},
2728
"devDependencies": {
2829
"standard": "^9.0.2"

yarn.lock

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ doctrine@^2.0.0:
211211
esutils "^2.0.2"
212212
isarray "^1.0.0"
213213

214+
dom-walk@^0.1.0:
215+
version "0.1.1"
216+
resolved "https://registry.yarnpkg.com/dom-walk/-/dom-walk-0.1.1.tgz#672226dc74c8f799ad35307df936aba11acd6018"
217+
214218
error-ex@^1.2.0:
215219
version "1.3.1"
216220
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.1.tgz#f855a86ce61adc4e8621c3cda21e7a7612c3a8dc"
@@ -484,6 +488,13 @@ glob@^7.0.0, glob@^7.0.3, glob@^7.0.5:
484488
once "^1.3.0"
485489
path-is-absolute "^1.0.0"
486490

491+
global@^4.3.0:
492+
version "4.3.1"
493+
resolved "https://registry.yarnpkg.com/global/-/global-4.3.1.tgz#5f757908c7cbabce54f386ae440e11e26b7916df"
494+
dependencies:
495+
min-document "^2.19.0"
496+
process "~0.5.1"
497+
487498
globals@^9.14.0:
488499
version "9.17.0"
489500
resolved "https://registry.yarnpkg.com/globals/-/globals-9.17.0.tgz#0c0ca696d9b9bb694d2e5470bd37777caad50286"
@@ -696,6 +707,12 @@ lodash@^4.0.0, lodash@^4.3.0:
696707
version "4.17.4"
697708
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
698709

710+
min-document@^2.19.0:
711+
version "2.19.0"
712+
resolved "https://registry.yarnpkg.com/min-document/-/min-document-2.19.0.tgz#7bd282e3f5842ed295bb748cdd9f1ffa2c824685"
713+
dependencies:
714+
dom-walk "^0.1.0"
715+
699716
minimatch@^3.0.2:
700717
version "3.0.3"
701718
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.3.tgz#2a4e4090b96b2db06a9d7df01055a62a77c9b774"
@@ -740,6 +757,12 @@ object-keys@^1.0.8:
740757
version "1.0.11"
741758
resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.0.11.tgz#c54601778ad560f1142ce0e01bcca8b56d13426d"
742759

760+
on-load@^3.2.0:
761+
version "3.2.0"
762+
resolved "https://registry.yarnpkg.com/on-load/-/on-load-3.2.0.tgz#dd3145d3a5c2faa5666920d1df674b69f0c2f66f"
763+
dependencies:
764+
global "^4.3.0"
765+
743766
once@^1.3.0:
744767
version "1.4.0"
745768
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
@@ -842,6 +865,10 @@ process-nextick-args@~1.0.6:
842865
version "1.0.7"
843866
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-1.0.7.tgz#150e20b756590ad3f91093f25a4f2ad8bff30ba3"
844867

868+
process@~0.5.1:
869+
version "0.5.2"
870+
resolved "https://registry.yarnpkg.com/process/-/process-0.5.2.tgz#1638d8a8e34c2f440a91db95ab9aeb677fc185cf"
871+
845872
progress@^1.1.8:
846873
version "1.1.8"
847874
resolved "https://registry.yarnpkg.com/progress/-/progress-1.1.8.tgz#e260c78f6161cdd9b0e56cc3e0a85de17c7a57be"

0 commit comments

Comments
 (0)