@jsenv/navi 0.10.2 → 0.11.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.
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 +5 -7
  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,94 +0,0 @@
1
- // autoFocus does not work so we focus in a useLayoutEffect,
2
- // see https://github.com/preactjs/preact/issues/1255
3
-
4
- import { useEffect, useLayoutEffect } from "preact/hooks";
5
-
6
- let blurEvent = null;
7
- let timeout;
8
- document.body.addEventListener(
9
- "blur",
10
- (e) => {
11
- blurEvent = e;
12
- setTimeout(() => {
13
- blurEvent = null;
14
- });
15
- },
16
- { capture: true },
17
- );
18
- document.body.addEventListener(
19
- "focus",
20
- () => {
21
- clearTimeout(timeout);
22
- blurEvent = null;
23
- },
24
- { capture: true },
25
- );
26
-
27
- export const useAutoFocus = (
28
- focusableElementRef,
29
- autoFocus,
30
- { autoFocusVisible, autoSelect } = {},
31
- ) => {
32
- useLayoutEffect(() => {
33
- if (!autoFocus) {
34
- return null;
35
- }
36
- const activeElement = document.activeElement;
37
- const focusableElement = focusableElementRef.current;
38
- focusableElement.focus({ focusVisible: autoFocusVisible });
39
- if (autoSelect) {
40
- focusableElement.select();
41
- // Keep the beginning of the text visible instead of scrolling to the end
42
- focusableElement.scrollLeft = 0;
43
- }
44
- return () => {
45
- const focusIsOnSelfOrInsideSelf =
46
- document.activeElement === focusableElement ||
47
- focusableElement.contains(document.activeElement);
48
- if (
49
- !focusIsOnSelfOrInsideSelf &&
50
- document.activeElement !== document.body
51
- ) {
52
- // focus is not on our element (or body) anymore
53
- // keep it where it is
54
- return;
55
- }
56
-
57
- // We have focus but we are unmounted
58
- // -> try to move focus back to something more meaningful that what browser would do
59
- // (browser would put it to document.body)
60
- // -> We'll try to move focus back to the element that had focus before we moved it to this element
61
-
62
- if (!document.body.contains(activeElement)) {
63
- // previously active element is no longer in the document
64
- return;
65
- }
66
-
67
- if (blurEvent) {
68
- // But if this element is unmounted during a blur, the element that is about to receive focus should prevail
69
- const elementAboutToReceiveFocus = blurEvent.relatedTarget;
70
- const isSelfOrInsideSelf =
71
- elementAboutToReceiveFocus === focusableElement ||
72
- focusableElement.contains(elementAboutToReceiveFocus);
73
- const isPreviouslyActiveElementOrInsideIt =
74
- elementAboutToReceiveFocus === activeElement ||
75
- (activeElement && activeElement.contains(elementAboutToReceiveFocus));
76
- if (!isSelfOrInsideSelf && !isPreviouslyActiveElementOrInsideIt) {
77
- // the element about to receive focus is not the input itself or inside it
78
- // and is not the previously active element or inside it
79
- // -> the element about to receive focus should prevail
80
- return;
81
- }
82
- }
83
-
84
- activeElement.focus();
85
- };
86
- }, []);
87
-
88
- useEffect(() => {
89
- if (autoFocus) {
90
- const focusableElement = focusableElementRef.current;
91
- focusableElement.scrollIntoView({ inline: "nearest", block: "nearest" });
92
- }
93
- }, []);
94
- };
@@ -1,33 +0,0 @@
1
- import { useLayoutEffect, useMemo, useRef } from "preact/hooks";
2
-
3
- import { useStableCallback } from "./use_stable_callback.js";
4
-
5
- export const useBatchDuringRender = (callback) => {
6
- const pendingCallArrayRef = useRef([]);
7
- const pendingCallArray = pendingCallArrayRef.current;
8
-
9
- callback = useStableCallback(callback);
10
- const callbackWithBatching = useMemo(() => {
11
- return (...args) => {
12
- if (isRenderingRef.current) {
13
- pendingCallArray.push(args);
14
- return;
15
- }
16
- callback(...args);
17
- };
18
- }, []);
19
-
20
- const isRenderingRef = useRef();
21
- isRenderingRef.current = true;
22
- useLayoutEffect(() => {
23
- isRenderingRef.current = false;
24
- if (pendingCallArray.length === 0) {
25
- return;
26
- }
27
- const copy = [...pendingCallArray];
28
- pendingCallArray.length = 0;
29
- callback(copy);
30
- });
31
-
32
- return callbackWithBatching;
33
- };
@@ -1,31 +0,0 @@
1
- import { useLayoutEffect, useRef, useState } from "preact/hooks";
2
-
3
- export const useDebounceTrue = (value, delay = 300) => {
4
- const [debouncedTrue, setDebouncedTrue] = useState(false);
5
- const timerRef = useRef(null);
6
-
7
- useLayoutEffect(() => {
8
- // If value is true or becomes true, start a timer
9
- if (value) {
10
- timerRef.current = setTimeout(() => {
11
- setDebouncedTrue(true);
12
- }, delay);
13
- } else {
14
- // If value becomes false, clear any pending timer and immediately set to false
15
- if (timerRef.current) {
16
- clearTimeout(timerRef.current);
17
- timerRef.current = null;
18
- }
19
- setDebouncedTrue(false);
20
- }
21
-
22
- // Cleanup function
23
- return () => {
24
- if (timerRef.current) {
25
- clearTimeout(timerRef.current);
26
- }
27
- };
28
- }, [value, delay]);
29
-
30
- return debouncedTrue;
31
- };
@@ -1,35 +0,0 @@
1
- /*
2
- * - Usage
3
- * useEffect(() => {
4
- * // here you want to know what has changed, causing useEffect to be called
5
- * }, [name, value])
6
- *
7
- * const diff = useDependenciesDiff({ name, value })
8
- * useEffect(() => {
9
- * console.log('useEffect called because', diff)
10
- * }, [name, value])
11
- */
12
-
13
- import { useMemo, useRef } from "preact/hooks";
14
-
15
- export const useDependenciesDiff = (inputs) => {
16
- const oldInputsRef = useRef(inputs);
17
- const inputValuesArray = Object.values(inputs);
18
- const inputKeysArray = Object.keys(inputs);
19
- const diffRef = useRef();
20
- useMemo(() => {
21
- const oldInputs = oldInputsRef.current;
22
- const diff = {};
23
- for (const key of inputKeysArray) {
24
- const previous = oldInputs[key];
25
- const current = inputs[key];
26
- if (previous !== current) {
27
- diff[key] = { previous, current };
28
- }
29
- }
30
- diffRef.current = diff;
31
- oldInputsRef.current = inputs;
32
- }, inputValuesArray);
33
-
34
- return diffRef.current;
35
- };
@@ -1,20 +0,0 @@
1
- import { initFocusGroup } from "@jsenv/dom";
2
- import { useLayoutEffect } from "preact/hooks";
3
-
4
- export const useFocusGroup = (
5
- elementRef,
6
- { enabled = true, direction, skipTab, loop, name } = {},
7
- ) => {
8
- useLayoutEffect(() => {
9
- if (!enabled) {
10
- return null;
11
- }
12
- const focusGroup = initFocusGroup(elementRef.current, {
13
- direction,
14
- skipTab,
15
- loop,
16
- name,
17
- });
18
- return focusGroup.cleanup;
19
- }, [direction, skipTab, loop, name]);
20
- };
@@ -1,78 +0,0 @@
1
- import { useRef } from "preact/hooks";
2
-
3
- let debug = false;
4
-
5
- /**
6
- * Picks the best initial value from three options using a simple priority system.
7
- *
8
- * @param {any} externalValue - Value from props or parent component
9
- * @param {any} fallbackValue - Backup value if external value isn't useful
10
- * @param {any} defaultValue - Final fallback (usually empty/neutral value)
11
- *
12
- * @returns {any} The chosen value using this priority:
13
- * 1. externalValue (if provided and different from default)
14
- * 2. fallbackValue (if external value is missing/same as default)
15
- * 3. defaultValue (if nothing else works)
16
- *
17
- * @example
18
- * resolveInitialValue("hello", "backup", "") → "hello"
19
- * resolveInitialValue(undefined, "backup", "") → "backup"
20
- * resolveInitialValue("", "backup", "") → "backup" (empty same as default)
21
- */
22
- export const resolveInitialValue = (
23
- externalValue,
24
- fallbackValue,
25
- defaultValue,
26
- ) => {
27
- if (externalValue !== undefined && externalValue !== defaultValue) {
28
- return externalValue;
29
- }
30
- if (fallbackValue !== undefined) {
31
- return fallbackValue;
32
- }
33
- return defaultValue;
34
- };
35
-
36
- /**
37
- * Hook that syncs external value changes to a setState function.
38
- * Always syncs when external value changes, regardless of what it changes to.
39
- *
40
- * @param {any} externalValue - Value from props or parent component to watch for changes
41
- * @param {any} defaultValue - Default value to use when external value is undefined
42
- * @param {Function} setValue - Function to call when external value changes
43
- * @param {string} name - Parameter name for debugging
44
- */
45
- export const useExternalValueSync = (
46
- externalValue,
47
- defaultValue,
48
- setValue,
49
- name = "",
50
- ) => {
51
- // Track external value changes and sync them
52
- const previousExternalValueRef = useRef(externalValue);
53
- if (externalValue !== previousExternalValueRef.current) {
54
- previousExternalValueRef.current = externalValue;
55
- // Always sync external value changes - use defaultValue only when external is undefined
56
- const valueToSet =
57
- externalValue === undefined ? defaultValue : externalValue;
58
- if (debug) {
59
- console.debug(
60
- `useExternalValueSync(${name}) syncing external value change: ${valueToSet}`,
61
- );
62
- }
63
- setValue(valueToSet);
64
- }
65
- };
66
-
67
- const UNSET = {};
68
- export const useInitialValue = (compute) => {
69
- const initialValueRef = useRef(UNSET);
70
- let initialValue = initialValueRef.current;
71
- if (initialValue !== UNSET) {
72
- return initialValue;
73
- }
74
-
75
- initialValue = compute();
76
- initialValueRef.current = initialValue;
77
- return initialValue;
78
- };
@@ -1,19 +0,0 @@
1
- import {
2
- isVisited,
3
- visitedUrlsSignal,
4
- } from "../browser_integration/browser_integration.js";
5
-
6
- /**
7
- * Hook that reactively checks if a URL is visited.
8
- * Re-renders when the visited URL set changes.
9
- *
10
- * @param {string} url - The URL to check
11
- * @returns {boolean} Whether the URL has been visited
12
- */
13
- export const useIsVisited = (url) => {
14
- // Access the signal to create reactive dependency
15
- // eslint-disable-next-line no-unused-expressions
16
- visitedUrlsSignal.value;
17
-
18
- return isVisited(url);
19
- };
@@ -1,38 +0,0 @@
1
- import { createRef } from "preact";
2
- import { useMemo, useRef } from "preact/hooks";
3
-
4
- export const useRefArray = (items, keyFromItem) => {
5
- const refMapRef = useRef(new Map());
6
- const previousKeySetRef = useRef(new Set());
7
-
8
- return useMemo(() => {
9
- const refMap = refMapRef.current;
10
- const previousKeySet = previousKeySetRef.current;
11
- const currentKeySet = new Set();
12
- const refArray = [];
13
-
14
- for (let i = 0; i < items.length; i++) {
15
- const item = items[i];
16
- const key = keyFromItem(item);
17
- currentKeySet.add(key);
18
-
19
- const refForKey = refMap.get(key);
20
- if (refForKey) {
21
- refArray[i] = refForKey;
22
- } else {
23
- const newRef = createRef();
24
- refMap.set(key, newRef);
25
- refArray[i] = newRef;
26
- }
27
- }
28
-
29
- for (const key of previousKeySet) {
30
- if (!currentKeySet.has(key)) {
31
- refMap.delete(key);
32
- }
33
- }
34
- previousKeySetRef.current = currentKeySet;
35
-
36
- return refArray;
37
- }, [items]);
38
- };
@@ -1,50 +0,0 @@
1
- import { useSignal } from "@preact/signals";
2
- import { useRef } from "preact/hooks";
3
-
4
- /**
5
- * Creates a signal that stays synchronized with an external value,
6
- * only updating the signal when the value actually changes.
7
- *
8
- * This hook solves a common reactive UI pattern where:
9
- * 1. A signal controls a UI element (like an input field)
10
- * 2. The UI element can be modified by user interaction
11
- * 3. When the external "source of truth" changes, it should take precedence
12
- *
13
- * @param {any} value - The external value to sync with (the "source of truth")
14
- * @param {any} [initialValue] - Optional initial value for the signal (defaults to value)
15
- * @returns {Signal} A signal that tracks the external value but allows temporary local changes
16
- *
17
- * @example
18
- * const FileNameEditor = ({ file }) => {
19
- * // Signal stays in sync with file.name, but allows user editing
20
- * const nameSignal = useSignalSync(file.name);
21
- *
22
- * return (
23
- * <Editable
24
- * valueSignal={nameSignal} // User can edit this
25
- * action={renameFileAction} // Saves changes
26
- * />
27
- * );
28
- * };
29
- *
30
- * // Scenario:
31
- * // 1. file.name = "doc.txt", nameSignal.value = "doc.txt"
32
- * // 2. User types "report" -> nameSignal.value = "report.txt"
33
- * // 3. External update: file.name = "shared-doc.txt"
34
- * // 4. Next render: nameSignal.value = "shared-doc.txt" (model wins!)
35
- *
36
- */
37
-
38
- export const useSignalSync = (value, initialValue = value) => {
39
- const signal = useSignal(initialValue);
40
- const previousValueRef = useRef(value);
41
-
42
- // Only update signal when external value actually changes
43
- // This preserves user input between external changes
44
- if (previousValueRef.current !== value) {
45
- previousValueRef.current = value;
46
- signal.value = value; // Model takes precedence
47
- }
48
-
49
- return signal;
50
- };
@@ -1,68 +0,0 @@
1
- /**
2
- * Custom hook creating a stable callback that doesn't trigger re-renders.
3
- *
4
- * PROBLEM: Parent components often forget to use useCallback, causing library
5
- * components to re-render unnecessarily when receiving callback props.
6
- *
7
- * SOLUTION: Library components can use this hook to create stable callback
8
- * references internally, making them defensive against parents who don't
9
- * optimize their callbacks. This ensures library components don't force
10
- * consumers to think about useCallback.
11
- *
12
- * USAGE:
13
- * ```js
14
- * // Parent component (consumer) - no useCallback needed
15
- * const Parent = () => {
16
- * const [count, setCount] = useState(0);
17
- *
18
- * // Parent naturally creates new function reference each render
19
- * // (forgetting useCallback is common and shouldn't break performance)
20
- * return <LibraryButton onClick={(e) => setCount(count + 1)} />;
21
- * };
22
- *
23
- * // Library component - defensive against changing callbacks
24
- * const LibraryButton = ({ onClick }) => {
25
- * // ✅ Create stable reference from parent's potentially changing callback
26
- * const stableClick = useStableCallback(onClick);
27
- *
28
- * // Internal expensive components won't re-render when parent updates
29
- * return <ExpensiveInternalButton onClick={stableClick} />;
30
- * };
31
- *
32
- * // Deep internal component gets stable reference
33
- * const ExpensiveInternalButton = memo(({ onClick }) => {
34
- * // This won't re-render when Parent's count changes
35
- * // But onClick will always call the latest Parent callback
36
- * return <button onClick={onClick}>Click me</button>;
37
- * });
38
- * ```
39
- *
40
- * Perfect for library components that need performance without burdening consumers.
41
- */
42
-
43
- import { useRef } from "preact/hooks";
44
-
45
- export const useStableCallback = (callback, mapper) => {
46
- const callbackRef = useRef();
47
- callbackRef.current = callback;
48
- const stableCallbackRef = useRef();
49
-
50
- // Return original falsy value directly when callback is not a function
51
- if (!callback) {
52
- return callback;
53
- }
54
-
55
- const existingStableCallback = stableCallbackRef.current;
56
- if (existingStableCallback) {
57
- return existingStableCallback;
58
- }
59
- const stableCallback = (...args) => {
60
- const currentCallback = callbackRef.current;
61
- if (mapper) {
62
- args = mapper(...args);
63
- }
64
- return currentCallback(...args);
65
- };
66
- stableCallbackRef.current = stableCallback;
67
- return stableCallback;
68
- };
@@ -1,47 +0,0 @@
1
- import { useCallback, useRef, useState } from "preact/hooks";
2
- import { addIntoArray, removeFromArray } from "../utils/array_add_remove.js";
3
- import {
4
- resolveInitialValue,
5
- useExternalValueSync,
6
- } from "./use_initial_value.js";
7
-
8
- const FIRST_MOUNT = {};
9
- export const useStateArray = (
10
- externalValue,
11
- fallbackValue,
12
- defaultValue = [],
13
- ) => {
14
- const initialValueRef = useRef(FIRST_MOUNT);
15
- if (initialValueRef.current === FIRST_MOUNT) {
16
- const initialValue = resolveInitialValue(
17
- externalValue,
18
- fallbackValue,
19
- defaultValue,
20
- );
21
- initialValueRef.current = initialValue;
22
- }
23
- const initialValue = initialValueRef.current;
24
- const [array, setArray] = useState(initialValue);
25
-
26
- // Only sync external value changes if externalValue was explicitly provided
27
- useExternalValueSync(externalValue, defaultValue, setArray, "state_array");
28
-
29
- const add = useCallback((valueToAdd) => {
30
- setArray((array) => {
31
- const newArray = addIntoArray(array, valueToAdd);
32
- return newArray;
33
- });
34
- }, []);
35
-
36
- const remove = useCallback((valueToRemove) => {
37
- setArray((array) => {
38
- return removeFromArray(array, valueToRemove);
39
- });
40
- }, []);
41
-
42
- const reset = useCallback(() => {
43
- setArray(initialValue);
44
- }, [initialValue]);
45
-
46
- return [array, add, remove, reset];
47
- };