veles 0.0.7 → 0.0.9

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/index.d.cts CHANGED
@@ -1,18 +1,39 @@
1
- import { V as VelesElement, a as VelesComponent } from './types.d-DgVBp6oa.cjs';
2
- export { F as Fragment, c as createElement } from './fragment-CU26z590.cjs';
3
- export { S as State, c as createState } from './create-state-D1JASFVs.cjs';
1
+ import { V as VelesElement, a as VelesComponentObject, c as VelesChildren } from './types.d-CjiJHqth.cjs';
2
+ export { Fragment, jsx as createElement } from './jsx-runtime.cjs';
3
+ export { S as State, c as createState } from './index-BkD2Za6F.cjs';
4
4
 
5
+ /**
6
+ * Attach Veles component tree to a regular HTML node.
7
+ * Right now it will wrap the app into an additional `<div>` tag.
8
+ *
9
+ * It returns a function which when executed, will remove the Veles
10
+ * tree from DOM and remove all subscriptions.
11
+ */
5
12
  declare function attachComponent({ htmlElement, component, }: {
6
13
  htmlElement: HTMLElement;
7
- component: VelesElement | VelesComponent;
14
+ component: VelesElement | VelesComponentObject;
8
15
  }): () => void;
9
16
 
10
17
  declare function onMount(cb: () => void | Function): void;
11
18
  declare function onUnmount(cb: Function): void;
12
19
 
20
+ /**
21
+ * Create a reference which has special treatment if passed as
22
+ * ref={ref} to any DOM Node. `ref.current` will contain the
23
+ * rendered node, even if it changes.
24
+ */
13
25
  declare function createRef<T>(initialRefValue?: T | null): {
14
26
  velesRef: true;
15
27
  current: T | null;
16
28
  };
17
29
 
18
- export { attachComponent, createRef, onMount, onUnmount };
30
+ declare function createContext<T>(): {
31
+ Provider: ({ value, children }: {
32
+ value: T;
33
+ children?: VelesChildren;
34
+ }) => VelesComponentObject | VelesElement;
35
+ addContext: (value: T) => void;
36
+ readContext: () => T;
37
+ };
38
+
39
+ export { attachComponent, createContext, createRef, onMount, onUnmount };
package/dist/index.d.ts CHANGED
@@ -1,18 +1,39 @@
1
- import { V as VelesElement, a as VelesComponent } from './types.d-DgVBp6oa.js';
2
- export { F as Fragment, c as createElement } from './fragment-IVSEC7-Q.js';
3
- export { S as State, c as createState } from './create-state-Bo6TT4qP.js';
1
+ import { V as VelesElement, a as VelesComponentObject, c as VelesChildren } from './types.d-CjiJHqth.js';
2
+ export { Fragment, jsx as createElement } from './jsx-runtime.js';
3
+ export { S as State, c as createState } from './index-DBdVF_11.js';
4
4
 
5
+ /**
6
+ * Attach Veles component tree to a regular HTML node.
7
+ * Right now it will wrap the app into an additional `<div>` tag.
8
+ *
9
+ * It returns a function which when executed, will remove the Veles
10
+ * tree from DOM and remove all subscriptions.
11
+ */
5
12
  declare function attachComponent({ htmlElement, component, }: {
6
13
  htmlElement: HTMLElement;
7
- component: VelesElement | VelesComponent;
14
+ component: VelesElement | VelesComponentObject;
8
15
  }): () => void;
9
16
 
10
17
  declare function onMount(cb: () => void | Function): void;
11
18
  declare function onUnmount(cb: Function): void;
12
19
 
20
+ /**
21
+ * Create a reference which has special treatment if passed as
22
+ * ref={ref} to any DOM Node. `ref.current` will contain the
23
+ * rendered node, even if it changes.
24
+ */
13
25
  declare function createRef<T>(initialRefValue?: T | null): {
14
26
  velesRef: true;
15
27
  current: T | null;
16
28
  };
17
29
 
18
- export { attachComponent, createRef, onMount, onUnmount };
30
+ declare function createContext<T>(): {
31
+ Provider: ({ value, children }: {
32
+ value: T;
33
+ children?: VelesChildren;
34
+ }) => VelesComponentObject | VelesElement;
35
+ addContext: (value: T) => void;
36
+ readContext: () => T;
37
+ };
38
+
39
+ export { attachComponent, createContext, createRef, onMount, onUnmount };
package/dist/index.js CHANGED
@@ -1,16 +1,17 @@
1
- import {
2
- Fragment
3
- } from "./chunk-X6QYYW56.js";
4
- import {
5
- createState
6
- } from "./chunk-MH6DPZ3V.js";
7
1
  import {
8
2
  callMountHandlers,
3
+ callUnmountHandlers,
4
+ createContext,
5
+ createState,
6
+ getExecutedComponentVelesNode,
7
+ renderTree
8
+ } from "./chunk-VW64LMPA.js";
9
+ import {
10
+ Fragment,
9
11
  createElement,
10
- getComponentVelesNode,
11
12
  onMount,
12
13
  onUnmount
13
- } from "./chunk-ILNLS6QO.js";
14
+ } from "./chunk-2KPVWPOL.js";
14
15
 
