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