eleva 1.2.7-alpha → 1.2.8-alpha
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 +2 -2
- package/dist/eleva.cjs.js +730 -0
- package/dist/eleva.cjs.js.map +1 -0
- package/dist/eleva.d.ts +305 -80
- package/dist/eleva.esm.js +278 -142
- package/dist/eleva.esm.js.map +1 -1
- package/dist/eleva.min.js +2 -1
- package/dist/eleva.min.js.map +1 -1
- package/dist/eleva.umd.js +278 -142
- package/dist/eleva.umd.js.map +1 -1
- package/package.json +10 -13
- package/src/core/Eleva.js +148 -84
- package/src/modules/Emitter.js +48 -18
- package/src/modules/Renderer.js +23 -6
- package/src/modules/Signal.js +25 -9
- package/src/modules/TemplateEngine.js +42 -27
- package/types/core/Eleva.d.ts +143 -80
- package/types/core/Eleva.d.ts.map +1 -1
- package/types/modules/Emitter.d.ts +35 -19
- package/types/modules/Emitter.d.ts.map +1 -1
- package/types/modules/Renderer.d.ts +26 -9
- package/types/modules/Renderer.d.ts.map +1 -1
- package/types/modules/Signal.d.ts +28 -12
- package/types/modules/Signal.d.ts.map +1 -1
- package/types/modules/TemplateEngine.d.ts +35 -17
- package/types/modules/TemplateEngine.d.ts.map +1 -1
package/dist/eleva.esm.js
CHANGED
|
@@ -1,42 +1,58 @@
|
|
|
1
|
+
/*! Eleva v1.2.8-alpha | MIT License | https://elevajs.com */
|
|
1
2
|
/**
|
|
2
3
|
* @class 🔒 TemplateEngine
|
|
3
|
-
* @classdesc
|
|
4
|
-
* Provides
|
|
5
|
-
*
|
|
4
|
+
* @classdesc A secure template engine that handles interpolation and dynamic attribute parsing.
|
|
5
|
+
* Provides a safe way to evaluate expressions in templates while preventing XSS attacks.
|
|
6
|
+
* All methods are static and can be called directly on the class.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const template = "Hello, {{name}}!";
|
|
10
|
+
* const data = { name: "World" };
|
|
11
|
+
* const result = TemplateEngine.parse(template, data); // Returns: "Hello, World!"
|
|
6
12
|
*/
|
|
7
13
|
class TemplateEngine {
|
|
8
14
|
/**
|
|
9
|
-
*
|
|
15
|
+
* @private {RegExp} Regular expression for matching template expressions in the format {{ expression }}
|
|
16
|
+
*/
|
|
17
|
+
static expressionPattern = /\{\{\s*(.*?)\s*\}\}/g;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Parses a template string, replacing expressions with their evaluated values.
|
|
21
|
+
* Expressions are evaluated in the provided data context.
|
|
10
22
|
*
|
|
11
|
-
* @
|
|
12
|
-
* @
|
|
13
|
-
* @
|
|
23
|
+
* @public
|
|
24
|
+
* @static
|
|
25
|
+
* @param {string} template - The template string to parse.
|
|
26
|
+
* @param {Object} data - The data context for evaluating expressions.
|
|
27
|
+
* @returns {string} The parsed template with expressions replaced by their values.
|
|
28
|
+
* @example
|
|
29
|
+
* const result = TemplateEngine.parse("{{user.name}} is {{user.age}} years old", {
|
|
30
|
+
* user: { name: "John", age: 30 }
|
|
31
|
+
* }); // Returns: "John is 30 years old"
|
|
14
32
|
*/
|
|
15
33
|
static parse(template, data) {
|
|
16
|
-
if (
|
|
17
|
-
return template.replace(
|
|
18
|
-
return this.evaluate(expr, data);
|
|
19
|
-
});
|
|
34
|
+
if (typeof template !== "string") return template;
|
|
35
|
+
return template.replace(this.expressionPattern, (_, expression) => this.evaluate(expression, data));
|
|
20
36
|
}
|
|
21
37
|
|
|
22
38
|
/**
|
|
23
|
-
* Evaluates
|
|
39
|
+
* Evaluates an expression in the context of the provided data object.
|
|
40
|
+
* Note: This does not provide a true sandbox and evaluated expressions may access global scope.
|
|
24
41
|
*
|
|
25
|
-
* @
|
|
26
|
-
* @
|
|
27
|
-
* @
|
|
42
|
+
* @public
|
|
43
|
+
* @static
|
|
44
|
+
* @param {string} expression - The expression to evaluate.
|
|
45
|
+
* @param {Object} data - The data context for evaluation.
|
|
46
|
+
* @returns {*} The result of the evaluation, or an empty string if evaluation fails.
|
|
47
|
+
* @example
|
|
48
|
+
* const result = TemplateEngine.evaluate("user.name", { user: { name: "John" } }); // Returns: "John"
|
|
49
|
+
* const age = TemplateEngine.evaluate("user.age", { user: { age: 30 } }); // Returns: 30
|
|
28
50
|
*/
|
|
29
|
-
static evaluate(
|
|
30
|
-
if (
|
|
51
|
+
static evaluate(expression, data) {
|
|
52
|
+
if (typeof expression !== "string") return expression;
|
|
31
53
|
try {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
} catch (error) {
|
|
35
|
-
console.error(`Template evaluation error:`, {
|
|
36
|
-
expression: expr,
|
|
37
|
-
data,
|
|
38
|
-
error: error.message
|
|
39
|
-
});
|
|
54
|
+
return new Function("data", `with(data) { return ${expression}; }`)(data);
|
|
55
|
+
} catch {
|
|
40
56
|
return "";
|
|
41
57
|
}
|
|
42
58
|
}
|
|
@@ -44,20 +60,26 @@ class TemplateEngine {
|
|
|
44
60
|
|
|
45
61
|
/**
|
|
46
62
|
* @class ⚡ Signal
|
|
47
|
-
* @classdesc
|
|
48
|
-
*
|
|
49
|
-
*
|
|
63
|
+
* @classdesc A reactive data holder that enables fine-grained reactivity in the Eleva framework.
|
|
64
|
+
* Signals notify registered watchers when their value changes, enabling efficient DOM updates
|
|
65
|
+
* through targeted patching rather than full re-renders.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* const count = new Signal(0);
|
|
69
|
+
* count.watch((value) => console.log(`Count changed to: ${value}`));
|
|
70
|
+
* count.value = 1; // Logs: "Count changed to: 1"
|
|
50
71
|
*/
|
|
51
72
|
class Signal {
|
|
52
73
|
/**
|
|
53
|
-
* Creates a new Signal instance.
|
|
74
|
+
* Creates a new Signal instance with the specified initial value.
|
|
54
75
|
*
|
|
76
|
+
* @public
|
|
55
77
|
* @param {*} value - The initial value of the signal.
|
|
56
78
|
*/
|
|
57
79
|
constructor(value) {
|
|
58
|
-
/** @private {
|
|
80
|
+
/** @private {T} Internal storage for the signal's current value, where T is the type of the initial value */
|
|
59
81
|
this._value = value;
|
|
60
|
-
/** @private {Set<function>} Collection of callback functions to be notified when value changes */
|
|
82
|
+
/** @private {Set<function(T): void>} Collection of callback functions to be notified when value changes, where T is the value type */
|
|
61
83
|
this._watchers = new Set();
|
|
62
84
|
/** @private {boolean} Flag to prevent multiple synchronous watcher notifications and batch updates into microtasks */
|
|
63
85
|
this._pending = false;
|
|
@@ -66,7 +88,8 @@ class Signal {
|
|
|
66
88
|
/**
|
|
67
89
|
* Gets the current value of the signal.
|
|
68
90
|
*
|
|
69
|
-
* @
|
|
91
|
+
* @public
|
|
92
|
+
* @returns {T} The current value, where T is the type of the initial value.
|
|
70
93
|
*/
|
|
71
94
|
get value() {
|
|
72
95
|
return this._value;
|
|
@@ -74,8 +97,11 @@ class Signal {
|
|
|
74
97
|
|
|
75
98
|
/**
|
|
76
99
|
* Sets a new value for the signal and notifies all registered watchers if the value has changed.
|
|
100
|
+
* The notification is batched using microtasks to prevent multiple synchronous updates.
|
|
77
101
|
*
|
|
78
|
-
* @
|
|
102
|
+
* @public
|
|
103
|
+
* @param {T} newVal - The new value to set, where T is the type of the initial value.
|
|
104
|
+
* @returns {void}
|
|
79
105
|
*/
|
|
80
106
|
set value(newVal) {
|
|
81
107
|
if (newVal !== this._value) {
|
|
@@ -86,9 +112,15 @@ class Signal {
|
|
|
86
112
|
|
|
87
113
|
/**
|
|
88
114
|
* Registers a watcher function that will be called whenever the signal's value changes.
|
|
115
|
+
* The watcher will receive the new value as its argument.
|
|
89
116
|
*
|
|
90
|
-
* @
|
|
117
|
+
* @public
|
|
118
|
+
* @param {function(T): void} fn - The callback function to invoke on value change, where T is the value type.
|
|
91
119
|
* @returns {function(): boolean} A function to unsubscribe the watcher.
|
|
120
|
+
* @example
|
|
121
|
+
* const unsubscribe = signal.watch((value) => console.log(value));
|
|
122
|
+
* // Later...
|
|
123
|
+
* unsubscribe(); // Stops watching for changes
|
|
92
124
|
*/
|
|
93
125
|
watch(fn) {
|
|
94
126
|
this._watchers.add(fn);
|
|
@@ -115,66 +147,105 @@ class Signal {
|
|
|
115
147
|
}
|
|
116
148
|
|
|
117
149
|
/**
|
|
118
|
-
* @class
|
|
119
|
-
* @classdesc
|
|
120
|
-
*
|
|
121
|
-
*
|
|
150
|
+
* @class 📡 Emitter
|
|
151
|
+
* @classdesc A robust event emitter that enables inter-component communication through a publish-subscribe pattern.
|
|
152
|
+
* Components can emit events and listen for events from other components, facilitating loose coupling
|
|
153
|
+
* and reactive updates across the application.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* const emitter = new Emitter();
|
|
157
|
+
* emitter.on('user:login', (user) => console.log(`User logged in: ${user.name}`));
|
|
158
|
+
* emitter.emit('user:login', { name: 'John' }); // Logs: "User logged in: John"
|
|
122
159
|
*/
|
|
123
160
|
class Emitter {
|
|
124
161
|
/**
|
|
125
162
|
* Creates a new Emitter instance.
|
|
163
|
+
*
|
|
164
|
+
* @public
|
|
126
165
|
*/
|
|
127
166
|
constructor() {
|
|
128
|
-
/** @
|
|
129
|
-
this.
|
|
167
|
+
/** @private {Map<string, Set<function(any): void>>} Map of event names to their registered handler functions */
|
|
168
|
+
this._events = new Map();
|
|
130
169
|
}
|
|
131
170
|
|
|
132
171
|
/**
|
|
133
|
-
* Registers an event handler for the specified event.
|
|
172
|
+
* Registers an event handler for the specified event name.
|
|
173
|
+
* The handler will be called with the event data when the event is emitted.
|
|
134
174
|
*
|
|
135
|
-
* @
|
|
136
|
-
* @param {
|
|
175
|
+
* @public
|
|
176
|
+
* @param {string} event - The name of the event to listen for.
|
|
177
|
+
* @param {function(any): void} handler - The callback function to invoke when the event occurs.
|
|
178
|
+
* @returns {function(): boolean} A function to unsubscribe the event handler.
|
|
179
|
+
* @example
|
|
180
|
+
* const unsubscribe = emitter.on('user:login', (user) => console.log(user));
|
|
181
|
+
* // Later...
|
|
182
|
+
* unsubscribe(); // Stops listening for the event
|
|
137
183
|
*/
|
|
138
184
|
on(event, handler) {
|
|
139
|
-
(this.
|
|
185
|
+
if (!this._events.has(event)) this._events.set(event, new Set());
|
|
186
|
+
this._events.get(event).add(handler);
|
|
187
|
+
return () => this.off(event, handler);
|
|
140
188
|
}
|
|
141
189
|
|
|
142
190
|
/**
|
|
143
|
-
* Removes
|
|
191
|
+
* Removes an event handler for the specified event name.
|
|
192
|
+
* If no handler is provided, all handlers for the event are removed.
|
|
144
193
|
*
|
|
194
|
+
* @public
|
|
145
195
|
* @param {string} event - The name of the event.
|
|
146
|
-
* @param {function(
|
|
196
|
+
* @param {function(any): void} [handler] - The specific handler function to remove.
|
|
197
|
+
* @returns {void}
|
|
147
198
|
*/
|
|
148
199
|
off(event, handler) {
|
|
149
|
-
if (this.
|
|
150
|
-
|
|
200
|
+
if (!this._events.has(event)) return;
|
|
201
|
+
if (handler) {
|
|
202
|
+
const handlers = this._events.get(event);
|
|
203
|
+
handlers.delete(handler);
|
|
204
|
+
// Remove the event if there are no handlers left
|
|
205
|
+
if (handlers.size === 0) this._events.delete(event);
|
|
206
|
+
} else {
|
|
207
|
+
this._events.delete(event);
|
|
151
208
|
}
|
|
152
209
|
}
|
|
153
210
|
|
|
154
211
|
/**
|
|
155
|
-
* Emits an event
|
|
212
|
+
* Emits an event with the specified data to all registered handlers.
|
|
213
|
+
* Handlers are called synchronously in the order they were registered.
|
|
156
214
|
*
|
|
157
|
-
* @
|
|
158
|
-
* @param {
|
|
215
|
+
* @public
|
|
216
|
+
* @param {string} event - The name of the event to emit.
|
|
217
|
+
* @param {...any} args - Optional arguments to pass to the event handlers.
|
|
218
|
+
* @returns {void}
|
|
159
219
|
*/
|
|
160
220
|
emit(event, ...args) {
|
|
161
|
-
(this.
|
|
221
|
+
if (!this._events.has(event)) return;
|
|
222
|
+
this._events.get(event).forEach(handler => handler(...args));
|
|
162
223
|
}
|
|
163
224
|
}
|
|
164
225
|
|
|
165
226
|
/**
|
|
166
227
|
* @class 🎨 Renderer
|
|
167
|
-
* @classdesc
|
|
168
|
-
* Provides methods for
|
|
169
|
-
*
|
|
228
|
+
* @classdesc A DOM renderer that handles efficient DOM updates through patching and diffing.
|
|
229
|
+
* Provides methods for updating the DOM by comparing new and old structures and applying
|
|
230
|
+
* only the necessary changes, minimizing layout thrashing and improving performance.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* const renderer = new Renderer();
|
|
234
|
+
* const container = document.getElementById("app");
|
|
235
|
+
* const newHtml = "<div>Updated content</div>";
|
|
236
|
+
* renderer.patchDOM(container, newHtml);
|
|
170
237
|
*/
|
|
171
238
|
class Renderer {
|
|
172
239
|
/**
|
|
173
240
|
* Patches the DOM of a container element with new HTML content.
|
|
241
|
+
* This method efficiently updates the DOM by comparing the new content with the existing
|
|
242
|
+
* content and applying only the necessary changes.
|
|
174
243
|
*
|
|
244
|
+
* @public
|
|
175
245
|
* @param {HTMLElement} container - The container element to patch.
|
|
176
246
|
* @param {string} newHtml - The new HTML content to apply.
|
|
177
|
-
* @
|
|
247
|
+
* @returns {void}
|
|
248
|
+
* @throws {Error} If container is not an HTMLElement or newHtml is not a string.
|
|
178
249
|
*/
|
|
179
250
|
patchDOM(container, newHtml) {
|
|
180
251
|
if (!(container instanceof HTMLElement)) {
|
|
@@ -191,10 +262,14 @@ class Renderer {
|
|
|
191
262
|
|
|
192
263
|
/**
|
|
193
264
|
* Diffs two DOM trees (old and new) and applies updates to the old DOM.
|
|
265
|
+
* This method recursively compares nodes and their attributes, applying only
|
|
266
|
+
* the necessary changes to minimize DOM operations.
|
|
194
267
|
*
|
|
268
|
+
* @private
|
|
195
269
|
* @param {HTMLElement} oldParent - The original DOM element.
|
|
196
270
|
* @param {HTMLElement} newParent - The new DOM element.
|
|
197
|
-
* @
|
|
271
|
+
* @returns {void}
|
|
272
|
+
* @throws {Error} If either parent is not an HTMLElement.
|
|
198
273
|
*/
|
|
199
274
|
diff(oldParent, newParent) {
|
|
200
275
|
if (!(oldParent instanceof HTMLElement) || !(newParent instanceof HTMLElement)) {
|
|
@@ -241,10 +316,13 @@ class Renderer {
|
|
|
241
316
|
|
|
242
317
|
/**
|
|
243
318
|
* Updates the attributes of an element to match those of a new element.
|
|
319
|
+
* Handles special cases for ARIA attributes, data attributes, and boolean properties.
|
|
244
320
|
*
|
|
321
|
+
* @private
|
|
245
322
|
* @param {HTMLElement} oldEl - The element to update.
|
|
246
323
|
* @param {HTMLElement} newEl - The element providing the updated attributes.
|
|
247
|
-
* @
|
|
324
|
+
* @returns {void}
|
|
325
|
+
* @throws {Error} If either element is not an HTMLElement.
|
|
248
326
|
*/
|
|
249
327
|
updateAttributes(oldEl, newEl) {
|
|
250
328
|
if (!(oldEl instanceof HTMLElement) || !(newEl instanceof HTMLElement)) {
|
|
@@ -299,101 +377,143 @@ class Renderer {
|
|
|
299
377
|
}
|
|
300
378
|
|
|
301
379
|
/**
|
|
302
|
-
* Defines the structure and behavior of a component.
|
|
303
380
|
* @typedef {Object} ComponentDefinition
|
|
304
381
|
* @property {function(Object<string, any>): (Object<string, any>|Promise<Object<string, any>>)} [setup]
|
|
305
|
-
* Optional setup function that initializes the component's
|
|
306
|
-
* Receives props and context as an argument and should return an object containing the component's state.
|
|
307
|
-
* Can return either a synchronous object or a Promise that resolves to an object for async initialization.
|
|
308
|
-
*
|
|
382
|
+
* Optional setup function that initializes the component's state and returns reactive data
|
|
309
383
|
* @property {function(Object<string, any>): string} template
|
|
310
|
-
* Required function that defines the component's HTML structure
|
|
311
|
-
* Receives the merged context (props + setup data) and must return an HTML template string.
|
|
312
|
-
* Supports dynamic expressions using {{ }} syntax for reactive data binding.
|
|
313
|
-
*
|
|
384
|
+
* Required function that defines the component's HTML structure
|
|
314
385
|
* @property {function(Object<string, any>): string} [style]
|
|
315
|
-
* Optional function that
|
|
316
|
-
* Receives the merged context and returns a CSS string that will be automatically scoped to the component.
|
|
317
|
-
* Styles are injected into the component's container and only affect elements within it.
|
|
318
|
-
*
|
|
386
|
+
* Optional function that provides component-scoped CSS styles
|
|
319
387
|
* @property {Object<string, ComponentDefinition>} [children]
|
|
320
|
-
* Optional object
|
|
321
|
-
|
|
322
|
-
|
|
388
|
+
* Optional object defining nested child components
|
|
389
|
+
*/
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* @typedef {Object} ElevaPlugin
|
|
393
|
+
* @property {function(Eleva, Object<string, any>): void} install
|
|
394
|
+
* Function that installs the plugin into the Eleva instance
|
|
395
|
+
* @property {string} name
|
|
396
|
+
* Unique identifier name for the plugin
|
|
397
|
+
*/
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* @typedef {Object} MountResult
|
|
401
|
+
* @property {HTMLElement} container
|
|
402
|
+
* The DOM element where the component is mounted
|
|
403
|
+
* @property {Object<string, any>} data
|
|
404
|
+
* The component's reactive state and context data
|
|
405
|
+
* @property {function(): void} unmount
|
|
406
|
+
* Function to clean up and unmount the component
|
|
323
407
|
*/
|
|
324
408
|
|
|
325
409
|
/**
|
|
326
410
|
* @class 🧩 Eleva
|
|
327
|
-
* @classdesc
|
|
328
|
-
*
|
|
411
|
+
* @classdesc A modern, signal-based component runtime framework that provides lifecycle hooks,
|
|
412
|
+
* scoped styles, and plugin support. Eleva manages component registration, plugin integration,
|
|
413
|
+
* event handling, and DOM rendering with a focus on performance and developer experience.
|
|
414
|
+
*
|
|
415
|
+
* @example
|
|
416
|
+
* const app = new Eleva("myApp");
|
|
417
|
+
* app.component("myComponent", {
|
|
418
|
+
* template: (ctx) => `<div>Hello ${ctx.props.name}</div>`,
|
|
419
|
+
* setup: (ctx) => ({ count: new Signal(0) })
|
|
420
|
+
* });
|
|
421
|
+
* app.mount(document.getElementById("app"), "myComponent", { name: "World" });
|
|
329
422
|
*/
|
|
330
423
|
class Eleva {
|
|
331
424
|
/**
|
|
332
|
-
* Creates a new Eleva instance.
|
|
425
|
+
* Creates a new Eleva instance with the specified name and configuration.
|
|
333
426
|
*
|
|
334
|
-
* @
|
|
335
|
-
* @param {
|
|
427
|
+
* @public
|
|
428
|
+
* @param {string} name - The unique identifier name for this Eleva instance.
|
|
429
|
+
* @param {Object<string, any>} [config={}] - Optional configuration object for the instance.
|
|
430
|
+
* May include framework-wide settings and default behaviors.
|
|
336
431
|
*/
|
|
337
432
|
constructor(name, config = {}) {
|
|
338
|
-
/** @
|
|
433
|
+
/** @public {string} The unique identifier name for this Eleva instance */
|
|
339
434
|
this.name = name;
|
|
340
|
-
/** @
|
|
435
|
+
/** @public {Object<string, any>} Optional configuration object for the Eleva instance */
|
|
341
436
|
this.config = config;
|
|
342
|
-
/** @
|
|
343
|
-
this._components = {};
|
|
344
|
-
/** @private {Array<Object>} Collection of installed plugin instances */
|
|
345
|
-
this._plugins = [];
|
|
346
|
-
/** @private {string[]} Array of lifecycle hook names supported by the component */
|
|
347
|
-
this._lifecycleHooks = ["onBeforeMount", "onMount", "onBeforeUpdate", "onUpdate", "onUnmount"];
|
|
348
|
-
/** @private {boolean} Flag indicating if component is currently mounted */
|
|
349
|
-
this._isMounted = false;
|
|
350
|
-
/** @private {Emitter} Instance of the event emitter for handling component events */
|
|
437
|
+
/** @public {Emitter} Instance of the event emitter for handling component events */
|
|
351
438
|
this.emitter = new Emitter();
|
|
352
|
-
/** @
|
|
439
|
+
/** @public {typeof Signal} Static reference to the Signal class for creating reactive state */
|
|
353
440
|
this.signal = Signal;
|
|
354
|
-
/** @
|
|
441
|
+
/** @public {Renderer} Instance of the renderer for handling DOM updates and patching */
|
|
355
442
|
this.renderer = new Renderer();
|
|
443
|
+
|
|
444
|
+
/** @private {Map<string, ComponentDefinition>} Registry of all component definitions by name */
|
|
445
|
+
this._components = new Map();
|
|
446
|
+
/** @private {Map<string, ElevaPlugin>} Collection of installed plugin instances by name */
|
|
447
|
+
this._plugins = new Map();
|
|
448
|
+
/** @private {string[]} Array of lifecycle hook names supported by components */
|
|
449
|
+
this._lifecycleHooks = ["onBeforeMount", "onMount", "onBeforeUpdate", "onUpdate", "onUnmount"];
|
|
450
|
+
/** @private {boolean} Flag indicating if the root component is currently mounted */
|
|
451
|
+
this._isMounted = false;
|
|
356
452
|
}
|
|
357
453
|
|
|
358
454
|
/**
|
|
359
455
|
* Integrates a plugin with the Eleva framework.
|
|
456
|
+
* The plugin's install function will be called with the Eleva instance and provided options.
|
|
457
|
+
* After installation, the plugin will be available for use by components.
|
|
360
458
|
*
|
|
361
|
-
* @
|
|
362
|
-
* @param {
|
|
363
|
-
* @
|
|
459
|
+
* @public
|
|
460
|
+
* @param {ElevaPlugin} plugin - The plugin object which must have an `install` function.
|
|
461
|
+
* @param {Object<string, any>} [options={}] - Optional configuration options for the plugin.
|
|
462
|
+
* @returns {Eleva} The Eleva instance (for method chaining).
|
|
463
|
+
* @example
|
|
464
|
+
* app.use(myPlugin, { option1: "value1" });
|
|
364
465
|
*/
|
|
365
466
|
use(plugin, options = {}) {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
}
|
|
369
|
-
this._plugins.push(plugin);
|
|
467
|
+
plugin.install(this, options);
|
|
468
|
+
this._plugins.set(plugin.name, plugin);
|
|
370
469
|
return this;
|
|
371
470
|
}
|
|
372
471
|
|
|
373
472
|
/**
|
|
374
|
-
* Registers a component with the Eleva instance.
|
|
473
|
+
* Registers a new component with the Eleva instance.
|
|
474
|
+
* The component will be available for mounting using its registered name.
|
|
375
475
|
*
|
|
376
|
-
* @
|
|
476
|
+
* @public
|
|
477
|
+
* @param {string} name - The unique name of the component to register.
|
|
377
478
|
* @param {ComponentDefinition} definition - The component definition including setup, template, style, and children.
|
|
378
|
-
* @returns {Eleva} The Eleva instance (for chaining).
|
|
479
|
+
* @returns {Eleva} The Eleva instance (for method chaining).
|
|
480
|
+
* @throws {Error} If the component name is already registered.
|
|
481
|
+
* @example
|
|
482
|
+
* app.component("myButton", {
|
|
483
|
+
* template: (ctx) => `<button>${ctx.props.text}</button>`,
|
|
484
|
+
* style: () => "button { color: blue; }"
|
|
485
|
+
* });
|
|
379
486
|
*/
|
|
380
487
|
component(name, definition) {
|
|
381
|
-
|
|
488
|
+
/** @type {Map<string, ComponentDefinition>} */
|
|
489
|
+
this._components.set(name, definition);
|
|
382
490
|
return this;
|
|
383
491
|
}
|
|
384
492
|
|
|
385
493
|
/**
|
|
386
494
|
* Mounts a registered component to a DOM element.
|
|
495
|
+
* This will initialize the component, set up its reactive state, and render it to the DOM.
|
|
387
496
|
*
|
|
388
|
-
* @
|
|
389
|
-
* @param {
|
|
497
|
+
* @public
|
|
498
|
+
* @param {HTMLElement} container - The DOM element where the component will be mounted.
|
|
499
|
+
* @param {string|ComponentDefinition} compName - The name of the registered component or a direct component definition.
|
|
390
500
|
* @param {Object<string, any>} [props={}] - Optional properties to pass to the component.
|
|
391
|
-
* @returns {
|
|
392
|
-
*
|
|
501
|
+
* @returns {Promise<MountResult>}
|
|
502
|
+
* A Promise that resolves to an object containing:
|
|
503
|
+
* - container: The mounted component's container element
|
|
504
|
+
* - data: The component's reactive state and context
|
|
505
|
+
* - unmount: Function to clean up and unmount the component
|
|
506
|
+
* @throws {Error} If the container is not found, or component is not registered.
|
|
507
|
+
* @example
|
|
508
|
+
* const instance = await app.mount(document.getElementById("app"), "myComponent", { text: "Click me" });
|
|
509
|
+
* // Later...
|
|
510
|
+
* instance.unmount();
|
|
393
511
|
*/
|
|
394
|
-
mount(container, compName, props = {}) {
|
|
512
|
+
async mount(container, compName, props = {}) {
|
|
395
513
|
if (!container) throw new Error(`Container not found: ${container}`);
|
|
396
|
-
|
|
514
|
+
|
|
515
|
+
/** @type {ComponentDefinition} */
|
|
516
|
+
const definition = typeof compName === "string" ? this._components.get(compName) : compName;
|
|
397
517
|
if (!definition) throw new Error(`Component "${compName}" not registered.`);
|
|
398
518
|
if (typeof definition.template !== "function") throw new Error("Component template must be a function");
|
|
399
519
|
|
|
@@ -423,6 +543,7 @@ class Eleva {
|
|
|
423
543
|
const context = {
|
|
424
544
|
props,
|
|
425
545
|
emitter: this.emitter,
|
|
546
|
+
/** @type {(v: any) => Signal} */
|
|
426
547
|
signal: v => new this.signal(v),
|
|
427
548
|
...this._prepareLifecycleHooks()
|
|
428
549
|
};
|
|
@@ -431,15 +552,18 @@ class Eleva {
|
|
|
431
552
|
* Processes the mounting of the component.
|
|
432
553
|
*
|
|
433
554
|
* @param {Object<string, any>} data - Data returned from the component's setup function.
|
|
434
|
-
* @returns {
|
|
555
|
+
* @returns {MountResult} An object with the container, merged context data, and an unmount function.
|
|
435
556
|
*/
|
|
436
557
|
const processMount = data => {
|
|
437
558
|
const mergedContext = {
|
|
438
559
|
...context,
|
|
439
560
|
...data
|
|
440
561
|
};
|
|
562
|
+
/** @type {Array<() => void>} */
|
|
441
563
|
const watcherUnsubscribers = [];
|
|
564
|
+
/** @type {Array<MountResult>} */
|
|
442
565
|
const childInstances = [];
|
|
566
|
+
/** @type {Array<() => void>} */
|
|
443
567
|
const cleanupListeners = [];
|
|
444
568
|
|
|
445
569
|
// Execute before hooks
|
|
@@ -472,9 +596,9 @@ class Eleva {
|
|
|
472
596
|
* When a Signal's value changes, the component will re-render to reflect the updates.
|
|
473
597
|
* Stores unsubscribe functions to clean up watchers when component unmounts.
|
|
474
598
|
*/
|
|
475
|
-
Object.values(data)
|
|
599
|
+
for (const val of Object.values(data)) {
|
|
476
600
|
if (val instanceof Signal) watcherUnsubscribers.push(val.watch(render));
|
|
477
|
-
}
|
|
601
|
+
}
|
|
478
602
|
render();
|
|
479
603
|
return {
|
|
480
604
|
container,
|
|
@@ -485,9 +609,9 @@ class Eleva {
|
|
|
485
609
|
* @returns {void}
|
|
486
610
|
*/
|
|
487
611
|
unmount: () => {
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
612
|
+
for (const fn of watcherUnsubscribers) fn();
|
|
613
|
+
for (const fn of cleanupListeners) fn();
|
|
614
|
+
for (const child of childInstances) child.unmount();
|
|
491
615
|
mergedContext.onUnmount && mergedContext.onUnmount();
|
|
492
616
|
container.innerHTML = "";
|
|
493
617
|
}
|
|
@@ -499,26 +623,31 @@ class Eleva {
|
|
|
499
623
|
}
|
|
500
624
|
|
|
501
625
|
/**
|
|
502
|
-
* Prepares default no-operation lifecycle hook functions.
|
|
626
|
+
* Prepares default no-operation lifecycle hook functions for a component.
|
|
627
|
+
* These hooks will be called at various stages of the component's lifecycle.
|
|
503
628
|
*
|
|
504
|
-
* @returns {Object<string, function(): void>} An object with keys for lifecycle hooks mapped to empty functions.
|
|
505
629
|
* @private
|
|
630
|
+
* @returns {Object<string, function(): void>} An object mapping lifecycle hook names to empty functions.
|
|
631
|
+
* The returned object will be merged with the component's context.
|
|
506
632
|
*/
|
|
507
633
|
_prepareLifecycleHooks() {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
634
|
+
/** @type {Object<string, () => void>} */
|
|
635
|
+
const hooks = {};
|
|
636
|
+
for (const hook of this._lifecycleHooks) {
|
|
637
|
+
hooks[hook] = () => {};
|
|
638
|
+
}
|
|
639
|
+
return hooks;
|
|
512
640
|
}
|
|
513
641
|
|
|
514
642
|
/**
|
|
515
643
|
* Processes DOM elements for event binding based on attributes starting with "@".
|
|
516
|
-
*
|
|
644
|
+
* This method handles the event delegation system and ensures proper cleanup of event listeners.
|
|
517
645
|
*
|
|
518
|
-
* @param {HTMLElement} container - The container element in which to search for events.
|
|
519
|
-
* @param {Object<string, any>} context - The current context containing event handler definitions.
|
|
520
|
-
* @param {Array<Function>} cleanupListeners - Array to collect cleanup functions for each event listener.
|
|
521
646
|
* @private
|
|
647
|
+
* @param {HTMLElement} container - The container element in which to search for event attributes.
|
|
648
|
+
* @param {Object<string, any>} context - The current component context containing event handler definitions.
|
|
649
|
+
* @param {Array<Function>} cleanupListeners - Array to collect cleanup functions for each event listener.
|
|
650
|
+
* @returns {void}
|
|
522
651
|
*/
|
|
523
652
|
_processEvents(container, context, cleanupListeners) {
|
|
524
653
|
const elements = container.querySelectorAll("*");
|
|
@@ -541,12 +670,14 @@ class Eleva {
|
|
|
541
670
|
|
|
542
671
|
/**
|
|
543
672
|
* Injects scoped styles into the component's container.
|
|
673
|
+
* The styles are automatically prefixed to prevent style leakage to other components.
|
|
544
674
|
*
|
|
545
|
-
* @param {HTMLElement} container - The container element.
|
|
546
|
-
* @param {string} compName - The component name used to identify the style element.
|
|
547
|
-
* @param {function(Object<string, any>): string} [styleFn] - A function that returns CSS styles as a string.
|
|
548
|
-
* @param {Object<string, any>} context - The current context for style interpolation.
|
|
549
675
|
* @private
|
|
676
|
+
* @param {HTMLElement} container - The container element where styles should be injected.
|
|
677
|
+
* @param {string} compName - The component name used to identify the style element.
|
|
678
|
+
* @param {function(Object<string, any>): string} [styleFn] - Optional function that returns CSS styles as a string.
|
|
679
|
+
* @param {Object<string, any>} context - The current component context for style interpolation.
|
|
680
|
+
* @returns {void}
|
|
550
681
|
*/
|
|
551
682
|
_injectStyles(container, compName, styleFn, context) {
|
|
552
683
|
if (!styleFn) return;
|
|
@@ -561,30 +692,35 @@ class Eleva {
|
|
|
561
692
|
|
|
562
693
|
/**
|
|
563
694
|
* Mounts child components within the parent component's container.
|
|
695
|
+
* This method handles the recursive mounting of nested components.
|
|
564
696
|
*
|
|
565
|
-
* @param {HTMLElement} container - The parent container element.
|
|
566
|
-
* @param {Object<string, ComponentDefinition>} [children] - An object mapping child component selectors to their definitions.
|
|
567
|
-
* @param {Array<object>} childInstances - An array to store the mounted child component instances.
|
|
568
697
|
* @private
|
|
698
|
+
* @param {HTMLElement} container - The parent container element.
|
|
699
|
+
* @param {Object<string, ComponentDefinition>} [children] - Object mapping of child component selectors to their definitions.
|
|
700
|
+
* @param {Array<MountResult>} childInstances - Array to store the mounted child component instances.
|
|
701
|
+
* @returns {void}
|
|
569
702
|
*/
|
|
570
|
-
_mountChildren(container, children, childInstances) {
|
|
571
|
-
|
|
703
|
+
async _mountChildren(container, children, childInstances) {
|
|
704
|
+
for (const child of childInstances) child.unmount();
|
|
572
705
|
childInstances.length = 0;
|
|
573
|
-
|
|
574
|
-
|
|
706
|
+
if (!children) return;
|
|
707
|
+
for (const childSelector of Object.keys(children)) {
|
|
708
|
+
if (!childSelector) continue;
|
|
709
|
+
for (const childEl of container.querySelectorAll(childSelector)) {
|
|
710
|
+
if (!(childEl instanceof HTMLElement)) continue;
|
|
575
711
|
const props = {};
|
|
576
|
-
|
|
712
|
+
for (const {
|
|
577
713
|
name,
|
|
578
714
|
value
|
|
579
|
-
})
|
|
715
|
+
} of [...childEl.attributes]) {
|
|
580
716
|
if (name.startsWith("eleva-prop-")) {
|
|
581
717
|
props[name.replace("eleva-prop-", "")] = value;
|
|
582
718
|
}
|
|
583
|
-
}
|
|
584
|
-
const instance = this.mount(childEl, children[childSelector], props);
|
|
719
|
+
}
|
|
720
|
+
const instance = await this.mount(childEl, children[childSelector], props);
|
|
585
721
|
childInstances.push(instance);
|
|
586
|
-
}
|
|
587
|
-
}
|
|
722
|
+
}
|
|
723
|
+
}
|
|
588
724
|
}
|
|
589
725
|
}
|
|
590
726
|
|