@pure-ds/core 0.7.57 → 0.7.58

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.
@@ -3,9 +3,24 @@
3
3
  * Kept separate to avoid pulling live-only logic into the base runtime bundle.
4
4
  */
5
5
 
6
- import { AutoDefiner as CoreAutoDefiner } from "pure-web/auto-definer";
7
6
  import { PDS } from "../pds-singleton.js";
8
7
 
8
+ let __coreAutoDefinerCtorPromise = null;
9
+
10
+ async function getCoreAutoDefinerCtor() {
11
+ if (!__coreAutoDefinerCtorPromise) {
12
+ __coreAutoDefinerCtorPromise = import("pure-web/auto-definer")
13
+ .then((mod) => mod?.AutoDefiner || mod?.default?.AutoDefiner || mod?.default)
14
+ .then((ctor) => {
15
+ if (typeof ctor !== "function") {
16
+ throw new Error("AutoDefiner constructor not found in pure-web/auto-definer");
17
+ }
18
+ return ctor;
19
+ });
20
+ }
21
+ return __coreAutoDefinerCtorPromise;
22
+ }
23
+
9
24
  const __ABSOLUTE_URL_PATTERN__ = /^[a-z][a-z0-9+\-.]*:\/\//i;
10
25
  const __MODULE_URL__ = (() => {
11
26
  try {
@@ -357,7 +372,7 @@ export async function setupAutoDefinerAndEnhancers(options, { baseEnhancers = []
357
372
  // Setup AutoDefiner in browser context (it already observes shadow DOMs)
358
373
  let autoDefiner = null;
359
374
  if (typeof window !== "undefined" && typeof document !== "undefined") {
360
- const AutoDefinerCtor = CoreAutoDefiner;
375
+ const AutoDefinerCtor = await getCoreAutoDefinerCtor();
361
376
 
362
377
  const defaultMapper = (tag) => {
363
378
  switch (tag) {
package/src/js/pds.d.ts CHANGED
@@ -458,6 +458,24 @@ export class PDS extends EventTarget {
458
458
  */
459
459
  static ask(message: AskMessage, options?: AskOptions): Promise<any>;
460
460
 
461
+ /**
462
+ * Reactive state container class for building reactive components.
463
+ * Fires custom events ('change' and 'change:propertyName') when state properties are modified.
464
+ */
465
+ static State: typeof State;
466
+
467
+ /**
468
+ * Create a reactive component with automatic re-rendering on state changes.
469
+ * Each state mutation will automatically re-render the component.
470
+ */
471
+ static createReactiveComponent: typeof createReactiveComponent;
472
+
473
+ /**
474
+ * Bind a state container to an element, auto-updating specific properties when state changes.
475
+ * Useful for partial updates without full re-renders.
476
+ */
477
+ static bindState: typeof bindState;
478
+
461
479
  /**
462
480
  * Current configuration after PDS.start() completes - read-only, frozen after initialization.
463
481
  * Contains the complete configuration used to initialize PDS, including mode, design, preset, and theme.
@@ -754,3 +772,50 @@ export class SchemaForm extends HTMLElement {
754
772
 
755
773
  export function parse(html: PDSParseInput, ...values: unknown[]): NodeListOf<ChildNode>;
756
774
  export function html(html: PDSParseInput, ...values: unknown[]): DocumentFragment;
775
+
776
+ /**
777
+ * Reactive state container that fires custom events on property mutations.
778
+ * When properties change, it emits "change" and "change:propertyName" events.
779
+ *
780
+ * @example
781
+ * ```javascript
782
+ * const state = new State({ count: 0 });
783
+ * state.on('change:count', (e) => console.log('Count changed to:', e.detail.value));
784
+ * state.count++; // Fires events and updates DOM
785
+ * ```
786
+ */
787
+ export class State<T extends Record<string, any> = Record<string, any>> {
788
+ constructor(initialValue?: T, eventTarget?: EventTarget);
789
+ on(event: string, callback: (e: CustomEvent) => void): this;
790
+ once(event: string, callback: (e: CustomEvent) => void): this;
791
+ off(event: string, callback: (e: CustomEvent) => void): this;
792
+ [key: string]: any;
793
+ }
794
+
795
+ /**
796
+ * Create a reactive component with automatic re-rendering on state changes.
797
+ *
798
+ * @param initialState - Initial state object
799
+ * @param renderFn - Function that takes state and returns a DOM fragment
800
+ * @param container - Container element where the component renders
801
+ * @returns The reactive state proxy
802
+ */
803
+ export function createReactiveComponent<T extends Record<string, any>>(
804
+ initialState: T,
805
+ renderFn: (state: State<T>) => DocumentFragment | Node,
806
+ container: HTMLElement
807
+ ): State<T>;
808
+
809
+ /**
810
+ * Bind a state container to an element, auto-updating specific properties when state changes.
811
+ *
812
+ * @param element - Element to update
813
+ * @param state - Reactive state container
814
+ * @param bindings - Map of property names to update handlers
815
+ * @returns Cleanup function to remove event listeners
816
+ */
817
+ export function bindState<T extends Record<string, any>>(
818
+ element: HTMLElement,
819
+ state: State<T>,
820
+ bindings: Record<keyof T, (el: HTMLElement, value: any, oldValue?: any) => void>
821
+ ): () => void;
package/src/js/pds.js CHANGED
@@ -77,6 +77,7 @@ import {
77
77
  resolveThemePreference,
78
78
  } from "./pds-core/pds-theme-utils.js";
79
79
  import { configurePDSLogger, pdsLog } from "./common/pds-log.js";
80
+ import { State, createReactiveComponent, bindState } from "./pds-reactive.js";
80
81
 
81
82
  let __autoCompletePromise = null;
82
83
  let __askPromise = null;
@@ -646,6 +647,15 @@ PDS.parse = parse;
646
647
  export const html = common.parseFragment;
647
648
  PDS.html = html;
648
649
 
650
+ /** Reactive state container for building reactive components */
651
+ PDS.State = State;
652
+
653
+ // /** Create a reactive component with automatic re-rendering */
654
+ // PDS.createReactiveComponent = createReactiveComponent;
655
+
656
+ /** Bind state to DOM elements for auto-updating */
657
+ PDS.bindState = bindState;
658
+
649
659
  /** Create a constructable CSSStyleSheet from a CSS string */
650
660
  PDS.createStylesheet = createStylesheet;
651
661
 
@@ -1184,4 +1194,7 @@ export {
1184
1194
  setLocale,
1185
1195
  getLocalizationState,
1186
1196
  createJSONLocalization,
1197
+ State,
1198
+ createReactiveComponent,
1199
+ bindState,
1187
1200
  };