@zyrab/domo 1.0.0 → 1.0.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/index.js +33 -2
- package/package.json +1 -1
- package/src/core/Domo.js +53 -9
- package/src/core/DomoSVG.js +2 -0
- package/src/core/Router.js +3 -1
package/index.js
CHANGED
|
@@ -1,4 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import DomoClass from "./src/core/Domo.js";
|
|
2
4
|
import Router from "./src/core/Router.js";
|
|
5
|
+
import DomoSVGClass from "./src/core/DomoSVG.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {import('./src/core/Domo.js').default} DomoInstance
|
|
9
|
+
* @typedef {import('./src/core/DomoSVG.js').default} DomoSVGInstance
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {((tag?: string) => DomoInstance) & { Class: typeof DomoClass }} DomoFactory
|
|
14
|
+
* @typedef {((tag?: string) => DomoSVGInstance)} DomoSVGFactory
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {string} [tag]
|
|
19
|
+
* @returns {DomoInstance}
|
|
20
|
+
*/
|
|
21
|
+
function Domo(tag) {
|
|
22
|
+
return new DomoClass(tag);
|
|
23
|
+
}
|
|
24
|
+
Domo.Class = DomoClass;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {string} [tag]
|
|
28
|
+
* @returns {DomoSVGInstance}
|
|
29
|
+
*/
|
|
30
|
+
function DSVG(tag) {
|
|
31
|
+
return new DomoSVGClass(tag);
|
|
32
|
+
}
|
|
33
|
+
DSVG.Class = DomoSVGClass;
|
|
3
34
|
|
|
4
|
-
export { Domo, Router };
|
|
35
|
+
export { Domo, Router, DSVG, DomoClass };
|
package/package.json
CHANGED
package/src/core/Domo.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Domo - Lightweight helper class for creating and manipulating DOM elements fluently.
|
|
3
|
+
* @typedef {import('./Domo').default} Domo
|
|
3
4
|
*/
|
|
4
5
|
class Domo {
|
|
5
6
|
/**
|
|
@@ -69,6 +70,9 @@ class Domo {
|
|
|
69
70
|
|
|
70
71
|
/**
|
|
71
72
|
* Adds classes
|
|
73
|
+
* accepted types:
|
|
74
|
+
* string
|
|
75
|
+
* string[]
|
|
72
76
|
* @param {string|string[]} classes
|
|
73
77
|
*/
|
|
74
78
|
cls(classes) {
|
|
@@ -80,6 +84,9 @@ class Domo {
|
|
|
80
84
|
|
|
81
85
|
/**
|
|
82
86
|
* Removes classes
|
|
87
|
+
* accepted types:
|
|
88
|
+
* string
|
|
89
|
+
* string[]
|
|
83
90
|
* @param {string|string[]} classes
|
|
84
91
|
*/
|
|
85
92
|
rmvCls(classes) {
|
|
@@ -160,13 +167,34 @@ class Domo {
|
|
|
160
167
|
}
|
|
161
168
|
|
|
162
169
|
/**
|
|
163
|
-
* Adds
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
170
|
+
* Adds one or multiple event listeners to the element.
|
|
171
|
+
*
|
|
172
|
+
* Supports both:
|
|
173
|
+
* - A single event: `on('click', handler, options)`
|
|
174
|
+
* - Multiple events via object map: `on({ click: handler, mouseenter: [handler, options] })`
|
|
175
|
+
*
|
|
176
|
+
* @param {string | Record<string, Function | [Function, AddEventListenerOptions]>} eventMapOrName
|
|
177
|
+
* Either an event name string, or an object mapping event names to handlers (or [handler, options] tuples).
|
|
178
|
+
* @param {EventListenerOrEventListenerObject} [callback]
|
|
179
|
+
* Callback to execute when the event is triggered (used when first param is a string).
|
|
180
|
+
* @param {AddEventListenerOptions} [options={}]
|
|
181
|
+
* Options for `addEventListener` (used when first param is a string).
|
|
182
|
+
* @returns {this} The current instance for chaining.
|
|
167
183
|
*/
|
|
168
|
-
|
|
169
|
-
|
|
184
|
+
|
|
185
|
+
on(eventMapOrName, callback, options = {}) {
|
|
186
|
+
if (typeof eventMapOrName === "object" && eventMapOrName !== null) {
|
|
187
|
+
for (const [event, value] of Object.entries(eventMapOrName)) {
|
|
188
|
+
if (typeof value === "function") {
|
|
189
|
+
this.element.addEventListener(event, value);
|
|
190
|
+
} else if (Array.isArray(value)) {
|
|
191
|
+
const [cb, opts] = value;
|
|
192
|
+
this.element.addEventListener(event, cb, opts);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
} else {
|
|
196
|
+
this.element.addEventListener(eventMapOrName, callback, options);
|
|
197
|
+
}
|
|
170
198
|
return this;
|
|
171
199
|
}
|
|
172
200
|
|
|
@@ -180,6 +208,7 @@ class Domo {
|
|
|
180
208
|
|
|
181
209
|
/**
|
|
182
210
|
* Delegates events to closest matching ancestor.
|
|
211
|
+
* accepts a map of selectors to event handlers
|
|
183
212
|
* @param {string} event
|
|
184
213
|
* @param {Record<string, (e: Event, target: Element) => void>} selectors
|
|
185
214
|
* @param {AddEventListenerOptions} [options]
|
|
@@ -197,6 +226,7 @@ class Domo {
|
|
|
197
226
|
|
|
198
227
|
/**
|
|
199
228
|
* Delegates events using element.matches.
|
|
229
|
+
* accepts a map of selectors to event handlers
|
|
200
230
|
* @param {string} event
|
|
201
231
|
* @param {Record<string, (e: Event, target: Element) => void>} selectors
|
|
202
232
|
* @param {AddEventListenerOptions} [options]
|
|
@@ -217,8 +247,15 @@ class Domo {
|
|
|
217
247
|
}
|
|
218
248
|
|
|
219
249
|
/**
|
|
220
|
-
* Appends children
|
|
221
|
-
*
|
|
250
|
+
* Appends children to the element.
|
|
251
|
+
* Accepts:
|
|
252
|
+
* - DOM nodes
|
|
253
|
+
* - Domo instances
|
|
254
|
+
* - DocumentFragments
|
|
255
|
+
* - Primitive strings/numbers (as text nodes)
|
|
256
|
+
* - Nested arrays of above
|
|
257
|
+
* @param {(Node|string|number|Domo|DocumentFragment|Array<any>)[]} children
|
|
258
|
+
* @returns {Domo}
|
|
222
259
|
*/
|
|
223
260
|
child(children = []) {
|
|
224
261
|
const flattenedChildren = children.flat();
|
|
@@ -238,8 +275,15 @@ class Domo {
|
|
|
238
275
|
|
|
239
276
|
/**
|
|
240
277
|
* Replaces a child element or self with a new one.
|
|
278
|
+
* * Accepts:
|
|
279
|
+
* - DOM nodes
|
|
280
|
+
* - Domo instances
|
|
281
|
+
* - DocumentFragments
|
|
282
|
+
* - Primitive strings/numbers (as text nodes)
|
|
283
|
+
* - Nested arrays of above
|
|
284
|
+
*
|
|
241
285
|
* @param {Node} child
|
|
242
|
-
* @param {any} newChild
|
|
286
|
+
* @param {(Node|string|number|Domo|DocumentFragment|Array<any>)[]} newChild
|
|
243
287
|
*/
|
|
244
288
|
replace(child, newChild) {
|
|
245
289
|
const instance = this._handleElementInstance(newChild);
|
package/src/core/DomoSVG.js
CHANGED
package/src/core/Router.js
CHANGED
|
@@ -125,7 +125,7 @@ function restoreScroll() {
|
|
|
125
125
|
window.scrollTo(0, pos || 0);
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
|
|
128
|
+
const Router = {
|
|
129
129
|
/**
|
|
130
130
|
* Mount point of the router, where components will be rendered.
|
|
131
131
|
* @returns {HTMLElement} The root DOM element used by the router.
|
|
@@ -201,3 +201,5 @@ export const Router = {
|
|
|
201
201
|
if (typeof fn === "function") _listeners.push(fn);
|
|
202
202
|
},
|
|
203
203
|
};
|
|
204
|
+
|
|
205
|
+
export default Router;
|