@jsenv/navi 0.10.2 → 0.11.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 (207) hide show
  1. package/dist/jsenv_navi.js +13838 -23291
  2. package/dist/jsenv_navi.js.map +1281 -0
  3. package/package.json +6 -8
  4. package/index.js +0 -122
  5. package/src/action_private_properties.js +0 -11
  6. package/src/action_proxy_test.html +0 -353
  7. package/src/action_run_states.js +0 -5
  8. package/src/actions.js +0 -1401
  9. package/src/browser_integration/browser_integration.js +0 -216
  10. package/src/browser_integration/document_back_and_forward.js +0 -17
  11. package/src/browser_integration/document_loading_signal.js +0 -100
  12. package/src/browser_integration/document_state_signal.js +0 -9
  13. package/src/browser_integration/document_url_signal.js +0 -9
  14. package/src/browser_integration/use_is_visited.js +0 -19
  15. package/src/browser_integration/via_history.js +0 -232
  16. package/src/browser_integration/via_navigation.js +0 -168
  17. package/src/components/action_execution/form_context.js +0 -5
  18. package/src/components/action_execution/render_actionable_component.jsx +0 -29
  19. package/src/components/action_execution/use_action.js +0 -99
  20. package/src/components/action_execution/use_execute_action.js +0 -193
  21. package/src/components/action_execution/use_run_on_mount.js +0 -9
  22. package/src/components/action_renderer.jsx +0 -125
  23. package/src/components/callout/callout.js +0 -990
  24. package/src/components/callout/callout_demo.html +0 -201
  25. package/src/components/callout/test_dynamic_positioning.html +0 -161
  26. package/src/components/callout/test_html_document_iframe.html +0 -182
  27. package/src/components/demos/0_button_demo.html +0 -707
  28. package/src/components/demos/10_column_reordering_debug.html +0 -277
  29. package/src/components/demos/11_table_selection_debug.html +0 -432
  30. package/src/components/demos/1_checkbox_demo.html +0 -754
  31. package/src/components/demos/2_input_textual_demo.html +0 -286
  32. package/src/components/demos/3_radio_demo.html +0 -874
  33. package/src/components/demos/4_select_demo.html +0 -100
  34. package/src/components/demos/5_list_scrollable_demo.html +0 -153
  35. package/src/components/demos/6_tablist_demo.html +0 -77
  36. package/src/components/demos/7_table_selection_demo.html +0 -176
  37. package/src/components/demos/8_table_fixed_headers_demo.html +0 -584
  38. package/src/components/demos/9_table_column_drag_demo.html +0 -325
  39. package/src/components/demos/action/0_button_demo.html +0 -204
  40. package/src/components/demos/action/10_shortcuts_demo.html +0 -189
  41. package/src/components/demos/action/11_nested_shortcuts_demo.xhtml +0 -401
  42. package/src/components/demos/action/1_input_text_demo.html +0 -876
  43. package/src/components/demos/action/2_form_multiple.html +0 -303
  44. package/src/components/demos/action/3_details_demo.html +0 -203
  45. package/src/components/demos/action/4_input_checkbox_demo.html +0 -731
  46. package/src/components/demos/action/5_input_checkbox_state_demo.html +0 -270
  47. package/src/components/demos/action/6_checkbox_list_demo.html +0 -341
  48. package/src/components/demos/action/7_radio_list_demo.html +0 -357
  49. package/src/components/demos/action/8_editable_demo.html +0 -431
  50. package/src/components/demos/action/9_link_demo.html +0 -194
  51. package/src/components/demos/demo.md +0 -0
  52. package/src/components/demos/route/basic/basic.html +0 -14
  53. package/src/components/demos/route/basic/basic_route_demo.jsx +0 -224
  54. package/src/components/demos/route/multi/multi.html +0 -14
  55. package/src/components/demos/route/multi/multi_route_demo.jsx +0 -277
  56. package/src/components/demos/ui_transition/0_action_renderer_ui_transition_demo.html +0 -695
  57. package/src/components/demos/ui_transition/1_nested_ui_transition_demo.html +0 -429
  58. package/src/components/demos/ui_transition/2_height_transition_test.html +0 -295
  59. package/src/components/details/details.jsx +0 -245
  60. package/src/components/details/summary_marker.jsx +0 -141
  61. package/src/components/edition/editable.jsx +0 -186
  62. package/src/components/error_boundary_context.js +0 -9
  63. package/src/components/field/README.md +0 -247
  64. package/src/components/field/button.jsx +0 -429
  65. package/src/components/field/checkbox_list.jsx +0 -185
  66. package/src/components/field/collect_form_element_values.js +0 -82
  67. package/src/components/field/custom_field.js +0 -106
  68. package/src/components/field/form.jsx +0 -209
  69. package/src/components/field/input.jsx +0 -16
  70. package/src/components/field/input_checkbox.jsx +0 -434
  71. package/src/components/field/input_radio.jsx +0 -432
  72. package/src/components/field/input_textual.jsx +0 -389
  73. package/src/components/field/label.jsx +0 -46
  74. package/src/components/field/radio_list.jsx +0 -183
  75. package/src/components/field/select.jsx +0 -256
  76. package/src/components/field/use_action_events.js +0 -132
  77. package/src/components/field/use_form_events.js +0 -59
  78. package/src/components/field/use_ui_state_controller.js +0 -506
  79. package/src/components/item_tracker/README.md +0 -461
  80. package/src/components/item_tracker/use_isolated_item_tracker.jsx +0 -209
  81. package/src/components/item_tracker/use_isolated_item_tracker_demo.html +0 -148
  82. package/src/components/item_tracker/use_isolated_item_tracker_demo.jsx +0 -460
  83. package/src/components/item_tracker/use_item_tracker.jsx +0 -143
  84. package/src/components/item_tracker/use_item_tracker_demo.html +0 -207
  85. package/src/components/item_tracker/use_item_tracker_demo.jsx +0 -216
  86. package/src/components/keyboard_shortcuts/active_keyboard_shortcuts.jsx +0 -87
  87. package/src/components/keyboard_shortcuts/aria_key_shortcuts.js +0 -61
  88. package/src/components/keyboard_shortcuts/keyboard_key_meta.js +0 -17
  89. package/src/components/keyboard_shortcuts/keyboard_shortcuts.js +0 -371
  90. package/src/components/keyboard_shortcuts/os.js +0 -9
  91. package/src/components/layout/demos/demo_flex.html +0 -638
  92. package/src/components/layout/demos/demo_layout_style_buttons.html +0 -351
  93. package/src/components/layout/demos/demo_layout_style_input.html +0 -226
  94. package/src/components/layout/demos/demo_layout_style_text.html +0 -514
  95. package/src/components/layout/flex.jsx +0 -109
  96. package/src/components/layout/layout_context.jsx +0 -3
  97. package/src/components/layout/spacing.jsx +0 -20
  98. package/src/components/layout/use_layout_style.js +0 -249
  99. package/src/components/link/link.jsx +0 -267
  100. package/src/components/link/link_with_icon.jsx +0 -52
  101. package/src/components/loader/loader_background.jsx +0 -372
  102. package/src/components/loader/loading_spinner.jsx +0 -68
  103. package/src/components/loader/network_speed.js +0 -83
  104. package/src/components/loader/rectangle_loading.jsx +0 -244
  105. package/src/components/props_composition/demos/demo_with_props_style.html +0 -81
  106. package/src/components/props_composition/with_props_class_name.js +0 -37
  107. package/src/components/props_composition/with_props_style.js +0 -26
  108. package/src/components/route.jsx +0 -19
  109. package/src/components/selection/selection.jsx +0 -1583
  110. package/src/components/svg/font_sized_svg.jsx +0 -59
  111. package/src/components/svg/icon_and_text.jsx +0 -21
  112. package/src/components/svg/svg_mask_overlay.jsx +0 -105
  113. package/src/components/table/drag/table_drag.jsx +0 -506
  114. package/src/components/table/resize/table_resize.jsx +0 -650
  115. package/src/components/table/resize/table_size.js +0 -43
  116. package/src/components/table/selection/table_selection.js +0 -106
  117. package/src/components/table/selection/table_selection.jsx +0 -203
  118. package/src/components/table/sticky/sticky_group.js +0 -354
  119. package/src/components/table/sticky/table_sticky.js +0 -25
  120. package/src/components/table/sticky/table_sticky.jsx +0 -501
  121. package/src/components/table/table.jsx +0 -721
  122. package/src/components/table/table_css.js +0 -211
  123. package/src/components/table/table_ui.jsx +0 -49
  124. package/src/components/table/use_cells_and_columns.js +0 -90
  125. package/src/components/table/use_object_array_to_cells.js +0 -46
  126. package/src/components/table/z_indexes.js +0 -23
  127. package/src/components/tablist/tablist.jsx +0 -99
  128. package/src/components/text/demos/demo_text_and_icon.html +0 -421
  129. package/src/components/text/overflow.jsx +0 -15
  130. package/src/components/text/text.jsx +0 -83
  131. package/src/components/text/text_and_count.jsx +0 -28
  132. package/src/components/ui_transition.jsx +0 -128
  133. package/src/components/use_auto_focus.js +0 -94
  134. package/src/components/use_batch_during_render.js +0 -33
  135. package/src/components/use_debounce_true.js +0 -31
  136. package/src/components/use_dependencies_diff.js +0 -35
  137. package/src/components/use_focus_group.js +0 -20
  138. package/src/components/use_initial_value.js +0 -78
  139. package/src/components/use_is_visited.js +0 -19
  140. package/src/components/use_ref_array.js +0 -38
  141. package/src/components/use_signal_sync.js +0 -50
  142. package/src/components/use_stable_callback.js +0 -68
  143. package/src/components/use_state_array.js +0 -47
  144. package/src/docs/actions.md +0 -250
  145. package/src/docs/demos/resource/action_status.jsx +0 -42
  146. package/src/docs/demos/resource/demo.md +0 -1
  147. package/src/docs/demos/resource/resource_demo_0.html +0 -84
  148. package/src/docs/demos/resource/resource_demo_10_post_gc.html +0 -364
  149. package/src/docs/demos/resource/resource_demo_11_describe_many.html +0 -362
  150. package/src/docs/demos/resource/resource_demo_2.html +0 -173
  151. package/src/docs/demos/resource/resource_demo_3_filtered_users.html +0 -415
  152. package/src/docs/demos/resource/resource_demo_4_details.html +0 -284
  153. package/src/docs/demos/resource/resource_demo_5_renderer_lazy.html +0 -115
  154. package/src/docs/demos/resource/resource_demo_6_gc.html +0 -217
  155. package/src/docs/demos/resource/resource_demo_7_child_gc.html +0 -240
  156. package/src/docs/demos/resource/resource_demo_8_proxy_gc.html +0 -319
  157. package/src/docs/demos/resource/resource_demo_9_describe_one.html +0 -472
  158. package/src/docs/demos/resource/tata.jsx +0 -3
  159. package/src/docs/demos/resource/toto.jsx +0 -3
  160. package/src/docs/demos/user_nav/user_nav.html +0 -12
  161. package/src/docs/demos/user_nav/user_nav.jsx +0 -330
  162. package/src/docs/resource_dependencies.md +0 -103
  163. package/src/docs/resource_with_params.md +0 -80
  164. package/src/navi_css_vars.js +0 -14
  165. package/src/notes.md +0 -34
  166. package/src/route/route.js +0 -596
  167. package/src/route/route.xtest.html +0 -228
  168. package/src/store/array_signal_store.js +0 -537
  169. package/src/store/local_storage_signal.js +0 -17
  170. package/src/store/resource_graph.js +0 -1304
  171. package/src/store/tests/resource_graph_autoreload_demo.html +0 -12
  172. package/src/store/tests/resource_graph_autoreload_demo.jsx +0 -964
  173. package/src/store/tests/resource_graph_dependencies.test_manual.js +0 -95
  174. package/src/store/value_in_local_storage.js +0 -187
  175. package/src/symbol_object_signal.js +0 -1
  176. package/src/use_action_data.js +0 -10
  177. package/src/use_action_status.js +0 -47
  178. package/src/utils/add_many_event_listeners.js +0 -15
  179. package/src/utils/array_add_remove.js +0 -61
  180. package/src/utils/array_signal.js +0 -15
  181. package/src/utils/compare_two_js_values.js +0 -172
  182. package/src/utils/execute_with_cleanup.js +0 -21
  183. package/src/utils/get_caller_info.js +0 -85
  184. package/src/utils/is_signal.js +0 -20
  185. package/src/utils/js_value_weak_map.js +0 -162
  186. package/src/utils/js_value_weak_map_demo.html +0 -690
  187. package/src/utils/merge_two_js_values.js +0 -53
  188. package/src/utils/stringify_for_display.js +0 -131
  189. package/src/utils/weak_effect.js +0 -48
  190. package/src/validation/constraints/confirm_constraint.js +0 -14
  191. package/src/validation/constraints/create_unique_value_constraint.js +0 -27
  192. package/src/validation/constraints/native_constraints.js +0 -338
  193. package/src/validation/constraints/readonly_constraint.js +0 -41
  194. package/src/validation/constraints/same_as_constraint.js +0 -42
  195. package/src/validation/constraints/single_space_constraint.js +0 -13
  196. package/src/validation/custom_constraint_validation.js +0 -793
  197. package/src/validation/custom_message.js +0 -18
  198. package/src/validation/demos/browser_style.png +0 -0
  199. package/src/validation/demos/demo_same_as_constraint.html +0 -259
  200. package/src/validation/demos/form_validation_demo.html +0 -142
  201. package/src/validation/demos/form_validation_demo_preact.html +0 -87
  202. package/src/validation/demos/form_validation_native_popover_demo.html +0 -168
  203. package/src/validation/demos/form_validation_vs_native_demo.html +0 -172
  204. package/src/validation/hooks/use_constraints.js +0 -23
  205. package/src/validation/hooks/use_custom_validation_ref.js +0 -73
  206. package/src/validation/hooks/use_validation_message.js +0 -19
  207. package/src/validation/input_change_effect.js +0 -106
