eleva 1.0.0-alpha → 1.0.0-rc.2
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/LICENSE +1 -1
- package/README.md +186 -137
- package/dist/eleva.cjs.js +978 -0
- package/dist/eleva.cjs.js.map +1 -0
- package/dist/eleva.d.ts +521 -79
- package/dist/eleva.esm.js +751 -265
- package/dist/eleva.esm.js.map +1 -1
- package/dist/eleva.umd.js +751 -265
- package/dist/eleva.umd.js.map +1 -1
- package/dist/eleva.umd.min.js +3 -0
- package/dist/eleva.umd.min.js.map +1 -0
- package/package.json +188 -62
- package/src/core/Eleva.js +388 -140
- package/src/modules/Emitter.js +64 -18
- package/src/modules/Renderer.js +257 -85
- package/src/modules/Signal.js +55 -13
- package/src/modules/TemplateEngine.js +45 -27
- package/types/core/Eleva.d.ts +317 -48
- package/types/core/Eleva.d.ts.map +1 -1
- package/types/modules/Emitter.d.ts +51 -19
- package/types/modules/Emitter.d.ts.map +1 -1
- package/types/modules/Renderer.d.ts +86 -12
- package/types/modules/Renderer.d.ts.map +1 -1
- package/types/modules/Signal.d.ts +48 -16
- package/types/modules/Signal.d.ts.map +1 -1
- package/types/modules/TemplateEngine.d.ts +38 -14
- package/types/modules/TemplateEngine.d.ts.map +1 -1
- package/dist/eleva.min.js +0 -2
- package/dist/eleva.min.js.map +0 -1
package/dist/eleva.d.ts
CHANGED
|
@@ -1,150 +1,592 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* @class 📡 Emitter
|
|
3
|
+
* @classdesc A robust event emitter that enables inter-component communication through a publish-subscribe pattern.
|
|
4
|
+
* Components can emit events and listen for events from other components, facilitating loose coupling
|
|
5
|
+
* and reactive updates across the application.
|
|
6
|
+
* Events are handled synchronously in the order they were registered, with proper cleanup
|
|
7
|
+
* of unsubscribed handlers.
|
|
8
|
+
* Event names should follow the format 'namespace:action' (e.g., 'user:login', 'cart:update').
|
|
3
9
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
10
|
+
* @example
|
|
11
|
+
* const emitter = new Emitter();
|
|
12
|
+
* emitter.on('user:login', (user) => console.log(`User logged in: ${user.name}`));
|
|
13
|
+
* emitter.emit('user:login', { name: 'John' }); // Logs: "User logged in: John"
|
|
6
14
|
*/
|
|
7
15
|
declare class Emitter {
|
|
8
|
-
/** @
|
|
9
|
-
|
|
10
|
-
[x: string]: Function[];
|
|
11
|
-
};
|
|
16
|
+
/** @private {Map<string, Set<(data: unknown) => void>>} Map of event names to their registered handler functions */
|
|
17
|
+
private _events;
|
|
12
18
|
/**
|
|
13
|
-
* Registers an event handler for the specified event.
|
|
19
|
+
* Registers an event handler for the specified event name.
|
|
20
|
+
* The handler will be called with the event data when the event is emitted.
|
|
21
|
+
* Event names should follow the format 'namespace:action' for consistency.
|
|
14
22
|
*
|
|
15
|
-
* @
|
|
16
|
-
* @param {
|
|
23
|
+
* @public
|
|
24
|
+
* @param {string} event - The name of the event to listen for (e.g., 'user:login').
|
|
25
|
+
* @param {(data: unknown) => void} handler - The callback function to invoke when the event occurs.
|
|
26
|
+
* @returns {() => void} A function to unsubscribe the event handler.
|
|
27
|
+
* @example
|
|
28
|
+
* const unsubscribe = emitter.on('user:login', (user) => console.log(user));
|
|
29
|
+
* // Later...
|
|
30
|
+
* unsubscribe(); // Stops listening for the event
|
|
17
31
|
*/
|
|
18
|
-
on(event: string, handler:
|
|
32
|
+
public on(event: string, handler: (data: unknown) => void): () => void;
|
|
19
33
|
/**
|
|
20
|
-
* Removes
|
|
34
|
+
* Removes an event handler for the specified event name.
|
|
35
|
+
* If no handler is provided, all handlers for the event are removed.
|
|
36
|
+
* Automatically cleans up empty event sets to prevent memory leaks.
|
|
21
37
|
*
|
|
22
|
-
* @
|
|
23
|
-
* @param {
|
|
38
|
+
* @public
|
|
39
|
+
* @param {string} event - The name of the event to remove handlers from.
|
|
40
|
+
* @param {(data: unknown) => void} [handler] - The specific handler function to remove.
|
|
41
|
+
* @returns {void}
|
|
42
|
+
* @example
|
|
43
|
+
* // Remove a specific handler
|
|
44
|
+
* emitter.off('user:login', loginHandler);
|
|
45
|
+
* // Remove all handlers for an event
|
|
46
|
+
* emitter.off('user:login');
|
|
24
47
|
*/
|
|
25
|
-
off(event: string, handler:
|
|
48
|
+
public off(event: string, handler?: (data: unknown) => void): void;
|
|
26
49
|
/**
|
|
27
|
-
* Emits an event
|
|
50
|
+
* Emits an event with the specified data to all registered handlers.
|
|
51
|
+
* Handlers are called synchronously in the order they were registered.
|
|
52
|
+
* If no handlers are registered for the event, the emission is silently ignored.
|
|
28
53
|
*
|
|
29
|
-
* @
|
|
30
|
-
* @param {
|
|
54
|
+
* @public
|
|
55
|
+
* @param {string} event - The name of the event to emit.
|
|
56
|
+
* @param {...unknown} args - Optional arguments to pass to the event handlers.
|
|
57
|
+
* @returns {void}
|
|
58
|
+
* @example
|
|
59
|
+
* // Emit an event with data
|
|
60
|
+
* emitter.emit('user:login', { name: 'John', role: 'admin' });
|
|
61
|
+
* // Emit an event with multiple arguments
|
|
62
|
+
* emitter.emit('cart:update', { items: [] }, { total: 0 });
|
|
31
63
|
*/
|
|
32
|
-
emit(event: string, ...args:
|
|
64
|
+
public emit(event: string, ...args: unknown[]): void;
|
|
33
65
|
}
|
|
34
66
|
|
|
35
67
|
/**
|
|
36
|
-
*
|
|
68
|
+
* @class ⚡ Signal
|
|
69
|
+
* @classdesc A reactive data holder that enables fine-grained reactivity in the Eleva framework.
|
|
70
|
+
* Signals notify registered watchers when their value changes, enabling efficient DOM updates
|
|
71
|
+
* through targeted patching rather than full re-renders.
|
|
72
|
+
* Updates are batched using microtasks to prevent multiple synchronous notifications.
|
|
73
|
+
* The class is generic, allowing type-safe handling of any value type T.
|
|
37
74
|
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
75
|
+
* @example
|
|
76
|
+
* const count = new Signal(0);
|
|
77
|
+
* count.watch((value) => console.log(`Count changed to: ${value}`));
|
|
78
|
+
* count.value = 1; // Logs: "Count changed to: 1"
|
|
79
|
+
* @template T
|
|
80
|
+
*/
|
|
81
|
+
declare class Signal<T> {
|
|
82
|
+
/**
|
|
83
|
+
* Creates a new Signal instance with the specified initial value.
|
|
84
|
+
*
|
|
85
|
+
* @public
|
|
86
|
+
* @param {T} value - The initial value of the signal.
|
|
87
|
+
*/
|
|
88
|
+
constructor(value: T);
|
|
89
|
+
/** @private {T} Internal storage for the signal's current value */
|
|
90
|
+
private _value;
|
|
91
|
+
/** @private {Set<(value: T) => void>} Collection of callback functions to be notified when value changes */
|
|
92
|
+
private _watchers;
|
|
93
|
+
/** @private {boolean} Flag to prevent multiple synchronous watcher notifications and batch updates into microtasks */
|
|
94
|
+
private _pending;
|
|
95
|
+
/**
|
|
96
|
+
* Sets a new value for the signal and notifies all registered watchers if the value has changed.
|
|
97
|
+
* The notification is batched using microtasks to prevent multiple synchronous updates.
|
|
98
|
+
*
|
|
99
|
+
* @public
|
|
100
|
+
* @param {T} newVal - The new value to set.
|
|
101
|
+
* @returns {void}
|
|
102
|
+
*/
|
|
103
|
+
public set value(newVal: T);
|
|
104
|
+
/**
|
|
105
|
+
* Gets the current value of the signal.
|
|
106
|
+
*
|
|
107
|
+
* @public
|
|
108
|
+
* @returns {T} The current value.
|
|
109
|
+
*/
|
|
110
|
+
public get value(): T;
|
|
111
|
+
/**
|
|
112
|
+
* Registers a watcher function that will be called whenever the signal's value changes.
|
|
113
|
+
* The watcher will receive the new value as its argument.
|
|
114
|
+
*
|
|
115
|
+
* @public
|
|
116
|
+
* @param {(value: T) => void} fn - The callback function to invoke on value change.
|
|
117
|
+
* @returns {() => boolean} A function to unsubscribe the watcher.
|
|
118
|
+
* @example
|
|
119
|
+
* const unsubscribe = signal.watch((value) => console.log(value));
|
|
120
|
+
* // Later...
|
|
121
|
+
* unsubscribe(); // Stops watching for changes
|
|
122
|
+
*/
|
|
123
|
+
public watch(fn: (value: T) => void): () => boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Notifies all registered watchers of a value change using microtask scheduling.
|
|
126
|
+
* Uses a pending flag to batch multiple synchronous updates into a single notification.
|
|
127
|
+
* All watcher callbacks receive the current value when executed.
|
|
128
|
+
*
|
|
129
|
+
* @private
|
|
130
|
+
* @returns {void}
|
|
131
|
+
*/
|
|
132
|
+
private _notify;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @class 🎨 Renderer
|
|
137
|
+
* @classdesc A high-performance DOM renderer that implements an optimized direct DOM diffing algorithm.
|
|
138
|
+
*
|
|
139
|
+
* Key features:
|
|
140
|
+
* - Single-pass diffing algorithm for efficient DOM updates
|
|
141
|
+
* - Key-based node reconciliation for optimal performance
|
|
142
|
+
* - Intelligent attribute handling for ARIA, data attributes, and boolean properties
|
|
143
|
+
* - Preservation of special Eleva-managed instances and style elements
|
|
144
|
+
* - Memory-efficient with reusable temporary containers
|
|
145
|
+
*
|
|
146
|
+
* The renderer is designed to minimize DOM operations while maintaining
|
|
147
|
+
* exact attribute synchronization and proper node identity preservation.
|
|
148
|
+
* It's particularly optimized for frequent updates and complex DOM structures.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* const renderer = new Renderer();
|
|
152
|
+
* const container = document.getElementById("app");
|
|
153
|
+
* const newHtml = "<div>Updated content</div>";
|
|
154
|
+
* renderer.patchDOM(container, newHtml);
|
|
40
155
|
*/
|
|
41
156
|
declare class Renderer {
|
|
42
157
|
/**
|
|
43
|
-
*
|
|
158
|
+
* A temporary container to hold the new HTML content while diffing.
|
|
159
|
+
* @private
|
|
160
|
+
* @type {HTMLElement}
|
|
161
|
+
*/
|
|
162
|
+
private _tempContainer;
|
|
163
|
+
/**
|
|
164
|
+
* Patches the DOM of the given container with the provided HTML string.
|
|
44
165
|
*
|
|
166
|
+
* @public
|
|
45
167
|
* @param {HTMLElement} container - The container element to patch.
|
|
46
|
-
* @param {string} newHtml - The new HTML
|
|
168
|
+
* @param {string} newHtml - The new HTML string.
|
|
169
|
+
* @returns {void}
|
|
170
|
+
* @throws {TypeError} If container is not an HTMLElement or newHtml is not a string.
|
|
171
|
+
* @throws {Error} If DOM patching fails.
|
|
47
172
|
*/
|
|
48
|
-
patchDOM(container: HTMLElement, newHtml: string): void;
|
|
173
|
+
public patchDOM(container: HTMLElement, newHtml: string): void;
|
|
49
174
|
/**
|
|
50
|
-
*
|
|
175
|
+
* Performs a diff between two DOM nodes and patches the old node to match the new node.
|
|
51
176
|
*
|
|
177
|
+
* @private
|
|
52
178
|
* @param {HTMLElement} oldParent - The original DOM element.
|
|
53
179
|
* @param {HTMLElement} newParent - The new DOM element.
|
|
180
|
+
* @returns {void}
|
|
181
|
+
*/
|
|
182
|
+
private _diff;
|
|
183
|
+
/**
|
|
184
|
+
* Patches a single node.
|
|
185
|
+
*
|
|
186
|
+
* @private
|
|
187
|
+
* @param {Node} oldNode - The original DOM node.
|
|
188
|
+
* @param {Node} newNode - The new DOM node.
|
|
189
|
+
* @returns {void}
|
|
190
|
+
*/
|
|
191
|
+
private _patchNode;
|
|
192
|
+
/**
|
|
193
|
+
* Removes a node from its parent.
|
|
194
|
+
*
|
|
195
|
+
* @private
|
|
196
|
+
* @param {HTMLElement} parent - The parent element containing the node to remove.
|
|
197
|
+
* @param {Node} node - The node to remove.
|
|
198
|
+
* @returns {void}
|
|
199
|
+
*/
|
|
200
|
+
private _removeNode;
|
|
201
|
+
/**
|
|
202
|
+
* Updates the attributes of an element to match a new element's attributes.
|
|
203
|
+
*
|
|
204
|
+
* @private
|
|
205
|
+
* @param {HTMLElement} oldEl - The original element to update.
|
|
206
|
+
* @param {HTMLElement} newEl - The new element to update.
|
|
207
|
+
* @returns {void}
|
|
208
|
+
*/
|
|
209
|
+
private _updateAttributes;
|
|
210
|
+
/**
|
|
211
|
+
* Determines if two nodes are the same based on their type, name, and key attributes.
|
|
212
|
+
*
|
|
213
|
+
* @private
|
|
214
|
+
* @param {Node} oldNode - The first node to compare.
|
|
215
|
+
* @param {Node} newNode - The second node to compare.
|
|
216
|
+
* @returns {boolean} True if the nodes are considered the same, false otherwise.
|
|
54
217
|
*/
|
|
55
|
-
|
|
218
|
+
private _isSameNode;
|
|
56
219
|
/**
|
|
57
|
-
*
|
|
220
|
+
* Creates a key map for the children of a parent node.
|
|
58
221
|
*
|
|
59
|
-
* @
|
|
60
|
-
* @param {
|
|
222
|
+
* @private
|
|
223
|
+
* @param {Array<Node>} children - The children of the parent node.
|
|
224
|
+
* @param {number} start - The start index of the children.
|
|
225
|
+
* @param {number} end - The end index of the children.
|
|
226
|
+
* @returns {Map<string, Node>} A key map for the children.
|
|
61
227
|
*/
|
|
62
|
-
|
|
228
|
+
private _createKeyMap;
|
|
229
|
+
/**
|
|
230
|
+
* Extracts the key attribute from a node if it exists.
|
|
231
|
+
*
|
|
232
|
+
* @private
|
|
233
|
+
* @param {Node} node - The node to extract the key from.
|
|
234
|
+
* @returns {string|null} The key attribute value or null if not found.
|
|
235
|
+
*/
|
|
236
|
+
private _getNodeKey;
|
|
63
237
|
}
|
|
64
238
|
|
|
65
239
|
/**
|
|
66
|
-
*
|
|
240
|
+
* @typedef {Object} ComponentDefinition
|
|
241
|
+
* @property {function(ComponentContext): (Record<string, unknown>|Promise<Record<string, unknown>>)} [setup]
|
|
242
|
+
* Optional setup function that initializes the component's state and returns reactive data
|
|
243
|
+
* @property {(function(ComponentContext): string|Promise<string>)} template
|
|
244
|
+
* Required function that defines the component's HTML structure
|
|
245
|
+
* @property {(function(ComponentContext): string)|string} [style]
|
|
246
|
+
* Optional function or string that provides component-scoped CSS styles
|
|
247
|
+
* @property {Record<string, ComponentDefinition>} [children]
|
|
248
|
+
* Optional object defining nested child components
|
|
249
|
+
*/
|
|
250
|
+
/**
|
|
251
|
+
* @typedef {Object} ComponentContext
|
|
252
|
+
* @property {Record<string, unknown>} props
|
|
253
|
+
* Component properties passed during mounting
|
|
254
|
+
* @property {Emitter} emitter
|
|
255
|
+
* Event emitter instance for component event handling
|
|
256
|
+
* @property {function<T>(value: T): Signal<T>} signal
|
|
257
|
+
* Factory function to create reactive Signal instances
|
|
258
|
+
* @property {function(LifecycleHookContext): Promise<void>} [onBeforeMount]
|
|
259
|
+
* Hook called before component mounting
|
|
260
|
+
* @property {function(LifecycleHookContext): Promise<void>} [onMount]
|
|
261
|
+
* Hook called after component mounting
|
|
262
|
+
* @property {function(LifecycleHookContext): Promise<void>} [onBeforeUpdate]
|
|
263
|
+
* Hook called before component update
|
|
264
|
+
* @property {function(LifecycleHookContext): Promise<void>} [onUpdate]
|
|
265
|
+
* Hook called after component update
|
|
266
|
+
* @property {function(UnmountHookContext): Promise<void>} [onUnmount]
|
|
267
|
+
* Hook called during component unmounting
|
|
268
|
+
*/
|
|
269
|
+
/**
|
|
270
|
+
* @typedef {Object} LifecycleHookContext
|
|
271
|
+
* @property {HTMLElement} container
|
|
272
|
+
* The DOM element where the component is mounted
|
|
273
|
+
* @property {ComponentContext} context
|
|
274
|
+
* The component's reactive state and context data
|
|
275
|
+
*/
|
|
276
|
+
/**
|
|
277
|
+
* @typedef {Object} UnmountHookContext
|
|
278
|
+
* @property {HTMLElement} container
|
|
279
|
+
* The DOM element where the component is mounted
|
|
280
|
+
* @property {ComponentContext} context
|
|
281
|
+
* The component's reactive state and context data
|
|
282
|
+
* @property {{
|
|
283
|
+
* watchers: Array<() => void>, // Signal watcher cleanup functions
|
|
284
|
+
* listeners: Array<() => void>, // Event listener cleanup functions
|
|
285
|
+
* children: Array<MountResult> // Child component instances
|
|
286
|
+
* }} cleanup
|
|
287
|
+
* Object containing cleanup functions and instances
|
|
288
|
+
*/
|
|
289
|
+
/**
|
|
290
|
+
* @typedef {Object} MountResult
|
|
291
|
+
* @property {HTMLElement} container
|
|
292
|
+
* The DOM element where the component is mounted
|
|
293
|
+
* @property {ComponentContext} data
|
|
294
|
+
* The component's reactive state and context data
|
|
295
|
+
* @property {function(): Promise<void>} unmount
|
|
296
|
+
* Function to clean up and unmount the component
|
|
297
|
+
*/
|
|
298
|
+
/**
|
|
299
|
+
* @typedef {Object} ElevaPlugin
|
|
300
|
+
* @property {function(Eleva, Record<string, unknown>): void} install
|
|
301
|
+
* Function that installs the plugin into the Eleva instance
|
|
302
|
+
* @property {string} name
|
|
303
|
+
* Unique identifier name for the plugin
|
|
304
|
+
*/
|
|
305
|
+
/**
|
|
306
|
+
* @class 🧩 Eleva
|
|
307
|
+
* @classdesc A modern, signal-based component runtime framework that provides lifecycle hooks,
|
|
308
|
+
* scoped styles, and plugin support. Eleva manages component registration, plugin integration,
|
|
309
|
+
* event handling, and DOM rendering with a focus on performance and developer experience.
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* // Basic component creation and mounting
|
|
313
|
+
* const app = new Eleva("myApp");
|
|
314
|
+
* app.component("myComponent", {
|
|
315
|
+
* setup: (ctx) => ({ count: ctx.signal(0) }),
|
|
316
|
+
* template: (ctx) => `<div>Hello ${ctx.props.name}</div>`
|
|
317
|
+
* });
|
|
318
|
+
* app.mount(document.getElementById("app"), "myComponent", { name: "World" });
|
|
67
319
|
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
320
|
+
* @example
|
|
321
|
+
* // Using lifecycle hooks
|
|
322
|
+
* app.component("lifecycleDemo", {
|
|
323
|
+
* setup: () => {
|
|
324
|
+
* return {
|
|
325
|
+
* onMount: ({ container, context }) => {
|
|
326
|
+
* console.log('Component mounted!');
|
|
327
|
+
* }
|
|
328
|
+
* };
|
|
329
|
+
* },
|
|
330
|
+
* template: `<div>Lifecycle Demo</div>`
|
|
331
|
+
* });
|
|
70
332
|
*/
|
|
71
333
|
declare class Eleva {
|
|
72
334
|
/**
|
|
73
|
-
* Creates a new Eleva instance.
|
|
335
|
+
* Creates a new Eleva instance with the specified name and configuration.
|
|
336
|
+
*
|
|
337
|
+
* @public
|
|
338
|
+
* @param {string} name - The unique identifier name for this Eleva instance.
|
|
339
|
+
* @param {Record<string, unknown>} [config={}] - Optional configuration object for the instance.
|
|
340
|
+
* May include framework-wide settings and default behaviors.
|
|
341
|
+
* @throws {Error} If the name is not provided or is not a string.
|
|
342
|
+
* @returns {Eleva} A new Eleva instance.
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* const app = new Eleva("myApp");
|
|
346
|
+
* app.component("myComponent", {
|
|
347
|
+
* setup: (ctx) => ({ count: ctx.signal(0) }),
|
|
348
|
+
* template: (ctx) => `<div>Hello ${ctx.props.name}!</div>`
|
|
349
|
+
* });
|
|
350
|
+
* app.mount(document.getElementById("app"), "myComponent", { name: "World" });
|
|
74
351
|
*
|
|
75
|
-
* @param {string} name - The name of the Eleva instance.
|
|
76
|
-
* @param {object} [config={}] - Optional configuration for the instance.
|
|
77
352
|
*/
|
|
78
|
-
constructor(name: string, config?:
|
|
79
|
-
name
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
353
|
+
constructor(name: string, config?: Record<string, unknown>);
|
|
354
|
+
/** @public {string} The unique identifier name for this Eleva instance */
|
|
355
|
+
public name: string;
|
|
356
|
+
/** @public {Object<string, unknown>} Optional configuration object for the Eleva instance */
|
|
357
|
+
public config: Record<string, unknown>;
|
|
358
|
+
/** @public {Emitter} Instance of the event emitter for handling component events */
|
|
359
|
+
public emitter: Emitter;
|
|
360
|
+
/** @public {typeof Signal} Static reference to the Signal class for creating reactive state */
|
|
361
|
+
public signal: typeof Signal;
|
|
362
|
+
/** @public {Renderer} Instance of the renderer for handling DOM updates and patching */
|
|
363
|
+
public renderer: Renderer;
|
|
364
|
+
/** @private {Map<string, ComponentDefinition>} Registry of all component definitions by name */
|
|
365
|
+
private _components;
|
|
366
|
+
/** @private {Map<string, ElevaPlugin>} Collection of installed plugin instances by name */
|
|
367
|
+
private _plugins;
|
|
368
|
+
/** @private {boolean} Flag indicating if the root component is currently mounted */
|
|
369
|
+
private _isMounted;
|
|
370
|
+
/** @private {number} Counter for generating unique component IDs */
|
|
371
|
+
private _componentCounter;
|
|
87
372
|
/**
|
|
88
373
|
* Integrates a plugin with the Eleva framework.
|
|
374
|
+
* The plugin's install function will be called with the Eleva instance and provided options.
|
|
375
|
+
* After installation, the plugin will be available for use by components.
|
|
89
376
|
*
|
|
90
|
-
* @
|
|
91
|
-
* @param {
|
|
92
|
-
* @
|
|
377
|
+
* @public
|
|
378
|
+
* @param {ElevaPlugin} plugin - The plugin object which must have an `install` function.
|
|
379
|
+
* @param {Object<string, unknown>} [options={}] - Optional configuration options for the plugin.
|
|
380
|
+
* @returns {Eleva} The Eleva instance (for method chaining).
|
|
381
|
+
* @example
|
|
382
|
+
* app.use(myPlugin, { option1: "value1" });
|
|
93
383
|
*/
|
|
94
|
-
use(plugin
|
|
384
|
+
public use(plugin: ElevaPlugin, options?: {
|
|
385
|
+
[x: string]: unknown;
|
|
386
|
+
}): Eleva;
|
|
95
387
|
/**
|
|
96
|
-
* Registers a component with the Eleva instance.
|
|
388
|
+
* Registers a new component with the Eleva instance.
|
|
389
|
+
* The component will be available for mounting using its registered name.
|
|
97
390
|
*
|
|
98
|
-
* @
|
|
99
|
-
* @param {
|
|
100
|
-
* @
|
|
391
|
+
* @public
|
|
392
|
+
* @param {string} name - The unique name of the component to register.
|
|
393
|
+
* @param {ComponentDefinition} definition - The component definition including setup, template, style, and children.
|
|
394
|
+
* @returns {Eleva} The Eleva instance (for method chaining).
|
|
395
|
+
* @throws {Error} If the component name is already registered.
|
|
396
|
+
* @example
|
|
397
|
+
* app.component("myButton", {
|
|
398
|
+
* template: (ctx) => `<button>${ctx.props.text}</button>`,
|
|
399
|
+
* style: `button { color: blue; }`
|
|
400
|
+
* });
|
|
101
401
|
*/
|
|
102
|
-
component(name: string, definition:
|
|
402
|
+
public component(name: string, definition: ComponentDefinition): Eleva;
|
|
103
403
|
/**
|
|
104
404
|
* Mounts a registered component to a DOM element.
|
|
405
|
+
* This will initialize the component, set up its reactive state, and render it to the DOM.
|
|
105
406
|
*
|
|
106
|
-
* @
|
|
107
|
-
* @param {
|
|
108
|
-
* @param {
|
|
109
|
-
* @
|
|
110
|
-
* @
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* @
|
|
117
|
-
*
|
|
407
|
+
* @public
|
|
408
|
+
* @param {HTMLElement} container - The DOM element where the component will be mounted.
|
|
409
|
+
* @param {string|ComponentDefinition} compName - The name of the registered component or a direct component definition.
|
|
410
|
+
* @param {Object<string, unknown>} [props={}] - Optional properties to pass to the component.
|
|
411
|
+
* @returns {Promise<MountResult>}
|
|
412
|
+
* A Promise that resolves to an object containing:
|
|
413
|
+
* - container: The mounted component's container element
|
|
414
|
+
* - data: The component's reactive state and context
|
|
415
|
+
* - unmount: Function to clean up and unmount the component
|
|
416
|
+
* @throws {Error} If the container is not found, or component is not registered.
|
|
417
|
+
* @example
|
|
418
|
+
* const instance = await app.mount(document.getElementById("app"), "myComponent", { text: "Click me" });
|
|
419
|
+
* // Later...
|
|
420
|
+
* instance.unmount();
|
|
118
421
|
*/
|
|
119
|
-
|
|
422
|
+
public mount(container: HTMLElement, compName: string | ComponentDefinition, props?: {
|
|
423
|
+
[x: string]: unknown;
|
|
424
|
+
}): Promise<MountResult>;
|
|
120
425
|
/**
|
|
121
426
|
* Processes DOM elements for event binding based on attributes starting with "@".
|
|
427
|
+
* This method handles the event delegation system and ensures proper cleanup of event listeners.
|
|
122
428
|
*
|
|
123
|
-
* @param {HTMLElement} container - The container element in which to search for events.
|
|
124
|
-
* @param {object} context - The current context containing event handler definitions.
|
|
125
429
|
* @private
|
|
430
|
+
* @param {HTMLElement} container - The container element in which to search for event attributes.
|
|
431
|
+
* @param {ComponentContext} context - The current component context containing event handler definitions.
|
|
432
|
+
* @param {Array<() => void>} listeners - Array to collect cleanup functions for each event listener.
|
|
433
|
+
* @returns {void}
|
|
126
434
|
*/
|
|
127
435
|
private _processEvents;
|
|
128
436
|
/**
|
|
129
437
|
* Injects scoped styles into the component's container.
|
|
438
|
+
* The styles are automatically prefixed to prevent style leakage to other components.
|
|
130
439
|
*
|
|
131
|
-
* @param {HTMLElement} container - The container element.
|
|
132
|
-
* @param {string} compName - The component name used to identify the style element.
|
|
133
|
-
* @param {Function} styleFn - A function that returns CSS styles as a string.
|
|
134
|
-
* @param {object} context - The current context for style interpolation.
|
|
135
440
|
* @private
|
|
441
|
+
* @param {HTMLElement} container - The container element where styles should be injected.
|
|
442
|
+
* @param {string} compId - The component ID used to identify the style element.
|
|
443
|
+
* @param {(function(ComponentContext): string)|string} styleDef - The component's style definition (function or string).
|
|
444
|
+
* @param {ComponentContext} context - The current component context for style interpolation.
|
|
445
|
+
* @returns {void}
|
|
136
446
|
*/
|
|
137
447
|
private _injectStyles;
|
|
138
448
|
/**
|
|
139
|
-
*
|
|
449
|
+
* Extracts props from an element's attributes that start with the specified prefix.
|
|
450
|
+
* This method is used to collect component properties from DOM elements.
|
|
140
451
|
*
|
|
141
|
-
* @param {HTMLElement} container - The parent container element.
|
|
142
|
-
* @param {object} children - An object mapping child component selectors to their definitions.
|
|
143
|
-
* @param {Array} childInstances - An array to store the mounted child component instances.
|
|
144
452
|
* @private
|
|
453
|
+
* @param {HTMLElement} element - The DOM element to extract props from
|
|
454
|
+
* @param {string} prefix - The prefix to look for in attributes
|
|
455
|
+
* @returns {Record<string, string>} An object containing the extracted props
|
|
456
|
+
* @example
|
|
457
|
+
* // For an element with attributes:
|
|
458
|
+
* // <div :name="John" :age="25">
|
|
459
|
+
* // Returns: { name: "John", age: "25" }
|
|
145
460
|
*/
|
|
146
|
-
private
|
|
461
|
+
private _extractProps;
|
|
462
|
+
/**
|
|
463
|
+
* Mounts all components within the parent component's container.
|
|
464
|
+
* This method handles mounting of explicitly defined children components.
|
|
465
|
+
*
|
|
466
|
+
* The mounting process follows these steps:
|
|
467
|
+
* 1. Cleans up any existing component instances
|
|
468
|
+
* 2. Mounts explicitly defined children components
|
|
469
|
+
*
|
|
470
|
+
* @private
|
|
471
|
+
* @param {HTMLElement} container - The container element to mount components in
|
|
472
|
+
* @param {Object<string, ComponentDefinition>} children - Map of selectors to component definitions for explicit children
|
|
473
|
+
* @param {Array<MountResult>} childInstances - Array to store all mounted component instances
|
|
474
|
+
* @returns {Promise<void>}
|
|
475
|
+
*
|
|
476
|
+
* @example
|
|
477
|
+
* // Explicit children mounting:
|
|
478
|
+
* const children = {
|
|
479
|
+
* 'UserProfile': UserProfileComponent,
|
|
480
|
+
* '#settings-panel': "settings-panel"
|
|
481
|
+
* };
|
|
482
|
+
*/
|
|
483
|
+
private _mountComponents;
|
|
147
484
|
}
|
|
485
|
+
type ComponentDefinition = {
|
|
486
|
+
/**
|
|
487
|
+
* Optional setup function that initializes the component's state and returns reactive data
|
|
488
|
+
*/
|
|
489
|
+
setup?: ((arg0: ComponentContext) => (Record<string, unknown> | Promise<Record<string, unknown>>)) | undefined;
|
|
490
|
+
/**
|
|
491
|
+
* Required function that defines the component's HTML structure
|
|
492
|
+
*/
|
|
493
|
+
template: ((arg0: ComponentContext) => string | Promise<string>);
|
|
494
|
+
/**
|
|
495
|
+
* Optional function or string that provides component-scoped CSS styles
|
|
496
|
+
*/
|
|
497
|
+
style?: string | ((arg0: ComponentContext) => string) | undefined;
|
|
498
|
+
/**
|
|
499
|
+
* Optional object defining nested child components
|
|
500
|
+
*/
|
|
501
|
+
children?: Record<string, ComponentDefinition> | undefined;
|
|
502
|
+
};
|
|
503
|
+
type ComponentContext = {
|
|
504
|
+
/**
|
|
505
|
+
* Component properties passed during mounting
|
|
506
|
+
*/
|
|
507
|
+
props: Record<string, unknown>;
|
|
508
|
+
/**
|
|
509
|
+
* Event emitter instance for component event handling
|
|
510
|
+
*/
|
|
511
|
+
emitter: Emitter;
|
|
512
|
+
/**
|
|
513
|
+
* <T>(value: T): Signal<T>} signal
|
|
514
|
+
* Factory function to create reactive Signal instances
|
|
515
|
+
*/
|
|
516
|
+
"": Function;
|
|
517
|
+
/**
|
|
518
|
+
* Hook called before component mounting
|
|
519
|
+
*/
|
|
520
|
+
onBeforeMount?: ((arg0: LifecycleHookContext) => Promise<void>) | undefined;
|
|
521
|
+
/**
|
|
522
|
+
* Hook called after component mounting
|
|
523
|
+
*/
|
|
524
|
+
onMount?: ((arg0: LifecycleHookContext) => Promise<void>) | undefined;
|
|
525
|
+
/**
|
|
526
|
+
* Hook called before component update
|
|
527
|
+
*/
|
|
528
|
+
onBeforeUpdate?: ((arg0: LifecycleHookContext) => Promise<void>) | undefined;
|
|
529
|
+
/**
|
|
530
|
+
* Hook called after component update
|
|
531
|
+
*/
|
|
532
|
+
onUpdate?: ((arg0: LifecycleHookContext) => Promise<void>) | undefined;
|
|
533
|
+
/**
|
|
534
|
+
* Hook called during component unmounting
|
|
535
|
+
*/
|
|
536
|
+
onUnmount?: ((arg0: UnmountHookContext) => Promise<void>) | undefined;
|
|
537
|
+
};
|
|
538
|
+
type LifecycleHookContext = {
|
|
539
|
+
/**
|
|
540
|
+
* The DOM element where the component is mounted
|
|
541
|
+
*/
|
|
542
|
+
container: HTMLElement;
|
|
543
|
+
/**
|
|
544
|
+
* The component's reactive state and context data
|
|
545
|
+
*/
|
|
546
|
+
context: ComponentContext;
|
|
547
|
+
};
|
|
548
|
+
type UnmountHookContext = {
|
|
549
|
+
/**
|
|
550
|
+
* The DOM element where the component is mounted
|
|
551
|
+
*/
|
|
552
|
+
container: HTMLElement;
|
|
553
|
+
/**
|
|
554
|
+
* The component's reactive state and context data
|
|
555
|
+
*/
|
|
556
|
+
context: ComponentContext;
|
|
557
|
+
/**
|
|
558
|
+
* Object containing cleanup functions and instances
|
|
559
|
+
*/
|
|
560
|
+
cleanup: {
|
|
561
|
+
watchers: Array<() => void>;
|
|
562
|
+
listeners: Array<() => void>;
|
|
563
|
+
children: Array<MountResult>;
|
|
564
|
+
};
|
|
565
|
+
};
|
|
566
|
+
type MountResult = {
|
|
567
|
+
/**
|
|
568
|
+
* The DOM element where the component is mounted
|
|
569
|
+
*/
|
|
570
|
+
container: HTMLElement;
|
|
571
|
+
/**
|
|
572
|
+
* The component's reactive state and context data
|
|
573
|
+
*/
|
|
574
|
+
data: ComponentContext;
|
|
575
|
+
/**
|
|
576
|
+
* Function to clean up and unmount the component
|
|
577
|
+
*/
|
|
578
|
+
unmount: () => Promise<void>;
|
|
579
|
+
};
|
|
580
|
+
type ElevaPlugin = {
|
|
581
|
+
/**
|
|
582
|
+
* Function that installs the plugin into the Eleva instance
|
|
583
|
+
*/
|
|
584
|
+
install: (arg0: Eleva, arg1: Record<string, unknown>) => void;
|
|
585
|
+
/**
|
|
586
|
+
* Unique identifier name for the plugin
|
|
587
|
+
*/
|
|
588
|
+
name: string;
|
|
589
|
+
};
|
|
148
590
|
|
|
149
591
|
//# sourceMappingURL=index.d.ts.map
|
|
150
592
|
|