ajo 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +9 -9
- package/index.test.js +9 -2
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -7,6 +7,8 @@ export const Portal = ({ host, children }) => render(host, children)
|
|
|
7
7
|
|
|
8
8
|
const Element = Symbol()
|
|
9
9
|
|
|
10
|
+
export const createTextNode = data => ({ [Element]: '#text', data, skip: true })
|
|
11
|
+
|
|
10
12
|
export const createElement = (name, props, ...children) => {
|
|
11
13
|
props = { ...props }
|
|
12
14
|
children = props.children ?? children
|
|
@@ -33,7 +35,7 @@ function* g(vnode, host) {
|
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
if (data) {
|
|
36
|
-
yield
|
|
38
|
+
yield createTextNode(data)
|
|
37
39
|
data = ''
|
|
38
40
|
}
|
|
39
41
|
|
|
@@ -43,15 +45,13 @@ function* g(vnode, host) {
|
|
|
43
45
|
: data += vnode
|
|
44
46
|
}
|
|
45
47
|
|
|
46
|
-
if (data) yield
|
|
48
|
+
if (data) yield createTextNode(data)
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
export const render = (vnode, host) => {
|
|
50
|
-
if (host?.nodeType !== Node.ELEMENT_NODE) return
|
|
51
|
-
|
|
52
52
|
let child = host.firstChild
|
|
53
53
|
|
|
54
|
-
for (const { [Element]: name, ref, children, ...props } of g(vnode, host)) {
|
|
54
|
+
for (const { [Element]: name, ref, skip, children, ...props } of g(vnode, host)) {
|
|
55
55
|
|
|
56
56
|
if (name === Skip) {
|
|
57
57
|
child = props.end ? null : child?.nextSibling ?? null
|
|
@@ -74,7 +74,7 @@ export const render = (vnode, host) => {
|
|
|
74
74
|
if (typeof ref === 'function') ref(node)
|
|
75
75
|
else if (typeof ref === 'object' && ref !== null) ref.current = node
|
|
76
76
|
|
|
77
|
-
render(children, node)
|
|
77
|
+
skip || render(children, node)
|
|
78
78
|
|
|
79
79
|
if (node === child) child = child.nextSibling
|
|
80
80
|
else if (node.contains?.(document.activeElement)) {
|
|
@@ -90,8 +90,8 @@ export const render = (vnode, host) => {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
while (child != null) {
|
|
93
|
-
const {
|
|
94
|
-
if (nodeType === Node.ELEMENT_NODE) dispose(child)
|
|
93
|
+
const { nextSibling } = child
|
|
94
|
+
if (child.nodeType === Node.ELEMENT_NODE) dispose(child)
|
|
95
95
|
host.removeChild(child)
|
|
96
96
|
child = nextSibling
|
|
97
97
|
}
|
|
@@ -127,7 +127,7 @@ const components = new WeakMap
|
|
|
127
127
|
|
|
128
128
|
export const createComponent = fn => ({ is, host, key, ...props }) =>
|
|
129
129
|
createElement(is ?? fn.is ?? 'div', {
|
|
130
|
-
...fn.host, ...host, key, ref: host => {
|
|
130
|
+
...fn.host, ...host, key, skip: true, ref: host => {
|
|
131
131
|
let cmp = components.get(host)
|
|
132
132
|
if (cmp == null) components.set(host, cmp = new Component(host, fn))
|
|
133
133
|
cmp.props = { ...fn.props, ...props }
|
package/index.test.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import 'backdom/register.js'
|
|
2
2
|
import { suite } from 'uvu'
|
|
3
3
|
import * as assert from 'uvu/assert'
|
|
4
|
-
import { createElement, render, } from './index.js'
|
|
4
|
+
import { createComponent, createElement, render, } from './index.js'
|
|
5
5
|
|
|
6
6
|
let it
|
|
7
7
|
|
|
@@ -37,7 +37,7 @@ it('should render a vnode', () => {
|
|
|
37
37
|
assert.snapshot(host.innerHTML, '<div foo="bar">foobar</div>')
|
|
38
38
|
})
|
|
39
39
|
|
|
40
|
-
it('should render a component', () => {
|
|
40
|
+
it('should render a stateless component', () => {
|
|
41
41
|
const host = document.createElement('div')
|
|
42
42
|
const Foo = ({ foo, children }) => createElement('div', { foo }, children)
|
|
43
43
|
debugger
|
|
@@ -45,4 +45,11 @@ it('should render a component', () => {
|
|
|
45
45
|
assert.snapshot(host.innerHTML, '<div foo="bar"><span>foobar</span></div>')
|
|
46
46
|
})
|
|
47
47
|
|
|
48
|
+
it('should render a stateful component', () => {
|
|
49
|
+
const host = document.createElement('div')
|
|
50
|
+
const Foo = createComponent(({ foo, children }) => createElement('div', { foo }, children))
|
|
51
|
+
render(createElement(Foo, { is: 'foo-component', foo: 'bar' }, createElement('span', null, 'foobar')), host)
|
|
52
|
+
assert.snapshot(host.innerHTML, '<foo-component><div foo="bar"><span>foobar</span></div></foo-component>')
|
|
53
|
+
})
|
|
54
|
+
|
|
48
55
|
it.run()
|