@@ -1,168 +0,0 @@
1
- import { updateDocumentUrl } from "./document_url_signal.js";
2
-
3
- let DEBUG = false;
4
-
5
- export const setupRoutingViaNavigation = (handler) => {
6
- updateDocumentUrl(navigation.currentEntry.url);
7
- navigation.addEventListener("currententrychange", () => {
8
- updateDocumentUrl(navigation.currentEntry.url);
9
- });
10
-
11
- let isReloadFromNavigationAPI = false;
12
- const navigationReload = navigation.reload;
13
- navigation.reload = (...args) => {
14
- isReloadFromNavigationAPI = true;
15
- navigationReload.call(navigation, ...args);
16
- isReloadFromNavigationAPI = false;
17
- };
18
-
19
- navigation.addEventListener("navigate", (event) => {
20
- if (!event.canIntercept) {
21
- return;
22
- }
23
- if (event.hashChange || event.downloadRequest !== null) {
24
- return;
25
- }
26
- if (
27
- !event.userInitiated &&
28
- event.navigationType === "reload" &&
29
- event.isTrusted &&
30
- !isReloadFromNavigationAPI
31
- ) {
32
- // let window.location.reload() reload the whole document
33
- // (used by jsenv hot reload)
34
- return;
35
- }
36
- const { signal } = event;
37
- const formAction = event.info?.formAction;
38
- const isReload = event.navigationType === "reload";
39
- const isReplace = event.navigationType === "replace";
40
- const currentUrl = navigation.currentEntry.url;
41
- const destinationUrl = event.destination.url;
42
- const currentState = navigation.currentEntry.getState();
43
- const destinationState = event.destination.getState();
44
- const formData = event.formData || event.info?.formData;
45
- const formUrl = event.info?.formUrl;
46
- const stopAbortController = new AbortController();
47
- const stopSignal = stopAbortController.signal;
48
- const removeStopButtonClickDetector = detectBrowserStopButtonClick(
49
- signal,
50
- () => {
51
- stopAbortController.abort("stop button clicked");
52
- },
53
- );
54
-
55
- if (DEBUG) {
56
- console.log("receive navigate event", {
57
- destinationUrl,
58
- destinationState,
59
- });
60
- }
61
-
62
- event.intercept({
63
- handler: async () => {
64
- if (event.info?.onStart) {
65
- event.info.onStart();
66
- }
67
- try {
68
- await handler(event, {
69
- abortSignal: signal,
70
- stopSignal,
71
- formAction,
72
- formData,
73
- formUrl,
74
- isReload,
75
- isReplace,
76
- currentUrl,
77
- destinationUrl,
78
- currentState,
79
- destinationState,
80
- });
81
- } catch (e) {
82
- console.error(e); // browser remains silent in case of error during handler so we explicitely log the error to the console
83
- throw e;
84
- } finally {
85
- removeStopButtonClickDetector();
86
- }
87
- },
88
- // https://github.com/WICG/navigation-api?tab=readme-ov-file#focus-management
89
- // without this, after clicking <a href="...">, the focus does to document.body
90
- // which is problematic for shortcuts for instance
91
- focusReset: "manual",
92
- });
93
- });
94
- };
95
-
96
- // setupNavigateHandler(async (event, { stopSignal, formAction, formData }) => {
97
- // if (formAction) {
98
- // const result = await applyAction(formAction, {
99
- // signal: stopSignal,
100
- // formData,
101
- // });
102
- // event.info.formActionCallback(result);
103
- // }
104
-
105
- // await navMethods.applyRouting({
106
- // sourceUrl: currentUrl,
107
- // targetUrl: formUrl || destinationUrl,
108
- // sourceState: currentState,
109
- // targetState: destinationState || currentState,
110
- // abortSignal,
111
- // stopSignal,
112
- // isReload,
113
- // isReplace,
114
- // info: event.info,
115
- // });
116
- // if (formUrl) {
117
- // const finishedPromise = event.target.transition.finished;
118
- // (async () => {
119
- // try {
120
- // await finishedPromise;
121
- // } finally {
122
- // navigation.navigate(window.location.href, {
123
- // state: navigation.currentEntry.getState(),
124
- // history: "replace",
125
- // });
126
- // }
127
- // })();
128
- // }
129
- // });
130
-
131
- /**
132
- * There is 2 distinct reason to abort a navigation:
133
- * - the user clicked the browser stop button
134
- * - the user navigate to an other page
135
- *
136
- * When navigating to an other page we don't want to abort anything, the routing does that
137
- * When clicking the stop button we want to cancel everything
138
- *
139
- * To detect that when aborted, we wait a setTimeout to see if we receive a new navigation
140
- * If yes it means this is an abort due to a new navigation
141
- * Otherwise it's an abort due to the stop button
142
- *
143
- * On top of that stop button must cancel X navigation so the last navigation detecting the stop click
144
- * is notifying any current navigation that stop button was clicked
145
- */
146
- let callEffect = () => {};
147
- const browserStopButtonClickCallbackSet = new Set();
148
- const detectBrowserStopButtonClick = (navigateEventSignal, callback) => {
149
- callEffect();
150
- browserStopButtonClickCallbackSet.add(callback);
151
- navigateEventSignal.addEventListener("abort", async () => {
152
- const timeout = setTimeout(() => {
153
- callEffect = () => {};
154
-
155
- for (const browserStopButtonClickCallback of browserStopButtonClickCallbackSet) {
156
- browserStopButtonClickCallback();
157
- }
158
- browserStopButtonClickCallbackSet.clear();
159
- });
160
- callEffect = () => {
161
- clearTimeout(timeout);
162
- };
163
- });
164
-
165
- return () => {
166
- browserStopButtonClickCallbackSet.delete(callback);
167
- };
168
- };
@@ -1,5 +0,0 @@
1
- import { createContext } from "preact";
2
-
3
- export const FormContext = createContext();
4
-
5
- export const FormActionContext = createContext();
@@ -1,29 +0,0 @@
1
- import { useContext } from "preact/hooks";
2
-
3
- import { FormContext } from "./form_context.js";
4
-
5
- export const renderActionableComponent = (
6
- props,
7
- ref,
8
- { Basic, WithAction, InsideForm, WithActionInsideForm },
9
- ) => {
10
- const { action, shortcuts } = props;
11
- const formContext = useContext(FormContext);
12
- const hasActionProps = Boolean(action || (shortcuts && shortcuts.length > 0));
13
- const considerInsideForm = Boolean(formContext);
14
-
15
- if (hasActionProps && WithAction) {
16
- if (considerInsideForm && WithActionInsideForm) {
17
- return (
18
- <WithActionInsideForm formContext={formContext} ref={ref} {...props} />
19
- );
20
- }
21
- return <WithAction ref={ref} {...props} />;
22
- }
23
-
24
- if (considerInsideForm && InsideForm) {
25
- return <InsideForm formContext={formContext} ref={ref} {...props} />;
26
- }
27
-
28
- return <Basic ref={ref} {...props} />;
29
- };
@@ -1,99 +0,0 @@
1
- import { useSignal } from "@preact/signals";
2
- import { useCallback, useRef } from "preact/hooks";
3
-
4
- import { createAction } from "../../actions.js";
5
- import { addIntoArray, removeFromArray } from "../../utils/array_add_remove.js";
6
-
7
- // used by form elements such as <input>, <select>, <textarea> to have their own action bound to a single parameter
8
- // when inside a <form> the form params are updated when the form element single param is updated
9
- export const useActionBoundToOneParam = (action, externalValue) => {
10
- const actionFirstArgSignal = useSignal(externalValue);
11
- const boundAction = useBoundAction(action, actionFirstArgSignal);
12
- const getValue = useCallback(() => actionFirstArgSignal.value, []);
13
- const setValue = useCallback((value) => {
14
- actionFirstArgSignal.value = value;
15
- }, []);
16
- const externalValueRef = useRef(externalValue);
17
- if (externalValue !== externalValueRef.current) {
18
- externalValueRef.current = externalValue;
19
- setValue(externalValue);
20
- }
21
-
22
- const value = getValue();
23
- return [boundAction, value, setValue];
24
- };
25
- export const useActionBoundToOneArrayParam = (
26
- action,
27
- name,
28
- externalValue,
29
- fallbackValue,
30
- defaultValue,
31
- ) => {
32
- const [boundAction, value, setValue] = useActionBoundToOneParam(
33
- action,
34
- name,
35
- externalValue,
36
- fallbackValue,
37
- defaultValue,
38
- );
39
-
40
- const add = (valueToAdd, valueArray = value) => {
41
- setValue(addIntoArray(valueArray, valueToAdd));
42
- };
43
-
44
- const remove = (valueToRemove, valueArray = value) => {
45
- setValue(removeFromArray(valueArray, valueToRemove));
46
- };
47
-
48
- const result = [boundAction, value, setValue];
49
- result.add = add;
50
- result.remove = remove;
51
- return result;
52
- };
53
- // used by <details> to just call their action
54
- export const useAction = (action, paramsSignal) => {
55
- return useBoundAction(action, paramsSignal);
56
- };
57
-
58
- const useBoundAction = (action, actionParamsSignal) => {
59
- const actionRef = useRef();
60
- const actionCallbackRef = useRef();
61
-
62
- if (!action) {
63
- return null;
64
- }
65
- if (isFunctionButNotAnActionFunction(action)) {
66
- actionCallbackRef.current = action;
67
- const existingAction = actionRef.current;
68
- if (existingAction) {
69
- return existingAction;
70
- }
71
- const actionFromFunction = createAction(
72
- (...args) => {
73
- return actionCallbackRef.current?.(...args);
74
- },
75
- {
76
- name: action.name,
77
- // We don't want to give empty params by default
78
- // we want to give undefined for regular functions
79
- params: undefined,
80
- },
81
- );
82
- if (!actionParamsSignal) {
83
- actionRef.current = actionFromFunction;
84
- return actionFromFunction;
85
- }
86
- const actionBoundToParams =
87
- actionFromFunction.bindParams(actionParamsSignal);
88
- actionRef.current = actionBoundToParams;
89
- return actionBoundToParams;
90
- }
91
- if (actionParamsSignal) {
92
- return action.bindParams(actionParamsSignal);
93
- }
94
- return action;
95
- };
96
-
97
- const isFunctionButNotAnActionFunction = (action) => {
98
- return typeof action === "function" && !action.isAction;
99
- };
@@ -1,193 +0,0 @@
1
- import { useCallback, useLayoutEffect, useRef, useState } from "preact/hooks";
2
- import {
3
- addCustomMessage,
4
- removeCustomMessage,
5
- } from "../../validation/custom_message.js";
6
- import { useResetErrorBoundary } from "../error_boundary_context.js";
7
-
8
- let debug = false;
9
-
10
- export const useExecuteAction = (
11
- elementRef,
12
- {
13
- errorEffect = "show_validation_message", // "show_validation_message" or "throw"
14
- errorMapping,
15
- } = {},
16
- ) => {
17
- // see https://medium.com/trabe/catching-asynchronous-errors-in-react-using-error-boundaries-5e8a5fd7b971
18
- // and https://codepen.io/dmail/pen/XJJqeGp?editors=0010
19
- // To change if https://github.com/preactjs/preact/issues/4754 lands
20
- const [error, setError] = useState(null);
21
- const resetErrorBoundary = useResetErrorBoundary();
22
- useLayoutEffect(() => {
23
- if (error) {
24
- error.__handled__ = true; // prevent jsenv from displaying it
25
- throw error;
26
- }
27
- }, [error]);
28
-
29
- const validationMessageTargetRef = useRef(null);
30
- const addErrorMessage = (error) => {
31
- let calloutAnchor = validationMessageTargetRef.current;
32
- let message;
33
- if (errorMapping) {
34
- const errorMappingResult = errorMapping(error);
35
- if (typeof errorMappingResult === "string") {
36
- message = errorMappingResult;
37
- } else if (Error.isError(errorMappingResult)) {
38
- message = errorMappingResult;
39
- } else if (
40
- typeof errorMappingResult === "object" &&
41
- errorMappingResult !== null
42
- ) {
43
- message = errorMappingResult.message || error.message;
44
- calloutAnchor = errorMappingResult.target || calloutAnchor;
45
- }
46
- } else {
47
- message = error;
48
- }
49
- addCustomMessage(calloutAnchor, "action_error", message, {
50
- // This error should not prevent <form> submission
51
- // so whenever user tries to submit the form the error is cleared
52
- // (Hitting enter key, clicking on submit button, etc. would allow to re-submit the form in error state)
53
- removeOnRequestAction: true,
54
- });
55
- };
56
- const removeErrorMessage = () => {
57
- const validationMessageTarget = validationMessageTargetRef.current;
58
- if (validationMessageTarget) {
59
- removeCustomMessage(validationMessageTarget, "action_error");
60
- }
61
- };
62
-
63
- useLayoutEffect(() => {
64
- const element = elementRef.current;
65
- if (!element) {
66
- return null;
67
- }
68
- const form = element.tagName === "FORM" ? element : element.form;
69
- if (!form) {
70
- return null;
71
- }
72
- const onReset = () => {
73
- removeErrorMessage();
74
- };
75
- form.addEventListener("reset", onReset);
76
- return () => {
77
- form.removeEventListener("reset", onReset);
78
- };
79
- });
80
-
81
- // const errorEffectRef = useRef();
82
- // errorEffectRef.current = errorEffect;
83
- const executeAction = useCallback(
84
- (actionEvent) => {
85
- const { action, actionOrigin, requester, event, method } =
86
- actionEvent.detail;
87
- const sharedActionEventDetail = {
88
- action,
89
- actionOrigin,
90
- requester,
91
- event,
92
- method,
93
- };
94
-
95
- if (debug) {
96
- console.debug(
97
- "executing action, requested by",
98
- requester,
99
- `(event: ${event?.type})`,
100
- );
101
- }
102
-
103
- const dispatchCustomEvent = (type, options) => {
104
- const element = elementRef.current;
105
- const customEvent = new CustomEvent(type, options);
106
- element.dispatchEvent(customEvent);
107
- };
108
- if (resetErrorBoundary) {
109
- resetErrorBoundary();
110
- }
111
- removeErrorMessage();
112
- setError(null);
113
-
114
- const validationMessageTarget = requester || elementRef.current;
115
- validationMessageTargetRef.current = validationMessageTarget;
116
-
117
- dispatchCustomEvent("actionstart", {
118
- detail: sharedActionEventDetail,
119
- });
120
-
121
- return action[method]({
122
- reason: `"${event.type}" event on ${(() => {
123
- const target = event.target;
124
- const tagName = target.tagName.toLowerCase();
125
-
126
- if (target.id) {
127
- return `${tagName}#${target.id}`;
128
- }
129
-
130
- const uiName = target.getAttribute("data-ui-name");
131
- if (uiName) {
132
- return `${tagName}[data-ui-name="${uiName}"]`;
133
- }
134
-
135
- return `<${tagName}>`;
136
- })()}`,
137
- onAbort: (reason) => {
138
- if (
139
- // at this stage the action side effect might have removed the <element> from the DOM
140
- // (in theory no because action side effect are batched to happen after)
141
- // but other side effects might do this
142
- elementRef.current
143
- ) {
144
- dispatchCustomEvent("actionabort", {
145
- detail: {
146
- ...sharedActionEventDetail,
147
- reason,
148
- },
149
- });
150
- }
151
- },
152
- onError: (error) => {
153
- if (
154
- // at this stage the action side effect might have removed the <element> from the DOM
155
- // (in theory no because action side effect are batched to happen after)
156
- // but other side effects might do this
157
- elementRef.current
158
- ) {
159
- dispatchCustomEvent("actionerror", {
160
- detail: {
161
- ...sharedActionEventDetail,
162
- error,
163
- },
164
- });
165
- }
166
- if (errorEffect === "show_validation_message") {
167
- addErrorMessage(error);
168
- } else if (errorEffect === "throw") {
169
- setError(error);
170
- }
171
- },
172
- onComplete: (data) => {
173
- if (
174
- // at this stage the action side effect might have removed the <element> from the DOM
175
- // (in theory no because action side effect are batched to happen after)
176
- // but other side effects might do this
177
- elementRef.current
178
- ) {
179
- dispatchCustomEvent("actionend", {
180
- detail: {
181
- ...sharedActionEventDetail,
182
- data,
183
- },
184
- });
185
- }
186
- },
187
- });
188
- },
189
- [errorEffect],
190
- );
191
-
192
- return executeAction;
193
- };
@@ -1,9 +0,0 @@
1
- import { useEffect } from "preact/hooks";
2
-
3
- export const useRunOnMount = (action, Component) => {
4
- useEffect(() => {
5
- action.run({
6
- reason: `<${Component.name} /> mounted`,
7
- });
8
- }, []);
9
- };
@@ -1,125 +0,0 @@
1
- import { useErrorBoundary, useLayoutEffect } from "preact/hooks";
2
- import { getActionPrivateProperties } from "../action_private_properties.js";
3
- import { useActionStatus } from "../use_action_status.js";
4
-
5
- import.meta.css = /* css */ `
6
- .action_error {
7
- padding: 20px;
8
- background: #fdd;
9
- border: 1px solid red;
10
- margin-top: 0;
11
- margin-bottom: 20px;
12
- }
13
- `;
14
-
15
- const renderIdleDefault = () => null;
16
- const renderLoadingDefault = () => null;
17
- const renderAbortedDefault = () => null;
18
- const renderErrorDefault = (error) => {
19
- let routeErrorText = error && error.message ? error.message : error;
20
- return <p className="action_error">An error occured: {routeErrorText}</p>;
21
- };
22
- const renderCompletedDefault = () => null;
23
-
24
- export const ActionRenderer = ({ action, children, disabled }) => {
25
- const {
26
- idle: renderIdle = renderIdleDefault,
27
- loading: renderLoading = renderLoadingDefault,
28
- aborted: renderAborted = renderAbortedDefault,
29
- error: renderError = renderErrorDefault,
30
- completed: renderCompleted,
31
- always: renderAlways,
32
- } = typeof children === "function" ? { completed: children } : children || {};
33
-
34
- if (disabled) {
35
- return null;
36
- }
37
-
38
- if (action === undefined) {
39
- throw new Error(
40
- "ActionRenderer requires an action to render, but none was provided.",
41
- );
42
- }
43
- const { idle, loading, aborted, error, data } = useActionStatus(action);
44
- const UIRenderedPromise = useUIRenderedPromise(action);
45
- const [errorBoundary, resetErrorBoundary] = useErrorBoundary();
46
-
47
- // Mark this action as bound to UI components (has renderers)
48
- // This tells the action system that errors should be caught and stored
49
- // in the action's error state rather than bubbling up
50
- useLayoutEffect(() => {
51
- if (action) {
52
- const { ui } = getActionPrivateProperties(action);
53
- ui.hasRenderers = true;
54
- }
55
- }, [action]);
56
-
57
- useLayoutEffect(() => {
58
- resetErrorBoundary();
59
- }, [action, loading, idle, resetErrorBoundary]);
60
-
61
- useLayoutEffect(() => {
62
- UIRenderedPromise.resolve();
63
- return () => {
64
- actionUIRenderedPromiseWeakMap.delete(action);
65
- };
66
- }, [action]);
67
-
68
- // If renderAlways is provided, it wins and handles all rendering
69
- if (renderAlways) {
70
- return renderAlways({ idle, loading, aborted, error, data });
71
- }
72
-
73
- if (idle) {
74
- return renderIdle(action);
75
- }
76
- if (errorBoundary) {
77
- return renderError(errorBoundary, "ui_error", action);
78
- }
79
- if (error) {
80
- return renderError(error, "action_error", action);
81
- }
82
- if (aborted) {
83
- return renderAborted(action);
84
- }
85
- let renderCompletedSafe;
86
- if (renderCompleted) {
87
- renderCompletedSafe = renderCompleted;
88
- } else {
89
- const { ui } = getActionPrivateProperties(action);
90
- if (ui.renderCompleted) {
91
- renderCompletedSafe = ui.renderCompleted;
92
- } else {
93
- renderCompletedSafe = renderCompletedDefault;
94
- }
95
- }
96
- if (loading) {
97
- if (action.canDisplayOldData && data !== undefined) {
98
- return renderCompletedSafe(data, action);
99
- }
100
- return renderLoading(action);
101
- }
102
-
103
- return renderCompletedSafe(data, action);
104
- };
105
-
106
- const defaultPromise = Promise.resolve();
107
- defaultPromise.resolve = () => {};
108
-
109
- const actionUIRenderedPromiseWeakMap = new WeakMap();
110
- const useUIRenderedPromise = (action) => {
111
- if (!action) {
112
- return defaultPromise;
113
- }
114
- const actionUIRenderedPromise = actionUIRenderedPromiseWeakMap.get(action);
115
- if (actionUIRenderedPromise) {
116
- return actionUIRenderedPromise;
117
- }
118
- let resolve;
119
- const promise = new Promise((res) => {
120
- resolve = res;
121
- });
122
- promise.resolve = resolve;
123
- actionUIRenderedPromiseWeakMap.set(action, promise);
124
- return promise;
125
- };