eleva 1.2.7-alpha → 1.2.9-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/dist/eleva.d.ts CHANGED
@@ -1,138 +1,398 @@
1
1
  /**
2
- * Defines the structure and behavior of a component.
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
+ *
7
+ * @example
8
+ * const emitter = new Emitter();
9
+ * emitter.on('user:login', (user) => console.log(`User logged in: ${user.name}`));
10
+ * emitter.emit('user:login', { name: 'John' }); // Logs: "User logged in: John"
11
+ */
12
+ declare class Emitter {
13
+ /** @private {Map<string, Set<function(any): void>>} Map of event names to their registered handler functions */
14
+ private _events;
15
+ /**
16
+ * Registers an event handler for the specified event name.
17
+ * The handler will be called with the event data when the event is emitted.
18
+ *
19
+ * @public
20
+ * @param {string} event - The name of the event to listen for.
21
+ * @param {function(any): void} handler - The callback function to invoke when the event occurs.
22
+ * @returns {function(): boolean} A function to unsubscribe the event handler.
23
+ * @example
24
+ * const unsubscribe = emitter.on('user:login', (user) => console.log(user));
25
+ * // Later...
26
+ * unsubscribe(); // Stops listening for the event
27
+ */
28
+ public on(event: string, handler: (arg0: any) => void): () => boolean;
29
+ /**
30
+ * Removes an event handler for the specified event name.
31
+ * If no handler is provided, all handlers for the event are removed.
32
+ *
33
+ * @public
34
+ * @param {string} event - The name of the event.
35
+ * @param {function(any): void} [handler] - The specific handler function to remove.
36
+ * @returns {void}
37
+ */
38
+ public off(event: string, handler?: (arg0: any) => void): void;
39
+ /**
40
+ * Emits an event with the specified data to all registered handlers.
41
+ * Handlers are called synchronously in the order they were registered.
42
+ *
43
+ * @public
44
+ * @param {string} event - The name of the event to emit.
45
+ * @param {...any} args - Optional arguments to pass to the event handlers.
46
+ * @returns {void}
47
+ */
48
+ public emit(event: string, ...args: any[]): void;
49
+ }
50
+
51
+ /**
52
+ * @class ⚡ Signal
53
+ * @classdesc A reactive data holder that enables fine-grained reactivity in the Eleva framework.
54
+ * Signals notify registered watchers when their value changes, enabling efficient DOM updates
55
+ * through targeted patching rather than full re-renders.
56
+ *
57
+ * @example
58
+ * const count = new Signal(0);
59
+ * count.watch((value) => console.log(`Count changed to: ${value}`));
60
+ * count.value = 1; // Logs: "Count changed to: 1"
61
+ */
62
+ declare class Signal {
63
+ /**
64
+ * Creates a new Signal instance with the specified initial value.
65
+ *
66
+ * @public
67
+ * @param {*} value - The initial value of the signal.
68
+ */
69
+ constructor(value: any);
70
+ /** @private {T} Internal storage for the signal's current value, where T is the type of the initial value */
71
+ private _value;
72
+ /** @private {Set<function(T): void>} Collection of callback functions to be notified when value changes, where T is the value type */
73
+ private _watchers;
74
+ /** @private {boolean} Flag to prevent multiple synchronous watcher notifications and batch updates into microtasks */
75
+ private _pending;
76
+ /**
77
+ * Sets a new value for the signal and notifies all registered watchers if the value has changed.
78
+ * The notification is batched using microtasks to prevent multiple synchronous updates.
79
+ *
80
+ * @public
81
+ * @param {T} newVal - The new value to set, where T is the type of the initial value.
82
+ * @returns {void}
83
+ */
84
+ public set value(newVal: T);
85
+ /**
86
+ * Gets the current value of the signal.
87
+ *
88
+ * @public
89
+ * @returns {T} The current value, where T is the type of the initial value.
90
+ */
91
+ public get value(): T;
92
+ /**
93
+ * Registers a watcher function that will be called whenever the signal's value changes.
94
+ * The watcher will receive the new value as its argument.
95
+ *
96
+ * @public
97
+ * @param {function(T): void} fn - The callback function to invoke on value change, where T is the value type.
98
+ * @returns {function(): boolean} A function to unsubscribe the watcher.
99
+ * @example
100
+ * const unsubscribe = signal.watch((value) => console.log(value));
101
+ * // Later...
102
+ * unsubscribe(); // Stops watching for changes
103
+ */
104
+ public watch(fn: (arg0: T) => void): () => boolean;
105
+ /**
106
+ * Notifies all registered watchers of a value change using microtask scheduling.
107
+ * Uses a pending flag to batch multiple synchronous updates into a single notification.
108
+ * All watcher callbacks receive the current value when executed.
109
+ *
110
+ * @private
111
+ * @returns {void}
112
+ */
113
+ private _notify;
114
+ }
115
+
116
+ /**
117
+ * @class 🎨 Renderer
118
+ * @classdesc A DOM renderer that handles efficient DOM updates through patching and diffing.
119
+ * Provides methods for updating the DOM by comparing new and old structures and applying
120
+ * only the necessary changes, minimizing layout thrashing and improving performance.
121
+ *
122
+ * @example
123
+ * const renderer = new Renderer();
124
+ * const container = document.getElementById("app");
125
+ * const newHtml = "<div>Updated content</div>";
126
+ * renderer.patchDOM(container, newHtml);
127
+ */
128
+ declare class Renderer {
129
+ /**
130
+ * Patches the DOM of a container element with new HTML content.
131
+ * This method efficiently updates the DOM by comparing the new content with the existing
132
+ * content and applying only the necessary changes.
133
+ *
134
+ * @public
135
+ * @param {HTMLElement} container - The container element to patch.
136
+ * @param {string} newHtml - The new HTML content to apply.
137
+ * @returns {void}
138
+ * @throws {Error} If container is not an HTMLElement or newHtml is not a string.
139
+ */
140
+ public patchDOM(container: HTMLElement, newHtml: string): void;
141
+ /**
142
+ * Diffs two DOM trees (old and new) and applies updates to the old DOM.
143
+ * This method recursively compares nodes and their attributes, applying only
144
+ * the necessary changes to minimize DOM operations.
145
+ *
146
+ * @private
147
+ * @param {HTMLElement} oldParent - The original DOM element.
148
+ * @param {HTMLElement} newParent - The new DOM element.
149
+ * @returns {void}
150
+ * @throws {Error} If either parent is not an HTMLElement.
151
+ */
152
+ private diff;
153
+ /**
154
+ * Updates the attributes of an element to match those of a new element.
155
+ * Handles special cases for ARIA attributes, data attributes, and boolean properties.
156
+ *
157
+ * @private
158
+ * @param {HTMLElement} oldEl - The element to update.
159
+ * @param {HTMLElement} newEl - The element providing the updated attributes.
160
+ * @returns {void}
161
+ * @throws {Error} If either element is not an HTMLElement.
162
+ */
163
+ private updateAttributes;
164
+ }
165
+
166
+ /**
3
167
  * @typedef {Object} ComponentDefinition
4
168
  * @property {function(Object<string, any>): (Object<string, any>|Promise<Object<string, any>>)} [setup]
5
- * Optional setup function that initializes the component's reactive state and lifecycle.
6
- * Receives props and context as an argument and should return an object containing the component's state.
7
- * Can return either a synchronous object or a Promise that resolves to an object for async initialization.
8
- *
169
+ * Optional setup function that initializes the component's state and returns reactive data
9
170
  * @property {function(Object<string, any>): string} template
10
- * Required function that defines the component's HTML structure.
11
- * Receives the merged context (props + setup data) and must return an HTML template string.
12
- * Supports dynamic expressions using {{ }} syntax for reactive data binding.
13
- *
171
+ * Required function that defines the component's HTML structure
14
172
  * @property {function(Object<string, any>): string} [style]
15
- * Optional function that defines component-scoped CSS styles.
16
- * Receives the merged context and returns a CSS string that will be automatically scoped to the component.
17
- * Styles are injected into the component's container and only affect elements within it.
18
- *
173
+ * Optional function that provides component-scoped CSS styles
19
174
  * @property {Object<string, ComponentDefinition>} [children]
20
- * Optional object that defines nested child components.
21
- * Keys are CSS selectors that match elements in the template where child components should be mounted.
22
- * Values are ComponentDefinition objects that define the structure and behavior of each child component.
175
+ * Optional object defining nested child components
176
+ */
177
+ /**
178
+ * @typedef {Object} ElevaPlugin
179
+ * @property {function(Eleva, Object<string, any>): void} install
180
+ * Function that installs the plugin into the Eleva instance
181
+ * @property {string} name
182
+ * Unique identifier name for the plugin
183
+ */
184
+ /**
185
+ * @typedef {Object} MountResult
186
+ * @property {HTMLElement} container
187
+ * The DOM element where the component is mounted
188
+ * @property {Object<string, any>} data
189
+ * The component's reactive state and context data
190
+ * @property {function(): void} unmount
191
+ * Function to clean up and unmount the component
23
192
  */
