@aegis-framework/pandora 0.3.1 → 0.4.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Pandora
2
2
 
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.
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. It includes built-in [lit-html](https://lit.dev/docs/libraries/standalone-templates/) integration for efficient DOM updates.
4
4
 
5
5
  ## Installation
6
6
 
@@ -10,11 +10,14 @@ npm install @aegis-framework/pandora
10
10
 
11
11
  # Using yarn
12
12
  yarn add @aegis-framework/pandora
13
+
14
+ # Using bun
15
+ bun add @aegis-framework/pandora
13
16
  ```
14
17
 
15
18
  ## Quick Start
16
19
 
17
- ### Basic Component
20
+ ### Basic Component (String Templates)
18
21
 
19
22
  ```javascript
20
23
  import { Component } from '@aegis-framework/pandora';
@@ -38,6 +41,25 @@ console.log(CustomElement.tag); // 'custom-element'
38
41
  window.customElements.define(CustomElement.tag, CustomElement);
39
42
  ```
40
43
 
44
+ ### Basic Component (lit-html Templates)
45
+
46
+ For better performance with frequent updates, use lit-html's tagged templates:
47
+
48
+ ```javascript
49
+ import { Component, html } from '@aegis-framework/pandora';
50
+
51
+ class CustomElement extends Component {
52
+ constructor() {
53
+ super();
54
+ this.props = { text: '' };
55
+ }
56
+
57
+ render() {
58
+ return html`<h2>${this.props.text}</h2>`;
59
+ }
60
+ }
61
+ ```
62
+
41
63
  Using it in the HTML:
42
64
 
43
65
  ```html
@@ -49,7 +71,7 @@ Using it in the HTML:
49
71
  Pandora supports generic types for props and state:
50
72
 
51
73
  ```typescript
52
- import { Component } from '@aegis-framework/pandora';
74
+ import { Component, html } from '@aegis-framework/pandora';
53
75
 
54
76
  interface MyProps {
55
77
  name: string;
@@ -73,7 +95,7 @@ class TypedComponent extends Component<MyProps, MyState> {
73
95
  }
74
96
 
75
97
  render() {
76
- return `
98
+ return html`
77
99
  <div class="${this.state.active ? 'active' : ''}">
78
100
  <h2>${this.props.name}</h2>
79
101
  <span>Count: ${this.props.count}</span>
@@ -83,6 +105,33 @@ class TypedComponent extends Component<MyProps, MyState> {
83
105
  }
84
106
  ```
85
107
 
108
+ ### Using Decorators
109
+
110
+ Pandora provides decorators for cleaner component registration and state binding:
111
+
112
+ ```typescript
113
+ import { Component, Register, Consumer, Prop, html } from '@aegis-framework/pandora';
114
+
115
+ @Register('my-counter')
116
+ class MyCounter extends Component {
117
+ @Prop()
118
+ initialCount: number = 0;
119
+
120
+ @Consumer('app.theme')
121
+ theme!: string;
122
+
123
+ state = { count: 0 };
124
+
125
+ render() {
126
+ return html`
127
+ <div class="theme-${this.theme}">
128
+ Count: ${this.state.count}
129
+ </div>
130
+ `;
131
+ }
132
+ }
133
+ ```
134
+
86
135
  ## Components
87
136
 
88
137
  ### Component
@@ -114,10 +163,10 @@ class MyComponent extends Component {
114
163
 
115
164
  ### ShadowComponent
116
165
 
117
- A component that uses Shadow DOM for style encapsulation.
166
+ A component that uses Shadow DOM for style encapsulation. ShadowComponent uses [Constructable Stylesheets](https://web.dev/constructable-stylesheets/) for high-performance styling - styles are shared across all instances of the same component class and updates use `replaceSync()` for fast synchronous changes.
118
167
 
119
168
  ```javascript
120
- import { ShadowComponent } from '@aegis-framework/pandora';
169
+ import { ShadowComponent, html } from '@aegis-framework/pandora';
121
170
 
122
171
  class MyShadowComponent extends ShadowComponent {
123
172
  constructor() {
@@ -128,6 +177,7 @@ class MyShadowComponent extends ShadowComponent {
128
177
  willMount() {
129
178
  // Set scoped styles
130
179
  this.setStyle({
180
+ ':host': { display: 'block' },
131
181
  h2: { color: 'blue' }
132
182
  });
133
183
 
@@ -135,7 +185,8 @@ class MyShadowComponent extends ShadowComponent {
135
185
  }
136
186
 
137
187
  render() {
138
- return `<h2>${this.props.text}</h2>`;
188
+ // Can use string templates or lit-html
189
+ return html`<h2>${this.props.text}</h2>`;
139
190
  }
140
191
  }
141
192
  ```
@@ -156,6 +207,7 @@ class MyShadowComponent extends ShadowComponent {
156
207
  | `props` | `object` | Component properties (read via proxy) |
157
208
  | `state` | `object` | Internal component state |
158
209
  | `dom` | `HTMLElement` | Reference to the component's DOM |
210
+ | `slotContent` | `string` | Original innerHTML content before rendering (for content projection) |
159
211
  | `width` | `number` | Computed width in pixels |
160
212
  | `height` | `number` | Computed height in pixels |
161
213
  | `isConnected` | `boolean` | Whether the component is connected to the DOM |
@@ -208,7 +260,7 @@ const items = element.queryAll('.item');
208
260
 
209
261
  ```javascript
210
262
  // Force re-render
211
- element.forceRender();
263
+ await element.forceRender();
212
264
 
213
265
  // Register ready callback
214
266
  element.ready(() => {
@@ -216,6 +268,8 @@ element.ready(() => {
216
268
  });
217
269
  ```
218
270
 
271
+ > **Note:** The `render()` method supports both sync and async returns. You can return a value directly, use `async/await`, or return a Promise. The component will automatically await Promises before updating the DOM.
272
+
219
273
  ### Lifecycle Hooks
220
274
 
221
275
  Override these methods to hook into the component lifecycle:
@@ -282,7 +336,8 @@ You can use HTML templates instead of the render method:
282
336
  ```html
283
337
  <template id="my-component">
284
338
  <div class="wrapper">
285
- <slot></slot>
339
+ <!-- For light DOM components, use slotContent in render() instead -->
340
+ <!-- For ShadowComponent, use native <slot> -->
286
341
  </div>
287
342
  </template>
288
343
  ```
@@ -290,18 +345,260 @@ You can use HTML templates instead of the render method:
290
345
  Or set templates programmatically:
291
346
 
292
347
  ```javascript
348
+ // String template
293
349
  MyComponent.template(`
294
350
  <div class="wrapper">
295
351
  <h2>{{title}}</h2>
296
352
  </div>
297
353
  `);
298
354
 
299
- // Or with a function for dynamic templates
355
+ // Function returning string
300
356
  MyComponent.template((context) => `
301
357
  <div class="wrapper">
302
358
  <h2>${context.props.title}</h2>
303
359
  </div>
304
360
  `);
361
+
362
+ // Function returning lit-html (recommended for dynamic content)
363
+ import { html } from '@aegis-framework/pandora';
364
+
365
+ MyComponent.template((context) => html`
366
+ <div class="wrapper">
367
+ <h2>${context.props.title}</h2>
368
+ <button @click=${() => context.handleClick()}>Click me</button>
369
+ </div>
370
+ `);
371
+
372
+ // Async function (for data fetching)
373
+ MyComponent.template(async (context) => {
374
+ const data = await fetchData(context.props.id);
375
+ return html`<div>${data.content}</div>`;
376
+ });
377
+ ```
378
+
379
+ ## lit-html Integration
380
+
381
+ Pandora includes [lit-html](https://lit.dev/docs/libraries/standalone-templates/) and re-exports its utilities for convenience. You don't need to install lit-html separately.
382
+
383
+ ### Exports
384
+
385
+ ```javascript
386
+ import {
387
+ // Tagged template functions
388
+ html, // For HTML templates
389
+ svg, // For SVG templates
390
+
391
+ // Special values
392
+ nothing, // Render nothing
393
+ noChange, // Skip updating a value
394
+
395
+ // Manual rendering (rarely needed)
396
+ render,
397
+
398
+ // Types (TypeScript)
399
+ TemplateResult,
400
+ SVGTemplateResult,
401
+ RenderOptions
402
+ } from '@aegis-framework/pandora';
403
+ ```
404
+
405
+ ### String vs lit-html Templates
406
+
407
+ Both rendering modes work seamlessly. Use strings for simple components, lit-html for complex UIs:
408
+
409
+ ```javascript
410
+ // String template (simple, familiar)
411
+ render() {
412
+ return `<div>${this.props.name}</div>`;
413
+ }
414
+
415
+ // lit-html template (efficient updates, event binding)
416
+ render() {
417
+ return html`
418
+ <div>
419
+ ${this.props.name}
420
+ <button @click=${() => this.handleClick()}>Click</button>
421
+ </div>
422
+ `;
423
+ }
424
+
425
+ // Async render (fetch data before rendering)
426
+ // Can use async/await OR return a Promise directly
427
+ async render() {
428
+ const data = await fetchUserData();
429
+ return html`<div>Hello, ${data.name}!</div>`;
430
+ }
431
+
432
+ // Promise-based render (no async keyword needed)
433
+ render() {
434
+ return fetchUserData().then(data =>
435
+ html`<div>Hello, ${data.name}!</div>`
436
+ );
437
+ }
438
+ ```
439
+
440
+ ### lit-html Features
441
+
442
+ When using `html` templates, you get access to lit-html features:
443
+
444
+ ```javascript
445
+ import { html, nothing } from '@aegis-framework/pandora';
446
+
447
+ class MyComponent extends Component {
448
+ render() {
449
+ return html`
450
+ <!-- Conditional rendering -->
451
+ ${this.state.show ? html`<div>Visible</div>` : nothing}
452
+
453
+ <!-- List rendering -->
454
+ <ul>
455
+ ${this.state.items.map(item => html`<li>${item}</li>`)}
456
+ </ul>
457
+
458
+ <!-- Event binding -->
459
+ <button @click=${(e) => this.handleClick(e)}>Click</button>
460
+
461
+ <!-- Property binding -->
462
+ <input .value=${this.state.name}>
463
+
464
+ <!-- Boolean attributes -->
465
+ <button ?disabled=${this.state.loading}>Submit</button>
466
+ `;
467
+ }
468
+ }
469
+ ```
470
+
471
+ ### Content Projection with slotContent
472
+
473
+ Use the `slotContent` property to include the original innerHTML in your templates:
474
+
475
+ ```html
476
+ <!-- Usage -->
477
+ <my-card>
478
+ <h3>Card Title</h3>
479
+ <p>Card content goes here.</p>
480
+ </my-card>
481
+ ```
482
+
483
+ ```javascript
484
+ class MyCard extends Component {
485
+ render() {
486
+ // String template
487
+ return `
488
+ <div class="card">
489
+ <div class="card-body">
490
+ ${this.slotContent}
491
+ </div>
492
+ </div>
493
+ `;
494
+ }
495
+ }
496
+
497
+ // Or with lit-html
498
+ class MyCard extends Component {
499
+ render() {
500
+ return html`
501
+ <div class="card">
502
+ <div class="card-body">
503
+ ${this.slotContent}
504
+ </div>
505
+ </div>
506
+ `;
507
+ }
508
+ }
509
+ ```
510
+
511
+ > **Note:** For ShadowComponent, use the native `<slot>` element instead of `slotContent` for proper Shadow DOM content projection.
512
+
513
+ ## Decorators
514
+
515
+ Pandora provides TypeScript/JavaScript decorators for cleaner component definitions.
516
+
517
+ > **Note:** Decorators require TypeScript or a build tool (Babel, esbuild, etc.) that supports the decorator syntax. For plain browser JavaScript, use the functional form: `Register('my-tag')(MyClass)` instead of `@Register('my-tag')`.
518
+
519
+ ### @Register(tagName)
520
+
521
+ Automatically registers the component with the CustomElementsRegistry:
522
+
523
+ ```typescript
524
+ import { Component, Register } from '@aegis-framework/pandora';
525
+
526
+ @Register('my-button')
527
+ class MyButton extends Component {
528
+ render() {
529
+ return `<button>${this.slotContent}</button>`;
530
+ }
531
+ }
532
+
533
+ // No need for: Registry.register('my-button', MyButton);
534
+ ```
535
+
536
+ ### @Consumer(stateKey)
537
+
538
+ Binds a class property to global Registry state. Automatically subscribes on connect and unsubscribes on disconnect:
539
+
540
+ ```typescript
541
+ import { Component, Register, Consumer, html } from '@aegis-framework/pandora';
542
+ import { Registry } from '@aegis-framework/pandora';
543
+
544
+ // Set initial state
545
+ Registry.setState('user.theme', 'light');
546
+ Registry.setState('user.name', 'Guest');
547
+
548
+ @Register('user-greeting')
549
+ class UserGreeting extends Component {
550
+ @Consumer('user.theme')
551
+ theme!: string;
552
+
553
+ @Consumer('user.name')
554
+ name!: string;
555
+
556
+ render() {
557
+ return html`
558
+ <div class="theme-${this.theme}">
559
+ Hello, ${this.name}!
560
+ </div>
561
+ `;
562
+ }
563
+ }
564
+
565
+ // Later, updating state automatically updates all @Consumer properties
566
+ Registry.setState('user.name', 'Alice'); // All UserGreeting instances re-render
567
+ ```
568
+
569
+ ### @Prop(attributeName?)
570
+
571
+ Declares a property that syncs with a DOM attribute:
572
+
573
+ ```typescript
574
+ import { Component, Register, Prop, html } from '@aegis-framework/pandora';
575
+
576
+ @Register('my-input')
577
+ class MyInput extends Component {
578
+ @Prop()
579
+ placeholder: string = '';
580
+
581
+ @Prop('max-length') // Custom attribute name
582
+ maxLength: number = 100;
583
+
584
+ @Prop()
585
+ disabled: boolean = false;
586
+
587
+ render() {
588
+ return html`
589
+ <input
590
+ placeholder=${this.placeholder}
591
+ maxlength=${this.maxLength}
592
+ ?disabled=${this.disabled}
593
+ >
594
+ `;
595
+ }
596
+ }
597
+ ```
598
+
599
+ ```html
600
+ <!-- Attributes sync with properties -->
601
+ <my-input placeholder="Enter text" max-length="50" disabled></my-input>
305
602
  ```
306
603
 
307
604
  ## Registry
@@ -598,10 +895,15 @@ const unsubscribeState = Registry.use('state', (component, state) => {
598
895
  return state;
599
896
  });
600
897
 
601
- // Render middleware - can modify rendered HTML
602
- const unsubscribeRender = Registry.use('render', (component, html) => {
603
- // Add wrapper or modify output
604
- return html;
898
+ // Render middleware - can modify rendered output
899
+ // The third parameter `renderType` indicates if the template is 'string' or 'lit'
900
+ const unsubscribeRender = Registry.use('render', (component, value, renderType) => {
901
+ if (renderType === 'string') {
902
+ // String templates can be modified directly
903
+ return value.replace('{{timestamp}}', Date.now());
904
+ }
905
+ // lit-html TemplateResults should be returned as-is or wrapped
906
+ return value;
605
907
  });
606
908
 
607
909
  // Clean up middleware
@@ -620,10 +922,14 @@ if (process.env.NODE_ENV === 'development') {
620
922
  return state;
621
923
  });
622
924
 
623
- // Track render performance
624
- Registry.use('render', (component, html) => {
625
- console.log(`[${component.static.tag}] rendered ${html.length} chars`);
626
- return html;
925
+ // Track render performance (type-aware)
926
+ Registry.use('render', (component, value, renderType) => {
927
+ if (renderType === 'string') {
928
+ console.log(`[${component.static.tag}] rendered ${value.length} chars (string)`);
929
+ } else {
930
+ console.log(`[${component.static.tag}] rendered (lit-html)`);
931
+ }
932
+ return value;
627
933
  });
628
934
  }
629
935
  ```
@@ -673,9 +979,15 @@ class ThemeToggle extends Component {
673
979
 
674
980
  ## Styling
675
981
 
982
+ Both `Component` and `ShadowComponent` use [Constructable Stylesheets](https://web.dev/constructable-stylesheets/) for optimal performance:
983
+
984
+ - **Shared across instances** - One stylesheet per component class, not per instance
985
+ - **Fast updates** - Uses `replaceSync()` for synchronous style changes
986
+ - **No DOM overhead** - No `<style>` element parsing
987
+
676
988
  ### Regular Components
677
989
 
678
- Styles are added to a shared `<style>` element in the document:
990
+ Styles are adopted into `document.adoptedStyleSheets` and automatically scoped using the component's tag name as a selector prefix:
679
991
 
680
992
  ```javascript
681
993
  element.setStyle({
@@ -687,11 +999,12 @@ element.setStyle({
687
999
  color: 'red'
688
1000
  }
689
1001
  });
1002
+ // Generates: my-element h2 { color: blue; ... }
690
1003
  ```
691
1004
 
692
1005
  ### Shadow Components
693
1006
 
694
- Styles are scoped to the Shadow DOM:
1007
+ Styles are adopted into the shadow root's `adoptedStyleSheets` and naturally scoped by Shadow DOM encapsulation:
695
1008
 
696
1009
  ```javascript
697
1010
  shadowElement.setStyle({
@@ -729,23 +1042,7 @@ For direct browser usage without a bundler:
729
1042
  ```html
730
1043
  <script src="path/to/pandora.browser.js"></script>
731
1044
  <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;
1045
+ const { Component, ShadowComponent, Registry, html } = Pandora;
749
1046
 
750
1047
  class CounterElement extends Component {
751
1048
  constructor() {
@@ -753,13 +1050,22 @@ Or use the Registry for component management:
753
1050
  this.state = { count: 0 };
754
1051
  }
755
1052
 
1053
+ didMount() {
1054
+ this.on('click', () => {
1055
+ this.setState({ count: this.state.count + 1 });
1056
+ });
1057
+ return Promise.resolve();
1058
+ }
1059
+
756
1060
  render() {
757
- return `<button>Count: ${this.state.count}</button>`;
1061
+ return html`<button>Count: ${this.state.count}</button>`;
758
1062
  }
759
1063
  }
760
1064
 
761
1065
  Registry.register('counter-element', CounterElement);
762
1066
  </script>
1067
+
1068
+ <counter-element></counter-element>
763
1069
  ```
764
1070
 
765
1071
  ## License