use-good-hooks 1.0.27 → 1.0.29

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.
package/README.md CHANGED
@@ -202,12 +202,12 @@ const Counter = ({ count }) => {
202
202
 
203
203
  - The previous value (undefined on first render)
204
204
 
205
- ### `useStateHistory`
205
+ ### `useHistoryState`
206
206
 
207
207
  Tracks the history of a state value, providing undo and redo capabilities. This is perfect for building editors, forms, or any UI where users might want to reverse their actions.
208
208
 
209
209
  ```typescript
210
- import useStateHistory from 'use-good-hooks/use-state-history';
210
+ import useHistoryState from 'use-good-hooks/use-history-state';
211
211
 
212
212
  const TextEditor = () => {
213
213
  const {
@@ -218,7 +218,7 @@ const TextEditor = () => {
218
218
  setState,
219
219
  state,
220
220
  undo
221
- } = useStateHistory('', { capacity: 10 });
221
+ } = useHistoryState('', { capacity: 10 });
222
222
 
223
223
  return (
224
224
  <div>
@@ -0,0 +1,9 @@
1
+ import { DebouncedFunc } from 'lodash';
2
+ import { DebounceSettings } from 'lodash';
3
+
4
+ export { DebounceSettings }
5
+
6
+ declare const useDebounceFn: <T extends (...args: any[]) => any>(fn: T, delay?: number, options?: DebounceSettings) => DebouncedFunc<(...args: Parameters<T>) => any>;
7
+ export default useDebounceFn;
8
+
9
+ export { }
@@ -0,0 +1,25 @@
1
+ import { useRef as o, useEffect as f } from "react";
2
+ import s from "lodash/debounce";
3
+ const m = 300, E = (r, e = m, n) => {
4
+ const t = o(r);
5
+ f(() => {
6
+ t.current = r;
7
+ }, [r]);
8
+ const u = o(
9
+ s(
10
+ (...c) => t.current(...c),
11
+ e,
12
+ n
13
+ )
14
+ );
15
+ return f(() => (u.current = s(
16
+ (...c) => t.current(...c),
17
+ e,
18
+ n
19
+ ), () => {
20
+ u.current.cancel();
21
+ }), [e, n]), u.current;
22
+ };
23
+ export {
24
+ E as default
25
+ };
@@ -0,0 +1,51 @@
1
+ import { DebounceSettings } from 'lodash';
2
+
3
+ export { DebounceSettings }
4
+
5
+ declare type HistoryAction<T> = {
6
+ type: 'CLEAR';
7
+ initialPresent: T;
8
+ } | {
9
+ type: 'REDO';
10
+ } | {
11
+ type: 'SET';
12
+ newPresent: T;
13
+ } | {
14
+ type: 'UNDO';
15
+ };
16
+
17
+ declare type HistoryOnChange<T> = ({ action, state }: {
18
+ action: HistoryAction<T>['type'];
19
+ state: HistoryState<T>['present'];
20
+ }) => void;
21
+
22
+ declare type HistoryState<T> = {
23
+ future: T[];
24
+ past: T[];
25
+ present: T | null;
26
+ };
27
+
28
+ declare type UseHistoryOptionsState<T> = {
29
+ debounceSettings?: DebounceSettings;
30
+ debounceTime?: number;
31
+ maxCapacity?: number;
32
+ onChange?: HistoryOnChange<T>;
33
+ };
34
+
35
+ declare const useHistoryState: <T>(initialPresent: T, options?: UseHistoryOptionsState<T>) => {
36
+ canRedo: boolean;
37
+ canUndo: boolean;
38
+ clear: () => void;
39
+ history: {
40
+ past: T[];
41
+ future: T[];
42
+ };
43
+ redo: () => void;
44
+ set: (newPresent: T) => void;
45
+ setDirect: (newPresent: T) => void;
46
+ state: T;
47
+ undo: () => void;
48
+ };
49
+ export default useHistoryState;
50
+
51
+ export { }
@@ -0,0 +1,114 @@
1
+ import { useRef as P, useReducer as b, useCallback as d } from "react";
2
+ import c from "lodash/cloneDeep";
3
+ import N from "lodash/isNil";
4
+ import f from "lodash/size";
5
+ import T from "./use-debounce-fn.js";
6
+ const U = {
7
+ past: [],
8
+ present: null,
9
+ future: []
10
+ }, A = ({
11
+ action: r,
12
+ maxCapacity: y,
13
+ onChange: s,
14
+ state: n
15
+ }) => {
16
+ const { past: p, present: o, future: l } = n;
17
+ if (r.type === "UNDO") {
18
+ if (f(p) === 0)
19
+ return n;
20
+ const e = p[f(p) - 1], u = {
21
+ past: p.slice(0, f(p) - 1),
22
+ present: c(e),
23
+ future: [c(o), ...l]
24
+ };
25
+ return s == null || s({
26
+ action: r.type,
27
+ state: u.present
28
+ }), u;
29
+ } else if (r.type === "REDO") {
30
+ if (f(l) === 0)
31
+ return n;
32
+ const e = l[0], t = l.slice(1), u = {
33
+ past: [...p, c(o)],
34
+ present: c(e),
35
+ future: t
36
+ };
37
+ return s == null || s({
38
+ action: r.type,
39
+ state: u.present
40
+ }), u;
41
+ } else if (r.type === "SET") {
42
+ const { newPresent: e } = r;
43
+ if (JSON.stringify(e) === JSON.stringify(o))
44
+ return n;
45
+ let t = [...p];
46
+ o !== null && (t = [...t, c(o)]), !N(y) && y > 0 && f(t) > y && (t = t.slice(f(t) - y));
47
+ const u = {
48
+ past: t,
49
+ present: c(e),
50
+ future: []
51
+ };
52
+ return s == null || s({
53
+ action: r.type,
54
+ state: u.present
55
+ }), u;
56
+ } else if (r.type === "CLEAR") {
57
+ const e = {
58
+ past: [],
59
+ present: c(r.initialPresent),
60
+ future: []
61
+ };
62
+ return s == null || s({
63
+ action: r.type,
64
+ state: e.present
65
+ }), e;
66
+ } else
67
+ throw new Error("Unsupported action type");
68
+ }, v = (r, y) => {
69
+ const { maxCapacity: s, debounceTime: n, debounceSettings: p, onChange: o } = y || {}, l = P(r), [e, t] = b(
70
+ (i, O) => A({
71
+ action: O,
72
+ maxCapacity: s,
73
+ onChange: o,
74
+ state: i
75
+ }),
76
+ {
77
+ ...U,
78
+ present: c(l.current)
79
+ }
80
+ ), u = f(e.future) !== 0, S = f(e.past) !== 0, R = d(() => t({
81
+ initialPresent: l.current,
82
+ type: "CLEAR"
83
+ }), []), D = d(() => {
84
+ u && t({ type: "REDO" });
85
+ }, [u]), m = T(
86
+ (i) => t({ type: "SET", newPresent: i }),
87
+ n,
88
+ p
89
+ ), w = d((i) => t({ type: "SET", newPresent: i }), []), E = d(() => {
90
+ S && t({ type: "UNDO" });
91
+ }, [S]), a = d(
92
+ (i) => {
93
+ n ? m(i) : w(i);
94
+ },
95
+ [n, m, w]
96
+ );
97
+ return {
98
+ canRedo: u,
99
+ canUndo: S,
100
+ clear: R,
101
+ history: {
102
+ past: e.past,
103
+ future: e.future
104
+ },
105
+ redo: D,
106
+ set: a,
107
+ setDirect: w,
108
+ state: e.present,
109
+ undo: E
110
+ };
111
+ };
112
+ export {
113
+ v as default
114
+ };
@@ -0,0 +1,9 @@
1
+ import { DebouncedFuncLeading } from 'lodash';
2
+ import { ThrottleSettings } from 'lodash';
3
+
4
+ export { ThrottleSettings }
5
+
6
+ declare const useThrottleFn: <T extends (...args: any[]) => any>(fn: T, delay?: number, options?: ThrottleSettings) => DebouncedFuncLeading<(...args: Parameters<T>) => any>;
7
+ export default useThrottleFn;
8
+
9
+ export { }
@@ -0,0 +1,25 @@
1
+ import { useRef as o, useEffect as f } from "react";
2
+ import s from "lodash/throttle";
3
+ const m = 300, E = (r, t = m, e) => {
4
+ const n = o(r);
5
+ f(() => {
6
+ n.current = r;
7
+ }, [r]);
8
+ const u = o(
9
+ s(
10
+ (...c) => n.current(...c),
11
+ t,
12
+ e
13
+ )
14
+ );
15
+ return f(() => (u.current = s(
16
+ (...c) => n.current(...c),
17
+ t,
18
+ e
19
+ ), () => {
20
+ u.current.cancel();
21
+ }), [t, e]), u.current;
22
+ };
23
+ export {
24
+ E as default
25
+ };
package/package.json CHANGED
@@ -49,5 +49,5 @@
49
49
  "test": "vitest",
50
50
  "test:coverage": "vitest --coverage"
51
51
  },
52
- "version": "1.0.27"
52
+ "version": "1.0.29"
53
53
  }