nesquick 0.0.16 → 0.0.18

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.
@@ -10,6 +10,10 @@ function functionizeProps(props) {
10
10
  }
11
11
  }
12
12
  }
13
+ const SVGNamespaces = new Map([
14
+ ["xlink", "http://www.w3.org/1999/xlink"],
15
+ ["xml", "http://www.w3.org/XML/1998/namespace"]
16
+ ]);
13
17
  class NesquickComponent {
14
18
  constructor(_render, props) {
15
19
  this._render = _render;
@@ -25,34 +29,106 @@ class NesquickComponent {
25
29
  functionizeProps(this.props);
26
30
  const element = this._render(this.props);
27
31
  if (this._xmlns) {
28
- element.inheritXmlns(this._xmlns);
32
+ element.setXmlns(this._xmlns);
29
33
  }
30
34
  const res = element.render(document);
31
35
  State_1.subscriptions.reset();
32
36
  return res;
33
37
  }
34
- if (this.props?.xmlns != null) {
35
- if (typeof this.props.xmlns === "function") {
36
- this._xmlns = this.props.xmlns();
38
+ if (this.props.xmlns != null) {
39
+ let namespace = typeof this.props.xmlns === "function" ? this.props.xmlns() : this.props.xmlns;
40
+ if (namespace != null) {
41
+ namespace = String(namespace);
42
+ this._xmlns = {
43
+ ns: namespace,
44
+ attributes: namespace === "http://www.w3.org/2000/svg" ? SVGNamespaces : null
45
+ };
37
46
  }
38
- else {
39
- this._xmlns = this.props.xmlns;
47
+ }
48
+ else if (this._render === "svg") {
49
+ this._xmlns = {
50
+ ns: "http://www.w3.org/2000/svg",
51
+ attributes: SVGNamespaces
52
+ };
53
+ }
54
+ let element;
55
+ if (this._xmlns?.ns) {
56
+ element = document.createElementNS(this._xmlns.ns, this._render);
57
+ if (this.props != null) {
58
+ if (this._xmlns.attributes) {
59
+ this._renderPropsNs(this._xmlns.attributes, element, this.props);
60
+ }
61
+ else {
62
+ this._renderProps(element, this.props);
63
+ }
40
64
  }
41
65
  }
42
- const element = this._xmlns ? document.createElementNS(this._xmlns, this._render) : document.createElement(this._render);
43
- if (this.props != null) {
44
- this._renderProps(element, this.props);
66
+ else {
67
+ element = document.createElement(this._render);
68
+ if (this.props != null) {
69
+ this._renderProps(element, this.props);
70
+ }
45
71
  }
46
72
  this._renderChildren(document, element, this.props.children);
73
+ if (this.props.ref != null) {
74
+ this.props.ref(element);
75
+ }
47
76
  this.props = {};
48
77
  State_1.subscriptions.reset();
49
78
  return element;
50
79
  }
51
- inheritXmlns(xmlns) {
52
- if (this.props?.xmlns != null) {
53
- this._xmlns = xmlns;
80
+ setXmlns(xmlns) {
81
+ this._xmlns = xmlns;
82
+ }
83
+ _getAttributeNs(attributes, k) {
84
+ const index = k.indexOf(":");
85
+ if (index > -1) {
86
+ const ns = k.substring(0, index);
87
+ const name = k.substring(index + 1);
88
+ const namespace = attributes.get(ns);
89
+ if (namespace != null) {
90
+ return {
91
+ namespace: namespace,
92
+ name: name
93
+ };
94
+ }
95
+ }
96
+ return null;
97
+ }
98
+ _renderPropsNs(attributes, element, props) {
99
+ for (const k in props) {
100
+ if (k !== "children" && k !== "xmlns") {
101
+ if (typeof props[k] === "function") {
102
+ if (k.startsWith("on")) {
103
+ element[k.toLowerCase()] = props[k];
104
+ }
105
+ else {
106
+ const attribute = this._getAttributeNs(attributes, k);
107
+ if (attribute) {
108
+ (0, State_1.useRender)(props[k], v => {
109
+ element.setAttributeNS(attribute.namespace, attribute.name, String(v));
110
+ });
111
+ }
112
+ else {
113
+ (0, State_1.useRender)(props[k], v => {
114
+ element.setAttribute(k, String(v));
115
+ });
116
+ }
117
+ }
118
+ }
119
+ else {
120
+ const attribute = this._getAttributeNs(attributes, k);
121
+ if (attribute) {
122
+ element.setAttributeNS(attribute.namespace, attribute.name, String(props[k]));
123
+ }
124
+ else {
125
+ element.setAttribute(k, String(props[k]));
126
+ }
127
+ }
128
+ }
54
129
  }
55
130
  }
131
+ ;
56
132
  _renderProps(element, props) {
57
133
  for (const k in props) {
58
134
  if (k !== "children" && k !== "xmlns") {
@@ -155,7 +231,7 @@ class NesquickComponent {
155
231
  nesquickChild.component = null;
156
232
  nesquickChild.fragment = Array.isArray(child) ? new NesquickFragment_1.NesquickFragment(child) : child;
157
233
  if (this._xmlns) {
158
- nesquickChild.fragment.inheritXmlns(this._xmlns);
234
+ nesquickChild.fragment.setXmlns(this._xmlns);
159
235
  }
160
236
  const node = nesquickChild.fragment.render(document);
161
237
  const lastChild = node.lastChild;
@@ -171,7 +247,7 @@ class NesquickComponent {
171
247
  nesquickChild.component = child;
172
248
  nesquickChild.fragment = null;
173
249
  if (this._xmlns) {
174
- child.inheritXmlns(this._xmlns);
250
+ child.setXmlns(this._xmlns);
175
251
  }
176
252
  const node = child.render(document);
177
253
  if (nesquickChild.node) {
@@ -18,6 +18,10 @@ export type NesquickParent = {
18
18
  replaceChild(newChild: Node, oldChild: Node): void;
19
19
  insertBefore(node: Node, child: Node | null): void;
20
20
  };
21
+ type XmlNs = {
22
+ ns: string;
23
+ attributes: Map<string, string> | null;
24
+ };
21
25
  export declare class NesquickComponent<P extends ComponentProps = {}> {
22
26
  private _render;
23
27
  protected props: P;
@@ -26,7 +30,9 @@ export declare class NesquickComponent<P extends ComponentProps = {}> {
26
30
  protected _children: NesquickChild[];
27
31
  constructor(_render: string | FunctionComponent<P>, props: P);
28
32
  render(document: VeactDocument): Node;
29
- inheritXmlns(xmlns: string | null): void;
33
+ setXmlns(xmlns: XmlNs | null): void;
34
+ private _getAttributeNs;
35
+ private _renderPropsNs;
30
36
  private _renderProps;
31
37
  protected _renderChildren(document: VeactDocument, parent: NesquickParent, children?: Children): void;
32
38
  protected _pushChild(): NesquickChild;
@@ -34,6 +34,7 @@ export declare namespace JSX {
34
34
  [k: string]: any;
35
35
  style?: StyleProps;
36
36
  xmlns?: string;
37
+ ref?: (el: T) => void;
37
38
  }
38
39
  export type StyleProps = {
39
40
  [K in keyof CSSStyleDeclaration]?: CSSStyleDeclaration[K] extends Function ? never : CSSStyleDeclaration[K] | (() => CSSStyleDeclaration[K]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nesquick",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "React-like library with focus on drawing performance",
5
5
  "types": "./lib/types",
6
6
  "main": "./lib",