@jsenv/navi 0.0.1 → 0.1.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.
Files changed (139) hide show
  1. package/dist/jsenv_navi.js +22959 -0
  2. package/index.js +66 -16
  3. package/package.json +23 -11
  4. package/src/actions.js +50 -26
  5. package/src/browser_integration/browser_integration.js +31 -6
  6. package/src/browser_integration/via_history.js +42 -9
  7. package/src/components/action_execution/render_actionable_component.jsx +6 -4
  8. package/src/components/action_execution/use_action.js +51 -282
  9. package/src/components/action_execution/use_execute_action.js +106 -92
  10. package/src/components/action_execution/use_run_on_mount.js +9 -0
  11. package/src/components/action_renderer.jsx +21 -32
  12. package/src/components/demos/0_button_demo.html +574 -103
  13. package/src/components/demos/10_column_reordering_debug.html +277 -0
  14. package/src/components/demos/11_table_selection_debug.html +432 -0
  15. package/src/components/demos/1_checkbox_demo.html +579 -202
  16. package/src/components/demos/2_input_textual_demo.html +81 -138
  17. package/src/components/demos/3_radio_demo.html +0 -2
  18. package/src/components/demos/4_select_demo.html +19 -23
  19. package/src/components/demos/6_tablist_demo.html +77 -0
  20. package/src/components/demos/7_table_selection_demo.html +176 -0
  21. package/src/components/demos/8_table_fixed_headers_demo.html +584 -0
  22. package/src/components/demos/9_table_column_drag_demo.html +325 -0
  23. package/src/components/demos/action/0_button_demo.html +2 -4
  24. package/src/components/demos/action/1_input_text_demo.html +643 -222
  25. package/src/components/demos/action/3_details_demo.html +146 -115
  26. package/src/components/demos/action/4_input_checkbox_demo.html +442 -322
  27. package/src/components/demos/action/5_input_checkbox_state_demo.html +270 -0
  28. package/src/components/demos/action/6_checkbox_list_demo.html +304 -72
  29. package/src/components/demos/action/7_radio_list_demo.html +310 -170
  30. package/src/components/demos/action/{8_editable_text_demo.html → 8_editable_demo.html} +65 -76
  31. package/src/components/demos/action/9_link_demo.html +84 -62
  32. package/src/components/demos/ui_transition/0_action_renderer_ui_transition_demo.html +695 -0
  33. package/src/components/demos/ui_transition/1_nested_ui_transition_demo.html +429 -0
  34. package/src/components/demos/ui_transition/2_height_transition_test.html +295 -0
  35. package/src/components/details/details.jsx +62 -64
  36. package/src/components/edition/editable.jsx +186 -0
  37. package/src/components/field/README.md +247 -0
  38. package/src/components/{input → field}/button.jsx +151 -130
  39. package/src/components/field/checkbox_list.jsx +184 -0
  40. package/src/components/{collect_form_element_values.js → field/collect_form_element_values.js} +7 -4
  41. package/src/components/{input → field}/field_css.js +4 -1
  42. package/src/components/field/form.jsx +211 -0
  43. package/src/components/{input → field}/input.jsx +1 -0
  44. package/src/components/{input → field}/input_checkbox.jsx +132 -155
  45. package/src/components/{input → field}/input_radio.jsx +135 -46
  46. package/src/components/field/input_textual.jsx +418 -0
  47. package/src/components/field/label.jsx +32 -0
  48. package/src/components/field/radio_list.jsx +182 -0
  49. package/src/components/{input → field}/select.jsx +17 -32
  50. package/src/components/field/use_action_events.js +132 -0
  51. package/src/components/field/use_form_events.js +55 -0
  52. package/src/components/field/use_ui_state_controller.js +506 -0
  53. package/src/components/item_tracker/README.md +461 -0
  54. package/src/components/item_tracker/use_isolated_item_tracker.jsx +209 -0
  55. package/src/components/item_tracker/use_isolated_item_tracker_demo.html +148 -0
  56. package/src/components/item_tracker/use_isolated_item_tracker_demo.jsx +460 -0
  57. package/src/components/item_tracker/use_item_tracker.jsx +143 -0
  58. package/src/components/item_tracker/use_item_tracker_demo.html +207 -0
  59. package/src/components/item_tracker/use_item_tracker_demo.jsx +216 -0
  60. package/src/components/keyboard_shortcuts/active_keyboard_shortcuts.jsx +87 -0
  61. package/src/components/keyboard_shortcuts/aria_key_shortcuts.js +61 -0
  62. package/src/components/keyboard_shortcuts/keyboard_key_meta.js +17 -0
  63. package/src/components/keyboard_shortcuts/keyboard_shortcuts.js +371 -0
  64. package/src/components/link/link.jsx +65 -102
  65. package/src/components/link/link_with_icon.jsx +52 -0
  66. package/src/components/loader/loader_background.jsx +85 -64
  67. package/src/components/loader/rectangle_loading.jsx +38 -19
  68. package/src/components/route.jsx +8 -4
  69. package/src/components/selection/selection.jsx +1583 -0
  70. package/src/components/svg/font_sized_svg.jsx +45 -0
  71. package/src/components/svg/icon_and_text.jsx +21 -0
  72. package/src/components/svg/svg_mask_overlay.jsx +105 -0
  73. package/src/components/table/drag/table_drag.jsx +506 -0
  74. package/src/components/table/resize/table_resize.jsx +650 -0
  75. package/src/components/table/resize/table_size.js +43 -0
  76. package/src/components/table/selection/table_selection.js +106 -0
  77. package/src/components/table/selection/table_selection.jsx +203 -0
  78. package/src/components/table/sticky/sticky_group.js +354 -0
  79. package/src/components/table/sticky/table_sticky.js +25 -0
  80. package/src/components/table/sticky/table_sticky.jsx +501 -0
  81. package/src/components/table/table.jsx +721 -0
  82. package/src/components/table/table_css.js +211 -0
  83. package/src/components/table/table_ui.jsx +49 -0
  84. package/src/components/table/use_cells_and_columns.js +90 -0
  85. package/src/components/table/use_object_array_to_cells.js +46 -0
  86. package/src/components/table/z_indexes.js +23 -0
  87. package/src/components/tablist/tablist.jsx +99 -0
  88. package/src/components/text/overflow.jsx +15 -0
  89. package/src/components/text/text_and_count.jsx +28 -0
  90. package/src/components/ui_transition.jsx +128 -0
  91. package/src/components/use_auto_focus.js +58 -7
  92. package/src/components/use_batch_during_render.js +33 -0
  93. package/src/components/use_debounce_true.js +7 -7
  94. package/src/components/use_dependencies_diff.js +35 -0
  95. package/src/components/use_focus_group.js +4 -3
  96. package/src/components/use_initial_value.js +8 -34
  97. package/src/components/use_signal_sync.js +1 -1
  98. package/src/components/use_stable_callback.js +68 -0
  99. package/src/components/use_state_array.js +16 -9
  100. package/src/docs/actions.md +22 -0
  101. package/src/notes.md +33 -12
  102. package/src/route/route.js +97 -47
  103. package/src/store/resource_graph.js +2 -1
  104. package/src/store/tests/{resource_graph_dependencies.test.js → resource_graph_dependencies.test_manual.js} +13 -13
  105. package/src/utils/is_signal.js +20 -0
  106. package/src/utils/stringify_for_display.js +4 -23
  107. package/src/validation/constraints/confirm_constraint.js +14 -0
  108. package/src/validation/constraints/create_unique_value_constraint.js +27 -0
  109. package/src/validation/constraints/native_constraints.js +313 -0
  110. package/src/validation/constraints/readonly_constraint.js +36 -0
  111. package/src/validation/constraints/single_space_constraint.js +13 -0
  112. package/src/validation/custom_constraint_validation.js +599 -0
  113. package/src/validation/custom_message.js +18 -0
  114. package/src/validation/demos/browser_style.png +0 -0
  115. package/src/validation/demos/form_validation_demo.html +142 -0
  116. package/src/validation/demos/form_validation_demo_preact.html +87 -0
  117. package/src/validation/demos/form_validation_native_popover_demo.html +168 -0
  118. package/src/validation/demos/form_validation_vs_native_demo.html +172 -0
  119. package/src/validation/demos/validation_message_demo.html +203 -0
  120. package/src/validation/hooks/use_constraints.js +23 -0
  121. package/src/validation/hooks/use_custom_validation_ref.js +73 -0
  122. package/src/validation/hooks/use_validation_message.js +19 -0
  123. package/src/validation/validation_message.js +741 -0
  124. package/src/components/editable_text/editable_text.jsx +0 -96
  125. package/src/components/form.jsx +0 -144
  126. package/src/components/input/checkbox_list.jsx +0 -294
  127. package/src/components/input/field.jsx +0 -61
  128. package/src/components/input/input_textual.jsx +0 -338
  129. package/src/components/input/radio_list.jsx +0 -283
  130. package/src/components/input/use_form_event.js +0 -20
  131. package/src/components/input/use_on_change.js +0 -12
  132. package/src/components/selection/selection.js +0 -5
  133. package/src/components/selection/selection_context.jsx +0 -262
  134. package/src/components/shortcut/shortcut_context.jsx +0 -390
  135. package/src/components/use_action_events.js +0 -37
  136. package/src/utils/iterable_weak_set.js +0 -62
  137. /package/src/components/demos/action/{11_nested_shortcuts_demo.html → 11_nested_shortcuts_demo.xhtml} +0 -0
  138. /package/src/components/{shortcut → keyboard_shortcuts}/os.js +0 -0
  139. /package/src/route/{route.test.html → route.xtest.html} +0 -0
