react-simplikit 0.0.34 → 0.0.36

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 (56) hide show
  1. package/README.md +1 -1
  2. package/dist/components/ImpressionArea/index.cjs +17 -20
  3. package/dist/components/ImpressionArea/index.d.cts +2 -2
  4. package/dist/components/SwitchCase/index.d.cts +4 -4
  5. package/dist/hooks/useAsyncEffect/index.d.cts +1 -1
  6. package/dist/hooks/useCallbackOncePerRender/index.d.cts +2 -3
  7. package/dist/hooks/useControlledState/index.d.cts +1 -1
  8. package/dist/hooks/useCounter/index.cjs +1 -1
  9. package/dist/hooks/useCounter/index.d.cts +3 -5
  10. package/dist/hooks/useDebouncedCallback/index.cjs +144 -0
  11. package/dist/hooks/useDebouncedCallback/index.d.cts +32 -0
  12. package/dist/hooks/useDoubleClick/index.d.cts +4 -4
  13. package/dist/hooks/useGeolocation/index.cjs +153 -0
  14. package/dist/hooks/useGeolocation/index.d.cts +106 -0
  15. package/dist/hooks/useImpressionRef/index.d.cts +2 -2
  16. package/dist/hooks/useIntersectionObserver/index.d.cts +2 -2
  17. package/dist/hooks/useIsomorphicLayoutEffect/index.d.cts +3 -3
  18. package/dist/hooks/useLongPress/index.cjs +123 -0
  19. package/dist/hooks/useLongPress/index.d.cts +70 -0
  20. package/dist/hooks/useMap/index.cjs +77 -0
  21. package/dist/hooks/useMap/index.d.cts +46 -0
  22. package/dist/hooks/usePrevious/index.d.cts +2 -2
  23. package/dist/hooks/useRefEffect/index.d.cts +1 -1
  24. package/dist/hooks/useStorageState/index.d.cts +1 -1
  25. package/dist/hooks/useThrottle/index.d.cts +1 -1
  26. package/dist/hooks/useVisibilityEvent/index.d.cts +1 -1
  27. package/dist/index.cjs +453 -98
  28. package/dist/index.d.cts +9 -1
  29. package/esm/components/ImpressionArea/index.d.ts +2 -2
  30. package/esm/components/ImpressionArea/index.js +17 -20
  31. package/esm/components/SwitchCase/index.d.ts +4 -4
  32. package/esm/hooks/useAsyncEffect/index.d.ts +1 -1
  33. package/esm/hooks/useCallbackOncePerRender/index.d.ts +2 -3
  34. package/esm/hooks/useControlledState/index.d.ts +1 -1
  35. package/esm/hooks/useCounter/index.d.ts +3 -5
  36. package/esm/hooks/useCounter/index.js +1 -1
  37. package/esm/hooks/useDebouncedCallback/index.d.ts +32 -0
  38. package/esm/hooks/useDebouncedCallback/index.js +117 -0
  39. package/esm/hooks/useDoubleClick/index.d.ts +4 -4
  40. package/esm/hooks/useGeolocation/index.d.ts +106 -0
  41. package/esm/hooks/useGeolocation/index.js +126 -0
  42. package/esm/hooks/useImpressionRef/index.d.ts +2 -2
  43. package/esm/hooks/useIntersectionObserver/index.d.ts +2 -2
  44. package/esm/hooks/useIsomorphicLayoutEffect/index.d.ts +3 -3
  45. package/esm/hooks/useLongPress/index.d.ts +70 -0
  46. package/esm/hooks/useLongPress/index.js +96 -0
  47. package/esm/hooks/useMap/index.d.ts +46 -0
  48. package/esm/hooks/useMap/index.js +50 -0
  49. package/esm/hooks/usePrevious/index.d.ts +2 -2
  50. package/esm/hooks/useRefEffect/index.d.ts +1 -1
  51. package/esm/hooks/useStorageState/index.d.ts +1 -1
  52. package/esm/hooks/useThrottle/index.d.ts +1 -1
  53. package/esm/hooks/useVisibilityEvent/index.d.ts +1 -1
  54. package/esm/index.d.ts +9 -1
  55. package/esm/index.js +421 -74
  56. package/package.json +2 -3
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/hooks/useLongPress/index.ts
21
+ var useLongPress_exports = {};
22
+ __export(useLongPress_exports, {
23
+ useLongPress: () => useLongPress
24
+ });
25
+ module.exports = __toCommonJS(useLongPress_exports);
26
+
27
+ // src/hooks/useLongPress/useLongPress.ts
28
+ var import_react2 = require("react");
29
+
30
+ // src/hooks/usePreservedCallback/usePreservedCallback.ts
31
+ var import_react = require("react");
32
+ function usePreservedCallback(callback) {
33
+ const callbackRef = (0, import_react.useRef)(callback);
34
+ (0, import_react.useEffect)(() => {
35
+ callbackRef.current = callback;
36
+ }, [callback]);
37
+ return (0, import_react.useCallback)((...args) => {
38
+ return callbackRef.current(...args);
39
+ }, []);
40
+ }
41
+
42
+ // src/hooks/useLongPress/useLongPress.ts
43
+ function useLongPress(onLongPress, { delay = 500, moveThreshold, onClick, onLongPressEnd } = {}) {
44
+ const timeoutRef = (0, import_react2.useRef)(null);
45
+ const isLongPressActiveRef = (0, import_react2.useRef)(false);
46
+ const initialPositionRef = (0, import_react2.useRef)({ x: 0, y: 0 });
47
+ const preservedOnLongPress = usePreservedCallback(onLongPress);
48
+ const preservedOnClick = usePreservedCallback(onClick || (() => {
49
+ }));
50
+ const preservedOnLongPressEnd = usePreservedCallback(onLongPressEnd || (() => {
51
+ }));
52
+ const hasThreshold = moveThreshold?.x !== void 0 || moveThreshold?.y !== void 0;
53
+ const getClientPosition = (0, import_react2.useCallback)((event) => {
54
+ if ("touches" in event.nativeEvent) {
55
+ const touch = event.nativeEvent.touches[0];
56
+ return { x: touch.clientX, y: touch.clientY };
57
+ }
58
+ return {
59
+ x: event.nativeEvent.clientX,
60
+ y: event.nativeEvent.clientY
61
+ };
62
+ }, []);
63
+ const isMovedBeyondThreshold = (0, import_react2.useCallback)(
64
+ (event) => {
65
+ const { x, y } = getClientPosition(event);
66
+ const deltaX = Math.abs(x - initialPositionRef.current.x);
67
+ const deltaY = Math.abs(y - initialPositionRef.current.y);
68
+ return moveThreshold?.x !== void 0 && deltaX > moveThreshold.x || moveThreshold?.y !== void 0 && deltaY > moveThreshold.y;
69
+ },
70
+ [getClientPosition, moveThreshold]
71
+ );
72
+ const cancelLongPress = (0, import_react2.useCallback)(() => {
73
+ if (timeoutRef.current !== null) {
74
+ window.clearTimeout(timeoutRef.current);
75
+ timeoutRef.current = null;
76
+ }
77
+ }, []);
78
+ const handlePressStart = (0, import_react2.useCallback)(
79
+ (event) => {
80
+ cancelLongPress();
81
+ const position = getClientPosition(event);
82
+ initialPositionRef.current = position;
83
+ isLongPressActiveRef.current = false;
84
+ timeoutRef.current = window.setTimeout(() => {
85
+ isLongPressActiveRef.current = true;
86
+ preservedOnLongPress(event);
87
+ }, delay);
88
+ },
89
+ [cancelLongPress, delay, getClientPosition, preservedOnLongPress]
90
+ );
91
+ const handlePressEnd = (0, import_react2.useCallback)(
92
+ (event) => {
93
+ if (isLongPressActiveRef.current) {
94
+ preservedOnLongPressEnd(event);
95
+ } else if (timeoutRef.current !== null) {
96
+ preservedOnClick(event);
97
+ }
98
+ cancelLongPress();
99
+ isLongPressActiveRef.current = false;
100
+ },
101
+ [cancelLongPress, preservedOnClick, preservedOnLongPressEnd]
102
+ );
103
+ const handlePressMove = (0, import_react2.useCallback)(
104
+ (event) => {
105
+ if (timeoutRef.current !== null && isMovedBeyondThreshold(event)) {
106
+ cancelLongPress();
107
+ }
108
+ },
109
+ [cancelLongPress, isMovedBeyondThreshold]
110
+ );
111
+ return {
112
+ onMouseDown: handlePressStart,
113
+ onMouseUp: handlePressEnd,
114
+ onMouseLeave: cancelLongPress,
115
+ onTouchStart: handlePressStart,
116
+ onTouchEnd: handlePressEnd,
117
+ ...hasThreshold ? { onTouchMove: handlePressMove, onMouseMove: handlePressMove } : {}
118
+ };
119
+ }
120
+ // Annotate the CommonJS export names for ESM import in node:
121
+ 0 && (module.exports = {
122
+ useLongPress
123
+ });
@@ -0,0 +1,70 @@
1
+ import { MouseEvent, TouchEvent } from 'react';
2
+
3
+ type Handler<E extends HTMLElement> = (event: MouseEvent<E> | TouchEvent<E>) => void;
4
+ type UseLongPressOptions<E extends HTMLElement> = {
5
+ delay?: number;
6
+ moveThreshold?: {
7
+ x?: number;
8
+ y?: number;
9
+ };
10
+ onClick?: Handler<E>;
11
+ onLongPressEnd?: Handler<E>;
12
+ };
13
+ /**
14
+ * @description
15
+ * `useLongPress` is a React hook that detects when an element is pressed and held for a specified duration.
16
+ * It handles both mouse and touch events, making it work consistently across desktop and mobile devices.
17
+ *
18
+ * @template {HTMLElement} E - The HTML element type that will use the long press handlers.
19
+ * @param {(event: React.MouseEvent<E> | React.TouchEvent<E>) => void} onLongPress - The callback function to be executed when a long press is detected.
20
+ * @param {UseLongPressOptions} [options] - Configuration options for the long press behavior.
21
+ * @param {number} [options.delay=500] - The time in milliseconds before triggering the long press. Defaults to 500ms.
22
+ * @param {Object} [options.moveThreshold] - Maximum movement allowed before canceling a long press.
23
+ * @param {number} [options.moveThreshold.x] - Maximum horizontal movement in pixels.
24
+ * @param {number} [options.moveThreshold.y] - Maximum vertical movement in pixels.
25
+ * @param {(event) => void} [options.onClick] - Optional function to execute on a normal click (press and release before delay).
26
+ * @param {(event) => void} [options.onLongPressEnd] - Optional function to execute when a long press ends.
27
+ *
28
+ * @returns {Object} Event handlers to attach to an element.
29
+ * - onMouseDown `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse down events.
30
+ * - onMouseUp `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse up events.
31
+ * - onMouseLeave `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse leave events.
32
+ * - onTouchStart `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for touch start events.
33
+ * - onTouchEnd `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for touch end events.
34
+ * - onMouseMove `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse move events. Included if `moveThreshold` is provided.
35
+ * - onTouchMove `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for touch move events. Included if `moveThreshold` is provided.
36
+ *
37
+ * @example
38
+ * import { useLongPress } from 'react-simplikit';
39
+ *
40
+ * function ContextMenu() {
41
+ * const [menuVisible, setMenuVisible] = useState(false);
42
+ *
43
+ * const longPressHandlers = useLongPress(
44
+ * () => setMenuVisible(true),
45
+ * {
46
+ * delay: 400,
47
+ * onClick: () => console.log('Normal click'),
48
+ * onLongPressEnd: () => console.log('Long press completed')
49
+ * }
50
+ * );
51
+ *
52
+ * return (
53
+ * <div>
54
+ * <button {...longPressHandlers}>Press and hold</button>
55
+ * {menuVisible && <div className="context-menu">Context Menu</div>}
56
+ * </div>
57
+ * );
58
+ * }
59
+ */
60
+ declare function useLongPress<E extends HTMLElement = HTMLElement>(onLongPress: Handler<E>, { delay, moveThreshold, onClick, onLongPressEnd }?: UseLongPressOptions<E>): {
61
+ onTouchMove?: ((event: MouseEvent<E> | TouchEvent<E>) => void) | undefined;
62
+ onMouseMove?: ((event: MouseEvent<E> | TouchEvent<E>) => void) | undefined;
63
+ onMouseDown: (event: MouseEvent<E> | TouchEvent<E>) => void;
64
+ onMouseUp: (event: MouseEvent<E> | TouchEvent<E>) => void;
65
+ onMouseLeave: () => void;
66
+ onTouchStart: (event: MouseEvent<E> | TouchEvent<E>) => void;
67
+ onTouchEnd: (event: MouseEvent<E> | TouchEvent<E>) => void;
68
+ };
69
+
70
+ export { useLongPress };
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/hooks/useMap/index.ts
21
+ var useMap_exports = {};
22
+ __export(useMap_exports, {
23
+ useMap: () => useMap
24
+ });
25
+ module.exports = __toCommonJS(useMap_exports);
26
+
27
+ // src/hooks/useMap/useMap.ts
28
+ var import_react2 = require("react");
29
+
30
+ // src/hooks/usePreservedReference/usePreservedReference.ts
31
+ var import_react = require("react");
32
+ function usePreservedReference(value, areValuesEqual = areDeeplyEqual) {
33
+ const ref = (0, import_react.useRef)(value);
34
+ return (0, import_react.useMemo)(() => {
35
+ if (!areValuesEqual(ref.current, value)) {
36
+ ref.current = value;
37
+ }
38
+ return ref.current;
39
+ }, [areValuesEqual, value]);
40
+ }
41
+ function areDeeplyEqual(x, y) {
42
+ return JSON.stringify(x) === JSON.stringify(y);
43
+ }
44
+
45
+ // src/hooks/useMap/useMap.ts
46
+ function useMap(initialState = /* @__PURE__ */ new Map()) {
47
+ const [map, setMap] = (0, import_react2.useState)(() => new Map(initialState));
48
+ const preservedInitialState = usePreservedReference(initialState);
49
+ const set = (0, import_react2.useCallback)((key, value) => {
50
+ setMap((prev) => {
51
+ const nextMap = new Map(prev);
52
+ nextMap.set(key, value);
53
+ return nextMap;
54
+ });
55
+ }, []);
56
+ const setAll = (0, import_react2.useCallback)((entries) => {
57
+ setMap(() => new Map(entries));
58
+ }, []);
59
+ const remove = (0, import_react2.useCallback)((key) => {
60
+ setMap((prev) => {
61
+ const nextMap = new Map(prev);
62
+ nextMap.delete(key);
63
+ return nextMap;
64
+ });
65
+ }, []);
66
+ const reset = (0, import_react2.useCallback)(() => {
67
+ setMap(() => new Map(preservedInitialState));
68
+ }, [preservedInitialState]);
69
+ const actions = (0, import_react2.useMemo)(() => {
70
+ return { set, setAll, remove, reset };
71
+ }, [set, setAll, remove, reset]);
72
+ return [map, actions];
73
+ }
74
+ // Annotate the CommonJS export names for ESM import in node:
75
+ 0 && (module.exports = {
76
+ useMap
77
+ });
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Defines the type for either a Map or an array of key-value pairs.
3
+ */
4
+ type MapOrEntries<K, V> = Map<K, V> | [K, V][];
5
+ /**
6
+ * Actions to manipulate the Map state.
7
+ */
8
+ type MapActions<K, V> = {
9
+ /** Sets a key-value pair in the map. */
10
+ set: (key: K, value: V) => void;
11
+ /** Sets multiple key-value pairs in the map at once. */
12
+ setAll: (entries: MapOrEntries<K, V>) => void;
13
+ /** Removes a key from the map. */
14
+ remove: (key: K) => void;
15
+ /** Resets the map to its initial state. */
16
+ reset: () => void;
17
+ };
18
+ /**
19
+ * Return type of the useMap hook.
20
+ * Hides certain methods to prevent direct mutations.
21
+ */
22
+ type UseMapReturn<K, V> = [Omit<Map<K, V>, 'set' | 'clear' | 'delete'>, MapActions<K, V>];
23
+ /**
24
+ * @description
25
+ * A React hook that manages a key-value Map as state.
26
+ * Provides efficient state management and stable action functions.
27
+ *
28
+ * @param {MapOrEntries<K, V>} initialState - Initial Map state (Map object or array of key-value pairs)
29
+ * @returns {UseMapReturn<K, V>} A tuple containing the Map state and actions to manipulate it
30
+ *
31
+ * @example
32
+ * ```tsx
33
+ * const [userMap, actions] = useMap<string, User>([
34
+ * ['user1', { name: 'John', age: 30 }]
35
+ * ]);
36
+ *
37
+ * // Using values from the Map
38
+ * const user1 = userMap.get('user1');
39
+ *
40
+ * // Updating the Map
41
+ * actions.set('user2', { name: 'Jane', age: 25 });
42
+ * ```
43
+ */
44
+ declare function useMap<K, V>(initialState?: MapOrEntries<K, V>): UseMapReturn<K, V>;
45
+
46
+ export { useMap };
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @description
3
- * Returns the previous value of the input state.
4
- * If a re-render occurs but the state value does not change, the previous value remains unchanged.
3
+ * `usePrevious` is a React hook that returns the previous value of the input state.
4
+ * It preserves the previous value unchanged when re-render occur without state changes.
5
5
  * If the state is an object or requires custom change detection, a `compare` function can be provided.