15
16
  // src/attach-component.ts
16
17
  function attachComponent({
@@ -18,10 +19,12 @@ function attachComponent({
18
19
  component
19
20
  }) {
20
21
  const wrappedApp = createElement("div", { children: [component] });
21
- const { velesElementNode } = getComponentVelesNode(wrappedApp);
22
+ const wrappedAppTree = renderTree(wrappedApp);
23
+ const velesElementNode = getExecutedComponentVelesNode(wrappedAppTree);
22
24
  htmlElement.appendChild(velesElementNode.html);
23
- callMountHandlers(wrappedApp);
25
+ callMountHandlers(wrappedAppTree);
24
26
  return () => {
27
+ callUnmountHandlers(wrappedAppTree);
25
28
  velesElementNode.html.remove();
26
29
  };
27
30
  }
@@ -36,6 +39,7 @@ function createRef(initialRefValue = null) {
36
39
  export {
37
40
  Fragment,
38
41
  attachComponent,
42
+ createContext,
39
43
  createElement,
40
44
  createRef,
41
45
  createState,
@@ -26,28 +26,30 @@ __export(jsx_runtime_exports, {
26
26
  });
27
27
  module.exports = __toCommonJS(jsx_runtime_exports);
28
28
 
29
- // src/_utils.ts
30
- function getComponentVelesNode(component) {
31
- const componentsTree = [];
32
- if ("velesStringElement" in component) {
33
- return {
34
- velesElementNode: component,
35
- componentsTree: []
36
- };
37
- }
38
- let childNode = component;
39
- while ("velesComponent" in childNode) {
40
- componentsTree.push(childNode);
41
- if ("velesStringElement" in childNode.tree) {
42
- return {
43
- velesElementNode: childNode.tree,
44
- componentsTree
45
- };
46
- } else {
47
- childNode = childNode.tree;
29
+ // src/create-element/create-text-element.ts
30
+ function createTextElement(text) {
31
+ const mountHandlers = [];
32
+ const unmountHandlers = [];
33
+ return {
34
+ velesStringElement: true,
35
+ // in case there is no text, we create an empty Text node, so we still can
36
+ // have a reference to it, replace it, call lifecycle methods, etc
37
+ html: document.createTextNode(text || ""),
38
+ _privateMethods: {
39
+ _addMountHandler(cb) {
40
+ mountHandlers.push(cb);
41
+ },
42
+ _callMountHandlers() {
43
+ mountHandlers.forEach((cb) => cb());
44
+ },
45
+ _addUnmountHandler: (cb) => {
46
+ unmountHandlers.push(cb);
47
+ },
48
+ _callUnmountHandlers: () => {
49
+ unmountHandlers.forEach((cb) => cb());
50
+ }
48
51
  }
49
- }
50
- return { velesElementNode: childNode, componentsTree };
52
+ };
51
53
  }
52
54
 
53
55
  // src/create-element/parse-children.ts
@@ -60,30 +62,36 @@ function parseChildren({
60
62
  if (children === void 0 || children === null) {
61
63
  return childComponents;
62
64
  }
65
+ let lastInsertedNode = null;
63
66
  (Array.isArray(children) ? children : [children]).forEach(
64
67
  (childComponent) => {
65
68
  if (typeof childComponent === "string") {
66
- const text = document.createTextNode(childComponent);
67
- htmlElement.append(text);
69
+ const textNode = createTextElement(childComponent);
70
+ htmlElement.append(textNode.html);
71
+ lastInsertedNode = textNode.html;
72
+ childComponents.push(textNode);
68
73
  } else if (typeof childComponent === "number") {
69
- const text = document.createTextNode(String(childComponent));
70
- htmlElement.append(text);
74
+ const textNode = createTextElement(String(childComponent));
75
+ htmlElement.append(textNode.html);
76
+ lastInsertedNode = textNode.html;
77
+ childComponents.push(textNode);
71
78
  } else if (typeof childComponent === "object" && childComponent && "velesNode" in childComponent && (childComponent == null ? void 0 : childComponent.velesNode)) {
72
79
  if (childComponent.phantom) {
73
80
  childComponent.childComponents.forEach((childComponentofPhantom) => {
74
81
  if ("velesNode" in childComponentofPhantom) {
75
82
  htmlElement.append(childComponentofPhantom.html);
76
83
  childComponentofPhantom.parentVelesElement = velesNode;
77
- } else {
78
- const { velesElementNode } = getComponentVelesNode(
79
- childComponentofPhantom
80
- );
84
+ lastInsertedNode = childComponentofPhantom.html;
85
+ } else if ("velesStringElement" in childComponentofPhantom) {
86
+ const velesElementNode = childComponentofPhantom;
81
87
  if (!velesElementNode) {
82
88
  console.error("can't find HTML tree in a component chain");
83
89
  } else {
84
90
  htmlElement.append(velesElementNode.html);
91
+ lastInsertedNode = velesElementNode.html;
85
92
  velesElementNode.parentVelesElement = velesNode;
86
93
  }
94
+ } else {
87
95
  }
88
96
  });
89
97
  childComponent.parentVelesElement = velesNode;
@@ -92,39 +100,18 @@ function parseChildren({
92
100
  htmlElement.append(childComponent.html);
93
101
  childComponent.parentVelesElement = velesNode;
94
102
  childComponents.push(childComponent);
103
+ lastInsertedNode = childComponent.html;
95
104
  }
96
- } else if (typeof childComponent === "object" && childComponent && "velesComponent" in childComponent && (childComponent == null ? void 0 : childComponent.velesComponent)) {
97
- const { componentsTree, velesElementNode } = getComponentVelesNode(childComponent);
98
- if (!velesElementNode) {
99
- console.error("can't find HTML tree in a component chain");
100
- } else {
101
- if ("velesNode" in velesElementNode && velesElementNode.phantom) {
102
- velesElementNode.childComponents.forEach(
103
- (childComponentofPhantom) => {
104
- if ("velesNode" in childComponentofPhantom) {
105
- htmlElement.append(childComponentofPhantom.html);
106
- childComponentofPhantom.parentVelesElement = velesNode;
107
- } else {
108
- const { componentsTree: componentsTree2, velesElementNode: velesElementNode2 } = getComponentVelesNode(childComponentofPhantom);
109
- if (!velesElementNode2) {
110
- console.error("can't find HTML tree in a component chain");
111
- } else {
112
- htmlElement.append(velesElementNode2.html);
113
- velesElementNode2.parentVelesElement = velesNode;
114
- }
115
- }
116
- }
117
- );
118
- } else {
119
- htmlElement.append(velesElementNode.html);
120
- }
121
- velesElementNode.parentVelesElement = velesNode;
122
- childComponents.push(childComponent);
123
- }
105
+ } else if (typeof childComponent === "object" && childComponent && "velesComponentObject" in childComponent) {
106
+ childComponent.insertAfter = lastInsertedNode;
107
+ childComponent.parentVelesElement = velesNode;
108
+ childComponents.push(childComponent);
109
+ lastInsertedNode = childComponent;
124
110
  } else if (typeof childComponent === "object" && childComponent && "velesStringElement" in childComponent && (childComponent == null ? void 0 : childComponent.velesStringElement)) {
125
111
  htmlElement.append(childComponent.html);
126
112
  childComponent.parentVelesElement = velesNode;
127
113
  childComponents.push(childComponent);
114
+ lastInsertedNode = childComponent.html;
128
115
  }
129
116
  }
130
117
  );
@@ -141,31 +128,30 @@ function assignAttributes({
141
128
  const isFunction = typeof value === "function";
142
129
  if (isFunction && value.velesAttribute === true) {
143
130
  const attributeValue = value(htmlElement, key, velesNode);
144
- htmlElement.setAttribute(key, attributeValue);
145
- } else if (
146
- // basically, any form of `on` handlers, like `onClick`, `onCopy`, etc
147
- isFunction && key.length > 2 && key.startsWith("on")
148
- ) {
149
- htmlElement.addEventListener(
150
- key[2].toLocaleLowerCase() + key.slice(3),
151
- value
152
- );
131
+ assignAttribute({ key, value: attributeValue, htmlElement });
153
132
  } else {
154
- htmlElement.setAttribute(key, value);
133
+ assignAttribute({ key, value, htmlElement });
155
134
  }
156
135
  });
157
136
  }
158
-
159
- // src/hooks/lifecycle.ts
160
- var contextStack = [];
161
- var currentContext = null;
162
- function addContext(newContext) {
163
- contextStack.push(newContext);
164
- currentContext = newContext;
165
- }
166
- function popContext() {
167
- contextStack.pop();
168
- currentContext = contextStack[contextStack.length - 1];
137
+ function assignAttribute({
138
+ key,
139
+ value,
140
+ htmlElement
141
+ }) {
142
+ if (
143
+ // basically, any form of `on` handlers, like `onClick`, `onCopy`, etc
144
+ typeof value === "function" && key.startsWith("on")
145
+ ) {
146
+ htmlElement.addEventListener(key.slice(2).toLocaleLowerCase(), value);
147
+ } else {
148
+ if (typeof value === "boolean") {
149
+ if (value)
150
+ htmlElement.setAttribute(key, "");
151
+ } else {
152
+ htmlElement.setAttribute(key, value);
153
+ }
154
+ }
169
155
  }
170
156
 
171
157
  // src/create-element/parse-component.ts
@@ -173,49 +159,27 @@ function parseComponent({
173
159
  element,
174
160
  props
175
161
  }) {
176
- const componentUnmountCbs = [];
177
- const componentMountCbs = [];
178
- const componentAPI = {
179
- onMount: (cb) => {
180
- componentMountCbs.push(cb);
181
- },
182
- onUnmount: (cb) => {
183
- componentUnmountCbs.push(cb);
184
- }
185
- };
186
- addContext(componentAPI);
187
- const _componentTree = element(props, componentAPI);
188
- const componentTree = typeof _componentTree === "string" || !_componentTree ? {
189
- velesStringElement: true,
190
- html: document.createTextNode(
191
- typeof _componentTree === "string" ? _componentTree : ""
192
- )
193
- } : _componentTree;
194
- popContext();
195
- const velesComponent = {
196
- velesComponent: true,
197
- tree: componentTree,
162
+ const mountCbs = [];
163
+ const unmountCbs = [];
164
+ return {
165
+ velesComponentObject: true,
166
+ element,
167
+ props,
198
168
  _privateMethods: {
169
+ _addMountHandler(cb) {
170
+ mountCbs.push(cb);
171
+ },
199
172
  _addUnmountHandler: (cb) => {
200
- componentAPI.onUnmount(cb);
173
+ unmountCbs.push(cb);
201
174
  },
202
175
  _callMountHandlers: () => {
203
- componentMountCbs.forEach((cb) => {
204
- const mountCbResult = cb();
205
- if (typeof mountCbResult === "function") {
206
- componentAPI.onUnmount(mountCbResult);
207
- }
208
- });
176
+ mountCbs.forEach((cb) => cb());
209
177
  },
210
178
  _callUnmountHandlers: () => {
211
- componentUnmountCbs.forEach((cb) => cb());
212
- if ("_privateMethods" in velesComponent.tree) {
213
- velesComponent.tree._privateMethods._callUnmountHandlers();
214
- }
179
+ unmountCbs.forEach((cb) => cb());
215
180
  }
216
181
  }
217
182
  };
218
- return velesComponent;
219
183
  }
220
184
 
221
185
  // src/create-element/create-element.ts
@@ -232,23 +196,25 @@ function createElement(element, props = {}) {
232
196
  htmlElement: newElement,
233
197
  velesNode
234
198
  });
235
- let unmountHandlers = [];
236
- const callUnmountHandlers = () => {
237
- unmountHandlers.forEach((cb) => cb());
238
- unmountHandlers = [];
239
- childComponents.forEach((childComponent) => {
240
- childComponent._privateMethods._callUnmountHandlers();
241
- });
242
- };
199
+ const unmountHandlers = [];
243
200
  velesNode.html = newElement;
244
201
  velesNode.velesNode = true;
245
202
  velesNode.childComponents = childComponents;
246
203
  velesNode.phantom = phantom;
204
+ const mountHandlers = [];
247
205
  velesNode._privateMethods = {
248
- _addUnmountHandler: (cb) => {
206
+ _addMountHandler(cb) {
207
+ mountHandlers.push(cb);
208
+ },
209
+ _callMountHandlers() {
210
+ mountHandlers.forEach((cb) => cb());
211
+ },
212
+ _addUnmountHandler(cb) {
249
213
  unmountHandlers.push(cb);
250
214
  },
251
- _callUnmountHandlers: callUnmountHandlers
215
+ _callUnmountHandlers() {
216
+ unmountHandlers.forEach((cb) => cb());
217
+ }
252
218
  };
253
219
  assignAttributes({ props: otherProps, htmlElement: newElement, velesNode });
254
220
  return velesNode;