eleva 1.2.16-beta → 1.2.18-beta
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 +27 -15
- package/dist/eleva.cjs.js +358 -172
- package/dist/eleva.cjs.js.map +1 -1
- package/dist/eleva.d.ts +260 -109
- package/dist/eleva.esm.js +358 -172
- package/dist/eleva.esm.js.map +1 -1
- package/dist/eleva.umd.js +358 -172
- package/dist/eleva.umd.js.map +1 -1
- package/dist/eleva.umd.min.js +2 -2
- package/dist/eleva.umd.min.js.map +1 -1
- package/package.json +1 -1
- package/src/core/Eleva.js +163 -92
- package/src/modules/Emitter.js +23 -7
- package/src/modules/Renderer.js +183 -82
- package/src/modules/Signal.js +10 -7
- package/src/modules/TemplateEngine.js +6 -3
- package/types/core/Eleva.d.ts +167 -71
- package/types/core/Eleva.d.ts.map +1 -1
- package/types/modules/Emitter.d.ts +26 -10
- package/types/modules/Emitter.d.ts.map +1 -1
- package/types/modules/Renderer.d.ts +56 -19
- package/types/modules/Renderer.d.ts.map +1 -1
- package/types/modules/Signal.d.ts +11 -9
- package/types/modules/Signal.d.ts.map +1 -1
- package/types/modules/TemplateEngine.d.ts +8 -5
- package/types/modules/TemplateEngine.d.ts.map +1 -1
package/dist/eleva.cjs.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! Eleva v1.2.
|
|
1
|
+
/*! Eleva v1.2.18-beta | MIT License | https://elevajs.com */
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
class TemplateEngine {
|
|
16
16
|
/**
|
|
17
17
|
* @private {RegExp} Regular expression for matching template expressions in the format {{ expression }}
|
|
18
|
+
* @type {RegExp}
|
|
18
19
|
*/
|
|
19
20
|
static expressionPattern = /\{\{\s*(.*?)\s*\}\}/g;
|
|
20
21
|
|
|
@@ -25,7 +26,7 @@ class TemplateEngine {
|
|
|
25
26
|
* @public
|
|
26
27
|
* @static
|
|
27
28
|
* @param {string} template - The template string to parse.
|
|
28
|
-
* @param {
|
|
29
|
+
* @param {Record<string, unknown>} data - The data context for evaluating expressions.
|
|
29
30
|
* @returns {string} The parsed template with expressions replaced by their values.
|
|
30
31
|
* @example
|
|
31
32
|
* const result = TemplateEngine.parse("{{user.name}} is {{user.age}} years old", {
|
|
@@ -40,12 +41,14 @@ class TemplateEngine {
|
|
|
40
41
|
/**
|
|
41
42
|
* Evaluates an expression in the context of the provided data object.
|
|
42
43
|
* Note: This does not provide a true sandbox and evaluated expressions may access global scope.
|
|
44
|
+
* The use of the `with` statement is necessary for expression evaluation but has security implications.
|
|
45
|
+
* Expressions should be carefully validated before evaluation.
|
|
43
46
|
*
|
|
44
47
|
* @public
|
|
45
48
|
* @static
|
|
46
49
|
* @param {string} expression - The expression to evaluate.
|
|
47
|
-
* @param {
|
|
48
|
-
* @returns {
|
|
50
|
+
* @param {Record<string, unknown>} data - The data context for evaluation.
|
|
51
|
+
* @returns {unknown} The result of the evaluation, or an empty string if evaluation fails.
|
|
49
52
|
* @example
|
|
50
53
|
* const result = TemplateEngine.evaluate("user.name", { user: { name: "John" } }); // Returns: "John"
|
|
51
54
|
* const age = TemplateEngine.evaluate("user.age", { user: { age: 30 } }); // Returns: 30
|
|
@@ -65,6 +68,8 @@ class TemplateEngine {
|
|
|
65
68
|
* @classdesc A reactive data holder that enables fine-grained reactivity in the Eleva framework.
|
|
66
69
|
* Signals notify registered watchers when their value changes, enabling efficient DOM updates
|
|
67
70
|
* through targeted patching rather than full re-renders.
|
|
71
|
+
* Updates are batched using microtasks to prevent multiple synchronous notifications.
|
|
72
|
+
* The class is generic, allowing type-safe handling of any value type T.
|
|
68
73
|
*
|
|
69
74
|
* @example
|
|
70
75
|
* const count = new Signal(0);
|
|
@@ -77,12 +82,12 @@ class Signal {
|
|
|
77
82
|
* Creates a new Signal instance with the specified initial value.
|
|
78
83
|
*
|
|
79
84
|
* @public
|
|
80
|
-
* @param {
|
|
85
|
+
* @param {T} value - The initial value of the signal.
|
|
81
86
|
*/
|
|
82
87
|
constructor(value) {
|
|
83
|
-
/** @private {T} Internal storage for the signal's current value
|
|
88
|
+
/** @private {T} Internal storage for the signal's current value */
|
|
84
89
|
this._value = value;
|
|
85
|
-
/** @private {Set<
|
|
90
|
+
/** @private {Set<(value: T) => void>} Collection of callback functions to be notified when value changes */
|
|
86
91
|
this._watchers = new Set();
|
|
87
92
|
/** @private {boolean} Flag to prevent multiple synchronous watcher notifications and batch updates into microtasks */
|
|
88
93
|
this._pending = false;
|
|
@@ -92,7 +97,7 @@ class Signal {
|
|
|
92
97
|
* Gets the current value of the signal.
|
|
93
98
|
*
|
|
94
99
|
* @public
|
|
95
|
-
* @returns {T} The current value
|
|
100
|
+
* @returns {T} The current value.
|
|
96
101
|
*/
|
|
97
102
|
get value() {
|
|
98
103
|
return this._value;
|
|
@@ -103,7 +108,7 @@ class Signal {
|
|
|
103
108
|
* The notification is batched using microtasks to prevent multiple synchronous updates.
|
|
104
109
|
*
|
|
105
110
|
* @public
|
|
106
|
-
* @param {T} newVal - The new value to set
|
|
111
|
+
* @param {T} newVal - The new value to set.
|
|
107
112
|
* @returns {void}
|
|
108
113
|
*/
|
|
109
114
|
set value(newVal) {
|
|
@@ -117,8 +122,8 @@ class Signal {
|
|
|
117
122
|
* The watcher will receive the new value as its argument.
|
|
118
123
|
*
|
|
119
124
|
* @public
|
|
120
|
-
* @param {
|
|
121
|
-
* @returns {
|
|
125
|
+
* @param {(value: T) => void} fn - The callback function to invoke on value change.
|
|
126
|
+
* @returns {() => boolean} A function to unsubscribe the watcher.
|
|
122
127
|
* @example
|
|
123
128
|
* const unsubscribe = signal.watch((value) => console.log(value));
|
|
124
129
|
* // Later...
|
|
@@ -141,6 +146,7 @@ class Signal {
|
|
|
141
146
|
if (this._pending) return;
|
|
142
147
|
this._pending = true;
|
|
143
148
|
queueMicrotask(() => {
|
|
149
|
+
/** @type {(fn: (value: T) => void) => void} */
|
|
144
150
|
this._watchers.forEach(fn => fn(this._value));
|
|
145
151
|
this._pending = false;
|
|
146
152
|
});
|
|
@@ -152,6 +158,9 @@ class Signal {
|
|
|
152
158
|
* @classdesc A robust event emitter that enables inter-component communication through a publish-subscribe pattern.
|
|
153
159
|
* Components can emit events and listen for events from other components, facilitating loose coupling
|
|
154
160
|
* and reactive updates across the application.
|
|
161
|
+
* Events are handled synchronously in the order they were registered, with proper cleanup
|
|
162
|
+
* of unsubscribed handlers.
|
|
163
|
+
* Event names should follow the format 'namespace:action' (e.g., 'user:login', 'cart:update').
|
|
155
164
|
*
|
|
156
165
|
* @example
|
|
157
166
|
* const emitter = new Emitter();
|
|
@@ -165,18 +174,19 @@ class Emitter {
|
|
|
165
174
|
* @public
|
|
166
175
|
*/
|
|
167
176
|
constructor() {
|
|
168
|
-
/** @private {Map<string, Set<
|
|
177
|
+
/** @private {Map<string, Set<(data: unknown) => void>>} Map of event names to their registered handler functions */
|
|
169
178
|
this._events = new Map();
|
|
170
179
|
}
|
|
171
180
|
|
|
172
181
|
/**
|
|
173
182
|
* Registers an event handler for the specified event name.
|
|
174
183
|
* The handler will be called with the event data when the event is emitted.
|
|
184
|
+
* Event names should follow the format 'namespace:action' for consistency.
|
|
175
185
|
*
|
|
176
186
|
* @public
|
|
177
|
-
* @param {string} event - The name of the event to listen for.
|
|
178
|
-
* @param {
|
|
179
|
-
* @returns {
|
|
187
|
+
* @param {string} event - The name of the event to listen for (e.g., 'user:login').
|
|
188
|
+
* @param {(data: unknown) => void} handler - The callback function to invoke when the event occurs.
|
|
189
|
+
* @returns {() => void} A function to unsubscribe the event handler.
|
|
180
190
|
* @example
|
|
181
191
|
* const unsubscribe = emitter.on('user:login', (user) => console.log(user));
|
|
182
192
|
* // Later...
|
|
@@ -191,11 +201,17 @@ class Emitter {
|
|
|
191
201
|
/**
|
|
192
202
|
* Removes an event handler for the specified event name.
|
|
193
203
|
* If no handler is provided, all handlers for the event are removed.
|
|
204
|
+
* Automatically cleans up empty event sets to prevent memory leaks.
|
|
194
205
|
*
|
|
195
206
|
* @public
|
|
196
|
-
* @param {string} event - The name of the event.
|
|
197
|
-
* @param {
|
|
207
|
+
* @param {string} event - The name of the event to remove handlers from.
|
|
208
|
+
* @param {(data: unknown) => void} [handler] - The specific handler function to remove.
|
|
198
209
|
* @returns {void}
|
|
210
|
+
* @example
|
|
211
|
+
* // Remove a specific handler
|
|
212
|
+
* emitter.off('user:login', loginHandler);
|
|
213
|
+
* // Remove all handlers for an event
|
|
214
|
+
* emitter.off('user:login');
|
|
199
215
|
*/
|
|
200
216
|
off(event, handler) {
|
|
201
217
|
if (!this._events.has(event)) return;
|
|
@@ -212,11 +228,17 @@ class Emitter {
|
|
|
212
228
|
/**
|
|
213
229
|
* Emits an event with the specified data to all registered handlers.
|
|
214
230
|
* Handlers are called synchronously in the order they were registered.
|
|
231
|
+
* If no handlers are registered for the event, the emission is silently ignored.
|
|
215
232
|
*
|
|
216
233
|
* @public
|
|
217
234
|
* @param {string} event - The name of the event to emit.
|
|
218
|
-
* @param {...
|
|
235
|
+
* @param {...unknown} args - Optional arguments to pass to the event handlers.
|
|
219
236
|
* @returns {void}
|
|
237
|
+
* @example
|
|
238
|
+
* // Emit an event with data
|
|
239
|
+
* emitter.emit('user:login', { name: 'John', role: 'admin' });
|
|
240
|
+
* // Emit an event with multiple arguments
|
|
241
|
+
* emitter.emit('cart:update', { items: [] }, { total: 0 });
|
|
220
242
|
*/
|
|
221
243
|
emit(event, ...args) {
|
|
222
244
|
if (!this._events.has(event)) return;
|
|
@@ -224,11 +246,27 @@ class Emitter {
|
|
|
224
246
|
}
|
|
225
247
|
}
|
|
226
248
|
|
|
249
|
+
/**
|
|
250
|
+
* A regular expression to match hyphenated lowercase letters.
|
|
251
|
+
* @private
|
|
252
|
+
* @type {RegExp}
|
|
253
|
+
*/
|
|
254
|
+
const CAMEL_RE = /-([a-z])/g;
|
|
255
|
+
|
|
227
256
|
/**
|
|
228
257
|
* @class 🎨 Renderer
|
|
229
|
-
* @classdesc A DOM renderer that
|
|
230
|
-
*
|
|
231
|
-
*
|
|
258
|
+
* @classdesc A high-performance DOM renderer that implements an optimized direct DOM diffing algorithm.
|
|
259
|
+
*
|
|
260
|
+
* Key features:
|
|
261
|
+
* - Single-pass diffing algorithm for efficient DOM updates
|
|
262
|
+
* - Key-based node reconciliation for optimal performance
|
|
263
|
+
* - Intelligent attribute handling for ARIA, data attributes, and boolean properties
|
|
264
|
+
* - Preservation of special Eleva-managed instances and style elements
|
|
265
|
+
* - Memory-efficient with reusable temporary containers
|
|
266
|
+
*
|
|
267
|
+
* The renderer is designed to minimize DOM operations while maintaining
|
|
268
|
+
* exact attribute synchronization and proper node identity preservation.
|
|
269
|
+
* It's particularly optimized for frequent updates and complex DOM structures.
|
|
232
270
|
*
|
|
233
271
|
* @example
|
|
234
272
|
* const renderer = new Renderer();
|
|
@@ -238,128 +276,173 @@ class Emitter {
|
|
|
238
276
|
*/
|
|
239
277
|
class Renderer {
|
|
240
278
|
/**
|
|
241
|
-
* Creates a new Renderer instance
|
|
279
|
+
* Creates a new Renderer instance.
|
|
242
280
|
* @public
|
|
243
281
|
*/
|
|
244
282
|
constructor() {
|
|
245
|
-
/**
|
|
283
|
+
/**
|
|
284
|
+
* A temporary container to hold the new HTML content while diffing.
|
|
285
|
+
* @private
|
|
286
|
+
* @type {HTMLElement}
|
|
287
|
+
*/
|
|
246
288
|
this._tempContainer = document.createElement("div");
|
|
247
289
|
}
|
|
248
290
|
|
|
249
291
|
/**
|
|
250
|
-
* Patches the DOM of
|
|
251
|
-
* Efficiently updates the DOM by parsing new HTML into a reusable container
|
|
252
|
-
* and applying only the necessary changes.
|
|
292
|
+
* Patches the DOM of the given container with the provided HTML string.
|
|
253
293
|
*
|
|
254
294
|
* @public
|
|
255
|
-
* @param {HTMLElement} container - The container
|
|
256
|
-
* @param {string} newHtml - The new HTML
|
|
295
|
+
* @param {HTMLElement} container - The container whose DOM will be patched.
|
|
296
|
+
* @param {string} newHtml - The new HTML string.
|
|
297
|
+
* @throws {TypeError} If the container is not an HTMLElement or newHtml is not a string.
|
|
298
|
+
* @throws {Error} If the DOM patching fails.
|
|
257
299
|
* @returns {void}
|
|
258
|
-
* @throws {Error} If container is not an HTMLElement, newHtml is not a string, or patching fails.
|
|
259
300
|
*/
|
|
260
301
|
patchDOM(container, newHtml) {
|
|
261
302
|
if (!(container instanceof HTMLElement)) {
|
|
262
|
-
throw new
|
|
303
|
+
throw new TypeError("Container must be an HTMLElement");
|
|
263
304
|
}
|
|
264
305
|
if (typeof newHtml !== "string") {
|
|
265
|
-
throw new
|
|
306
|
+
throw new TypeError("newHtml must be a string");
|
|
266
307
|
}
|
|
267
308
|
try {
|
|
268
|
-
// Directly set new HTML, replacing any existing content
|
|
269
309
|
this._tempContainer.innerHTML = newHtml;
|
|
270
310
|
this._diff(container, this._tempContainer);
|
|
271
|
-
} catch {
|
|
272
|
-
throw new Error(
|
|
311
|
+
} catch (error) {
|
|
312
|
+
throw new Error(`Failed to patch DOM: ${error.message}`);
|
|
273
313
|
}
|
|
274
314
|
}
|
|
275
315
|
|
|
276
316
|
/**
|
|
277
|
-
*
|
|
278
|
-
* This method recursively compares nodes and their attributes, applying only
|
|
279
|
-
* the necessary changes to minimize DOM operations.
|
|
317
|
+
* Performs a diff between two DOM nodes and patches the old node to match the new node.
|
|
280
318
|
*
|
|
281
319
|
* @private
|
|
282
|
-
* @param {
|
|
283
|
-
* @param {
|
|
320
|
+
* @param {Node} oldParent - The old parent node to be patched.
|
|
321
|
+
* @param {Node} newParent - The new parent node to compare.
|
|
284
322
|
* @returns {void}
|
|
285
323
|
*/
|
|
286
324
|
_diff(oldParent, newParent) {
|
|
287
|
-
if (oldParent.isEqualNode(newParent)) return;
|
|
288
|
-
const oldChildren = oldParent.childNodes;
|
|
289
|
-
const newChildren = newParent.childNodes;
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
325
|
+
if (oldParent === newParent || oldParent.isEqualNode?.(newParent)) return;
|
|
326
|
+
const oldChildren = Array.from(oldParent.childNodes);
|
|
327
|
+
const newChildren = Array.from(newParent.childNodes);
|
|
328
|
+
let oldStartIdx = 0,
|
|
329
|
+
newStartIdx = 0;
|
|
330
|
+
let oldEndIdx = oldChildren.length - 1;
|
|
331
|
+
let newEndIdx = newChildren.length - 1;
|
|
332
|
+
let oldKeyMap = null;
|
|
333
|
+
while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
|
|
334
|
+
let oldStartNode = oldChildren[oldStartIdx];
|
|
335
|
+
let newStartNode = newChildren[newStartIdx];
|
|
336
|
+
if (!oldStartNode) {
|
|
337
|
+
oldStartIdx++;
|
|
295
338
|
continue;
|
|
296
339
|
}
|
|
297
|
-
if (!
|
|
298
|
-
|
|
340
|
+
if (!newStartNode) {
|
|
341
|
+
newStartIdx++;
|
|
299
342
|
continue;
|
|
300
343
|
}
|
|
301
|
-
if (
|
|
302
|
-
|
|
303
|
-
|
|
344
|
+
if (this._keysMatch(oldStartNode, newStartNode)) {
|
|
345
|
+
this._patchNode(oldStartNode, newStartNode);
|
|
346
|
+
oldStartIdx++;
|
|
347
|
+
newStartIdx++;
|
|
348
|
+
} else {
|
|
349
|
+
oldKeyMap ??= this._createKeyMap(oldChildren, oldStartIdx, oldEndIdx);
|
|
350
|
+
const newKey = newStartNode.nodeType === Node.ELEMENT_NODE ? newStartNode.getAttribute("key") : null;
|
|
351
|
+
const moveIndex = newKey ? oldKeyMap.get(newKey) : undefined;
|
|
352
|
+
const oldNodeToMove = moveIndex !== undefined ? oldChildren[moveIndex] : null;
|
|
353
|
+
if (oldNodeToMove) {
|
|
354
|
+
this._patchNode(oldNodeToMove, newStartNode);
|
|
355
|
+
oldParent.insertBefore(oldNodeToMove, oldStartNode);
|
|
356
|
+
if (moveIndex !== undefined) oldChildren[moveIndex] = null;
|
|
357
|
+
} else {
|
|
358
|
+
oldParent.insertBefore(newStartNode.cloneNode(true), oldStartNode);
|
|
359
|
+
}
|
|
360
|
+
newStartIdx++;
|
|
304
361
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Cleanup
|
|
365
|
+
if (oldStartIdx > oldEndIdx) {
|
|
366
|
+
const refNode = newChildren[newEndIdx + 1] ? oldChildren[oldStartIdx] : null;
|
|
367
|
+
for (let i = newStartIdx; i <= newEndIdx; i++) {
|
|
368
|
+
if (newChildren[i]) oldParent.insertBefore(newChildren[i].cloneNode(true), refNode);
|
|
309
369
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
const
|
|
313
|
-
if (
|
|
314
|
-
oldParent.
|
|
315
|
-
continue;
|
|
370
|
+
} else if (newStartIdx > newEndIdx) {
|
|
371
|
+
for (let i = oldStartIdx; i <= oldEndIdx; i++) {
|
|
372
|
+
const node = oldChildren[i];
|
|
373
|
+
if (node && !(node.nodeName === "STYLE" && node.hasAttribute("data-e-style"))) {
|
|
374
|
+
oldParent.removeChild(node);
|
|
316
375
|
}
|
|
317
|
-
this._updateAttributes(oldNode, newNode);
|
|
318
|
-
this._diff(oldNode, newNode);
|
|
319
|
-
} else if (oldNode.nodeType === Node.TEXT_NODE && oldNode.nodeValue !== newNode.nodeValue) {
|
|
320
|
-
oldNode.nodeValue = newNode.nodeValue;
|
|
321
376
|
}
|
|
322
377
|
}
|
|
323
378
|
}
|
|
324
379
|
|
|
325
380
|
/**
|
|
326
|
-
*
|
|
327
|
-
*
|
|
381
|
+
* Checks if the node types match.
|
|
382
|
+
*
|
|
383
|
+
* @private
|
|
384
|
+
* @param {Node} oldNode - The old node.
|
|
385
|
+
* @param {Node} newNode - The new node.
|
|
386
|
+
* @returns {boolean} True if the nodes match, false otherwise.
|
|
387
|
+
*/
|
|
388
|
+
_keysMatch(oldNode, newNode) {
|
|
389
|
+
if (oldNode.nodeType !== Node.ELEMENT_NODE) return true;
|
|
390
|
+
const oldKey = oldNode.getAttribute("key");
|
|
391
|
+
const newKey = newNode.getAttribute("key");
|
|
392
|
+
return oldKey === newKey;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Patches a node.
|
|
397
|
+
*
|
|
398
|
+
* @private
|
|
399
|
+
* @param {Node} oldNode - The old node to patch.
|
|
400
|
+
* @param {Node} newNode - The new node to patch.
|
|
401
|
+
* @returns {void}
|
|
402
|
+
*/
|
|
403
|
+
_patchNode(oldNode, newNode) {
|
|
404
|
+
if (oldNode?._eleva_instance) return;
|
|
405
|
+
if (oldNode.nodeType !== newNode.nodeType || oldNode.nodeName !== newNode.nodeName) {
|
|
406
|
+
oldNode.replaceWith(newNode.cloneNode(true));
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (oldNode.nodeType === Node.ELEMENT_NODE) {
|
|
410
|
+
const oldEl = oldNode;
|
|
411
|
+
const newEl = newNode;
|
|
412
|
+
this._updateAttributes(oldEl, newEl);
|
|
413
|
+
this._diff(oldEl, newEl);
|
|
414
|
+
} else if (oldNode.nodeType === Node.TEXT_NODE && oldNode.nodeValue !== newNode.nodeValue) {
|
|
415
|
+
oldNode.nodeValue = newNode.nodeValue;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Updates the attributes of an element.
|
|
328
421
|
*
|
|
329
422
|
* @private
|
|
330
|
-
* @param {HTMLElement} oldEl - The element to update.
|
|
331
|
-
* @param {HTMLElement} newEl - The element
|
|
423
|
+
* @param {HTMLElement} oldEl - The old element to update.
|
|
424
|
+
* @param {HTMLElement} newEl - The new element to update.
|
|
332
425
|
* @returns {void}
|
|
333
426
|
*/
|
|
334
427
|
_updateAttributes(oldEl, newEl) {
|
|
335
428
|
const oldAttrs = oldEl.attributes;
|
|
336
429
|
const newAttrs = newEl.attributes;
|
|
337
430
|
|
|
338
|
-
//
|
|
339
|
-
for (
|
|
340
|
-
name
|
|
341
|
-
} of oldAttrs) {
|
|
342
|
-
if (!newEl.hasAttribute(name)) {
|
|
343
|
-
oldEl.removeAttribute(name);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
// Update/add new attributes
|
|
348
|
-
for (const attr of newAttrs) {
|
|
431
|
+
// Single pass for new/updated attributes
|
|
432
|
+
for (let i = 0; i < newAttrs.length; i++) {
|
|
349
433
|
const {
|
|
350
434
|
name,
|
|
351
435
|
value
|
|
352
|
-
} =
|
|
353
|
-
if (name
|
|
354
|
-
if (oldEl.getAttribute(name) === value) continue;
|
|
436
|
+
} = newAttrs[i];
|
|
437
|
+
if (name[0] === "@" || oldEl.getAttribute(name) === value) continue;
|
|
355
438
|
oldEl.setAttribute(name, value);
|
|
356
|
-
if (name
|
|
357
|
-
const
|
|
358
|
-
oldEl[
|
|
359
|
-
} else if (name
|
|
439
|
+
if (name[0] === "a" && name[4] === "-") {
|
|
440
|
+
const s = name.slice(5);
|
|
441
|
+
oldEl["aria" + s.replace(CAMEL_RE, (_, l) => l.toUpperCase())] = value;
|
|
442
|
+
} else if (name[0] === "d" && name[3] === "-") {
|
|
360
443
|
oldEl.dataset[name.slice(5)] = value;
|
|
361
444
|
} else {
|
|
362
|
-
const prop = name.
|
|
445
|
+
const prop = name.includes("-") ? name.replace(CAMEL_RE, (_, l) => l.toUpperCase()) : name;
|
|
363
446
|
if (prop in oldEl) {
|
|
364
447
|
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(oldEl), prop);
|
|
365
448
|
const isBoolean = typeof oldEl[prop] === "boolean" || descriptor?.get && typeof descriptor.get.call(oldEl) === "boolean";
|
|
@@ -371,39 +454,110 @@ class Renderer {
|
|
|
371
454
|
}
|
|
372
455
|
}
|
|
373
456
|
}
|
|
457
|
+
|
|
458
|
+
// Remove any attributes no longer present
|
|
459
|
+
for (let i = oldAttrs.length - 1; i >= 0; i--) {
|
|
460
|
+
const name = oldAttrs[i].name;
|
|
461
|
+
if (!newEl.hasAttribute(name)) {
|
|
462
|
+
oldEl.removeAttribute(name);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Creates a key map for the children of a parent node.
|
|
469
|
+
*
|
|
470
|
+
* @private
|
|
471
|
+
* @param {Array<Node>} children - The children of the parent node.
|
|
472
|
+
* @param {number} start - The start index of the children.
|
|
473
|
+
* @param {number} end - The end index of the children.
|
|
474
|
+
* @returns {Map<string, number>} A map of key to child index.
|
|
475
|
+
*/
|
|
476
|
+
_createKeyMap(children, start, end) {
|
|
477
|
+
const map = new Map();
|
|
478
|
+
for (let i = start; i <= end; i++) {
|
|
479
|
+
const child = children[i];
|
|
480
|
+
if (child?.nodeType === Node.ELEMENT_NODE) {
|
|
481
|
+
const key = child.getAttribute("key");
|
|
482
|
+
if (key) map.set(key, i);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return map;
|
|
374
486
|
}
|
|
375
487
|
}
|
|
376
488
|
|
|
377
489
|
/**
|
|
378
490
|
* @typedef {Object} ComponentDefinition
|
|
379
|
-
* @property {function(
|
|
491
|
+
* @property {function(ComponentContext): (Record<string, unknown>|Promise<Record<string, unknown>>)} [setup]
|
|
380
492
|
* Optional setup function that initializes the component's state and returns reactive data
|
|
381
|
-
* @property {function(
|
|
493
|
+
* @property {(function(ComponentContext): string|Promise<string>)} template
|
|
382
494
|
* Required function that defines the component's HTML structure
|
|
383
|
-
* @property {function(
|
|
384
|
-
* Optional function that provides component-scoped CSS styles
|
|
385
|
-
* @property {
|
|
495
|
+
* @property {(function(ComponentContext): string)|string} [style]
|
|
496
|
+
* Optional function or string that provides component-scoped CSS styles
|
|
497
|
+
* @property {Record<string, ComponentDefinition>} [children]
|
|
386
498
|
* Optional object defining nested child components
|
|
387
499
|
*/
|
|
388
500
|
|
|
389
501
|
/**
|
|
390
|
-
* @typedef {Object}
|
|
391
|
-
* @property {
|
|
392
|
-
*
|
|
393
|
-
* @property {
|
|
394
|
-
*
|
|
502
|
+
* @typedef {Object} ComponentContext
|
|
503
|
+
* @property {Record<string, unknown>} props
|
|
504
|
+
* Component properties passed during mounting
|
|
505
|
+
* @property {Emitter} emitter
|
|
506
|
+
* Event emitter instance for component event handling
|
|
507
|
+
* @property {function<T>(value: T): Signal<T>} signal
|
|
508
|
+
* Factory function to create reactive Signal instances
|
|
509
|
+
* @property {function(LifecycleHookContext): Promise<void>} [onBeforeMount]
|
|
510
|
+
* Hook called before component mounting
|
|
511
|
+
* @property {function(LifecycleHookContext): Promise<void>} [onMount]
|
|
512
|
+
* Hook called after component mounting
|
|
513
|
+
* @property {function(LifecycleHookContext): Promise<void>} [onBeforeUpdate]
|
|
514
|
+
* Hook called before component update
|
|
515
|
+
* @property {function(LifecycleHookContext): Promise<void>} [onUpdate]
|
|
516
|
+
* Hook called after component update
|
|
517
|
+
* @property {function(UnmountHookContext): Promise<void>} [onUnmount]
|
|
518
|
+
* Hook called during component unmounting
|
|
519
|
+
*/
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* @typedef {Object} LifecycleHookContext
|
|
523
|
+
* @property {HTMLElement} container
|
|
524
|
+
* The DOM element where the component is mounted
|
|
525
|
+
* @property {ComponentContext} context
|
|
526
|
+
* The component's reactive state and context data
|
|
527
|
+
*/
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* @typedef {Object} UnmountHookContext
|
|
531
|
+
* @property {HTMLElement} container
|
|
532
|
+
* The DOM element where the component is mounted
|
|
533
|
+
* @property {ComponentContext} context
|
|
534
|
+
* The component's reactive state and context data
|
|
535
|
+
* @property {{
|
|
536
|
+
* watchers: Array<() => void>, // Signal watcher cleanup functions
|
|
537
|
+
* listeners: Array<() => void>, // Event listener cleanup functions
|
|
538
|
+
* children: Array<MountResult> // Child component instances
|
|
539
|
+
* }} cleanup
|
|
540
|
+
* Object containing cleanup functions and instances
|
|
395
541
|
*/
|
|
396
542
|
|
|
397
543
|
/**
|
|
398
544
|
* @typedef {Object} MountResult
|
|
399
545
|
* @property {HTMLElement} container
|
|
400
546
|
* The DOM element where the component is mounted
|
|
401
|
-
* @property {
|
|
547
|
+
* @property {ComponentContext} data
|
|
402
548
|
* The component's reactive state and context data
|
|
403
|
-
* @property {function(): void} unmount
|
|
549
|
+
* @property {function(): Promise<void>} unmount
|
|
404
550
|
* Function to clean up and unmount the component
|
|
405
551
|
*/
|
|
406
552
|
|
|
553
|
+
/**
|
|
554
|
+
* @typedef {Object} ElevaPlugin
|
|
555
|
+
* @property {function(Eleva, Record<string, unknown>): void} install
|
|
556
|
+
* Function that installs the plugin into the Eleva instance
|
|
557
|
+
* @property {string} name
|
|
558
|
+
* Unique identifier name for the plugin
|
|
559
|
+
*/
|
|
560
|
+
|
|
407
561
|
/**
|
|
408
562
|
* @class 🧩 Eleva
|
|
409
563
|
* @classdesc A modern, signal-based component runtime framework that provides lifecycle hooks,
|
|
@@ -411,12 +565,26 @@ class Renderer {
|
|
|
411
565
|
* event handling, and DOM rendering with a focus on performance and developer experience.
|
|
412
566
|
*
|
|
413
567
|
* @example
|
|
568
|
+
* // Basic component creation and mounting
|
|
414
569
|
* const app = new Eleva("myApp");
|
|
415
570
|
* app.component("myComponent", {
|
|
416
|
-
*
|
|
417
|
-
*
|
|
571
|
+
* setup: (ctx) => ({ count: ctx.signal(0) }),
|
|
572
|
+
* template: (ctx) => `<div>Hello ${ctx.props.name}</div>`
|
|
418
573
|
* });
|
|
419
574
|
* app.mount(document.getElementById("app"), "myComponent", { name: "World" });
|
|
575
|
+
*
|
|
576
|
+
* @example
|
|
577
|
+
* // Using lifecycle hooks
|
|
578
|
+
* app.component("lifecycleDemo", {
|
|
579
|
+
* setup: () => {
|
|
580
|
+
* return {
|
|
581
|
+
* onMount: ({ container, context }) => {
|
|
582
|
+
* console.log('Component mounted!');
|
|
583
|
+
* }
|
|
584
|
+
* };
|
|
585
|
+
* },
|
|
586
|
+
* template: `<div>Lifecycle Demo</div>`
|
|
587
|
+
* });
|
|
420
588
|
*/
|
|
421
589
|
class Eleva {
|
|
422
590
|
/**
|
|
@@ -424,13 +592,24 @@ class Eleva {
|
|
|
424
592
|
*
|
|
425
593
|
* @public
|
|
426
594
|
* @param {string} name - The unique identifier name for this Eleva instance.
|
|
427
|
-
* @param {
|
|
595
|
+
* @param {Record<string, unknown>} [config={}] - Optional configuration object for the instance.
|
|
428
596
|
* May include framework-wide settings and default behaviors.
|
|
597
|
+
* @throws {Error} If the name is not provided or is not a string.
|
|
598
|
+
* @returns {Eleva} A new Eleva instance.
|
|
599
|
+
*
|
|
600
|
+
* @example
|
|
601
|
+
* const app = new Eleva("myApp");
|
|
602
|
+
* app.component("myComponent", {
|
|
603
|
+
* setup: (ctx) => ({ count: ctx.signal(0) }),
|
|
604
|
+
* template: (ctx) => `<div>Hello ${ctx.props.name}!</div>`
|
|
605
|
+
* });
|
|
606
|
+
* app.mount(document.getElementById("app"), "myComponent", { name: "World" });
|
|
607
|
+
*
|
|
429
608
|
*/
|
|
430
609
|
constructor(name, config = {}) {
|
|
431
610
|
/** @public {string} The unique identifier name for this Eleva instance */
|
|
432
611
|
this.name = name;
|
|
433
|
-
/** @public {Object<string,
|
|
612
|
+
/** @public {Object<string, unknown>} Optional configuration object for the Eleva instance */
|
|
434
613
|
this.config = config;
|
|
435
614
|
/** @public {Emitter} Instance of the event emitter for handling component events */
|
|
436
615
|
this.emitter = new Emitter();
|
|
@@ -443,8 +622,6 @@ class Eleva {
|
|
|
443
622
|
this._components = new Map();
|
|
444
623
|
/** @private {Map<string, ElevaPlugin>} Collection of installed plugin instances by name */
|
|
445
624
|
this._plugins = new Map();
|
|
446
|
-
/** @private {string[]} Array of lifecycle hook names supported by components */
|
|
447
|
-
this._lifecycleHooks = ["onBeforeMount", "onMount", "onBeforeUpdate", "onUpdate", "onUnmount"];
|
|
448
625
|
/** @private {boolean} Flag indicating if the root component is currently mounted */
|
|
449
626
|
this._isMounted = false;
|
|
450
627
|
}
|
|
@@ -456,7 +633,7 @@ class Eleva {
|
|
|
456
633
|
*
|
|
457
634
|
* @public
|
|
458
635
|
* @param {ElevaPlugin} plugin - The plugin object which must have an `install` function.
|
|
459
|
-
* @param {Object<string,
|
|
636
|
+
* @param {Object<string, unknown>} [options={}] - Optional configuration options for the plugin.
|
|
460
637
|
* @returns {Eleva} The Eleva instance (for method chaining).
|
|
461
638
|
* @example
|
|
462
639
|
* app.use(myPlugin, { option1: "value1" });
|
|
@@ -479,7 +656,7 @@ class Eleva {
|
|
|
479
656
|
* @example
|
|
480
657
|
* app.component("myButton", {
|
|
481
658
|
* template: (ctx) => `<button>${ctx.props.text}</button>`,
|
|
482
|
-
* style:
|
|
659
|
+
* style: `button { color: blue; }`
|
|
483
660
|
* });
|
|
484
661
|
*/
|
|
485
662
|
component(name, definition) {
|
|
@@ -495,7 +672,7 @@ class Eleva {
|
|
|
495
672
|
* @public
|
|
496
673
|
* @param {HTMLElement} container - The DOM element where the component will be mounted.
|
|
497
674
|
* @param {string|ComponentDefinition} compName - The name of the registered component or a direct component definition.
|
|
498
|
-
* @param {Object<string,
|
|
675
|
+
* @param {Object<string, unknown>} [props={}] - Optional properties to pass to the component.
|
|
499
676
|
* @returns {Promise<MountResult>}
|
|
500
677
|
* A Promise that resolves to an object containing:
|
|
501
678
|
* - container: The mounted component's container element
|
|
@@ -509,20 +686,17 @@ class Eleva {
|
|
|
509
686
|
*/
|
|
510
687
|
async mount(container, compName, props = {}) {
|
|
511
688
|
if (!container) throw new Error(`Container not found: ${container}`);
|
|
512
|
-
if (container._eleva_instance)
|
|
513
|
-
return container._eleva_instance;
|
|
514
|
-
}
|
|
689
|
+
if (container._eleva_instance) return container._eleva_instance;
|
|
515
690
|
|
|
516
691
|
/** @type {ComponentDefinition} */
|
|
517
692
|
const definition = typeof compName === "string" ? this._components.get(compName) : compName;
|
|
518
693
|
if (!definition) throw new Error(`Component "${compName}" not registered.`);
|
|
519
|
-
if (typeof definition.template !== "function") throw new Error("Component template must be a function");
|
|
520
694
|
|
|
521
695
|
/**
|
|
522
696
|
* Destructure the component definition to access core functionality.
|
|
523
697
|
* - setup: Optional function for component initialization and state management
|
|
524
|
-
* - template: Required function that returns the component's HTML structure
|
|
525
|
-
* - style: Optional function for component-scoped CSS styles
|
|
698
|
+
* - template: Required function or string that returns the component's HTML structure
|
|
699
|
+
* - style: Optional function or string for component-scoped CSS styles
|
|
526
700
|
* - children: Optional object defining nested child components
|
|
527
701
|
*/
|
|
528
702
|
const {
|
|
@@ -532,21 +706,12 @@ class Eleva {
|
|
|
532
706
|
children
|
|
533
707
|
} = definition;
|
|
534
708
|
|
|
535
|
-
/**
|
|
536
|
-
* Creates the initial context object for the component instance.
|
|
537
|
-
* This context provides core functionality and will be merged with setup data.
|
|
538
|
-
* @type {Object<string, any>}
|
|
539
|
-
* @property {Object<string, any>} props - Component properties passed during mounting
|
|
540
|
-
* @property {Emitter} emitter - Event emitter instance for component event handling
|
|
541
|
-
* @property {function(any): Signal} signal - Factory function to create reactive Signal instances
|
|
542
|
-
* @property {Object<string, function(): void>} ...lifecycleHooks - Prepared lifecycle hook functions
|
|
543
|
-
*/
|
|
709
|
+
/** @type {ComponentContext} */
|
|
544
710
|
const context = {
|
|
545
711
|
props,
|
|
546
712
|
emitter: this.emitter,
|
|
547
|
-
/** @type {(v:
|
|
548
|
-
signal: v => new this.signal(v)
|
|
549
|
-
...this._prepareLifecycleHooks()
|
|
713
|
+
/** @type {(v: unknown) => Signal<unknown>} */
|
|
714
|
+
signal: v => new this.signal(v)
|
|
550
715
|
};
|
|
551
716
|
|
|
552
717
|
/**
|
|
@@ -557,30 +722,38 @@ class Eleva {
|
|
|
557
722
|
* 3. Rendering the component
|
|
558
723
|
* 4. Managing component lifecycle
|
|
559
724
|
*
|
|
560
|
-
* @param {Object<string,
|
|
725
|
+
* @param {Object<string, unknown>} data - Data returned from the component's setup function
|
|
561
726
|
* @returns {Promise<MountResult>} An object containing:
|
|
562
727
|
* - container: The mounted component's container element
|
|
563
728
|
* - data: The component's reactive state and context
|
|
564
729
|
* - unmount: Function to clean up and unmount the component
|
|
565
730
|
*/
|
|
566
731
|
const processMount = async data => {
|
|
567
|
-
/** @type {
|
|
732
|
+
/** @type {ComponentContext} */
|
|
568
733
|
const mergedContext = {
|
|
569
734
|
...context,
|
|
570
735
|
...data
|
|
571
736
|
};
|
|
572
737
|
/** @type {Array<() => void>} */
|
|
573
|
-
const
|
|
738
|
+
const watchers = [];
|
|
574
739
|
/** @type {Array<MountResult>} */
|
|
575
740
|
const childInstances = [];
|
|
576
741
|
/** @type {Array<() => void>} */
|
|
577
|
-
const
|
|
742
|
+
const listeners = [];
|
|
578
743
|
|
|
579
744
|
// Execute before hooks
|
|
580
745
|
if (!this._isMounted) {
|
|
581
|
-
|
|
746
|
+
/** @type {LifecycleHookContext} */
|
|
747
|
+
await mergedContext.onBeforeMount?.({
|
|
748
|
+
container,
|
|
749
|
+
context: mergedContext
|
|
750
|
+
});
|
|
582
751
|
} else {
|
|
583
|
-
|
|
752
|
+
/** @type {LifecycleHookContext} */
|
|
753
|
+
await mergedContext.onBeforeUpdate?.({
|
|
754
|
+
container,
|
|
755
|
+
context: mergedContext
|
|
756
|
+
});
|
|
584
757
|
}
|
|
585
758
|
|
|
586
759
|
/**
|
|
@@ -590,17 +763,25 @@ class Eleva {
|
|
|
590
763
|
* 3. Processing events, injecting styles, and mounting child components.
|
|
591
764
|
*/
|
|
592
765
|
const render = async () => {
|
|
593
|
-
const templateResult = await template(mergedContext);
|
|
766
|
+
const templateResult = typeof template === "function" ? await template(mergedContext) : template;
|
|
594
767
|
const newHtml = TemplateEngine.parse(templateResult, mergedContext);
|
|
595
768
|
this.renderer.patchDOM(container, newHtml);
|
|
596
|
-
this._processEvents(container, mergedContext,
|
|
769
|
+
this._processEvents(container, mergedContext, listeners);
|
|
597
770
|
if (style) this._injectStyles(container, compName, style, mergedContext);
|
|
598
771
|
if (children) await this._mountComponents(container, children, childInstances);
|
|
599
772
|
if (!this._isMounted) {
|
|
600
|
-
|
|
773
|
+
/** @type {LifecycleHookContext} */
|
|
774
|
+
await mergedContext.onMount?.({
|
|
775
|
+
container,
|
|
776
|
+
context: mergedContext
|
|
777
|
+
});
|
|
601
778
|
this._isMounted = true;
|
|
602
779
|
} else {
|
|
603
|
-
|
|
780
|
+
/** @type {LifecycleHookContext} */
|
|
781
|
+
await mergedContext.onUpdate?.({
|
|
782
|
+
container,
|
|
783
|
+
context: mergedContext
|
|
784
|
+
});
|
|
604
785
|
}
|
|
605
786
|
};
|
|
606
787
|
|
|
@@ -610,7 +791,7 @@ class Eleva {
|
|
|
610
791
|
* Stores unsubscribe functions to clean up watchers when component unmounts.
|
|
611
792
|
*/
|
|
612
793
|
for (const val of Object.values(data)) {
|
|
613
|
-
if (val instanceof Signal)
|
|
794
|
+
if (val instanceof Signal) watchers.push(val.watch(render));
|
|
614
795
|
}
|
|
615
796
|
await render();
|
|
616
797
|
const instance = {
|
|
@@ -621,11 +802,20 @@ class Eleva {
|
|
|
621
802
|
*
|
|
622
803
|
* @returns {void}
|
|
623
804
|
*/
|
|
624
|
-
unmount: () => {
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
805
|
+
unmount: async () => {
|
|
806
|
+
/** @type {UnmountHookContext} */
|
|
807
|
+
await mergedContext.onUnmount?.({
|
|
808
|
+
container,
|
|
809
|
+
context: mergedContext,
|
|
810
|
+
cleanup: {
|
|
811
|
+
watchers: watchers,
|
|
812
|
+
listeners: listeners,
|
|
813
|
+
children: childInstances
|
|
814
|
+
}
|
|
815
|
+
});
|
|
816
|
+
for (const fn of watchers) fn();
|
|
817
|
+
for (const fn of listeners) fn();
|
|
818
|
+
for (const child of childInstances) await child.unmount();
|
|
629
819
|
container.innerHTML = "";
|
|
630
820
|
delete container._eleva_instance;
|
|
631
821
|
}
|
|
@@ -639,47 +829,37 @@ class Eleva {
|
|
|
639
829
|
return await processMount(setupResult);
|
|
640
830
|
}
|
|
641
831
|
|
|
642
|
-
/**
|
|
643
|
-
* Prepares default no-operation lifecycle hook functions for a component.
|
|
644
|
-
* These hooks will be called at various stages of the component's lifecycle.
|
|
645
|
-
*
|
|
646
|
-
* @private
|
|
647
|
-
* @returns {Object<string, function(): void>} An object mapping lifecycle hook names to empty functions.
|
|
648
|
-
* The returned object will be merged with the component's context.
|
|
649
|
-
*/
|
|
650
|
-
_prepareLifecycleHooks() {
|
|
651
|
-
/** @type {Object<string, () => void>} */
|
|
652
|
-
const hooks = {};
|
|
653
|
-
for (const hook of this._lifecycleHooks) {
|
|
654
|
-
hooks[hook] = () => {};
|
|
655
|
-
}
|
|
656
|
-
return hooks;
|
|
657
|
-
}
|
|
658
|
-
|
|
659
832
|
/**
|
|
660
833
|
* Processes DOM elements for event binding based on attributes starting with "@".
|
|
661
834
|
* This method handles the event delegation system and ensures proper cleanup of event listeners.
|
|
662
835
|
*
|
|
663
836
|
* @private
|
|
664
837
|
* @param {HTMLElement} container - The container element in which to search for event attributes.
|
|
665
|
-
* @param {
|
|
666
|
-
* @param {Array<
|
|
838
|
+
* @param {ComponentContext} context - The current component context containing event handler definitions.
|
|
839
|
+
* @param {Array<() => void>} listeners - Array to collect cleanup functions for each event listener.
|
|
667
840
|
* @returns {void}
|
|
668
841
|
*/
|
|
669
|
-
_processEvents(container, context,
|
|
842
|
+
_processEvents(container, context, listeners) {
|
|
843
|
+
/** @type {NodeListOf<Element>} */
|
|
670
844
|
const elements = container.querySelectorAll("*");
|
|
671
845
|
for (const el of elements) {
|
|
846
|
+
/** @type {NamedNodeMap} */
|
|
672
847
|
const attrs = el.attributes;
|
|
673
848
|
for (let i = 0; i < attrs.length; i++) {
|
|
849
|
+
/** @type {Attr} */
|
|
674
850
|
const attr = attrs[i];
|
|
675
851
|
if (!attr.name.startsWith("@")) continue;
|
|
852
|
+
|
|
853
|
+
/** @type {keyof HTMLElementEventMap} */
|
|
676
854
|
const event = attr.name.slice(1);
|
|
855
|
+
/** @type {string} */
|
|
677
856
|
const handlerName = attr.value;
|
|
857
|
+
/** @type {(event: Event) => void} */
|
|
678
858
|
const handler = context[handlerName] || TemplateEngine.evaluate(handlerName, context);
|
|
679
859
|
if (typeof handler === "function") {
|
|
680
860
|
el.addEventListener(event, handler);
|
|
681
861
|
el.removeAttribute(attr.name);
|
|
682
|
-
|
|
862
|
+
listeners.push(() => el.removeEventListener(event, handler));
|
|
683
863
|
}
|
|
684
864
|
}
|
|
685
865
|
}
|
|
@@ -692,18 +872,22 @@ class Eleva {
|
|
|
692
872
|
* @private
|
|
693
873
|
* @param {HTMLElement} container - The container element where styles should be injected.
|
|
694
874
|
* @param {string} compName - The component name used to identify the style element.
|
|
695
|
-
* @param {function(
|
|
696
|
-
* @param {
|
|
875
|
+
* @param {(function(ComponentContext): string)|string} styleDef - The component's style definition (function or string).
|
|
876
|
+
* @param {ComponentContext} context - The current component context for style interpolation.
|
|
697
877
|
* @returns {void}
|
|
698
878
|
*/
|
|
699
|
-
_injectStyles(container, compName,
|
|
700
|
-
|
|
879
|
+
_injectStyles(container, compName, styleDef, context) {
|
|
880
|
+
/** @type {string} */
|
|
881
|
+
const newStyle = typeof styleDef === "function" ? TemplateEngine.parse(styleDef(context), context) : styleDef;
|
|
882
|
+
/** @type {HTMLStyleElement|null} */
|
|
883
|
+
let styleEl = container.querySelector(`style[data-e-style="${compName}"]`);
|
|
884
|
+
if (styleEl && styleEl.textContent === newStyle) return;
|
|
701
885
|
if (!styleEl) {
|
|
702
886
|
styleEl = document.createElement("style");
|
|
703
|
-
styleEl.setAttribute("data-
|
|
887
|
+
styleEl.setAttribute("data-e-style", compName);
|
|
704
888
|
container.appendChild(styleEl);
|
|
705
889
|
}
|
|
706
|
-
styleEl.textContent =
|
|
890
|
+
styleEl.textContent = newStyle;
|
|
707
891
|
}
|
|
708
892
|
|
|
709
893
|
/**
|
|
@@ -713,7 +897,7 @@ class Eleva {
|
|
|
713
897
|
* @private
|
|
714
898
|
* @param {HTMLElement} element - The DOM element to extract props from
|
|
715
899
|
* @param {string} prefix - The prefix to look for in attributes
|
|
716
|
-
* @returns {
|
|
900
|
+
* @returns {Record<string, string>} An object containing the extracted props
|
|
717
901
|
* @example
|
|
718
902
|
* // For an element with attributes:
|
|
719
903
|
* // <div :name="John" :age="25">
|
|
@@ -759,7 +943,9 @@ class Eleva {
|
|
|
759
943
|
if (!selector) continue;
|
|
760
944
|
for (const el of container.querySelectorAll(selector)) {
|
|
761
945
|
if (!(el instanceof HTMLElement)) continue;
|
|
946
|
+
/** @type {Record<string, string>} */
|
|
762
947
|
const props = this._extractProps(el, ":");
|
|
948
|
+
/** @type {MountResult} */
|
|
763
949
|
const instance = await this.mount(el, component, props);
|
|
764
950
|
if (instance && !childInstances.includes(instance)) {
|
|
765
951
|
childInstances.push(instance);
|