@aegis-framework/pandora 0.1.7 → 0.3.0
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 +746 -27
- package/dist/pandora.browser.js +10 -0
- package/dist/pandora.browser.js.map +14 -0
- package/dist/pandora.js +10 -806
- package/dist/pandora.js.map +13 -1
- package/dist/types/Component.d.ts +266 -0
- package/dist/types/Component.d.ts.map +1 -0
- package/dist/types/Registry.d.ts +276 -0
- package/dist/types/Registry.d.ts.map +1 -0
- package/dist/types/ShadowComponent.d.ts +35 -0
- package/dist/types/ShadowComponent.d.ts.map +1 -0
- package/dist/types/Types.d.ts +9 -0
- package/dist/types/Types.d.ts.map +1 -0
- package/dist/types/Util.d.ts +16 -0
- package/dist/types/Util.d.ts.map +1 -0
- package/dist/types/browser.d.ts +7 -0
- package/dist/types/browser.d.ts.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/package.json +41 -22
- package/dist/pandora.min.js +0 -10
- package/dist/pandora.min.js.map +0 -1
- package/index.js +0 -3
- package/src/Component.js +0 -502
- package/src/Registry.js +0 -74
- package/src/ShadowComponent.js +0 -47
- package/src/Util.js +0 -66
package/README.md
CHANGED
|
@@ -1,48 +1,767 @@
|
|
|
1
1
|
# Pandora
|
|
2
2
|
|
|
3
|
-
Pandora is a Web Components library that allows you to create simple and reusable custom HTML elements.
|
|
3
|
+
Pandora is a lightweight Web Components library that allows you to create simple and reusable custom HTML elements with reactive state management, lifecycle hooks, and Shadow DOM support.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Using npm
|
|
9
|
+
npm install @aegis-framework/pandora
|
|
10
|
+
|
|
11
|
+
# Using yarn
|
|
12
|
+
yarn add @aegis-framework/pandora
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
### Basic Component
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
import { Component } from '@aegis-framework/pandora';
|
|
21
|
+
|
|
22
|
+
class CustomElement extends Component {
|
|
23
|
+
constructor() {
|
|
24
|
+
super();
|
|
25
|
+
this.props = {
|
|
26
|
+
text: ''
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
render() {
|
|
31
|
+
return `<h2>${this.props.text}</h2>`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
console.log(CustomElement.tag); // 'custom-element'
|
|
36
|
+
|
|
37
|
+
// Register the custom element
|
|
38
|
+
window.customElements.define(CustomElement.tag, CustomElement);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Using it in the HTML:
|
|
42
|
+
|
|
43
|
+
```html
|
|
44
|
+
<custom-element text="Hello World!"></custom-element>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### TypeScript Support
|
|
48
|
+
|
|
49
|
+
Pandora supports generic types for props and state:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { Component } from '@aegis-framework/pandora';
|
|
53
|
+
|
|
54
|
+
interface MyProps {
|
|
55
|
+
name: string;
|
|
56
|
+
count: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface MyState {
|
|
60
|
+
active: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
class TypedComponent extends Component<MyProps, MyState> {
|
|
64
|
+
constructor() {
|
|
65
|
+
super();
|
|
66
|
+
this.props = {
|
|
67
|
+
name: '',
|
|
68
|
+
count: 0
|
|
69
|
+
};
|
|
70
|
+
this.state = {
|
|
71
|
+
active: false
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
render() {
|
|
76
|
+
return `
|
|
77
|
+
<div class="${this.state.active ? 'active' : ''}">
|
|
78
|
+
<h2>${this.props.name}</h2>
|
|
79
|
+
<span>Count: ${this.props.count}</span>
|
|
80
|
+
</div>
|
|
81
|
+
`;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
```
|
|
4
85
|
|
|
5
86
|
## Components
|
|
6
87
|
|
|
88
|
+
### Component
|
|
89
|
+
|
|
90
|
+
The base class for creating custom elements.
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
class MyComponent extends Component {
|
|
94
|
+
constructor() {
|
|
95
|
+
super();
|
|
96
|
+
|
|
97
|
+
// Define props with default values
|
|
98
|
+
this.props = {
|
|
99
|
+
title: '',
|
|
100
|
+
visible: true
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// Define internal state
|
|
104
|
+
this.state = {
|
|
105
|
+
count: 0
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
render() {
|
|
110
|
+
return `<div>${this.props.title}: ${this.state.count}</div>`;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### ShadowComponent
|
|
116
|
+
|
|
117
|
+
A component that uses Shadow DOM for style encapsulation.
|
|
118
|
+
|
|
119
|
+
```javascript
|
|
120
|
+
import { ShadowComponent } from '@aegis-framework/pandora';
|
|
121
|
+
|
|
122
|
+
class MyShadowComponent extends ShadowComponent {
|
|
123
|
+
constructor() {
|
|
124
|
+
super();
|
|
125
|
+
this.props = { text: '' };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
willMount() {
|
|
129
|
+
// Set scoped styles
|
|
130
|
+
this.setStyle({
|
|
131
|
+
h2: { color: 'blue' }
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
return Promise.resolve();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
render() {
|
|
138
|
+
return `<h2>${this.props.text}</h2>`;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## API Reference
|
|
144
|
+
|
|
145
|
+
### Static Properties
|
|
146
|
+
|
|
147
|
+
| Property | Type | Description |
|
|
148
|
+
|----------|------|-------------|
|
|
149
|
+
| `tag` | `string` | The custom element tag name (auto-generated from class name or manually set) |
|
|
150
|
+
| `observedAttributes` | `string[]` | Attributes to observe for changes |
|
|
151
|
+
|
|
152
|
+
### Instance Properties
|
|
153
|
+
|
|
154
|
+
| Property | Type | Description |
|
|
155
|
+
|----------|------|-------------|
|
|
156
|
+
| `props` | `object` | Component properties (read via proxy) |
|
|
157
|
+
| `state` | `object` | Internal component state |
|
|
158
|
+
| `dom` | `HTMLElement` | Reference to the component's DOM |
|
|
159
|
+
| `width` | `number` | Computed width in pixels |
|
|
160
|
+
| `height` | `number` | Computed height in pixels |
|
|
161
|
+
| `isConnected` | `boolean` | Whether the component is connected to the DOM |
|
|
162
|
+
| `isReady` | `boolean` | Whether the component has completed its initial mount |
|
|
163
|
+
|
|
164
|
+
### Methods
|
|
165
|
+
|
|
166
|
+
#### State & Props
|
|
167
|
+
|
|
168
|
+
```javascript
|
|
169
|
+
// Update props (triggers update cycle)
|
|
170
|
+
element.setProps({ title: 'New Title' });
|
|
171
|
+
|
|
172
|
+
// Update state (triggers update cycle)
|
|
173
|
+
element.setState({ count: 5 });
|
|
174
|
+
|
|
175
|
+
// Set component styles
|
|
176
|
+
element.setStyle({
|
|
177
|
+
h2: { color: 'red', fontSize: '24px' }
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### Event Helpers
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
// Add event listener (chainable)
|
|
185
|
+
element.on('click', (e) => console.log('Clicked!'));
|
|
186
|
+
|
|
187
|
+
// Add one-time event listener (auto-removes after first trigger)
|
|
188
|
+
element.once('click', (e) => console.log('Only fires once!'));
|
|
189
|
+
|
|
190
|
+
// Remove event listener (chainable)
|
|
191
|
+
element.off('click', handler);
|
|
192
|
+
|
|
193
|
+
// Emit custom event
|
|
194
|
+
element.emit('custom-event', { data: 'value' });
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
#### Query Helpers
|
|
198
|
+
|
|
199
|
+
```javascript
|
|
200
|
+
// Query single element within component
|
|
201
|
+
const button = element.query('button');
|
|
202
|
+
|
|
203
|
+
// Query all matching elements
|
|
204
|
+
const items = element.queryAll('.item');
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
#### Rendering
|
|
208
|
+
|
|
209
|
+
```javascript
|
|
210
|
+
// Force re-render
|
|
211
|
+
element.forceRender();
|
|
212
|
+
|
|
213
|
+
// Register ready callback
|
|
214
|
+
element.ready(() => {
|
|
215
|
+
console.log('Component is ready!');
|
|
216
|
+
});
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Lifecycle Hooks
|
|
220
|
+
|
|
221
|
+
Override these methods to hook into the component lifecycle:
|
|
222
|
+
|
|
223
|
+
#### Mount Cycle
|
|
224
|
+
|
|
225
|
+
- `willMount()` - Called before the component is rendered
|
|
226
|
+
- `didMount()` - Called after the component is rendered
|
|
227
|
+
|
|
228
|
+
#### Update Cycle
|
|
229
|
+
|
|
230
|
+
- `willUpdate(origin, property, oldValue, newValue, oldObject, newObject)` - Called before an update
|
|
231
|
+
- `update(origin, property, oldValue, newValue, oldObject, newObject)` - Called during an update
|
|
232
|
+
- `didUpdate(origin, property, oldValue, newValue, oldObject, newObject)` - Called after an update
|
|
233
|
+
- `onStateUpdate(property, oldValue, newValue, oldObject, newObject)` - Called when state changes
|
|
234
|
+
- `onPropsUpdate(property, oldValue, newValue, oldObject, newObject)` - Called when props change
|
|
235
|
+
|
|
236
|
+
The `origin` parameter can be `'props'`, `'state'`, or `'attribute'` depending on what triggered the update.
|
|
237
|
+
|
|
238
|
+
#### Unmount Cycle
|
|
239
|
+
|
|
240
|
+
- `willUnmount()` - Called before the component is removed
|
|
241
|
+
- `unmount()` - Called when the component is being removed
|
|
242
|
+
- `didUnmount()` - Called after the component is removed
|
|
243
|
+
|
|
244
|
+
```javascript
|
|
245
|
+
class MyComponent extends Component {
|
|
246
|
+
willMount() {
|
|
247
|
+
console.log('About to mount');
|
|
248
|
+
return Promise.resolve();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
didMount() {
|
|
252
|
+
console.log('Mounted!');
|
|
253
|
+
return Promise.resolve();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
onPropsUpdate(property, oldValue, newValue) {
|
|
257
|
+
console.log(`Prop ${property} changed from ${oldValue} to ${newValue}`);
|
|
258
|
+
return Promise.resolve();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Observed Attributes
|
|
264
|
+
|
|
265
|
+
To react to HTML attribute changes, define which attributes to observe:
|
|
266
|
+
|
|
267
|
+
```javascript
|
|
268
|
+
class MyComponent extends Component {
|
|
269
|
+
static observedAttributes = ['title', 'count', 'disabled'];
|
|
270
|
+
|
|
271
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
272
|
+
// This is called automatically, which triggers the update cycle
|
|
273
|
+
// You can also override onPropsUpdate to handle attribute changes
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## Templates
|
|
279
|
+
|
|
280
|
+
You can use HTML templates instead of the render method:
|
|
281
|
+
|
|
282
|
+
```html
|
|
283
|
+
<template id="my-component">
|
|
284
|
+
<div class="wrapper">
|
|
285
|
+
<slot></slot>
|
|
286
|
+
</div>
|
|
287
|
+
</template>
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
Or set templates programmatically:
|
|
291
|
+
|
|
7
292
|
```javascript
|
|
8
|
-
|
|
9
|
-
class
|
|
293
|
+
MyComponent.template(`
|
|
294
|
+
<div class="wrapper">
|
|
295
|
+
<h2>{{title}}</h2>
|
|
296
|
+
</div>
|
|
297
|
+
`);
|
|
298
|
+
|
|
299
|
+
// Or with a function for dynamic templates
|
|
300
|
+
MyComponent.template((context) => `
|
|
301
|
+
<div class="wrapper">
|
|
302
|
+
<h2>${context.props.title}</h2>
|
|
303
|
+
</div>
|
|
304
|
+
`);
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
## Registry
|
|
10
308
|
|
|
11
|
-
|
|
12
|
-
super ();
|
|
309
|
+
The Registry provides centralized component management:
|
|
13
310
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
311
|
+
```javascript
|
|
312
|
+
import { Registry, Component } from '@aegis-framework/pandora';
|
|
313
|
+
|
|
314
|
+
// Register a component
|
|
315
|
+
Registry.register('my-tag', MyComponent);
|
|
19
316
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return `<h2>${this.props.text}</h2>`;
|
|
24
|
-
}
|
|
317
|
+
// Check if a component is registered
|
|
318
|
+
if (Registry.has('my-tag')) {
|
|
319
|
+
console.log('Component is registered');
|
|
25
320
|
}
|
|
26
321
|
|
|
27
|
-
|
|
322
|
+
// Get the component class
|
|
323
|
+
const MyComponentClass = Registry.get('my-tag');
|
|
28
324
|
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
window.customElements.define (CustomElement.tag, CustomElement);
|
|
325
|
+
// Evolve an existing component (hot reload)
|
|
326
|
+
Registry.evolve('my-tag', UpdatedComponent);
|
|
32
327
|
|
|
33
|
-
//
|
|
34
|
-
|
|
328
|
+
// Evolve and re-render all existing instances
|
|
329
|
+
Registry.evolve('my-tag', UpdatedComponent, true);
|
|
35
330
|
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
|
|
331
|
+
// Get all instances of a component
|
|
332
|
+
const instances = Registry.instances('my-tag');
|
|
333
|
+
|
|
334
|
+
// Execute callback on all instances
|
|
335
|
+
Registry.instances('my-tag', (instance) => {
|
|
336
|
+
instance.setProps({ updated: true });
|
|
39
337
|
});
|
|
40
338
|
|
|
339
|
+
// Create new instance with props
|
|
340
|
+
const element = Registry.instantiate('my-tag', { title: 'Hello' });
|
|
41
341
|
```
|
|
42
342
|
|
|
43
|
-
|
|
44
|
-
|
|
343
|
+
### Registry Methods
|
|
344
|
+
|
|
345
|
+
#### Component Management
|
|
346
|
+
|
|
347
|
+
| Method | Description |
|
|
348
|
+
|--------|-------------|
|
|
349
|
+
| `register(tag, component)` | Register a new component with the custom elements registry |
|
|
350
|
+
| `has(tag)` | Check if a component is registered |
|
|
351
|
+
| `get(tag)` | Get the component class by tag name |
|
|
352
|
+
| `evolve(tag, component, rerender?)` | Update an existing component's implementation (optionally re-render instances) |
|
|
353
|
+
| `instances(tag, callback?)` | Get all instances or execute callback on each |
|
|
354
|
+
| `instantiate(tag, props)` | Create a new instance with the given props |
|
|
355
|
+
|
|
356
|
+
#### Debugging
|
|
357
|
+
|
|
358
|
+
| Method/Property | Description |
|
|
359
|
+
|-----------------|-------------|
|
|
360
|
+
| `debug` | Boolean property - enable/disable debug logging |
|
|
361
|
+
| `log(message, data?)` | Log a debug message (only when debug is enabled) |
|
|
362
|
+
| `list()` | Get array of all registered component tags |
|
|
363
|
+
| `stats(tag?)` | Get stats for one or all components (instanceCount, isRegistered, isLazy) |
|
|
364
|
+
|
|
365
|
+
#### Global Lifecycle Hooks
|
|
366
|
+
|
|
367
|
+
| Method | Description |
|
|
368
|
+
|--------|-------------|
|
|
369
|
+
| `onMount(callback)` | Register callback for when any component mounts (returns unsubscribe) |
|
|
370
|
+
| `onUnmount(callback)` | Register callback for when any component unmounts (returns unsubscribe) |
|
|
371
|
+
|
|
372
|
+
#### Error Handling
|
|
373
|
+
|
|
374
|
+
| Method | Description |
|
|
375
|
+
|--------|-------------|
|
|
376
|
+
| `onError(callback)` | Register global error handler for component errors (returns unsubscribe) |
|
|
377
|
+
|
|
378
|
+
#### Component Aliases
|
|
379
|
+
|
|
380
|
+
| Method | Description |
|
|
381
|
+
|--------|-------------|
|
|
382
|
+
| `alias(aliasTag, originalTag)` | Create an alias tag name for an existing component |
|
|
383
|
+
| `isAlias(tag)` | Check if a tag is an alias |
|
|
384
|
+
| `getOriginalTag(aliasTag)` | Get the original tag name for an alias |
|
|
385
|
+
|
|
386
|
+
#### Lazy Loading
|
|
387
|
+
|
|
388
|
+
| Method | Description |
|
|
389
|
+
|--------|-------------|
|
|
390
|
+
| `lazy(tag, loader)` | Register a component to be loaded on first use |
|
|
391
|
+
| `isLazy(tag)` | Check if a component is registered as lazy |
|
|
392
|
+
| `preload(tag)` | Preload a lazy component without mounting |
|
|
393
|
+
|
|
394
|
+
#### Middleware
|
|
395
|
+
|
|
396
|
+
| Method | Description |
|
|
397
|
+
|--------|-------------|
|
|
398
|
+
| `use(type, middleware)` | Register middleware for 'props', 'state', or 'render' (returns unsubscribe) |
|
|
399
|
+
|
|
400
|
+
#### Global State
|
|
401
|
+
|
|
402
|
+
| Method | Description |
|
|
403
|
+
|--------|-------------|
|
|
404
|
+
| `setState(key, value)` | Set a global state value |
|
|
405
|
+
| `getState(key)` | Get a global state value |
|
|
406
|
+
| `hasState(key)` | Check if a state key exists |
|
|
407
|
+
| `deleteState(key)` | Delete a state value |
|
|
408
|
+
| `subscribe(key, callback)` | Subscribe to state changes (returns unsubscribe function) |
|
|
409
|
+
| `unsubscribe(key, callback)` | Unsubscribe from state changes |
|
|
410
|
+
| `getAllState()` | Get a copy of all global state |
|
|
411
|
+
| `clearState()` | Clear all global state |
|
|
412
|
+
|
|
413
|
+
## Global State
|
|
45
414
|
|
|
415
|
+
The Registry provides a simple global state system for sharing data between components:
|
|
416
|
+
|
|
417
|
+
```javascript
|
|
418
|
+
import { Registry } from '@aegis-framework/pandora';
|
|
419
|
+
|
|
420
|
+
// Set global state
|
|
421
|
+
Registry.setState('theme', 'dark');
|
|
422
|
+
Registry.setState('user', { name: 'John', id: 123 });
|
|
423
|
+
|
|
424
|
+
// Get global state
|
|
425
|
+
const theme = Registry.getState('theme'); // 'dark'
|
|
426
|
+
const user = Registry.getState('user'); // { name: 'John', id: 123 }
|
|
427
|
+
|
|
428
|
+
// Check if state exists
|
|
429
|
+
if (Registry.hasState('theme')) {
|
|
430
|
+
console.log('Theme is set');
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Subscribe to state changes
|
|
434
|
+
const unsubscribe = Registry.subscribe('theme', (newValue, oldValue) => {
|
|
435
|
+
console.log(`Theme changed from ${oldValue} to ${newValue}`);
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
// Update state (triggers subscribers)
|
|
439
|
+
Registry.setState('theme', 'light'); // Logs: "Theme changed from dark to light"
|
|
440
|
+
|
|
441
|
+
// Unsubscribe when done
|
|
442
|
+
unsubscribe();
|
|
443
|
+
|
|
444
|
+
// Delete state
|
|
445
|
+
Registry.deleteState('theme');
|
|
446
|
+
|
|
447
|
+
// Get all state
|
|
448
|
+
const allState = Registry.getAllState();
|
|
449
|
+
|
|
450
|
+
// Clear all state
|
|
451
|
+
Registry.clearState();
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
## Debugging
|
|
455
|
+
|
|
456
|
+
Enable debug mode to log component lifecycle events:
|
|
457
|
+
|
|
458
|
+
```javascript
|
|
459
|
+
import { Registry } from '@aegis-framework/pandora';
|
|
460
|
+
|
|
461
|
+
// Enable debug logging
|
|
462
|
+
Registry.debug = true;
|
|
463
|
+
|
|
464
|
+
// Manually log messages (only when debug is enabled)
|
|
465
|
+
Registry.log('Custom debug message', { data: 'value' });
|
|
466
|
+
|
|
467
|
+
// List all registered components
|
|
468
|
+
console.log(Registry.list()); // ['my-component', 'other-component']
|
|
469
|
+
|
|
470
|
+
// Get stats for a specific component
|
|
471
|
+
const stats = Registry.stats('my-component');
|
|
472
|
+
// { tag: 'my-component', instanceCount: 3, isRegistered: true, isLazy: false }
|
|
473
|
+
|
|
474
|
+
// Get stats for all components
|
|
475
|
+
const allStats = Registry.stats();
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
## Global Lifecycle Hooks
|
|
479
|
+
|
|
480
|
+
Listen to mount/unmount events across all components:
|
|
481
|
+
|
|
482
|
+
```javascript
|
|
483
|
+
import { Registry } from '@aegis-framework/pandora';
|
|
484
|
+
|
|
485
|
+
// Subscribe to all component mounts
|
|
486
|
+
const unsubscribeMount = Registry.onMount((component, tag) => {
|
|
487
|
+
console.log(`Component <${tag}> mounted`, component);
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
// Subscribe to all component unmounts
|
|
491
|
+
const unsubscribeUnmount = Registry.onUnmount((component, tag) => {
|
|
492
|
+
console.log(`Component <${tag}> unmounted`, component);
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
// Clean up when done
|
|
496
|
+
unsubscribeMount();
|
|
497
|
+
unsubscribeUnmount();
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
## Error Handling
|
|
501
|
+
|
|
502
|
+
Register global error handlers to catch component errors:
|
|
503
|
+
|
|
504
|
+
```javascript
|
|
505
|
+
import { Registry } from '@aegis-framework/pandora';
|
|
506
|
+
|
|
507
|
+
// Register a global error handler
|
|
508
|
+
const unsubscribe = Registry.onError((error, component, tag, lifecycle) => {
|
|
509
|
+
console.error(`Error in <${tag}> during ${lifecycle}:`, error);
|
|
510
|
+
// Send to error tracking service, etc.
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
// If no error handlers are registered, errors will be thrown normally
|
|
514
|
+
// If handlers are registered, they receive the error instead of it being thrown
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
## Component Aliases
|
|
518
|
+
|
|
519
|
+
Create alternative tag names for existing components:
|
|
520
|
+
|
|
521
|
+
```javascript
|
|
522
|
+
import { Registry } from '@aegis-framework/pandora';
|
|
523
|
+
|
|
524
|
+
// Register the original component
|
|
525
|
+
Registry.register('my-button', MyButton);
|
|
526
|
+
|
|
527
|
+
// Create aliases (must contain a hyphen, per custom elements spec)
|
|
528
|
+
Registry.alias('app-button', 'my-button');
|
|
529
|
+
Registry.alias('custom-button', 'my-button');
|
|
530
|
+
|
|
531
|
+
// Check if a tag is an alias
|
|
532
|
+
Registry.isAlias('app-button'); // true
|
|
533
|
+
Registry.isAlias('my-button'); // false
|
|
534
|
+
|
|
535
|
+
// Get the original tag
|
|
536
|
+
Registry.getOriginalTag('app-button'); // 'my-button'
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
```html
|
|
540
|
+
<!-- All of these create the same component -->
|
|
541
|
+
<my-button>Click me</my-button>
|
|
542
|
+
<app-button>Click me</app-button>
|
|
543
|
+
<custom-button>Click me</custom-button>
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
## Lazy Loading
|
|
547
|
+
|
|
548
|
+
Load components on demand when they're first used:
|
|
549
|
+
|
|
550
|
+
```javascript
|
|
551
|
+
import { Registry } from '@aegis-framework/pandora';
|
|
552
|
+
|
|
553
|
+
// Register a lazy component
|
|
554
|
+
Registry.lazy('heavy-chart', () => import('./components/HeavyChart.js'));
|
|
555
|
+
|
|
556
|
+
// The component will be loaded when first used in the DOM
|
|
557
|
+
// <heavy-chart></heavy-chart>
|
|
558
|
+
|
|
559
|
+
// Check if a component is lazy
|
|
560
|
+
Registry.isLazy('heavy-chart'); // true (before loaded), false (after loaded)
|
|
561
|
+
|
|
562
|
+
// Preload a lazy component (useful for anticipated navigation)
|
|
563
|
+
await Registry.preload('heavy-chart');
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
### Lazy Loading with Dynamic Imports
|
|
567
|
+
|
|
568
|
+
```javascript
|
|
569
|
+
// Components are loaded only when first mounted
|
|
570
|
+
Registry.lazy('data-table', async () => {
|
|
571
|
+
const module = await import('./components/DataTable.js');
|
|
572
|
+
return module.default;
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
Registry.lazy('chart-widget', () => import('./components/ChartWidget.js'));
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
## Middleware
|
|
579
|
+
|
|
580
|
+
Intercept and modify props, state, or render output across all components:
|
|
581
|
+
|
|
582
|
+
```javascript
|
|
583
|
+
import { Registry } from '@aegis-framework/pandora';
|
|
584
|
+
|
|
585
|
+
// Props middleware - runs on every setProps call
|
|
586
|
+
const unsubscribeProps = Registry.use('props', (component, props) => {
|
|
587
|
+
// Validate or transform props
|
|
588
|
+
return {
|
|
589
|
+
...props,
|
|
590
|
+
timestamp: Date.now() // Add timestamp to all props
|
|
591
|
+
};
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
// State middleware - runs on every setState call
|
|
595
|
+
const unsubscribeState = Registry.use('state', (component, state) => {
|
|
596
|
+
// Log state changes
|
|
597
|
+
console.log(`State update in <${component.static.tag}>:`, state);
|
|
598
|
+
return state;
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
// Render middleware - can modify rendered HTML
|
|
602
|
+
const unsubscribeRender = Registry.use('render', (component, html) => {
|
|
603
|
+
// Add wrapper or modify output
|
|
604
|
+
return html;
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
// Clean up middleware
|
|
608
|
+
unsubscribeProps();
|
|
609
|
+
unsubscribeState();
|
|
610
|
+
unsubscribeRender();
|
|
611
|
+
```
|
|
612
|
+
|
|
613
|
+
### Example: Development Tools Middleware
|
|
614
|
+
|
|
615
|
+
```javascript
|
|
616
|
+
if (process.env.NODE_ENV === 'development') {
|
|
617
|
+
// Log all state changes
|
|
618
|
+
Registry.use('state', (component, state) => {
|
|
619
|
+
console.log(`[${component.static.tag}] setState:`, state);
|
|
620
|
+
return state;
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
// Track render performance
|
|
624
|
+
Registry.use('render', (component, html) => {
|
|
625
|
+
console.log(`[${component.static.tag}] rendered ${html.length} chars`);
|
|
626
|
+
return html;
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
```
|
|
630
|
+
|
|
631
|
+
### Using Global State in Components
|
|
632
|
+
|
|
633
|
+
```javascript
|
|
634
|
+
class ThemeToggle extends Component {
|
|
635
|
+
constructor() {
|
|
636
|
+
super();
|
|
637
|
+
this.state = {
|
|
638
|
+
theme: Registry.getState('theme') || 'light'
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
didMount() {
|
|
643
|
+
// Subscribe to theme changes
|
|
644
|
+
this._unsubscribe = Registry.subscribe('theme', (newTheme) => {
|
|
645
|
+
this.setState({ theme: newTheme });
|
|
646
|
+
this.forceRender();
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
// Bind click event using event helpers
|
|
650
|
+
this.on('click', () => this.toggleTheme());
|
|
651
|
+
|
|
652
|
+
return Promise.resolve();
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
willUnmount() {
|
|
656
|
+
// Clean up subscription
|
|
657
|
+
if (this._unsubscribe) {
|
|
658
|
+
this._unsubscribe();
|
|
659
|
+
}
|
|
660
|
+
return Promise.resolve();
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
toggleTheme() {
|
|
664
|
+
const newTheme = this.state.theme === 'light' ? 'dark' : 'light';
|
|
665
|
+
Registry.setState('theme', newTheme);
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
render() {
|
|
669
|
+
return `<button>Current: ${this.state.theme}</button>`;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
## Styling
|
|
675
|
+
|
|
676
|
+
### Regular Components
|
|
677
|
+
|
|
678
|
+
Styles are added to a shared `<style>` element in the document:
|
|
679
|
+
|
|
680
|
+
```javascript
|
|
681
|
+
element.setStyle({
|
|
682
|
+
h2: {
|
|
683
|
+
color: 'blue',
|
|
684
|
+
fontSize: '24px'
|
|
685
|
+
},
|
|
686
|
+
'&:hover h2': {
|
|
687
|
+
color: 'red'
|
|
688
|
+
}
|
|
689
|
+
});
|
|
690
|
+
```
|
|
691
|
+
|
|
692
|
+
### Shadow Components
|
|
693
|
+
|
|
694
|
+
Styles are scoped to the Shadow DOM:
|
|
695
|
+
|
|
696
|
+
```javascript
|
|
697
|
+
shadowElement.setStyle({
|
|
698
|
+
':host': {
|
|
699
|
+
display: 'block'
|
|
700
|
+
},
|
|
701
|
+
h2: {
|
|
702
|
+
color: 'blue'
|
|
703
|
+
},
|
|
704
|
+
'@media (min-width: 768px)': {
|
|
705
|
+
h2: {
|
|
706
|
+
fontSize: '32px'
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
```
|
|
711
|
+
|
|
712
|
+
### CSS String
|
|
713
|
+
|
|
714
|
+
You can also pass raw CSS strings:
|
|
715
|
+
|
|
716
|
+
```javascript
|
|
717
|
+
element.setStyle(`
|
|
718
|
+
h2 {
|
|
719
|
+
color: blue;
|
|
720
|
+
font-size: 24px;
|
|
721
|
+
}
|
|
722
|
+
`);
|
|
723
|
+
```
|
|
724
|
+
|
|
725
|
+
## Browser Usage
|
|
726
|
+
|
|
727
|
+
For direct browser usage without a bundler:
|
|
728
|
+
|
|
729
|
+
```html
|
|
730
|
+
<script src="path/to/pandora.browser.js"></script>
|
|
731
|
+
<script>
|
|
732
|
+
class MyElement extends Pandora.Component {
|
|
733
|
+
render() {
|
|
734
|
+
return '<h2>Hello World!</h2>';
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
// Register using the Registry
|
|
739
|
+
Pandora.Registry.register('my-element', MyElement);
|
|
740
|
+
</script>
|
|
741
|
+
```
|
|
742
|
+
|
|
743
|
+
Or use the Registry for component management:
|
|
744
|
+
|
|
745
|
+
```html
|
|
746
|
+
<script src="path/to/pandora.browser.js"></script>
|
|
747
|
+
<script>
|
|
748
|
+
const { Component, Registry } = Pandora;
|
|
749
|
+
|
|
750
|
+
class CounterElement extends Component {
|
|
751
|
+
constructor() {
|
|
752
|
+
super();
|
|
753
|
+
this.state = { count: 0 };
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
render() {
|
|
757
|
+
return `<button>Count: ${this.state.count}</button>`;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
Registry.register('counter-element', CounterElement);
|
|
762
|
+
</script>
|
|
763
|
+
```
|
|
46
764
|
|
|
47
765
|
## License
|
|
48
|
-
|
|
766
|
+
|
|
767
|
+
This library is released under a [MIT License](./LICENSE).
|