@zyrab/domo 1.0.3 → 1.1.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zyrab/domo",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
4
4
  "description": "Minimalist DOM builder and chaining-friendly micro-framework with router support.",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/core/Domo.js CHANGED
@@ -63,9 +63,7 @@ class Domo {
63
63
  * @returns {string[]}
64
64
  */
65
65
  _parseClassList(input) {
66
- return Array.isArray(input)
67
- ? input.filter(Boolean)
68
- : String(input).split(" ").filter(Boolean);
66
+ return Array.isArray(input) ? input.filter(Boolean) : String(input).split(" ").filter(Boolean);
69
67
  }
70
68
 
71
69
  /**
@@ -264,7 +262,49 @@ class Domo {
264
262
  });
265
263
  return this;
266
264
  }
265
+ /**
266
+ * Appends children to the element.
267
+ * Alias for `child`.
268
+ * Accepts:
269
+ * - DOM nodes
270
+ * - Domo instances
271
+ * - DocumentFragments
272
+ * - Primitive strings/numbers (as text nodes)
273
+ * - Nested arrays of above
274
+ * @param {(Node|string|number|Domo|DocumentFragment|Array<any>)[]} children
275
+ * @returns {Domo}
276
+ */
277
+ append(children = []) {
278
+ return this.child(children);
279
+ }
267
280
 
281
+ /**
282
+ * Appends the current element to a target parent.
283
+ * Accepts:
284
+ * - HTMLElement
285
+ * - Domo instance
286
+ * - DocumentFragment
287
+ * @param {HTMLElement|Domo|DocumentFragment} target
288
+ * @returns {Domo}
289
+ */
290
+ appendTo(target) {
291
+ if (_handleElementInstance(target)) parent.appendChild(this.element);
292
+ return this;
293
+ }
294
+
295
+ /**
296
+ * Appends the current element to a target parent.
297
+ * Alias for `appendTo`.
298
+ * @param {HTMLElement|Domo|DocumentFragment} target
299
+ * @returns {Domo}
300
+ */
301
+ parent(target) {
302
+ return this.appendTo(target);
303
+ }
304
+
305
+ parent(parent) {
306
+ parent.appendChild(this.element);
307
+ }
268
308
  /**
269
309
  * Removes all children.
270
310
  */
@@ -286,13 +326,14 @@ class Domo {
286
326
  * @param {(Node|string|number|Domo|DocumentFragment|Array<any>)[]} newChild
287
327
  */
288
328
  replace(child, newChild) {
289
- const instance = this._handleElementInstance(newChild);
329
+ const newChild = this._handleElementInstance(newChild);
330
+ const child = this._handleElementInstance(child);
290
331
 
291
332
  if (child === this.element) {
292
- this.element.replaceWith(instance);
293
- this.element = instance;
333
+ this.element.replaceWith(newChild);
334
+ this.element = newChild;
294
335
  } else if (this.element.contains(child)) {
295
- child.replaceWith(instance);
336
+ child.replaceWith(newChild);
296
337
  }
297
338
 
298
339
  return this;
@@ -315,9 +356,7 @@ class Domo {
315
356
  */
316
357
  if(condition) {
317
358
  if (!condition) {
318
- return new Domo("if")
319
- .attr({ hidden: true })
320
- .data({ if: this.element.tagName.toLowerCase() });
359
+ return new Domo("if").attr({ hidden: true }).data({ if: this.element.tagName.toLowerCase() });
321
360
  }
322
361
  return this;
323
362
  }
@@ -3,12 +3,15 @@ let _listeners = [];
3
3
  const _scrollPositions = {};
4
4
  let _previousUrl = "";
5
5
 
6
- let _root = document.createElement("main");
7
- _root.id = "main";
6
+ let _root;
8
7
 
9
8
  function init() {
10
9
  ["DOMContentLoaded", "popstate"].forEach((event) =>
11
10
  window.addEventListener(event, async () => {
11
+ if (!_root) {
12
+ _root = document.createElement("main");
13
+ _root.id = "main";
14
+ }
12
15
  saveScroll(_previousUrl);
13
16
  const url = path();
14
17
  load(url);
@@ -21,7 +24,7 @@ function init() {
21
24
  async function render({ component, meta }, params) {
22
25
  try {
23
26
  const content = await component(params);
24
- _root.replaceChildren();
27
+ _root?.replaceChildren();
25
28
 
26
29
  if (content instanceof HTMLElement) {
27
30
  _root.appendChild(content);
@@ -34,9 +37,7 @@ async function render({ component, meta }, params) {
34
37
  }
35
38
  if (meta) {
36
39
  document.title = meta?.title;
37
- document
38
- .querySelector("meta[name='description']")
39
- .setAttribute("content", meta?.description);
40
+ document.querySelector("meta[name='description']").setAttribute("content", meta?.description);
40
41
  }
41
42
  } catch (error) {
42
43
  console.error("Rendering error:", error);
@@ -116,13 +117,13 @@ async function load(url) {
116
117
  function notify(info) {
117
118
  _listeners.forEach((cb) => cb(info));
118
119
  }
119
- function saveScroll(path = window.location.pathname) {
120
- _scrollPositions[path] = window.scrollY;
120
+ function saveScroll(path = window?.location?.pathname) {
121
+ _scrollPositions[path] = window?.scrollY;
121
122
  }
122
123
 
123
124
  function restoreScroll() {
124
- const pos = _scrollPositions[window.location.pathname];
125
- window.scrollTo(0, pos || 0);
125
+ const pos = _scrollPositions[window?.location?.pathname];
126
+ window?.scrollTo(0, pos || 0);
126
127
  }
127
128
 
128
129
  const Router = {