Skip to content

Commit 6533963

Browse files
authored
Merge pull request #2 from joshgillies/v1
v2
2 parents a19faff + e1cb05d commit 6533963

File tree

4 files changed

+2303
-130
lines changed

4 files changed

+2303
-130
lines changed

README.md

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,11 @@ const Button = component((render, text) => render`
1919
${text}
2020
</button>
2121
`)
22+
const myButton = Button()
2223

23-
app`${Button('Hello world!')}`
24-
```
25-
26-
### Pass your own [wire][wire] or bound node
27-
28-
```js
29-
const hyperHTML = require('hyperhtml')
30-
const component = require('hypercomponent')
31-
const app = hyperHTML.bind(document.body)
32-
33-
const Button = component((render, text) => render`
34-
<button>
35-
${text}
36-
</button>
37-
`)
24+
app`${myButton.render('Hello world!')}`
3825

39-
app`${[
40-
Button(hyperHTML.wire(), 'Hello world!'),
41-
Button(hyperHTML.wire(), 'Hello again!')
42-
]}`
26+
setTimeout(() => myButton.render('Hello there!'), 1000)
4327
```
4428

4529
### Subscribe to lifecycle events
@@ -50,10 +34,10 @@ const component = require('hypercomponent')
5034
const app = hyperHTML.bind(document.body)
5135

5236
const Button = component({
53-
onload: (e) => {
37+
load: (e) => {
5438
console.log(e, 'loaded')
5539
},
56-
onunload: (e) => {
40+
unload: (e) => {
5741
console.log(e, 'unloaded')
5842
},
5943
render: (render, text) => render`
@@ -63,16 +47,19 @@ const Button = component({
6347
`
6448
})
6549

50+
const button1 = Button()
51+
const button2 = Button()
52+
6653
app`${[
67-
Button(hyperHTML.wire(), 'Hello world!'),
68-
Button(hyperHTML.wire(), 'Hello again!')
54+
button1.render('Hello world!'),
55+
button2.render('Hello again!')
6956
]}`
7057
```
7158

7259
## See also:
7360

7461
- [yoshuawuyts/nanocomponent][nano]
75-
- [joshgillies/microcomponent][micro]
62+
- [yoshuawuyts/microcomponent][micro]
7663

7764
## License
7865

@@ -85,4 +72,4 @@ MIT
8572
[hyper]: https://github.com/WebReflection/hyperHTML
8673
[wire]: https://github.com/WebReflection/hyperHTML#wait--there-is-a-wire--in-the-code
8774
[nano]: https://github.com/yoshuawuyts/nanocomponent
88-
[micro]: https://github.com/joshgillies/microcomponent
75+
[micro]: https://github.com/yoshuawuyts/microcomponent

index.js

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,63 @@
1-
const hyperHTML = require('hyperhtml')
21
const onload = require('on-load')
2+
const html = require('hyperrender').html
3+
const svg = require('hyperrender').svg
34
const slice = Array.prototype.slice
45

5-
const WIRE_OR_BOUND_NODE = /(update|hyperHTML)$/
6-
const ONLOAD_ATTR = /^data-onloadid/
7-
86
module.exports = function hypercomponent (component) {
9-
const render = hyperHTML.wire()
10-
const renderer = typeof component === 'function'
11-
? component
12-
: component.render
13-
7+
const symbol = {
8+
render: typeof component === 'function' ? component : component.render,
9+
load: component && component.load,
10+
unload: component && component.unload
11+
}
1412
return function wireComponent () {
15-
const onloadHandler = component.onload
16-
const onunloadHandler = component.onunload
17-
const args = slice.call(arguments)
18-
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)
48-
}
49-
50-
return el
13+
const instance = new Component()
14+
instance._symbol = symbol
15+
instance._loaded = !(symbol.load || symbol.unload)
16+
instance._defaultArgs = slice.call(arguments)
17+
return instance
18+
}
19+
}
20+
21+
function Component () {
22+
const self = this
23+
24+
function wire () {
25+
return wire.html.apply(self, arguments)
26+
}
27+
28+
wire.html = html(this)
29+
wire.svg = svg(this)
30+
31+
this._wire = wire
32+
}
33+
34+
Component.prototype.render = function render () {
35+
const self = this
36+
let args = [this._wire] // first arg is always our wire
37+
38+
for (var
39+
i = 0,
40+
length = arguments.length;
41+
i < length; i++
42+
) {
43+
args[i + 1] = arguments[i] === undefined
44+
? this._defaultArgs[i] // assign default arg if incomming is undefined
45+
: arguments[i]
46+
}
47+
48+
if (this._loaded === false) {
49+
return onload(this._symbol.render.apply(this, args), load, unload)
50+
}
51+
52+
return this._symbol.render.apply(this, args)
53+
54+
function load () {
55+
self._loaded = true
56+
self._symbol.load.apply(null, arguments)
57+
}
58+
59+
function unload () {
60+
self._loaded = false
61+
self._symbol.unload.apply(null, arguments)
5162
}
5263
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
"universal"
2020
],
2121
"scripts": {
22+
"start": "bankai example --open --uglify=false",
2223
"test": "standard"
2324
},
2425
"dependencies": {
25-
"hyperhtml": "^0.8.6",
26+
"hyperrender": "^1.0.0",
2627
"on-load": "^3.2.0"
2728
},
2829
"devDependencies": {
30+
"bankai": "^7.1.1",
31+
"hyperhtml": "^0.9.1",
2932
"standard": "^9.0.2"
3033
}
3134
}

0 commit comments

Comments
 (0)