lumix-js 0.1.1 → 0.1.2

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/dist/dom.d.ts CHANGED
@@ -5,4 +5,8 @@ export interface Props {
5
5
  }
6
6
  export declare function h(tag: string | ((props: Props, ...children: any[]) => any), props: Props | null, ...children: any[]): any;
7
7
  export declare function Fragment(_props: any, ...children: any[]): any[];
8
- export declare function hydrate(root: HTMLElement, component: (props?: any) => HTMLElement, props?: any): void;
8
+ export declare function hydrate(root: HTMLElement, component: (props?: any) => any, props?: any): void;
9
+ export declare function mount(root: HTMLElement, component: (props?: any) => any, props?: any): {
10
+ update(nextComponent: (props?: any) => any): void;
11
+ destroy(): void;
12
+ };
package/dist/dom.js CHANGED
@@ -4,14 +4,16 @@ import { withHooks, runHooks } from "./lifecycle.js";
4
4
  export function h(tag, props, ...children) {
5
5
  if (typeof tag === "function") {
6
6
  const { result, mount, destroy } = withHooks(() => tag(props || {}, ...children));
7
- // If it's a component that returns a single element, we can attach hooks to it
8
- if (result instanceof HTMLElement) {
9
- if (mount.length > 0) {
10
- // Simple trick: use MutationObserver or just run on next tick if added
11
- setTimeout(() => runHooks(mount), 0);
12
- }
13
- if (destroy.length > 0) {
14
- result._luminDestroy = destroy;
7
+ // Attach hooks for single element or Fragment(array) roots.
8
+ const roots = Array.isArray(result) ? result : [result];
9
+ for (const r of roots) {
10
+ if (r instanceof HTMLElement) {
11
+ if (mount.length > 0) {
12
+ setTimeout(() => runHooks(mount), 0);
13
+ }
14
+ if (destroy.length > 0) {
15
+ r._luminDestroy = destroy;
16
+ }
15
17
  }
16
18
  }
17
19
  return result;
@@ -131,6 +133,61 @@ function unmount(node) {
131
133
  export function hydrate(root, component, props) {
132
134
  while (root.firstChild)
133
135
  root.removeChild(root.firstChild);
134
- const el = h(component, props || {});
135
- root.appendChild(el);
136
+ const out = h(component, props || {});
137
+ const nodes = Array.isArray(out) ? out : [out];
138
+ for (const n of nodes) {
139
+ if (n === null || n === undefined)
140
+ continue;
141
+ if (n instanceof Node)
142
+ root.appendChild(n);
143
+ else
144
+ root.appendChild(document.createTextNode(String(n)));
145
+ }
146
+ }
147
+ export function mount(root, component, props) {
148
+ const start = document.createComment("lumix-root-start");
149
+ const end = document.createComment("lumix-root-end");
150
+ while (root.firstChild)
151
+ root.removeChild(root.firstChild);
152
+ root.appendChild(start);
153
+ root.appendChild(end);
154
+ let currentNodes = [];
155
+ let currentComponent = component;
156
+ let currentProps = props;
157
+ const render = () => {
158
+ for (const node of currentNodes) {
159
+ unmount(node);
160
+ if (node.parentNode)
161
+ node.parentNode.removeChild(node);
162
+ }
163
+ currentNodes = [];
164
+ const out = h(currentComponent, currentProps || {});
165
+ const nodes = Array.isArray(out) ? out : [out];
166
+ for (const n of nodes) {
167
+ if (n === null || n === undefined)
168
+ continue;
169
+ const node = n instanceof Node ? n : document.createTextNode(String(n));
170
+ root.insertBefore(node, end);
171
+ currentNodes.push(node);
172
+ }
173
+ };
174
+ render();
175
+ return {
176
+ update(nextComponent) {
177
+ currentComponent = nextComponent;
178
+ render();
179
+ },
180
+ destroy() {
181
+ for (const node of currentNodes) {
182
+ unmount(node);
183
+ if (node.parentNode)
184
+ node.parentNode.removeChild(node);
185
+ }
186
+ currentNodes = [];
187
+ if (start.parentNode)
188
+ start.parentNode.removeChild(start);
189
+ if (end.parentNode)
190
+ end.parentNode.removeChild(end);
191
+ },
192
+ };
136
193
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lumix-js",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "The modular runtime for LumixJS",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",