6
6
  * By default, state changes are detected using `prev === next`.
7
7
  *
@@ -3,7 +3,7 @@ import { DependencyList } from 'react';
3
3
  type CleanupCallback = () => void;
4
4
  /**
5
5
  * @description
6
- * `useRefEffect` is a custom hook that helps you set a reference to a specific DOM element and execute a callback whenever the element changes.
6
+ * `useRefEffect` is a React hook that helps you set a reference to a specific DOM element and execute a callback whenever the element changes.
7
7
  * This hook calls a cleanup function whenever the element changes to prevent memory leaks.
8
8
  *
9
9
  * @param {(element: Element) => CleanupCallback | void} callback - A callback function that is executed when the element is set. This function can return a cleanup function.
@@ -23,7 +23,7 @@ type StorageStateOptionsWithSerializer<T> = StorageStateOptions<T> & {
23
23
  type SerializableGuard<T extends readonly any[]> = T[0] extends any ? T : T[0] extends never ? 'Received a non-serializable value' : T;
24
24
  /**
25
25
  * @description
26
- * A React hook that functions like `useState` but persists the state value in browser storage.
26
+ * `useStorageState` is a React that functions like `useState` but persists the state value in browser storage.
27
27
  * The value is retained across page reloads and can be shared between tabs when using `localStorage`.
28
28
  *
29
29
  * @param {string} key - The key used to store the value in storage.
@@ -16,7 +16,7 @@ declare function throttle<F extends (...args: any[]) => void>(func: F, throttleM
16
16
 
17
17
  /**
18
18
  * @description
19
- * A React hook that creates a throttled version of a callback function.
19
+ * `useThrottle` is a React hook that creates a throttled version of a callback function.
20
20
  * This is useful for limiting the rate at which a function can be called,
21
21
  * such as when handling scroll or resize events.
22
22
  *
@@ -3,7 +3,7 @@ type Options = {
3
3
  };
4
4
  /**
5
5
  * @description
6
- * A React hook that listens to changes in the document's visibility state and triggers a callback.
6
+ * `useVisibilityEvent` is a React hook that listens to changes in the document's visibility state and triggers a callback.
7
7
  *
8
8
  * @param {(visibilityState: 'visible' | 'hidden') => void} callback - A function to be called
9
9
  * when the visibility state changes. It receives the current visibility state ('visible' or 'hidden') as an argument.