ajo 0.0.3 → 0.0.4

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.
Files changed (3) hide show
  1. package/index.js +22 -20
  2. package/index.test.js +8 -0
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -8,9 +8,11 @@ export const Portal = ({ host, children }) => render(host, children)
8
8
  const Element = Symbol()
9
9
 
10
10
  export const createElement = (name, props, ...children) => {
11
- const { length } = children
12
- if (length > 0) (props ?? (props = {})).children = length === 1 ? children[0] : children
13
- return { ...props, [Element]: name }
11
+ props = { ...props }
12
+ children = props.children ?? children
13
+ if (children.length > 0) props.children = children.length === 1 ? children[0] : children
14
+ props[Element] = name
15
+ return props
14
16
  }
15
17
 
16
18
  function* g(vnode, host) {
@@ -45,6 +47,8 @@ function* g(vnode, host) {
45
47
  }
46
48
 
47
49
  export const render = (vnode, host) => {
50
+ if (host?.nodeType !== Node.ELEMENT_NODE) return
51
+
48
52
  let child = host.firstChild
49
53
 
50
54
  for (const { [Element]: name, ref, children, ...props } of g(vnode, host)) {
@@ -65,13 +69,12 @@ export const render = (vnode, host) => {
65
69
  ? document.createTextNode('')
66
70
  : document.createElement(name)
67
71
 
68
- if (name === '#text') {
69
- if (node.data !== props.data) node.data = props.data
70
- } else {
71
- patch(props, node)
72
- setRef(ref, node)
73
- render(children, node)
74
- }
72
+ patch(props, node)
73
+
74
+ if (typeof ref === 'function') ref(node)
75
+ else if (typeof ref === 'object' && ref !== null) ref.current = node
76
+
77
+ render(children, node)
75
78
 
76
79
  if (node === child) child = child.nextSibling
77
80
  else if (node.contains?.(document.activeElement)) {
@@ -88,13 +91,18 @@ export const render = (vnode, host) => {
88
91
 
89
92
  while (child != null) {
90
93
  const { nodeType, nextSibling } = child
91
- if (nodeType === 1) dispose(child)
94
+ if (nodeType === Node.ELEMENT_NODE) dispose(child)
92
95
  host.removeChild(child)
93
96
  child = nextSibling
94
97
  }
95
98
  }
96
99
 
97
100
  const patch = (props, node) => {
101
+ if (node.nodeType === Node.TEXT_NODE) {
102
+ if (node.data !== props.data) node.data = props.data
103
+ return
104
+ }
105
+
98
106
  for (const name of new Set([...node.getAttributeNames(), ...keys(props)])) {
99
107
  let value = props[name]
100
108
 
@@ -115,21 +123,15 @@ const patch = (props, node) => {
115
123
  }
116
124
  }
117
125
 
118
- const setRef = (ref, node) => {
119
- if (typeof ref === 'function') ref(node)
120
- else if (typeof ref === 'object' && ref !== null) ref.current = node
121
- }
122
-
123
126
  const components = new WeakMap
124
127
 
125
- export const createComponent = fn => ({ is, host, key, ref, ...props }) =>
128
+ export const createComponent = fn => ({ is, host, key, ...props }) =>
126
129
  createElement(is ?? fn.is ?? 'div', {
127
- ...fn.host, ...host, key, ['ref']: host => {
130
+ ...fn.host, ...host, key, ref: host => {
128
131
  let cmp = components.get(host)
129
132
  if (cmp == null) components.set(host, cmp = new Component(host, fn))
130
133
  cmp.props = { ...fn.props, ...props }
131
134
  cmp.update()
132
- setRef(ref, host)
133
135
  }
134
136
  })
135
137
 
@@ -163,7 +165,7 @@ class Component {
163
165
  }
164
166
 
165
167
  *[Symbol.iterator]() {
166
- while (true) yield this.props
168
+ while (this.host) yield this.props
167
169
  }
168
170
  }
169
171
 
package/index.test.js CHANGED
@@ -37,4 +37,12 @@ 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', () => {
41
+ const host = document.createElement('div')
42
+ const Foo = ({ foo, children }) => createElement('div', { foo }, children)
43
+ debugger
44
+ render(createElement(Foo, { foo: 'bar' }, createElement('span', null, 'foobar')), host)
45
+ assert.snapshot(host.innerHTML, '<div foo="bar"><span>foobar</span></div>')
46
+ })
47
+
40
48
  it.run()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ajo",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "ajo is a JavaScript view library for building user interfaces",
5
5
  "type": "module",
6
6
  "main": "index.js",