veles 0.0.1 → 0.0.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/README.md +2 -0
- package/dist/jsx-runtime.cjs +6 -2
- package/dist/jsx-runtime.cjs.map +1 -1
- package/dist/jsx-runtime.d.cts +1 -1
- package/dist/jsx-runtime.d.ts +1 -1
- package/dist/jsx-runtime.js +3 -1
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# Veles
|
|
2
2
|
|
|
3
3
|

|
|
4
|
+
[](https://bundlephobia.com/result?p=veles)
|
|
5
|
+
[](https://www.npmjs.com/package/veles)
|
|
4
6
|
|
|
5
7
|
> The library is in very early stages and is not published yet as some crucial APIs are still under development
|
|
6
8
|
|
package/dist/jsx-runtime.cjs
CHANGED
|
@@ -20,7 +20,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
var jsx_runtime_exports = {};
|
|
21
21
|
__export(jsx_runtime_exports, {
|
|
22
22
|
Fragment: () => Fragment,
|
|
23
|
-
jsx: () => createElement
|
|
23
|
+
jsx: () => createElement,
|
|
24
|
+
jsxDEV: () => createElement,
|
|
25
|
+
jsxs: () => createElement
|
|
24
26
|
});
|
|
25
27
|
module.exports = __toCommonJS(jsx_runtime_exports);
|
|
26
28
|
|
|
@@ -271,6 +273,8 @@ function Fragment({ children }) {
|
|
|
271
273
|
// Annotate the CommonJS export names for ESM import in node:
|
|
272
274
|
0 && (module.exports = {
|
|
273
275
|
Fragment,
|
|
274
|
-
jsx
|
|
276
|
+
jsx,
|
|
277
|
+
jsxDEV,
|
|
278
|
+
jsxs
|
|
275
279
|
});
|
|
276
280
|
//# sourceMappingURL=jsx-runtime.cjs.map
|
package/dist/jsx-runtime.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/jsx-runtime.ts","../src/utils.ts","../src/create-element/parse-children.ts","../src/create-element/assign-attributes.ts","../src/hooks/lifecycle.ts","../src/create-element/parse-component.ts","../src/create-element/create-element.ts","../src/fragment.ts"],"sourcesContent":["export { createElement as jsx } from \"./create-element\";\nexport { Fragment } from \"./fragment\";\n","import type { VelesComponent, VelesElement, VelesStringElement } from \"./types\";\n\nfunction getComponentVelesNode(\n component: VelesComponent | VelesElement | VelesStringElement\n): {\n velesElementNode: VelesElement | VelesStringElement;\n componentsTree: VelesComponent[];\n} {\n const componentsTree: VelesComponent[] = [];\n\n if (\"velesStringElement\" in component) {\n return {\n velesElementNode: component,\n componentsTree: [],\n };\n }\n\n let childNode: VelesComponent | VelesElement = component;\n // we can have multiple components nested, we need to get\n // to the actual HTML to attach it\n while (\"velesComponent\" in childNode) {\n componentsTree.push(childNode);\n if (\"velesStringElement\" in childNode.tree) {\n return {\n velesElementNode: childNode.tree,\n componentsTree,\n };\n } else {\n childNode = childNode.tree;\n }\n }\n\n return { velesElementNode: childNode, componentsTree };\n}\n\nfunction identity<T>(value1: T, value2: T) {\n return value1 === value2;\n}\n\nexport { getComponentVelesNode, identity };\n","import { getComponentVelesNode } from \"../utils\";\n\nimport type {\n VelesComponent,\n VelesElement,\n VelesStringElement,\n VelesElementProps,\n} from \"../types\";\n\nfunction parseChildren({\n children,\n htmlElement,\n velesNode,\n}: {\n children: VelesElementProps[\"children\"];\n htmlElement: HTMLElement;\n velesNode: VelesElement;\n}) {\n const childComponents: (\n | VelesElement\n | VelesComponent\n | VelesStringElement\n )[] = [];\n\n if (children === undefined || children === null) {\n return childComponents;\n }\n\n (Array.isArray(children) ? children : [children]).forEach(\n (childComponent) => {\n if (typeof childComponent === \"string\") {\n const text = document.createTextNode(childComponent);\n htmlElement.append(text);\n } else if (\n typeof childComponent === \"object\" &&\n childComponent &&\n \"velesNode\" in childComponent &&\n childComponent?.velesNode\n ) {\n if (childComponent.phantom) {\n // we need to get ALL the children of it and attach it to this node\n childComponent.childComponents.forEach((childComponentofPhantom) => {\n if (\"velesNode\" in childComponentofPhantom) {\n htmlElement.append(childComponentofPhantom.html);\n childComponentofPhantom.parentVelesElement = velesNode;\n } else {\n const { componentsTree, velesElementNode } =\n getComponentVelesNode(childComponentofPhantom);\n\n if (!velesElementNode) {\n console.error(\"can't find HTML tree in a component chain\");\n } else {\n htmlElement.append(velesElementNode.html);\n\n // TODO: address the same concern as below\n componentsTree.forEach((component) => {\n component._privateMethods._callMountHandlers();\n });\n\n velesElementNode.parentVelesElement = velesNode;\n }\n }\n });\n childComponent.parentVelesElement = velesNode;\n childComponents.push(childComponent);\n } else {\n // TODO: check that it is a valid DOM Node\n htmlElement.append(childComponent.html);\n childComponent.parentVelesElement = velesNode;\n childComponents.push(childComponent);\n }\n } else if (\n typeof childComponent === \"object\" &&\n childComponent &&\n \"velesComponent\" in childComponent &&\n childComponent?.velesComponent\n ) {\n // we need to save the whole components chain, so that\n // we can trigger `mount` hooks on all of them correctly\n const { componentsTree, velesElementNode } =\n getComponentVelesNode(childComponent);\n\n if (!velesElementNode) {\n console.error(\"can't find HTML tree in a component chain\");\n } else {\n if (\"velesNode\" in velesElementNode && velesElementNode.phantom) {\n // we need to get ALL the children of it and attach it to this node\n velesElementNode.childComponents.forEach(\n (childComponentofPhantom) => {\n if (\"velesNode\" in childComponentofPhantom) {\n htmlElement.append(childComponentofPhantom.html);\n childComponentofPhantom.parentVelesElement = velesNode;\n } else {\n const { componentsTree, velesElementNode } =\n getComponentVelesNode(childComponentofPhantom);\n\n if (!velesElementNode) {\n console.error(\"can't find HTML tree in a component chain\");\n } else {\n htmlElement.append(velesElementNode.html);\n\n // Same explanation as below. Components are mounted synchronously\n setTimeout(() => {\n componentsTree.forEach((component) => {\n component._privateMethods._callMountHandlers();\n });\n }, 0);\n velesElementNode.parentVelesElement = velesNode;\n }\n }\n }\n );\n } else {\n htmlElement.append(velesElementNode.html);\n }\n\n /**\n * Components are mounted synchronously, so we can safely wait for the next\n * CPU tick and be sure that new markup is attached to DOM.\n */\n setTimeout(() => {\n componentsTree.forEach((component) => {\n component._privateMethods._callMountHandlers();\n });\n }, 0);\n velesElementNode.parentVelesElement = velesNode;\n childComponents.push(childComponent);\n }\n } else if (\n typeof childComponent === \"object\" &&\n childComponent &&\n \"velesStringElement\" in childComponent &&\n childComponent?.velesStringElement\n ) {\n // TODO: check that it is a valid DOM Node\n htmlElement.append(childComponent.html);\n childComponent.parentVelesElement = velesNode;\n childComponents.push(childComponent);\n }\n }\n );\n\n return childComponents;\n}\n\nexport { parseChildren };\n","import type { VelesElement } from \"../types\";\n\nfunction assignAttributes({\n props,\n htmlElement,\n velesNode,\n}: {\n props: Record<string, any>;\n htmlElement: HTMLElement;\n velesNode: VelesElement;\n}) {\n Object.entries(props).forEach(([key, value]) => {\n const isFunction = typeof value === \"function\";\n if (isFunction && value.velesAttribute === true) {\n const attributeValue = value(htmlElement, key, velesNode);\n htmlElement.setAttribute(key, attributeValue);\n } else if (\n // basically, any form of `on` handlers, like `onClick`, `onCopy`, etc\n isFunction &&\n key.length > 2 &&\n key.startsWith(\"on\")\n ) {\n // TODO: think if this is robust enough\n htmlElement.addEventListener(\n key[2].toLocaleLowerCase() + key.slice(3),\n value\n );\n } else {\n htmlElement.setAttribute(key, value);\n }\n });\n}\n\nexport { assignAttributes };\n","import { ComponentAPI } from \"../types\";\n\n// lifecycle hooks\n// currently, all components need to be synchronous\n// so we execute them and set background context\n// since components can be nested, we need to keep the array\nconst contextStack: ComponentAPI[] = [];\n// all hooks need to know the current context\n// it should way more convenient this way compared to passing\n// `componentAPI` to every method\nlet currentContext: ComponentAPI | null = null;\n\nfunction addContext(newContext: ComponentAPI) {\n contextStack.push(newContext);\n currentContext = newContext;\n}\n\nfunction popContext() {\n contextStack.pop();\n currentContext = contextStack[contextStack.length - 1];\n}\n\nfunction onMount(cb: Function) {\n if (currentContext) {\n currentContext.onMount(cb);\n } else {\n console.error(\"missing current context\");\n }\n}\n\nfunction onUnmount(cb: Function) {\n if (currentContext) {\n currentContext.onUnmount(cb);\n } else {\n console.error(\"missing current context\");\n }\n}\n\nexport { addContext, popContext, onMount, onUnmount };\n","import { addContext, popContext } from \"../hooks/lifecycle\";\n\nimport type {\n VelesComponent,\n VelesStringElement,\n VelesElementProps,\n ComponentAPI,\n ComponentFunction,\n} from \"../types\";\n\nfunction parseComponent({\n element,\n props,\n}: {\n element: ComponentFunction;\n props: VelesElementProps;\n}) {\n const componentUnmountCbs: Function[] = [];\n const componentMountCbs: Function[] = [];\n const componentAPI: ComponentAPI = {\n onMount: (cb) => {\n componentMountCbs.push(cb);\n },\n onUnmount: (cb) => {\n componentUnmountCbs.push(cb);\n },\n };\n // at this moment we enter new context\n addContext(componentAPI);\n const _componentTree = element(props, componentAPI);\n\n const componentTree =\n typeof _componentTree === \"string\" || !_componentTree\n ? ({\n velesStringElement: true,\n html: document.createTextNode(\n typeof _componentTree === \"string\" ? _componentTree : \"\"\n ),\n } as VelesStringElement)\n : _componentTree;\n\n // here we exit our context\n popContext();\n const velesComponent: VelesComponent = {\n velesComponent: true,\n tree: componentTree,\n _privateMethods: {\n _addUnmountHandler: (cb: Function) => {\n componentAPI.onUnmount(cb);\n },\n _callMountHandlers: () => {\n componentMountCbs.forEach((cb) => cb());\n },\n _callUnmountHandlers: () => {\n componentUnmountCbs.forEach((cb) => cb());\n // this should trigger recursive checks, whether it is a VelesNode or VelesComponent\n // string Nodes don't have lifecycle handlers\n if (\"_privateMethods\" in velesComponent.tree) {\n velesComponent.tree._privateMethods._callUnmountHandlers();\n }\n },\n },\n };\n\n return velesComponent;\n}\n\nexport { parseComponent };\n","import { parseChildren } from \"./parse-children\";\nimport { assignAttributes } from \"./assign-attributes\";\nimport { parseComponent } from \"./parse-component\";\n\nimport type {\n VelesComponent,\n VelesElement,\n VelesElementProps,\n ComponentFunction,\n} from \"../types\";\n\nfunction createElement(\n element: string | ComponentFunction,\n props: VelesElementProps = {}\n): VelesElement | VelesComponent {\n if (typeof element === \"string\") {\n const { children, ref, phantom = false, ...otherProps } = props;\n const newElement = document.createElement(element);\n const velesNode = {} as VelesElement;\n\n if (ref?.velesRef) {\n ref.current = newElement;\n }\n\n const childComponents = parseChildren({\n children,\n htmlElement: newElement,\n velesNode,\n });\n\n // these handlers are attached directly to the DOM element\n // specifically, the top level node which is rendered after\n // using `useValue` function and also listeners from\n // `useAttribute`\n let unmountHandlers: Function[] = [];\n const callUnmountHandlers = () => {\n unmountHandlers.forEach((cb) => cb());\n unmountHandlers = [];\n\n childComponents.forEach((childComponent) => {\n childComponent._privateMethods._callUnmountHandlers();\n });\n };\n\n velesNode.html = newElement;\n velesNode.velesNode = true;\n velesNode.childComponents = childComponents;\n velesNode.phantom = phantom;\n velesNode._privateMethods = {\n _addUnmountHandler: (cb: Function) => {\n unmountHandlers.push(cb);\n },\n _callUnmountHandlers: callUnmountHandlers,\n };\n\n // assign all the DOM attributes, including event listeners\n assignAttributes({ props: otherProps, htmlElement: newElement, velesNode });\n\n return velesNode;\n\n // functions mean that we want to render another component\n } else if (typeof element === \"function\") {\n return parseComponent({ element, props });\n }\n\n // otherwise we use the API wrong, so we throw an error\n throw new Error(\n \"Veles createElement expects a valid DOM string or another component\"\n );\n}\n\nexport { createElement };\n","import { createElement } from \"./create-element\";\n\nimport type { VelesChildren } from \"./types\";\n\nfunction Fragment({ children }: { children: VelesChildren }) {\n return createElement(\"div\", {\n phantom: true,\n children,\n });\n}\n\nexport { Fragment };\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEA,SAAS,sBACP,WAIA;AACA,QAAM,iBAAmC,CAAC;AAE1C,MAAI,wBAAwB,WAAW;AACrC,WAAO;AAAA,MACL,kBAAkB;AAAA,MAClB,gBAAgB,CAAC;AAAA,IACnB;AAAA,EACF;AAEA,MAAI,YAA2C;AAG/C,SAAO,oBAAoB,WAAW;AACpC,mBAAe,KAAK,SAAS;AAC7B,QAAI,wBAAwB,UAAU,MAAM;AAC1C,aAAO;AAAA,QACL,kBAAkB,UAAU;AAAA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AE1E,WAAO;AAAA,EAGT,WAAW,OAAO,YAAY,YAAY;AACxC,WAAO,eAAe,EAAE,SAAS,MAAM,CAAC;AAAA,EAC1C;AAGA,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACF;;;ACjEA,SAAS,SAAS,EAAE,SAAS,GAAgC;AAC3D,SAAO,cAAc,OAAO;AAAA,IAC1B,SAAS;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":["componentsTree","velesElementNode"]}
|
|
1
|
+
{"version":3,"sources":["../src/jsx-runtime.ts","../src/utils.ts","../src/create-element/parse-children.ts","../src/create-element/assign-attributes.ts","../src/hooks/lifecycle.ts","../src/create-element/parse-component.ts","../src/create-element/create-element.ts","../src/fragment.ts"],"sourcesContent":["export {\n createElement as jsx,\n createElement as jsxs,\n createElement as jsxDEV,\n} from \"./create-element\";\nexport { Fragment } from \"./fragment\";\n","import type { VelesComponent, VelesElement, VelesStringElement } from \"./types\";\n\nfunction getComponentVelesNode(\n component: VelesComponent | VelesElement | VelesStringElement\n): {\n velesElementNode: VelesElement | VelesStringElement;\n componentsTree: VelesComponent[];\n} {\n const componentsTree: VelesComponent[] = [];\n\n if (\"velesStringElement\" in component) {\n return {\n velesElementNode: component,\n componentsTree: [],\n };\n }\n\n let childNode: VelesComponent | VelesElement = component;\n // we can have multiple components nested, we need to get\n // to the actual HTML to attach it\n while (\"velesComponent\" in childNode) {\n componentsTree.push(childNode);\n if (\"velesStringElement\" in childNode.tree) {\n return {\n velesElementNode: childNode.tree,\n componentsTree,\n };\n } else {\n childNode = childNode.tree;\n }\n }\n\n return { velesElementNode: childNode, componentsTree };\n}\n\nfunction identity<T>(value1: T, value2: T) {\n return value1 === value2;\n}\n\nexport { getComponentVelesNode, identity };\n","import { getComponentVelesNode } from \"../utils\";\n\nimport type {\n VelesComponent,\n VelesElement,\n VelesStringElement,\n VelesElementProps,\n} from \"../types\";\n\nfunction parseChildren({\n children,\n htmlElement,\n velesNode,\n}: {\n children: VelesElementProps[\"children\"];\n htmlElement: HTMLElement;\n velesNode: VelesElement;\n}) {\n const childComponents: (\n | VelesElement\n | VelesComponent\n | VelesStringElement\n )[] = [];\n\n if (children === undefined || children === null) {\n return childComponents;\n }\n\n (Array.isArray(children) ? children : [children]).forEach(\n (childComponent) => {\n if (typeof childComponent === \"string\") {\n const text = document.createTextNode(childComponent);\n htmlElement.append(text);\n } else if (\n typeof childComponent === \"object\" &&\n childComponent &&\n \"velesNode\" in childComponent &&\n childComponent?.velesNode\n ) {\n if (childComponent.phantom) {\n // we need to get ALL the children of it and attach it to this node\n childComponent.childComponents.forEach((childComponentofPhantom) => {\n if (\"velesNode\" in childComponentofPhantom) {\n htmlElement.append(childComponentofPhantom.html);\n childComponentofPhantom.parentVelesElement = velesNode;\n } else {\n const { componentsTree, velesElementNode } =\n getComponentVelesNode(childComponentofPhantom);\n\n if (!velesElementNode) {\n console.error(\"can't find HTML tree in a component chain\");\n } else {\n htmlElement.append(velesElementNode.html);\n\n // TODO: address the same concern as below\n componentsTree.forEach((component) => {\n component._privateMethods._callMountHandlers();\n });\n\n velesElementNode.parentVelesElement = velesNode;\n }\n }\n });\n childComponent.parentVelesElement = velesNode;\n childComponents.push(childComponent);\n } else {\n // TODO: check that it is a valid DOM Node\n htmlElement.append(childComponent.html);\n childComponent.parentVelesElement = velesNode;\n childComponents.push(childComponent);\n }\n } else if (\n typeof childComponent === \"object\" &&\n childComponent &&\n \"velesComponent\" in childComponent &&\n childComponent?.velesComponent\n ) {\n // we need to save the whole components chain, so that\n // we can trigger `mount` hooks on all of them correctly\n const { componentsTree, velesElementNode } =\n getComponentVelesNode(childComponent);\n\n if (!velesElementNode) {\n console.error(\"can't find HTML tree in a component chain\");\n } else {\n if (\"velesNode\" in velesElementNode && velesElementNode.phantom) {\n // we need to get ALL the children of it and attach it to this node\n velesElementNode.childComponents.forEach(\n (childComponentofPhantom) => {\n if (\"velesNode\" in childComponentofPhantom) {\n htmlElement.append(childComponentofPhantom.html);\n childComponentofPhantom.parentVelesElement = velesNode;\n } else {\n const { componentsTree, velesElementNode } =\n getComponentVelesNode(childComponentofPhantom);\n\n if (!velesElementNode) {\n console.error(\"can't find HTML tree in a component chain\");\n } else {\n htmlElement.append(velesElementNode.html);\n\n // Same explanation as below. Components are mounted synchronously\n setTimeout(() => {\n componentsTree.forEach((component) => {\n component._privateMethods._callMountHandlers();\n });\n }, 0);\n velesElementNode.parentVelesElement = velesNode;\n }\n }\n }\n );\n } else {\n htmlElement.append(velesElementNode.html);\n }\n\n /**\n * Components are mounted synchronously, so we can safely wait for the next\n * CPU tick and be sure that new markup is attached to DOM.\n */\n setTimeout(() => {\n componentsTree.forEach((component) => {\n component._privateMethods._callMountHandlers();\n });\n }, 0);\n velesElementNode.parentVelesElement = velesNode;\n childComponents.push(childComponent);\n }\n } else if (\n typeof childComponent === \"object\" &&\n childComponent &&\n \"velesStringElement\" in childComponent &&\n childComponent?.velesStringElement\n ) {\n // TODO: check that it is a valid DOM Node\n htmlElement.append(childComponent.html);\n childComponent.parentVelesElement = velesNode;\n childComponents.push(childComponent);\n }\n }\n );\n\n return childComponents;\n}\n\nexport { parseChildren };\n","import type { VelesElement } from \"../types\";\n\nfunction assignAttributes({\n props,\n htmlElement,\n velesNode,\n}: {\n props: Record<string, any>;\n htmlElement: HTMLElement;\n velesNode: VelesElement;\n}) {\n Object.entries(props).forEach(([key, value]) => {\n const isFunction = typeof value === \"function\";\n if (isFunction && value.velesAttribute === true) {\n const attributeValue = value(htmlElement, key, velesNode);\n htmlElement.setAttribute(key, attributeValue);\n } else if (\n // basically, any form of `on` handlers, like `onClick`, `onCopy`, etc\n isFunction &&\n key.length > 2 &&\n key.startsWith(\"on\")\n ) {\n // TODO: think if this is robust enough\n htmlElement.addEventListener(\n key[2].toLocaleLowerCase() + key.slice(3),\n value\n );\n } else {\n htmlElement.setAttribute(key, value);\n }\n });\n}\n\nexport { assignAttributes };\n","import { ComponentAPI } from \"../types\";\n\n// lifecycle hooks\n// currently, all components need to be synchronous\n// so we execute them and set background context\n// since components can be nested, we need to keep the array\nconst contextStack: ComponentAPI[] = [];\n// all hooks need to know the current context\n// it should way more convenient this way compared to passing\n// `componentAPI` to every method\nlet currentContext: ComponentAPI | null = null;\n\nfunction addContext(newContext: ComponentAPI) {\n contextStack.push(newContext);\n currentContext = newContext;\n}\n\nfunction popContext() {\n contextStack.pop();\n currentContext = contextStack[contextStack.length - 1];\n}\n\nfunction onMount(cb: Function) {\n if (currentContext) {\n currentContext.onMount(cb);\n } else {\n console.error(\"missing current context\");\n }\n}\n\nfunction onUnmount(cb: Function) {\n if (currentContext) {\n currentContext.onUnmount(cb);\n } else {\n console.error(\"missing current context\");\n }\n}\n\nexport { addContext, popContext, onMount, onUnmount };\n","import { addContext, popContext } from \"../hooks/lifecycle\";\n\nimport type {\n VelesComponent,\n VelesStringElement,\n VelesElementProps,\n ComponentAPI,\n ComponentFunction,\n} from \"../types\";\n\nfunction parseComponent({\n element,\n props,\n}: {\n element: ComponentFunction;\n props: VelesElementProps;\n}) {\n const componentUnmountCbs: Function[] = [];\n const componentMountCbs: Function[] = [];\n const componentAPI: ComponentAPI = {\n onMount: (cb) => {\n componentMountCbs.push(cb);\n },\n onUnmount: (cb) => {\n componentUnmountCbs.push(cb);\n },\n };\n // at this moment we enter new context\n addContext(componentAPI);\n const _componentTree = element(props, componentAPI);\n\n const componentTree =\n typeof _componentTree === \"string\" || !_componentTree\n ? ({\n velesStringElement: true,\n html: document.createTextNode(\n typeof _componentTree === \"string\" ? _componentTree : \"\"\n ),\n } as VelesStringElement)\n : _componentTree;\n\n // here we exit our context\n popContext();\n const velesComponent: VelesComponent = {\n velesComponent: true,\n tree: componentTree,\n _privateMethods: {\n _addUnmountHandler: (cb: Function) => {\n componentAPI.onUnmount(cb);\n },\n _callMountHandlers: () => {\n componentMountCbs.forEach((cb) => cb());\n },\n _callUnmountHandlers: () => {\n componentUnmountCbs.forEach((cb) => cb());\n // this should trigger recursive checks, whether it is a VelesNode or VelesComponent\n // string Nodes don't have lifecycle handlers\n if (\"_privateMethods\" in velesComponent.tree) {\n velesComponent.tree._privateMethods._callUnmountHandlers();\n }\n },\n },\n };\n\n return velesComponent;\n}\n\nexport { parseComponent };\n","import { parseChildren } from \"./parse-children\";\nimport { assignAttributes } from \"./assign-attributes\";\nimport { parseComponent } from \"./parse-component\";\n\nimport type {\n VelesComponent,\n VelesElement,\n VelesElementProps,\n ComponentFunction,\n} from \"../types\";\n\nfunction createElement(\n element: string | ComponentFunction,\n props: VelesElementProps = {}\n): VelesElement | VelesComponent {\n if (typeof element === \"string\") {\n const { children, ref, phantom = false, ...otherProps } = props;\n const newElement = document.createElement(element);\n const velesNode = {} as VelesElement;\n\n if (ref?.velesRef) {\n ref.current = newElement;\n }\n\n const childComponents = parseChildren({\n children,\n htmlElement: newElement,\n velesNode,\n });\n\n // these handlers are attached directly to the DOM element\n // specifically, the top level node which is rendered after\n // using `useValue` function and also listeners from\n // `useAttribute`\n let unmountHandlers: Function[] = [];\n const callUnmountHandlers = () => {\n unmountHandlers.forEach((cb) => cb());\n unmountHandlers = [];\n\n childComponents.forEach((childComponent) => {\n childComponent._privateMethods._callUnmountHandlers();\n });\n };\n\n velesNode.html = newElement;\n velesNode.velesNode = true;\n velesNode.childComponents = childComponents;\n velesNode.phantom = phantom;\n velesNode._privateMethods = {\n _addUnmountHandler: (cb: Function) => {\n unmountHandlers.push(cb);\n },\n _callUnmountHandlers: callUnmountHandlers,\n };\n\n // assign all the DOM attributes, including event listeners\n assignAttributes({ props: otherProps, htmlElement: newElement, velesNode });\n\n return velesNode;\n\n // functions mean that we want to render another component\n } else if (typeof element === \"function\") {\n return parseComponent({ element, props });\n }\n\n // otherwise we use the API wrong, so we throw an error\n throw new Error(\n \"Veles createElement expects a valid DOM string or another component\"\n );\n}\n\nexport { createElement };\n","import { createElement } from \"./create-element\";\n\nimport type { VelesChildren } from \"./types\";\n\nfunction Fragment({ children }: { children: VelesChildren }) {\n return createElement(\"div\", {\n phantom: true,\n children,\n });\n}\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Q,CAAC,OAAO,GAAG,CAAC;AACpC,wBAAkB,CAAC;AAEnB,sBAAgB,QAAQ,CAAC,mBAAmB;AAC1C,uBAAe,gBAAgB,qBAAqB;AAAA,MACtD,CAAC;AAAA,IACH;AAEA,cAAU,OAAO;AACjB,cAAU,YAAY;AACtB,cAAU,kBAAkB;AAC5B,cAAU,UAAU;AACpB,cAAU,kBAAkB;AAAA,MAC1B,oBAAoB,CAAC,OAAiB;AACpC,wBAAgB,KAAK,EAAE;AAAA,MACzB;AAAA,MACA,sBAAsB;AAAA,IACxB;AAGA,qBAAiB,EAAE,OAAO,YAAY,aAAa,YAAY,UAAU,CAAC;AAE1E,WAAO;AAAA,EAGT,WAAW,OAAO,YAAY,YAAY;AACxC,WAAO,eAAe,EAAE,SAAS,MAAM,CAAC;AAAA,EAC1C;AAGA,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACF;;;ACjEA,SAAS,SAAS,EAAE,SAAS,GAAgC;AAC3D,SAAO,cAAc,OAAO;AAAA,IAC1B,SAAS;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":["componentsTree","velesElementNode"]}
|
package/dist/jsx-runtime.d.cts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { F as Fragment, c as jsx } from './jsx-runtime-vDysOz5d.cjs';
|
|
1
|
+
export { F as Fragment, c as jsx, c as jsxDEV, c as jsxs } from './jsx-runtime-vDysOz5d.cjs';
|
package/dist/jsx-runtime.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { F as Fragment, c as jsx } from './jsx-runtime-vDysOz5d.js';
|
|
1
|
+
export { F as Fragment, c as jsx, c as jsxDEV, c as jsxs } from './jsx-runtime-vDysOz5d.js';
|
package/dist/jsx-runtime.js
CHANGED
package/package.json
CHANGED