@@ -0,0 +1,506 @@
1
+ import { createPubSub } from "@jsenv/dom";
2
+ import { createContext } from "preact";
3
+ import {
4
+ useContext,
5
+ useLayoutEffect,
6
+ useMemo,
7
+ useRef,
8
+ useState,
9
+ } from "preact/hooks";
10
+
11
+ import { useNavState } from "../../browser_integration/browser_integration.js";
12
+ import { FormContext } from "../action_execution/form_context.js";
13
+ import { useInitialValue } from "../use_initial_value.js";
14
+
15
+ const DEBUG_UI_STATE_CONTROLLER = false;
16
+ const DEBUG_UI_GROUP_STATE_CONTROLLER = false;
17
+ const debugUIState = (...args) => {
18
+ if (DEBUG_UI_STATE_CONTROLLER) {
19
+ console.debug(...args);
20
+ }
21
+ };
22
+ const debugUIGroup = (...args) => {
23
+ if (DEBUG_UI_GROUP_STATE_CONTROLLER) {
24
+ console.debug(...args);
25
+ }
26
+ };
27
+
28
+ export const UIStateControllerContext = createContext();
29
+ export const UIStateContext = createContext();
30
+ export const ParentUIStateControllerContext = createContext();
31
+
32
+ export const FieldNameContext = createContext();
33
+ export const ReadOnlyContext = createContext();
34
+ export const DisabledContext = createContext();
35
+ export const RequiredContext = createContext();
36
+ export const LoadingContext = createContext();
37
+ export const LoadingElementContext = createContext();
38
+
39
+ /**
40
+ * UI State Controller Hook
41
+ *
42
+ * Manages the relationship between external state (props) and UI state (what user sees).
43
+ * Allows UI state to diverge temporarily for responsive interactions, with mechanisms
44
+ * to sync back to external state when needed.
45
+ *
46
+ * Key features:
47
+ * - Immediate UI updates for responsive interactions
48
+ * - State divergence with sync capabilities (resetUIState)
49
+ * - Group integration for coordinated form inputs
50
+ * - External control via custom events (onsetuistate/onresetuistate)
51
+ * - Error recovery and form reset support
52
+ *
53
+ * See README.md for detailed usage examples and patterns.
54
+ */
55
+ export const useUIStateController = (
56
+ props,
57
+ componentType,
58
+ {
59
+ statePropName = "value",
60
+ defaultStatePropName = "defaultValue",
61
+ fallbackState = "",
62
+ getStateFromProp = (prop) => prop,
63
+ getPropFromState = (state) => state,
64
+ } = {},
65
+ ) => {
66
+ const parentUIStateController = useContext(ParentUIStateControllerContext);
67
+ const formContext = useContext(FormContext);
68
+ const { id, name, onUIStateChange, action } = props;
69
+ const uncontrolled = !formContext && !action;
70
+ const [navState, setNavState] = useNavState(id);
71
+
72
+ const uiStateControllerRef = useRef();
73
+ const hasStateProp = Object.hasOwn(props, statePropName);
74
+ const state = props[statePropName];
75
+ const defaultState = props[defaultStatePropName];
76
+ const stateInitial = useInitialValue(() => {
77
+ if (hasStateProp) {
78
+ // controlled by state prop ("value" or "checked")
79
+ return getStateFromProp(state);
80
+ }
81
+ if (defaultState) {
82
+ // not controlled but want an initial state (a value or being checked)
83
+ return getStateFromProp(defaultState);
84
+ }
85
+ if (formContext && navState) {
86
+ // not controlled but want to use value from nav state
87
+ // (I think this should likely move earlier to win over the hasUIStateProp when it's undefined)
88
+ return getStateFromProp(navState);
89
+ }
90
+ return getStateFromProp(fallbackState);
91
+ });
92
+
93
+ /**
94
+ * This check is needed only for basic field because
95
+ * When using action/form we consider the action/form code
96
+ * will have a side effect that will re-render the component with the up-to-date state
97
+ *
98
+ * In practice we set the checked from the backend state
99
+ * We use action to fetch the new state and update the local state
100
+ * The component re-renders so it's the action/form that is considered as responsible
101
+ * to update the state and as a result allowed to have "checked"/"value" prop without "onUIStateChange"
102
+ */
103
+ const readOnly =
104
+ uncontrolled &&
105
+ hasStateProp &&
106
+ !onUIStateChange &&
107
+ !parentUIStateController;
108
+ if (readOnly && import.meta.dev) {
109
+ console.warn(
110
+ `"${componentType}" is controlled by "${statePropName}" prop. Replace it by "${defaultStatePropName}" or combine it with "onUIStateChange" to make field interactive.`,
111
+ );
112
+ }
113
+
114
+ const [
115
+ notifyParentAboutChildMount,
116
+ notifyParentAboutChildUIStateChange,
117
+ notifyParentAboutChildUnmount,
118
+ ] = useParentControllerNotifiers(
119
+ parentUIStateController,
120
+ uiStateControllerRef,
121
+ componentType,
122
+ );
123
+ useLayoutEffect(() => {
124
+ notifyParentAboutChildMount();
125
+ return notifyParentAboutChildUnmount;
126
+ }, []);
127
+
128
+ const existingUIStateController = uiStateControllerRef.current;
129
+ if (existingUIStateController) {
130
+ existingUIStateController._checkForUpdates({
131
+ readOnly,
132
+ name,
133
+ onUIStateChange,
134
+ getPropFromState,
135
+ getStateFromProp,
136
+ hasStateProp,
137
+ stateInitial,
138
+ state,
139
+ });
140
+ return existingUIStateController;
141
+ }
142
+ debugUIState(
143
+ `Creating "${componentType}" ui state controller - initial state:`,
144
+ JSON.stringify(stateInitial),
145
+ );
146
+ const [publishUIState, subscribeUIState] = createPubSub();
147
+ const uiStateController = {
148
+ _checkForUpdates: ({
149
+ readOnly,
150
+ name,
151
+ onUIStateChange,
152
+ getPropFromState,
153
+ getStateFromProp,
154
+ hasStateProp,
155
+ stateInitial,
156
+ state,
157
+ }) => {
158
+ uiStateController.readOnly = readOnly;
159
+ uiStateController.name = name;
160
+ uiStateController.onUIStateChange = onUIStateChange;
161
+ uiStateController.getPropFromState = getPropFromState;
162
+ uiStateController.getStateFromProp = getStateFromProp;
163
+ uiStateController.stateInitial = stateInitial;
164
+
165
+ if (hasStateProp) {
166
+ uiStateController.hasStateProp = true;
167
+ const currentState = uiStateController.state;
168
+ if (state !== currentState) {
169
+ uiStateController.state = state;
170
+ uiStateController.setUIState(
171
+ uiStateController.getPropFromState(state),
172
+ new CustomEvent("state_prop"),
173
+ );
174
+ }
175
+ } else if (uiStateController.hasStateProp) {
176
+ uiStateController.hasStateProp = false;
177
+ uiStateController.state = uiStateController.stateInitial;
178
+ }
179
+ },
180
+
181
+ componentType,
182
+ readOnly,
183
+ name,
184
+ hasStateProp,
185
+ state: stateInitial,
186
+ uiState: stateInitial,
187
+ onUIStateChange,
188
+ getPropFromState,
189
+ getStateFromProp,
190
+ setUIState: (prop, e) => {
191
+ const newUIState = uiStateController.getStateFromProp(prop);
192
+ if (formContext) {
193
+ setNavState(prop);
194
+ }
195
+ const currentUIState = uiStateController.uiState;
196
+ if (newUIState === currentUIState) {
197
+ return;
198
+ }
199
+ debugUIState(
200
+ `${componentType}.setUIState(${JSON.stringify(newUIState)}, "${e.type}") -> updating to ${JSON.stringify(newUIState)}`,
201
+ );
202
+ uiStateController.uiState = newUIState;
203
+ publishUIState(newUIState);
204
+ uiStateController.onUIStateChange?.(newUIState, e);
205
+ notifyParentAboutChildUIStateChange(e);
206
+ },
207
+ resetUIState: (e) => {
208
+ const currentState = uiStateController.state;
209
+ uiStateController.setUIState(currentState, e);
210
+ },
211
+ actionEnd: () => {
212
+ debugUIState(`"${componentType}" actionEnd called`);
213
+ if (formContext) {
214
+ setNavState(undefined);
215
+ }
216
+ },
217
+ subscribe: subscribeUIState,
218
+ };
219
+ uiStateControllerRef.current = uiStateController;
220
+ return uiStateController;
221
+ };
222
+
223
+ const NO_PARENT = [() => {}, () => {}, () => {}];
224
+ const useParentControllerNotifiers = (
225
+ parentUIStateController,
226
+ uiStateControllerRef,
227
+ componentType,
228
+ ) => {
229
+ return useMemo(() => {
230
+ if (!parentUIStateController) {
231
+ return NO_PARENT;
232
+ }
233
+
234
+ const parentComponentType = parentUIStateController.componentType;
235
+ const notifyParentAboutChildMount = () => {
236
+ const uiStateController = uiStateControllerRef.current;
237
+ debugUIState(
238
+ `"${componentType}" registering into "${parentComponentType}"`,
239
+ );
240
+ parentUIStateController.registerChild(uiStateController);
241
+ };
242
+
243
+ const notifyParentAboutChildUIStateChange = (e) => {
244
+ const uiStateController = uiStateControllerRef.current;
245
+ debugUIState(
246
+ `"${componentType}" notifying "${parentComponentType}" of ui state change`,
247
+ );
248
+ parentUIStateController.onChildUIStateChange(uiStateController, e);
249
+ };
250
+
251
+ const notifyParentAboutChildUnmount = () => {
252
+ const uiStateController = uiStateControllerRef.current;
253
+ debugUIState(
254
+ `"${componentType}" unregistering from "${parentComponentType}"`,
255
+ );
256
+ parentUIStateController.unregisterChild(uiStateController);
257
+ };
258
+
259
+ return [
260
+ notifyParentAboutChildMount,
261
+ notifyParentAboutChildUIStateChange,
262
+ notifyParentAboutChildUnmount,
263
+ ];
264
+ }, []);
265
+ };
266
+
267
+ /**
268
+ * UI Group State Controller Hook
269
+ *
270
+ * This hook manages a collection of child UI state controllers and aggregates their states
271
+ * into a unified group state. It provides a way to coordinate multiple form inputs that
272
+ * work together as a logical unit.
273
+ *
274
+ * What it provides:
275
+ *
276
+ * 1. **Child State Aggregation**:
277
+ * - Collects state from multiple child UI controllers
278
+ * - Combines them into a single meaningful group state
279
+ * - Updates group state automatically when any child changes
280
+ *
281
+ * 2. **Child Filtering**:
282
+ * - Can filter which child controllers to include based on component type
283
+ * - Useful for mixed content where only specific inputs matter
284
+ * - Enables type-safe aggregation patterns
285
+ *
286
+ * 3. **Group Operations**:
287
+ * - Provides `resetUIState()` that cascades to all children
288
+ * - Enables group-level operations like "clear all" or "reset form section"
289
+ * - Maintains consistency across related inputs
290
+ *
291
+ * 4. **External State Management**:
292
+ * - Notifies external code of group state changes via `onUIStateChange`
293
+ * - Allows external systems to react to group-level state changes
294
+ * - Supports complex form validation and submission logic
295
+ *
296
+ * Why use it:
297
+ * - When you have multiple related inputs that should be treated as one logical unit
298
+ * - For implementing checkbox lists, radio groups, or form sections
299
+ * - When you need to perform operations on multiple inputs simultaneously
300
+ * - To aggregate input states for validation or submission
301
+ *
302
+ * How it works:
303
+ * - Child controllers automatically register themselves when mounted
304
+ * - Group controller listens for child state changes and re-aggregates
305
+ * - Custom aggregation function determines how child states combine
306
+ * - Group state updates trigger notifications to external code
307
+ *
308
+ * @param {Object} props - Component props containing onUIStateChange callback
309
+ * @param {string} componentType - Type identifier for this group controller
310
+ * @param {Object} config - Configuration object
311
+ * @param {string} [config.childComponentType] - Filter children by this type (e.g., "checkbox")
312
+ * @param {Function} config.aggregateChildStates - Function to aggregate child states
313
+ * @param {any} [config.emptyState] - State to use when no children have values
314
+ * @returns {Object} UI group state controller
315
+ *
316
+ * Usage Examples:
317
+ * - **Checkbox List**: Aggregates multiple checkboxes into array of checked values
318
+ * - **Radio Group**: Manages radio buttons to ensure single selection
319
+ * - **Form Section**: Groups related inputs for validation and reset operations
320
+ * - **Dynamic Lists**: Handles variable number of repeated input groups
321
+ */
322
+
323
+ export const useUIGroupStateController = (
324
+ props,
325
+ componentType,
326
+ { childComponentType, aggregateChildStates, emptyState = undefined },
327
+ ) => {
328
+ if (typeof aggregateChildStates !== "function") {
329
+ throw new TypeError("aggregateChildStates must be a function");
330
+ }
331
+ const parentUIStateController = useContext(ParentUIStateControllerContext);
332
+ const { onUIStateChange, name } = props;
333
+ const childUIStateControllerArrayRef = useRef([]);
334
+ const childUIStateControllerArray = childUIStateControllerArrayRef.current;
335
+ const uiStateControllerRef = useRef();
336
+
337
+ const groupIsRenderingRef = useRef(false);
338
+ const pendingChangeRef = useRef(false);
339
+ groupIsRenderingRef.current = true;
340
+ pendingChangeRef.current = false;
341
+
342
+ const [
343
+ notifyParentAboutChildMount,
344
+ notifyParentAboutChildUIStateChange,
345
+ notifyParentAboutChildUnmount,
346
+ ] = useParentControllerNotifiers(
347
+ parentUIStateController,
348
+ uiStateControllerRef,
349
+ componentType,
350
+ );
351
+ useLayoutEffect(() => {
352
+ notifyParentAboutChildMount();
353
+ return notifyParentAboutChildUnmount;
354
+ }, []);
355
+
356
+ const onChange = (_, e) => {
357
+ if (groupIsRenderingRef.current) {
358
+ pendingChangeRef.current = true;
359
+ return;
360
+ }
361
+ const newUIState = aggregateChildStates(
362
+ childUIStateControllerArray,
363
+ emptyState,
364
+ );
365
+ const uiStateController = uiStateControllerRef.current;
366
+ uiStateController.setUIState(newUIState, e);
367
+ };
368
+
369
+ useLayoutEffect(() => {
370
+ groupIsRenderingRef.current = false;
371
+ if (pendingChangeRef.current) {
372
+ pendingChangeRef.current = false;
373
+ onChange(
374
+ null,
375
+ new CustomEvent(`${componentType}_batched_ui_state_update`),
376
+ );
377
+ }
378
+ });
379
+
380
+ const existingUIStateController = uiStateControllerRef.current;
381
+ if (existingUIStateController) {
382
+ existingUIStateController.name = name;
383
+ existingUIStateController.onUIStateChange = onUIStateChange;
384
+ return existingUIStateController;
385
+ }
386
+ debugUIGroup(
387
+ childComponentType === "*"
388
+ ? `Creating "${componentType}" ui state controller (monitoring all descendants ui state(s))"`
389
+ : `Creating "${componentType}" ui state controller (monitoring "${childComponentType}" ui state(s))`,
390
+ );
391
+
392
+ const [publishUIState, subscribeUIState] = createPubSub();
393
+ const isMonitoringChild = (childUIStateController) => {
394
+ if (childComponentType === "*") {
395
+ return true;
396
+ }
397
+ return childUIStateController.componentType === childComponentType;
398
+ };
399
+ const uiStateController = {
400
+ componentType,
401
+ name,
402
+ onUIStateChange,
403
+ uiState: emptyState,
404
+ setUIState: (newUIState, e) => {
405
+ const currentUIState = uiStateController.uiState;
406
+ if (newUIState === currentUIState) {
407
+ return;
408
+ }
409
+ uiStateController.uiState = newUIState;
410
+ debugUIGroup(
411
+ `${componentType}.setUIState(${JSON.stringify(newUIState)}, "${e.type}") -> updates from ${JSON.stringify(currentUIState)} to ${JSON.stringify(newUIState)}`,
412
+ );
413
+ publishUIState(newUIState);
414
+ uiStateController.onUIStateChange?.(newUIState, e);
415
+ notifyParentAboutChildUIStateChange(e);
416
+ },
417
+ registerChild: (childUIStateController) => {
418
+ if (!isMonitoringChild(childUIStateController)) {
419
+ return;
420
+ }
421
+ const childComponentType = childUIStateController.componentType;
422
+ childUIStateControllerArray.push(childUIStateController);
423
+ debugUIGroup(
424
+ `${componentType}.registerChild("${childComponentType}") -> registered (total: ${childUIStateControllerArray.length})`,
425
+ );
426
+ onChange(
427
+ childUIStateController,
428
+ new CustomEvent(`${childComponentType}_mount`),
429
+ );
430
+ },
431
+ onChildUIStateChange: (childUIStateController, e) => {
432
+ if (!isMonitoringChild(childUIStateController)) {
433
+ return;
434
+ }
435
+ debugUIGroup(
436
+ `${componentType}.onChildUIStateChange("${childComponentType}") to ${JSON.stringify(
437
+ childUIStateController.uiState,
438
+ )}`,
439
+ );
440
+ onChange(childUIStateController, e);
441
+ },
442
+ unregisterChild: (childUIStateController) => {
443
+ if (!isMonitoringChild(childUIStateController)) {
444
+ return;
445
+ }
446
+ const childComponentType = childUIStateController.componentType;
447
+ const index = childUIStateControllerArray.indexOf(childUIStateController);
448
+ if (index === -1) {
449
+ debugUIGroup(
450
+ `${componentType}.unregisterChild("${childComponentType}") -> not found`,
451
+ );
452
+ return;
453
+ }
454
+ childUIStateControllerArray.splice(index, 1);
455
+ debugUIGroup(
456
+ `${componentType}.unregisterChild("${childComponentType}") -> unregisteed (remaining: ${childUIStateControllerArray.length})`,
457
+ );
458
+ onChange(
459
+ childUIStateController,
460
+ new CustomEvent(`${childComponentType}_unmount`),
461
+ );
462
+ },
463
+ resetUIState: (e) => {
464
+ // we should likely batch the changes that will be reported for performances
465
+ for (const childUIStateController of childUIStateControllerArray) {
466
+ childUIStateController.resetUIState(e);
467
+ }
468
+ },
469
+ actionEnd: (e) => {
470
+ for (const childUIStateController of childUIStateControllerArray) {
471
+ childUIStateController.actionEnd(e);
472
+ }
473
+ },
474
+ subscribe: subscribeUIState,
475
+ };
476
+ uiStateControllerRef.current = uiStateController;
477
+ return uiStateController;
478
+ };
479
+
480
+ /**
481
+ * Hook to track UI state from a UI state controller
482
+ *
483
+ * This hook allows external code to react to UI state changes without
484
+ * causing the controller itself to re-create. It returns the current UI state
485
+ * and will cause re-renders when the UI state changes.
486
+ *
487
+ * @param {Object} uiStateController - The UI state controller to track
488
+ * @returns {any} The current UI state
489
+ */
490
+ export const useUIState = (uiStateController) => {
491
+ const [trackedUIState, setTrackedUIState] = useState(
492
+ uiStateController.uiState,
493
+ );
494
+
495
+ useLayoutEffect(() => {
496
+ // Subscribe to UI state changes
497
+ const unsubscribe = uiStateController.subscribe(setTrackedUIState);
498
+
499
+ // Sync with current state in case it changed before subscription
500
+ setTrackedUIState(uiStateController.uiState);
501
+
502
+ return unsubscribe;
503
+ }, [uiStateController]);
504
+
505
+ return trackedUIState;
506
+ };