24
193
  /**
25
194
  * @class 🧩 Eleva
26
- * @classdesc Signal-based component runtime framework with lifecycle hooks, scoped styles, and plugin support.
27
- * Manages component registration, plugin integration, event handling, and DOM rendering.
195
+ * @classdesc A modern, signal-based component runtime framework that provides lifecycle hooks,
196
+ * scoped styles, and plugin support. Eleva manages component registration, plugin integration,
197
+ * event handling, and DOM rendering with a focus on performance and developer experience.
198
+ *
199
+ * @example
200
+ * const app = new Eleva("myApp");
201
+ * app.component("myComponent", {
202
+ * template: (ctx) => `<div>Hello ${ctx.props.name}</div>`,
203
+ * setup: (ctx) => ({ count: new Signal(0) })
204
+ * });
205
+ * app.mount(document.getElementById("app"), "myComponent", { name: "World" });
28
206
  */
29
207
  declare class Eleva {
30
208
  /**
31
- * Creates a new Eleva instance.
209
+ * Creates a new Eleva instance with the specified name and configuration.
32
210
  *
33
- * @param {string} name - The name of the Eleva instance.
34
- * @param {Object<string, any>} [config={}] - Optional configuration for the instance.
211
+ * @public
212
+ * @param {string} name - The unique identifier name for this Eleva instance.
213
+ * @param {Object<string, any>} [config={}] - Optional configuration object for the instance.
214
+ * May include framework-wide settings and default behaviors.
35
215
  */
36
216
  constructor(name: string, config?: {
37
217
  [x: string]: any;
38
218
  });
39
- /** @type {string} The unique identifier name for this Eleva instance */
40
- name: string;
41
- /** @type {Object<string, any>} Optional configuration object for the Eleva instance */
42
- config: {
219
+ /** @public {string} The unique identifier name for this Eleva instance */
220
+ public name: string;
221
+ /** @public {Object<string, any>} Optional configuration object for the Eleva instance */
222
+ public config: {
43
223
  [x: string]: any;
44
224
  };
45
- /** @type {Object<string, ComponentDefinition>} Object storing registered component definitions by name */
46
- _components: {
47
- [x: string]: ComponentDefinition;
48
- };
49
- /** @private {Array<Object>} Collection of installed plugin instances */
225
+ /** @public {Emitter} Instance of the event emitter for handling component events */
226
+ public emitter: Emitter;
227
+ /** @public {typeof Signal} Static reference to the Signal class for creating reactive state */
228
+ public signal: typeof Signal;
229
+ /** @public {Renderer} Instance of the renderer for handling DOM updates and patching */
230
+ public renderer: Renderer;
231
+ /** @private {Map<string, ComponentDefinition>} Registry of all component definitions by name */
232
+ private _components;
233
+ /** @private {Map<string, ElevaPlugin>} Collection of installed plugin instances by name */
50
234
  private _plugins;
51
- /** @private {string[]} Array of lifecycle hook names supported by the component */
235
+ /** @private {string[]} Array of lifecycle hook names supported by components */
52
236
  private _lifecycleHooks;
53
- /** @private {boolean} Flag indicating if component is currently mounted */
237
+ /** @private {boolean} Flag indicating if the root component is currently mounted */
54
238
  private _isMounted;
55
- /** @private {Emitter} Instance of the event emitter for handling component events */
56
- private emitter;
57
- /** @private {Signal} Instance of the signal for handling plugin and component signals */
58
- private signal;
59
- /** @private {Renderer} Instance of the renderer for handling DOM updates and patching */
60
- private renderer;
61
239
  /**
62
240
  * Integrates a plugin with the Eleva framework.
241
+ * The plugin's install function will be called with the Eleva instance and provided options.
242
+ * After installation, the plugin will be available for use by components.
63
243
  *
64
- * @param {Object} plugin - The plugin object which should have an `install` function.
65
- * @param {Object<string, any>} [options={}] - Optional options to pass to the plugin.
66
- * @returns {Eleva} The Eleva instance (for chaining).
244
+ * @public
245
+ * @param {ElevaPlugin} plugin - The plugin object which must have an `install` function.
246
+ * @param {Object<string, any>} [options={}] - Optional configuration options for the plugin.
247
+ * @returns {Eleva} The Eleva instance (for method chaining).
248
+ * @example
249
+ * app.use(myPlugin, { option1: "value1" });
67
250
  */
68
- use(plugin: Object, options?: {
251
+ public use(plugin: ElevaPlugin, options?: {
69
252
  [x: string]: any;
70
253
  }): Eleva;
71
254
  /**
72
- * Registers a component with the Eleva instance.
255
+ * Registers a new component with the Eleva instance.
256
+ * The component will be available for mounting using its registered name.
73
257
  *
74
- * @param {string} name - The name of the component.
258
+ * @public
259
+ * @param {string} name - The unique name of the component to register.
75
260
  * @param {ComponentDefinition} definition - The component definition including setup, template, style, and children.
76
- * @returns {Eleva} The Eleva instance (for chaining).
261
+ * @returns {Eleva} The Eleva instance (for method chaining).
262
+ * @throws {Error} If the component name is already registered.
263
+ * @example
264
+ * app.component("myButton", {
265
+ * template: (ctx) => `<button>${ctx.props.text}</button>`,
266
+ * style: () => "button { color: blue; }"
267
+ * });
77
268
  */
78
- component(name: string, definition: ComponentDefinition): Eleva;
269
+ public component(name: string, definition: ComponentDefinition): Eleva;
79
270
  /**
80
271
  * Mounts a registered component to a DOM element.
272
+ * This will initialize the component, set up its reactive state, and render it to the DOM.
81
273
  *
82
- * @param {HTMLElement} container - A DOM element where the component will be mounted.
83
- * @param {string|ComponentDefinition} compName - The name of the component to mount or a component definition.
274
+ * @public
275
+ * @param {HTMLElement} container - The DOM element where the component will be mounted.
276
+ * @param {string|ComponentDefinition} compName - The name of the registered component or a direct component definition.
84
277
  * @param {Object<string, any>} [props={}] - Optional properties to pass to the component.
85
- * @returns {object|Promise<object>} An object representing the mounted component instance, or a Promise that resolves to it for asynchronous setups.
86
- * @throws {Error} If the container is not found or if the component is not registered.
278
+ * @returns {Promise<MountResult>}
279
+ * A Promise that resolves to an object containing:
280
+ * - container: The mounted component's container element
281
+ * - data: The component's reactive state and context
282
+ * - unmount: Function to clean up and unmount the component
283
+ * @throws {Error} If the container is not found, or component is not registered.
284
+ * @example
285
+ * const instance = await app.mount(document.getElementById("app"), "myComponent", { text: "Click me" });
286
+ * // Later...
287
+ * instance.unmount();
87
288
  */
88
- mount(container: HTMLElement, compName: string | ComponentDefinition, props?: {
289
+ public mount(container: HTMLElement, compName: string | ComponentDefinition, props?: {
89
290
  [x: string]: any;
90
- }): object | Promise<object>;
291
+ }): Promise<MountResult>;
91
292
  /**
92
- * Prepares default no-operation lifecycle hook functions.
293
+ * Prepares default no-operation lifecycle hook functions for a component.
294
+ * These hooks will be called at various stages of the component's lifecycle.
93
295
  *
94
- * @returns {Object<string, function(): void>} An object with keys for lifecycle hooks mapped to empty functions.
95
296
  * @private
297
+ * @returns {Object<string, function(): void>} An object mapping lifecycle hook names to empty functions.
298
+ * The returned object will be merged with the component's context.
96
299
  */
97
300
  private _prepareLifecycleHooks;
98
301
  /**
99
302
  * Processes DOM elements for event binding based on attributes starting with "@".
100
- * Tracks listeners for cleanup during unmount.
303
+ * This method handles the event delegation system and ensures proper cleanup of event listeners.
101
304
  *
102
- * @param {HTMLElement} container - The container element in which to search for events.
103
- * @param {Object<string, any>} context - The current context containing event handler definitions.
104
- * @param {Array<Function>} cleanupListeners - Array to collect cleanup functions for each event listener.
105
305
  * @private
306
+ * @param {HTMLElement} container - The container element in which to search for event attributes.
307
+ * @param {Object<string, any>} context - The current component context containing event handler definitions.
308
+ * @param {Array<Function>} cleanupListeners - Array to collect cleanup functions for each event listener.
309
+ * @returns {void}
106
310
  */
107
311
  private _processEvents;
108
312
  /**
109
313
  * Injects scoped styles into the component's container.
314
+ * The styles are automatically prefixed to prevent style leakage to other components.
110
315
  *
111
- * @param {HTMLElement} container - The container element.
112
- * @param {string} compName - The component name used to identify the style element.
113
- * @param {function(Object<string, any>): string} [styleFn] - A function that returns CSS styles as a string.
114
- * @param {Object<string, any>} context - The current context for style interpolation.
115
316
  * @private
317
+ * @param {HTMLElement} container - The container element where styles should be injected.
318
+ * @param {string} compName - The component name used to identify the style element.
319
+ * @param {function(Object<string, any>): string} [styleFn] - Optional function that returns CSS styles as a string.
320
+ * @param {Object<string, any>} context - The current component context for style interpolation.
321
+ * @returns {void}
116
322
  */
117
323
  private _injectStyles;
118
324
  /**
119
- * Mounts child components within the parent component's container.
325
+ * Extracts props from an element's attributes that start with 'eleva-prop-'.
326
+ * This method is used to collect component properties from DOM elements.
327
+ *
328
+ * @private
329
+ * @param {HTMLElement} element - The DOM element to extract props from
330
+ * @returns {Object<string, any>} An object containing the extracted props
331
+ * @example
332
+ * // For an element with attributes:
333
+ * // <div eleva-prop-name="John" eleva-prop-age="25">
334
+ * // Returns: { name: "John", age: "25" }
335
+ */
336
+ private _extractProps;
337
+ /**
338
+ * Mounts a single component instance to a container element.
339
+ * This method handles the actual mounting of a component with its props.
340
+ *
341
+ * @private
342
+ * @param {HTMLElement} container - The container element to mount the component to
343
+ * @param {string|ComponentDefinition} component - The component to mount, either as a name or definition
344
+ * @param {Object<string, any>} props - The props to pass to the component
345
+ * @returns {Promise<MountResult>} A promise that resolves to the mounted component instance
346
+ * @throws {Error} If the container is not a valid HTMLElement
347
+ */
348
+ private _mountComponentInstance;
349
+ /**
350
+ * Mounts components found by a selector in the container.
351
+ * This method handles mounting multiple instances of the same component type.
120
352
  *
121
- * @param {HTMLElement} container - The parent container element.
122
- * @param {Object<string, ComponentDefinition>} [children] - An object mapping child component selectors to their definitions.
123
- * @param {Array<object>} childInstances - An array to store the mounted child component instances.
124
353
  * @private
354
+ * @param {HTMLElement} container - The container to search for components
355
+ * @param {string} selector - The CSS selector to find components
356
+ * @param {string|ComponentDefinition} component - The component to mount
357
+ * @param {Array<MountResult>} instances - Array to store the mounted component instances
358
+ * @returns {Promise<void>}
125
359
  */
126
- private _mountChildren;
360
+ private _mountComponentsBySelector;
361
+ /**
362
+ * Mounts all components within the parent component's container.
363
+ * This method implements a dual mounting system that handles both:
364
+ * 1. Explicitly defined children components (passed through the children parameter)
365
+ * 2. Template-referenced components (found in the template using component names)
366
+ *
367
+ * The mounting process follows these steps:
368
+ * 1. Cleans up any existing component instances
369
+ * 2. Mounts explicitly defined children components
370
+ * 3. Mounts template-referenced components
371
+ *
372
+ * @private
373
+ * @param {HTMLElement} container - The container element to mount components in
374
+ * @param {Object<string, ComponentDefinition>} children - Map of selectors to component definitions for explicit children
375
+ * @param {Array<MountResult>} childInstances - Array to store all mounted component instances
376
+ * @returns {Promise<void>}
377
+ *
378
+ * @example
379
+ * // Explicit children mounting:
380
+ * const children = {
381
+ * '.user-profile': UserProfileComponent,
382
+ * '.settings-panel': SettingsComponent
383
+ * };
384
+ *
385
+ * // Template-referenced components:
386
+ * // <div>
387
+ * // <user-profile eleva-prop-name="John"></user-profile>
388
+ * // <settings-panel eleva-prop-theme="dark"></settings-panel>
389
+ * // </div>
390
+ */
391
+ private _mountComponents;
127
392
  }
128
- /**
129
- * Defines the structure and behavior of a component.
130
- */
131
393
  type ComponentDefinition = {
132
394
  /**
133
- * Optional setup function that initializes the component's reactive state and lifecycle.
134
- * Receives props and context as an argument and should return an object containing the component's state.
135
- * Can return either a synchronous object or a Promise that resolves to an object for async initialization.
395
+ * Optional setup function that initializes the component's state and returns reactive data
136
396
  */
137
397
  setup?: ((arg0: {
138
398
  [x: string]: any;
@@ -142,30 +402,52 @@ type ComponentDefinition = {
142
402
  [x: string]: any;
143
403
  }>)) | undefined;
144
404
  /**
145
- * Required function that defines the component's HTML structure.
146
- * Receives the merged context (props + setup data) and must return an HTML template string.
147
- * Supports dynamic expressions using {{ }} syntax for reactive data binding.
405
+ * Required function that defines the component's HTML structure
148
406
  */
149
407
  template: (arg0: {
150
408
  [x: string]: any;
151
409
  }) => string;
152
410
  /**
153
- * Optional function that defines component-scoped CSS styles.
154
- * Receives the merged context and returns a CSS string that will be automatically scoped to the component.
155
- * Styles are injected into the component's container and only affect elements within it.
411
+ * Optional function that provides component-scoped CSS styles
156
412
  */
157
413
  style?: ((arg0: {
158
414
  [x: string]: any;
159
415
  }) => string) | undefined;
160
416
  /**
161
- * Optional object that defines nested child components.
162
- * Keys are CSS selectors that match elements in the template where child components should be mounted.
163
- * Values are ComponentDefinition objects that define the structure and behavior of each child component.
417
+ * Optional object defining nested child components
164
418
  */
165
419
  children?: {
166
420
  [x: string]: ComponentDefinition;
167
421
  } | undefined;
168
422
  };
423
+ type ElevaPlugin = {
424
+ /**
425
+ * Function that installs the plugin into the Eleva instance
426
+ */
427
+ install: (arg0: Eleva, arg1: {
428
+ [x: string]: any;
429
+ }) => void;
430
+ /**
431
+ * Unique identifier name for the plugin
432
+ */
433
+ name: string;
434
+ };
435
+ type MountResult = {
436
+ /**
437
+ * The DOM element where the component is mounted
438
+ */
439
+ container: HTMLElement;
440
+ /**
441
+ * The component's reactive state and context data
442
+ */
443
+ data: {
444
+ [x: string]: any;
445
+ };
446
+ /**
447
+ * Function to clean up and unmount the component
448
+ */
449
+ unmount: () => void;
450
+ };
169
451
 
170
452
  //# sourceMappingURL=index.d.ts.map
171
453