@thi.ng/rdom 0.9.20 → 0.10.1

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2022-11-23T22:46:54Z
3
+ - **Last updated**: 2022-12-10T14:04:38Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,18 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [0.10.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/rdom@0.10.0) (2022-11-30)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add DOM comment support ([#367](https://github.com/thi-ng/umbrella/issues/367)), other refactorings ([3fd5f8e](https://github.com/thi-ng/umbrella/commit/3fd5f8e))
17
+ - add $comment(), isComment()
18
+ - add Component.$comment() syntax sugar
19
+ - add comment check/branch in $tree()
20
+ - update args for $addChild(), $remove(), $moveTo()
21
+ - update $text(), $html() to support SVG elements
22
+ - add doc strings
23
+
12
24
  ## [0.9.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/rdom@0.9.0) (2022-07-12)
13
25
 
14
26
  #### 🚀 Features
package/README.md CHANGED
@@ -173,7 +173,7 @@ node --experimental-repl-await
173
173
  > const rdom = await import("@thi.ng/rdom");
174
174
  ```
175
175
 
176
- Package sizes (gzipped, pre-treeshake): ESM: 4.08 KB
176
+ Package sizes (brotli'd, pre-treeshake): ESM: 3.81 KB
177
177
 
178
178
  ## Dependencies
179
179
 
package/checks.d.ts CHANGED
@@ -1,3 +1,19 @@
1
1
  import type { IComponent } from "./api.js";
2
+ /**
3
+ * Returns true if given hiccup component describes a comment node. I.e. is of
4
+ * the form `[COMMENT, "foo"...]`.
5
+ *
6
+ * @remarks
7
+ * See thi.ng/hiccup docs for reference:
8
+ * - https://docs.thi.ng/umbrella/hiccup/functions/serialize.html
9
+ *
10
+ * @param tree
11
+ */
12
+ export declare const isComment: (tree: any[]) => boolean;
13
+ /**
14
+ * Returns true, if given value has a {@link IComponent.mount} function.
15
+ *
16
+ * @param x
17
+ */
2
18
  export declare const isComponent: (x: any) => x is IComponent<any>;
3
19
  //# sourceMappingURL=checks.d.ts.map
package/checks.js CHANGED
@@ -1,2 +1,19 @@
1
1
  import { implementsFunction } from "@thi.ng/checks/implements-function";
2
+ import { COMMENT } from "@thi.ng/hiccup/api";
3
+ /**
4
+ * Returns true if given hiccup component describes a comment node. I.e. is of
5
+ * the form `[COMMENT, "foo"...]`.
6
+ *
7
+ * @remarks
8
+ * See thi.ng/hiccup docs for reference:
9
+ * - https://docs.thi.ng/umbrella/hiccup/functions/serialize.html
10
+ *
11
+ * @param tree
12
+ */
13
+ export const isComment = (tree) => tree[0] === COMMENT;
14
+ /**
15
+ * Returns true, if given value has a {@link IComponent.mount} function.
16
+ *
17
+ * @param x
18
+ */
2
19
  export const isComponent = (x) => implementsFunction(x, "mount");
package/component.d.ts CHANGED
@@ -10,15 +10,102 @@ export declare abstract class Component<T = any> implements IComponent<T> {
10
10
  abstract mount(parent: Element, index?: NumOrElement, ...xs: any[]): Promise<Element>;
11
11
  unmount(): Promise<void>;
12
12
  update(state?: T): void;
13
+ /**
14
+ * Syntax sugar for {@link $el}, using this component's
15
+ * {@link IComponent.el} as default `parent`.
16
+ *
17
+ * @param tag
18
+ * @param attribs
19
+ * @param body
20
+ * @param parent
21
+ * @param idx
22
+ */
13
23
  $el(tag: string, attribs?: any, body?: any, parent?: Element | undefined, idx?: NumOrElement): Element;
24
+ /**
25
+ * Syntax sugar for {@link $comment}, creates a new comment DOM node using
26
+ * this component's {@link IComponent.el} as default `parent`.
27
+ *
28
+ * @param body
29
+ * @param parent
30
+ * @param idx
31
+ */
32
+ $comment(body: string | string[], parent?: Element | undefined, idx?: NumOrElement): Comment;
33
+ /**
34
+ * Syntax sugar for {@link $clear}, using this component's
35
+ * {@link IComponent.el} as default element to clear.
36
+ *
37
+ * @param el
38
+ */
14
39
  $clear(el?: Element): Element;
40
+ /**
41
+ * Same as {@link $compile}.
42
+ *
43
+ * @param tree
44
+ */
15
45
  $compile(tree: any): IComponent<any>;
46
+ /**
47
+ * Same as {@link $tree}.
48
+ *
49
+ * @param tree
50
+ * @param root
51
+ * @param index
52
+ */
16
53
  $tree(tree: any, root?: Element, index?: NumOrElement): Promise<any>;
17
- $text(body: any): void;
18
- $html(body: MaybeDeref<string>): void;
54
+ /**
55
+ * Syntax sugar for {@link $text}, using this component's
56
+ * {@link IComponent.el} as default element to edit.
57
+ *
58
+ * @remarks
59
+ * If using the default element, assumes `this.el` is an existing
60
+ * `HTMLElement`.
61
+ *
62
+ * @param body
63
+ * @param el
64
+ */
65
+ $text(body: any, el?: HTMLElement): void;
66
+ /**
67
+ * Syntax sugar for {@link $html}, using this component's
68
+ * {@link IComponent.el} as default element to edit.
69
+ *
70
+ * @remarks
71
+ * If using the default element, assumes `this.el` is an existing
72
+ * `HTMLElement` or `SVGElement`.
73
+ *
74
+ * @param body
75
+ * @param el
76
+ */
77
+ $html(body: MaybeDeref<string>, el?: HTMLElement | SVGElement): void;
78
+ /**
79
+ * Syntax sugar for {@link $attribs}, using this component's
80
+ * {@link IComponent.el} as default element to edit.
81
+ *
82
+ * @param attribs
83
+ * @param el
84
+ */
19
85
  $attribs(attribs: any, el?: Element): void;
86
+ /**
87
+ * Syntax sugar for {@link $style}, using this component's
88
+ * {@link IComponent.el} as default element to edit.
89
+ *
90
+ * @param rules
91
+ * @param el
92
+ */
20
93
  $style(rules: any, el?: Element): void;
94
+ /**
95
+ * Syntax sugar for {@link $remove}, using this component's
96
+ * {@link IComponent.el} as default element to remove.
97
+ *
98
+ * @param el
99
+ */
21
100
  $remove(el?: Element): void;
101
+ /**
102
+ * Syntax sugar for {@link $moveTo}, using this component's
103
+ * {@link IComponent.el} as default element to migrate.
104
+ *
105
+ * @param newParent
106
+ * @param el
107
+ * @param idx
108
+ */
22
109
  $moveTo(newParent: Element, el?: Element, idx?: NumOrElement): void;
23
110
  }
24
111
  //# sourceMappingURL=component.d.ts.map
package/component.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { $compile } from "./compile.js";
2
- import { $attribs, $clear, $el, $html, $moveTo, $remove, $style, $text, $tree, } from "./dom.js";
2
+ import { $attribs, $clear, $comment, $el, $html, $moveTo, $remove, $style, $text, $tree, } from "./dom.js";
3
3
  /**
4
4
  * Abstract base class / {@link IComponent} implementation. Provides
5
5
  * additional convenience methods for DOM element creation &
@@ -12,33 +12,122 @@ export class Component {
12
12
  }
13
13
  // @ts-ignore args
14
14
  update(state) { }
15
+ /**
16
+ * Syntax sugar for {@link $el}, using this component's
17
+ * {@link IComponent.el} as default `parent`.
18
+ *
19
+ * @param tag
20
+ * @param attribs
21
+ * @param body
22
+ * @param parent
23
+ * @param idx
24
+ */
15
25
  $el(tag, attribs, body, parent = this.el, idx) {
16
26
  return $el(tag, attribs, body, parent, idx);
17
27
  }
28
+ /**
29
+ * Syntax sugar for {@link $comment}, creates a new comment DOM node using
30
+ * this component's {@link IComponent.el} as default `parent`.
31
+ *
32
+ * @param body
33
+ * @param parent
34
+ * @param idx
35
+ */
36
+ $comment(body, parent = this.el, idx) {
37
+ return $comment(body, parent, idx);
38
+ }
39
+ /**
40
+ * Syntax sugar for {@link $clear}, using this component's
41
+ * {@link IComponent.el} as default element to clear.
42
+ *
43
+ * @param el
44
+ */
18
45
  $clear(el = this.el) {
19
46
  return $clear(el);
20
47
  }
48
+ /**
49
+ * Same as {@link $compile}.
50
+ *
51
+ * @param tree
52
+ */
21
53
  $compile(tree) {
22
54
  return $compile(tree);
23
55
  }
56
+ /**
57
+ * Same as {@link $tree}.
58
+ *
59
+ * @param tree
60
+ * @param root
61
+ * @param index
62
+ */
24
63
  $tree(tree, root = this.el, index) {
25
64
  return $tree(tree, root, index);
26
65
  }
27
- $text(body) {
28
- this.el && $text(this.el, body);
66
+ /**
67
+ * Syntax sugar for {@link $text}, using this component's
68
+ * {@link IComponent.el} as default element to edit.
69
+ *
70
+ * @remarks
71
+ * If using the default element, assumes `this.el` is an existing
72
+ * `HTMLElement`.
73
+ *
74
+ * @param body
75
+ * @param el
76
+ */
77
+ $text(body, el = this.el) {
78
+ $text(el, body);
29
79
  }
30
- $html(body) {
31
- this.el && $html(this.el, body);
80
+ /**
81
+ * Syntax sugar for {@link $html}, using this component's
82
+ * {@link IComponent.el} as default element to edit.
83
+ *
84
+ * @remarks
85
+ * If using the default element, assumes `this.el` is an existing
86
+ * `HTMLElement` or `SVGElement`.
87
+ *
88
+ * @param body
89
+ * @param el
90
+ */
91
+ $html(body, el = this.el) {
92
+ $html(el, body);
32
93
  }
94
+ /**
95
+ * Syntax sugar for {@link $attribs}, using this component's
96
+ * {@link IComponent.el} as default element to edit.
97
+ *
98
+ * @param attribs
99
+ * @param el
100
+ */
33
101
  $attribs(attribs, el = this.el) {
34
102
  $attribs(el, attribs);
35
103
  }
104
+ /**
105
+ * Syntax sugar for {@link $style}, using this component's
106
+ * {@link IComponent.el} as default element to edit.
107
+ *
108
+ * @param rules
109
+ * @param el
110
+ */
36
111
  $style(rules, el = this.el) {
37
112
  $style(el, rules);
38
113
  }
114
+ /**
115
+ * Syntax sugar for {@link $remove}, using this component's
116
+ * {@link IComponent.el} as default element to remove.
117
+ *
118
+ * @param el
119
+ */
39
120
  $remove(el = this.el) {
40
121
  $remove(el);
41
122
  }
123
+ /**
124
+ * Syntax sugar for {@link $moveTo}, using this component's
125
+ * {@link IComponent.el} as default element to migrate.
126
+ *
127
+ * @param newParent
128
+ * @param el
129
+ * @param idx
130
+ */
42
131
  $moveTo(newParent, el = this.el, idx) {
43
132
  $moveTo(newParent, el, idx);
44
133
  }
package/dom.d.ts CHANGED
@@ -1,22 +1,26 @@
1
1
  import { MaybeDeref } from "@thi.ng/api/deref";
2
2
  import type { NumOrElement } from "./api.js";
3
3
  /**
4
- * hdom-style DOM tree creation from hiccup format. Returns DOM element
5
- * of `tree` root. See {@link $el} for further details.
4
+ * hdom-style DOM tree creation from hiccup format. Returns DOM element of
5
+ * `tree` root. See {@link $el} for further details.
6
6
  *
7
7
  * @remarks
8
8
  * Supports elements given in these forms:
9
9
  *
10
10
  * - {@link IComponent} instance
11
- * - {@link IDeref} instance (must resolve to another supported type in
12
- * this list)
11
+ * - {@link IDeref} instance (must resolve to another supported type in this
12
+ * list)
13
13
  * - `["div#id.class", {...attribs}, ...children]`
14
+ * - `[COMMENT, "foo", "bar"...]` (DOM comment node)
14
15
  * - `[IComponent, ...mountargs]`
15
16
  * - `[function, ...args]`
16
17
  * - ES6 iterable of the above (for child values only!)
17
18
  *
18
- * Any other values will be cast to strings and added as spans to
19
- * current `parent`.
19
+ * Any other values will be cast to strings and added as spans to current
20
+ * `parent`.
21
+ *
22
+ * Note: `COMMENT` is defined as constant in thi.ng/hiccup package. Also see
23
+ * {@link $comment} function to create comments directly.
20
24
  *
21
25
  * @param tree -
22
26
  * @param parent -
@@ -41,9 +45,50 @@ export declare const $tree: (tree: any, parent: Element, idx?: NumOrElement) =>
41
45
  * @param idx -
42
46
  */
43
47
  export declare const $el: (tag: string, attribs: any, body?: any, parent?: Element, idx?: NumOrElement) => Element;
44
- export declare const $addChild: (parent: Element, child: Element, idx?: NumOrElement) => void;
45
- export declare const $remove: (el: Element) => void;
46
- export declare const $moveTo: (newParent: Element, el: Element, idx?: NumOrElement) => void;
48
+ /**
49
+ * Similar to {@link $el}, but creates a new comment DOM node using provided
50
+ * body. If `parent` is given, the comment will be attached or inserted as child
51
+ * at `idx`. Returns comment node.
52
+ *
53
+ * @remarks
54
+ * See thi.ng/hiccup docs for reference:
55
+ * - https://docs.thi.ng/umbrella/hiccup/functions/serialize.html
56
+ *
57
+ * @param body
58
+ * @param parent
59
+ * @param idx
60
+ */
61
+ export declare const $comment: (body: string | string[], parent?: Element, idx?: NumOrElement) => Comment;
62
+ /**
63
+ * Appends or inserts `child` as child element of `parent`. The default `idx` of
64
+ * -1 means the child will be appended, else uses `parent.insertBefore()` to
65
+ * insert at given index.
66
+ *
67
+ * @param parent
68
+ * @param child
69
+ * @param idx
70
+ */
71
+ export declare const $addChild: (parent: Element, child: Element | Comment, idx?: NumOrElement) => void;
72
+ /**
73
+ * Removes given element or comment from the DOM.
74
+ *
75
+ * @param el
76
+ */
77
+ export declare const $remove: (el: Element | Comment) => void;
78
+ /**
79
+ * Migrates given element to `newParent`, following the same append or insertion
80
+ * logic as {@link $addChild}.
81
+ *
82
+ * @param newParent
83
+ * @param el
84
+ * @param idx
85
+ */
86
+ export declare const $moveTo: (newParent: Element, el: Element | Comment, idx?: NumOrElement) => void;
87
+ /**
88
+ * Removes all content from given element.
89
+ *
90
+ * @param el
91
+ */
47
92
  export declare const $clear: (el: Element) => Element;
48
93
  /**
49
94
  * Same as `el.innerText = body`, however if `body` is an
@@ -53,7 +98,7 @@ export declare const $clear: (el: Element) => Element;
53
98
  * @param el -
54
99
  * @param body -
55
100
  */
56
- export declare const $text: (el: HTMLElement, body: any) => void;
101
+ export declare const $text: (el: HTMLElement | SVGElement, body: any) => void;
57
102
  /**
58
103
  * Same as `el.innerHtml = body`, use with caution! If `body` is an
59
104
  * {@link @thi.ng/api#IDeref} it'll be automatically deref'd.
@@ -61,7 +106,7 @@ export declare const $text: (el: HTMLElement, body: any) => void;
61
106
  * @param el -
62
107
  * @param body -
63
108
  */
64
- export declare const $html: (el: HTMLElement, body: MaybeDeref<string>) => void;
109
+ export declare const $html: (el: HTMLElement | SVGElement, body: MaybeDeref<string>) => void;
65
110
  /**
66
111
  * Takes an object of attributes and applies them to given DOM element.
67
112
  *
package/dom.js CHANGED
@@ -11,31 +11,37 @@ import { ATTRIB_JOIN_DELIMS, NO_SPANS, RE_TAG, SVG_TAGS, } from "@thi.ng/hiccup/
11
11
  import { mergeClasses, mergeEmmetAttribs } from "@thi.ng/hiccup/attribs";
12
12
  import { formatPrefixes } from "@thi.ng/hiccup/prefix";
13
13
  import { XML_SVG, XML_XLINK, XML_XMLNS } from "@thi.ng/prefixes/xml";
14
- import { isComponent } from "./checks.js";
14
+ import { isComment, isComponent } from "./checks.js";
15
15
  /**
16
- * hdom-style DOM tree creation from hiccup format. Returns DOM element
17
- * of `tree` root. See {@link $el} for further details.
16
+ * hdom-style DOM tree creation from hiccup format. Returns DOM element of
17
+ * `tree` root. See {@link $el} for further details.
18
18
  *
19
19
  * @remarks
20
20
  * Supports elements given in these forms:
21
21
  *
22
22
  * - {@link IComponent} instance
23
- * - {@link IDeref} instance (must resolve to another supported type in
24
- * this list)
23
+ * - {@link IDeref} instance (must resolve to another supported type in this
24
+ * list)
25
25
  * - `["div#id.class", {...attribs}, ...children]`
26
+ * - `[COMMENT, "foo", "bar"...]` (DOM comment node)
26
27
  * - `[IComponent, ...mountargs]`
27
28
  * - `[function, ...args]`
28
29
  * - ES6 iterable of the above (for child values only!)
29
30
  *
30
- * Any other values will be cast to strings and added as spans to
31
- * current `parent`.
31
+ * Any other values will be cast to strings and added as spans to current
32
+ * `parent`.
33
+ *
34
+ * Note: `COMMENT` is defined as constant in thi.ng/hiccup package. Also see
35
+ * {@link $comment} function to create comments directly.
32
36
  *
33
37
  * @param tree -
34
38
  * @param parent -
35
39
  * @param idx -
36
40
  */
37
41
  export const $tree = async (tree, parent, idx = -1) => isArray(tree)
38
- ? $treeElem(tree, parent, idx)
42
+ ? isComment(tree)
43
+ ? $comment(tree.slice(1), parent, idx)
44
+ : $treeElem(tree, parent, idx)
39
45
  : isComponent(tree)
40
46
  ? tree.mount(parent, idx)
41
47
  : isDeref(tree)
@@ -121,6 +127,37 @@ export const $el = (tag, attribs, body, parent, idx = -1) => {
121
127
  parent && $addChild(parent, el, idx);
122
128
  return el;
123
129
  };
130
+ /**
131
+ * Similar to {@link $el}, but creates a new comment DOM node using provided
132
+ * body. If `parent` is given, the comment will be attached or inserted as child
133
+ * at `idx`. Returns comment node.
134
+ *
135
+ * @remarks
136
+ * See thi.ng/hiccup docs for reference:
137
+ * - https://docs.thi.ng/umbrella/hiccup/functions/serialize.html
138
+ *
139
+ * @param body
140
+ * @param parent
141
+ * @param idx
142
+ */
143
+ export const $comment = (body, parent, idx = -1) => {
144
+ const comment = document.createComment(isString(body)
145
+ ? body
146
+ : body.length < 2
147
+ ? body[0] || ""
148
+ : ["", ...body, ""].join("\n"));
149
+ parent && $addChild(parent, comment, idx);
150
+ return comment;
151
+ };
152
+ /**
153
+ * Appends or inserts `child` as child element of `parent`. The default `idx` of
154
+ * -1 means the child will be appended, else uses `parent.insertBefore()` to
155
+ * insert at given index.
156
+ *
157
+ * @param parent
158
+ * @param child
159
+ * @param idx
160
+ */
124
161
  export const $addChild = (parent, child, idx = -1) => {
125
162
  isNumber(idx)
126
163
  ? idx < 0 || idx >= parent.children.length
@@ -128,11 +165,29 @@ export const $addChild = (parent, child, idx = -1) => {
128
165
  : parent.insertBefore(child, parent.children[idx])
129
166
  : parent.insertBefore(child, idx);
130
167
  };
168
+ /**
169
+ * Removes given element or comment from the DOM.
170
+ *
171
+ * @param el
172
+ */
131
173
  export const $remove = (el) => el.remove();
174
+ /**
175
+ * Migrates given element to `newParent`, following the same append or insertion
176
+ * logic as {@link $addChild}.
177
+ *
178
+ * @param newParent
179
+ * @param el
180
+ * @param idx
181
+ */
132
182
  export const $moveTo = (newParent, el, idx = -1) => {
133
183
  $remove(el);
134
184
  $addChild(newParent, el, idx);
135
185
  };
186
+ /**
187
+ * Removes all content from given element.
188
+ *
189
+ * @param el
190
+ */
136
191
  export const $clear = (el) => ((el.innerHTML = ""), el);
137
192
  /**
138
193
  * Same as `el.innerText = body`, however if `body` is an
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/rdom",
3
- "version": "0.9.20",
3
+ "version": "0.10.1",
4
4
  "description": "Lightweight, reactive, VDOM-less UI/DOM components with async lifecycle and @thi.ng/hiccup compatible",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -35,18 +35,18 @@
35
35
  "test": "testament test"
36
36
  },
37
37
  "dependencies": {
38
- "@thi.ng/api": "^8.5.0",
39
- "@thi.ng/checks": "^3.3.3",
40
- "@thi.ng/errors": "^2.2.4",
41
- "@thi.ng/hiccup": "^4.2.24",
42
- "@thi.ng/paths": "^5.1.23",
43
- "@thi.ng/prefixes": "^2.1.13",
44
- "@thi.ng/rstream": "^7.2.29",
45
- "@thi.ng/strings": "^3.3.18"
38
+ "@thi.ng/api": "^8.5.1",
39
+ "@thi.ng/checks": "^3.3.4",
40
+ "@thi.ng/errors": "^2.2.5",
41
+ "@thi.ng/hiccup": "^4.2.25",
42
+ "@thi.ng/paths": "^5.1.24",
43
+ "@thi.ng/prefixes": "^2.1.14",
44
+ "@thi.ng/rstream": "^7.2.30",
45
+ "@thi.ng/strings": "^3.3.19"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@microsoft/api-extractor": "^7.33.5",
49
- "@thi.ng/testament": "^0.3.5",
49
+ "@thi.ng/testament": "^0.3.6",
50
50
  "rimraf": "^3.0.2",
51
51
  "tools": "^0.0.1",
52
52
  "typedoc": "^0.23.20",
@@ -140,5 +140,5 @@
140
140
  "status": "beta",
141
141
  "year": 2020
142
142
  },
143
- "gitHead": "75ec32ff7f1b7b5e72e7a04ace24732cd5d6c774\n"
143
+ "gitHead": "5178ea1d7f4b2de86cf5101bdd73f6567518c0bd\n"
144
144
  }