@elementor/utils 0.3.1 → 0.5.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - e953081: add search in css classes list
8
+ create useDebounceState in util
9
+
10
+ ## 0.4.0
11
+
12
+ ### Minor Changes
13
+
14
+ - 571ff75: Add `debounce` util
15
+
3
16
  ## 0.3.1
4
17
 
5
18
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -1,3 +1,5 @@
1
+ import * as React from 'react';
2
+
1
3
  type ElementorErrorOptions = {
2
4
  cause?: Error['cause'];
3
5
  context?: Record<string, unknown> | null;
@@ -26,10 +28,29 @@ declare const createError: <T extends ElementorErrorOptions["context"]>({ code,
26
28
  cause?: unknown;
27
29
  };
28
30
  captureStackTrace(targetObject: object, constructorOpt?: Function): void;
29
- prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
31
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
30
32
  stackTraceLimit: number;
31
33
  };
32
34
 
33
35
  declare const ensureError: (error: unknown) => Error;
34
36
 
35
- export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, createError, ensureError };
37
+ type UseDebounceStateOptions = {
38
+ delay?: number;
39
+ initialValue?: string;
40
+ };
41
+ type UseDebounceStateResult = {
42
+ debouncedValue: string;
43
+ inputValue: string;
44
+ handleChange: (val: string) => void;
45
+ setInputValue: React.Dispatch<React.SetStateAction<string>>;
46
+ };
47
+ declare function useDebounceState(options?: UseDebounceStateOptions): UseDebounceStateResult;
48
+
49
+ declare function debounce<TArgs extends any[]>(fn: (...args: TArgs) => void, wait: number): {
50
+ (...args: TArgs): void;
51
+ flush: (...args: TArgs) => void;
52
+ cancel: () => void;
53
+ pending: () => boolean;
54
+ };
55
+
56
+ export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, type UseDebounceStateOptions, type UseDebounceStateResult, createError, debounce, ensureError, useDebounceState };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import * as React from 'react';
2
+
1
3
  type ElementorErrorOptions = {
2
4
  cause?: Error['cause'];
3
5
  context?: Record<string, unknown> | null;
@@ -26,10 +28,29 @@ declare const createError: <T extends ElementorErrorOptions["context"]>({ code,
26
28
  cause?: unknown;
27
29
  };
28
30
  captureStackTrace(targetObject: object, constructorOpt?: Function): void;
29
- prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
31
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
30
32
  stackTraceLimit: number;
31
33
  };
32
34
 
33
35
  declare const ensureError: (error: unknown) => Error;
34
36
 
35
- export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, createError, ensureError };
37
+ type UseDebounceStateOptions = {
38
+ delay?: number;
39
+ initialValue?: string;
40
+ };
41
+ type UseDebounceStateResult = {
42
+ debouncedValue: string;
43
+ inputValue: string;
44
+ handleChange: (val: string) => void;
45
+ setInputValue: React.Dispatch<React.SetStateAction<string>>;
46
+ };
47
+ declare function useDebounceState(options?: UseDebounceStateOptions): UseDebounceStateResult;
48
+
49
+ declare function debounce<TArgs extends any[]>(fn: (...args: TArgs) => void, wait: number): {
50
+ (...args: TArgs): void;
51
+ flush: (...args: TArgs) => void;
52
+ cancel: () => void;
53
+ pending: () => boolean;
54
+ };
55
+
56
+ export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, type UseDebounceStateOptions, type UseDebounceStateResult, createError, debounce, ensureError, useDebounceState };
package/dist/index.js CHANGED
@@ -22,7 +22,9 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  ElementorError: () => ElementorError,
24
24
  createError: () => createError,
25
- ensureError: () => ensureError
25
+ debounce: () => debounce,
26
+ ensureError: () => ensureError,
27
+ useDebounceState: () => useDebounceState
26
28
  });
27
29
  module.exports = __toCommonJS(index_exports);
28
30
 
@@ -61,10 +63,76 @@ var ensureError = (error) => {
61
63
  }
62
64
  return new Error(`Unexpected non-error thrown: ${message}`, { cause });
63
65
  };
66
+
67
+ // src/use-debounce-state.ts
68
+ var import_react = require("react");
69
+
70
+ // src/debounce.ts
71
+ function debounce(fn, wait) {
72
+ let timer = null;
73
+ const cancel = () => {
74
+ if (!timer) {
75
+ return;
76
+ }
77
+ clearTimeout(timer);
78
+ timer = null;
79
+ };
80
+ const flush = (...args) => {
81
+ cancel();
82
+ fn(...args);
83
+ };
84
+ const run = (...args) => {
85
+ cancel();
86
+ timer = setTimeout(() => {
87
+ fn(...args);
88
+ timer = null;
89
+ }, wait);
90
+ };
91
+ const pending = () => !!timer;
92
+ run.flush = flush;
93
+ run.cancel = cancel;
94
+ run.pending = pending;
95
+ return run;
96
+ }
97
+
98
+ // src/use-debounce-state.ts
99
+ function useDebounceState(options = {}) {
100
+ const { delay = 300, initialValue = "" } = options;
101
+ const [debouncedValue, setDebouncedValue] = (0, import_react.useState)(initialValue);
102
+ const [inputValue, setInputValue] = (0, import_react.useState)(initialValue);
103
+ const runRef = (0, import_react.useRef)(null);
104
+ (0, import_react.useEffect)(() => {
105
+ return () => {
106
+ runRef.current?.cancel?.();
107
+ };
108
+ }, []);
109
+ const debouncedSetValue = (0, import_react.useCallback)(
110
+ (val) => {
111
+ runRef.current?.cancel?.();
112
+ runRef.current = debounce(() => {
113
+ setDebouncedValue(val);
114
+ }, delay);
115
+ runRef.current();
116
+ },
117
+ [delay]
118
+ );
119
+ const handleChange = (val) => {
120
+ setInputValue(val);
121
+ debouncedSetValue(val);
122
+ };
123
+ return {
124
+ debouncedValue,
125
+ inputValue,
126
+ handleChange,
127
+ setInputValue
128
+ };
129
+ }
64
130
  // Annotate the CommonJS export names for ESM import in node:
65
131
  0 && (module.exports = {
66
132
  ElementorError,
67
133
  createError,
68
- ensureError
134
+ debounce,
135
+ ensureError,
136
+ useDebounceState
69
137
  });
70
138
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/errors/elementor-error.ts","../src/errors/create-error.ts","../src/errors/ensure-error.ts"],"sourcesContent":["export { ElementorError, createError, ensureError } from './errors';\nexport type { ElementorErrorOptions, CreateErrorParams } from './errors';\n","export type ElementorErrorOptions = {\n\tcause?: Error[ 'cause' ];\n\tcontext?: Record< string, unknown > | null;\n\tcode: string;\n};\n\nexport class ElementorError extends Error {\n\treadonly context: ElementorErrorOptions[ 'context' ];\n\treadonly code: ElementorErrorOptions[ 'code' ];\n\n\tconstructor( message: string, { code, context = null, cause = null }: ElementorErrorOptions ) {\n\t\tsuper( message, { cause } );\n\t\tthis.context = context;\n\t\tthis.code = code;\n\t}\n}\n","import { ElementorError, type ElementorErrorOptions } from './elementor-error';\n\nexport type CreateErrorParams = {\n\tcode: ElementorErrorOptions[ 'code' ];\n\tmessage: string;\n};\n\nexport const createError = < T extends ElementorErrorOptions[ 'context' ] >( { code, message }: CreateErrorParams ) => {\n\treturn class extends ElementorError {\n\t\tconstructor( { cause, context }: { cause?: ElementorErrorOptions[ 'cause' ]; context?: T } = {} ) {\n\t\t\tsuper( message, { cause, code, context } );\n\t\t}\n\t};\n};\n","export const ensureError = ( error: unknown ) => {\n\tif ( error instanceof Error ) {\n\t\treturn error;\n\t}\n\n\tlet message: string;\n\tlet cause: unknown = null;\n\n\ttry {\n\t\tmessage = JSON.stringify( error );\n\t} catch ( e ) {\n\t\tcause = e;\n\t\tmessage = 'Unable to stringify the thrown value';\n\t}\n\treturn new Error( `Unexpected non-error thrown: ${ message }`, { cause } );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAChC;AAAA,EACA;AAAA,EAET,YAAa,SAAiB,EAAE,MAAM,UAAU,MAAM,QAAQ,KAAK,GAA2B;AAC7F,UAAO,SAAS,EAAE,MAAM,CAAE;AAC1B,SAAK,UAAU;AACf,SAAK,OAAO;AAAA,EACb;AACD;;;ACRO,IAAM,cAAc,CAAkD,EAAE,MAAM,QAAQ,MAA0B;AACtH,SAAO,cAAc,eAAe;AAAA,IACnC,YAAa,EAAE,OAAO,QAAQ,IAA+D,CAAC,GAAI;AACjG,YAAO,SAAS,EAAE,OAAO,MAAM,QAAQ,CAAE;AAAA,IAC1C;AAAA,EACD;AACD;;;ACbO,IAAM,cAAc,CAAE,UAAoB;AAChD,MAAK,iBAAiB,OAAQ;AAC7B,WAAO;AAAA,EACR;AAEA,MAAI;AACJ,MAAI,QAAiB;AAErB,MAAI;AACH,cAAU,KAAK,UAAW,KAAM;AAAA,EACjC,SAAU,GAAI;AACb,YAAQ;AACR,cAAU;AAAA,EACX;AACA,SAAO,IAAI,MAAO,gCAAiC,OAAQ,IAAI,EAAE,MAAM,CAAE;AAC1E;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/errors/elementor-error.ts","../src/errors/create-error.ts","../src/errors/ensure-error.ts","../src/use-debounce-state.ts","../src/debounce.ts"],"sourcesContent":["export { ElementorError, createError, ensureError } from './errors';\nexport type { ElementorErrorOptions, CreateErrorParams } from './errors';\nexport { useDebounceState, type UseDebounceStateOptions, type UseDebounceStateResult } from './use-debounce-state';\nexport { debounce } from './debounce';\n","export type ElementorErrorOptions = {\n\tcause?: Error[ 'cause' ];\n\tcontext?: Record< string, unknown > | null;\n\tcode: string;\n};\n\nexport class ElementorError extends Error {\n\treadonly context: ElementorErrorOptions[ 'context' ];\n\treadonly code: ElementorErrorOptions[ 'code' ];\n\n\tconstructor( message: string, { code, context = null, cause = null }: ElementorErrorOptions ) {\n\t\tsuper( message, { cause } );\n\t\tthis.context = context;\n\t\tthis.code = code;\n\t}\n}\n","import { ElementorError, type ElementorErrorOptions } from './elementor-error';\n\nexport type CreateErrorParams = {\n\tcode: ElementorErrorOptions[ 'code' ];\n\tmessage: string;\n};\n\nexport const createError = < T extends ElementorErrorOptions[ 'context' ] >( { code, message }: CreateErrorParams ) => {\n\treturn class extends ElementorError {\n\t\tconstructor( { cause, context }: { cause?: ElementorErrorOptions[ 'cause' ]; context?: T } = {} ) {\n\t\t\tsuper( message, { cause, code, context } );\n\t\t}\n\t};\n};\n","export const ensureError = ( error: unknown ) => {\n\tif ( error instanceof Error ) {\n\t\treturn error;\n\t}\n\n\tlet message: string;\n\tlet cause: unknown = null;\n\n\ttry {\n\t\tmessage = JSON.stringify( error );\n\t} catch ( e ) {\n\t\tcause = e;\n\t\tmessage = 'Unable to stringify the thrown value';\n\t}\n\treturn new Error( `Unexpected non-error thrown: ${ message }`, { cause } );\n};\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport type * as React from 'react';\n\nimport { debounce } from './debounce';\n\nexport type UseDebounceStateOptions = {\n\tdelay?: number;\n\tinitialValue?: string;\n};\n\nexport type UseDebounceStateResult = {\n\tdebouncedValue: string;\n\tinputValue: string;\n\thandleChange: ( val: string ) => void;\n\tsetInputValue: React.Dispatch< React.SetStateAction< string > >;\n};\n\nexport function useDebounceState( options: UseDebounceStateOptions = {} ): UseDebounceStateResult {\n\tconst { delay = 300, initialValue = '' } = options;\n\n\tconst [ debouncedValue, setDebouncedValue ] = useState( initialValue );\n\tconst [ inputValue, setInputValue ] = useState( initialValue );\n\n\tconst runRef = useRef< ReturnType< typeof debounce > | null >( null );\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\trunRef.current?.cancel?.();\n\t\t};\n\t}, [] );\n\n\tconst debouncedSetValue = useCallback(\n\t\t( val: string ) => {\n\t\t\trunRef.current?.cancel?.();\n\t\t\trunRef.current = debounce( () => {\n\t\t\t\tsetDebouncedValue( val );\n\t\t\t}, delay );\n\t\t\trunRef.current();\n\t\t},\n\t\t[ delay ]\n\t);\n\n\tconst handleChange = ( val: string ) => {\n\t\tsetInputValue( val );\n\t\tdebouncedSetValue( val );\n\t};\n\n\treturn {\n\t\tdebouncedValue,\n\t\tinputValue,\n\t\thandleChange,\n\t\tsetInputValue,\n\t};\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function debounce< TArgs extends any[] >( fn: ( ...args: TArgs ) => void, wait: number ) {\n\tlet timer: ReturnType< typeof setTimeout > | null = null;\n\n\tconst cancel = () => {\n\t\tif ( ! timer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tclearTimeout( timer );\n\t\ttimer = null;\n\t};\n\n\tconst flush = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\tfn( ...args );\n\t};\n\n\tconst run = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\ttimer = setTimeout( () => {\n\t\t\tfn( ...args );\n\n\t\t\ttimer = null;\n\t\t}, wait );\n\t};\n\n\tconst pending = () => !! timer;\n\n\trun.flush = flush;\n\trun.cancel = cancel;\n\trun.pending = pending;\n\n\treturn run;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAChC;AAAA,EACA;AAAA,EAET,YAAa,SAAiB,EAAE,MAAM,UAAU,MAAM,QAAQ,KAAK,GAA2B;AAC7F,UAAO,SAAS,EAAE,MAAM,CAAE;AAC1B,SAAK,UAAU;AACf,SAAK,OAAO;AAAA,EACb;AACD;;;ACRO,IAAM,cAAc,CAAkD,EAAE,MAAM,QAAQ,MAA0B;AACtH,SAAO,cAAc,eAAe;AAAA,IACnC,YAAa,EAAE,OAAO,QAAQ,IAA+D,CAAC,GAAI;AACjG,YAAO,SAAS,EAAE,OAAO,MAAM,QAAQ,CAAE;AAAA,IAC1C;AAAA,EACD;AACD;;;ACbO,IAAM,cAAc,CAAE,UAAoB;AAChD,MAAK,iBAAiB,OAAQ;AAC7B,WAAO;AAAA,EACR;AAEA,MAAI;AACJ,MAAI,QAAiB;AAErB,MAAI;AACH,cAAU,KAAK,UAAW,KAAM;AAAA,EACjC,SAAU,GAAI;AACb,YAAQ;AACR,cAAU;AAAA,EACX;AACA,SAAO,IAAI,MAAO,gCAAiC,OAAQ,IAAI,EAAE,MAAM,CAAE;AAC1E;;;ACfA,mBAAyD;;;ACClD,SAAS,SAAiC,IAAgC,MAAe;AAC/F,MAAI,QAAgD;AAEpD,QAAM,SAAS,MAAM;AACpB,QAAK,CAAE,OAAQ;AACd;AAAA,IACD;AAEA,iBAAc,KAAM;AACpB,YAAQ;AAAA,EACT;AAEA,QAAM,QAAQ,IAAK,SAAiB;AACnC,WAAO;AAEP,OAAI,GAAG,IAAK;AAAA,EACb;AAEA,QAAM,MAAM,IAAK,SAAiB;AACjC,WAAO;AAEP,YAAQ,WAAY,MAAM;AACzB,SAAI,GAAG,IAAK;AAEZ,cAAQ;AAAA,IACT,GAAG,IAAK;AAAA,EACT;AAEA,QAAM,UAAU,MAAM,CAAC,CAAE;AAEzB,MAAI,QAAQ;AACZ,MAAI,SAAS;AACb,MAAI,UAAU;AAEd,SAAO;AACR;;;ADnBO,SAAS,iBAAkB,UAAmC,CAAC,GAA4B;AACjG,QAAM,EAAE,QAAQ,KAAK,eAAe,GAAG,IAAI;AAE3C,QAAM,CAAE,gBAAgB,iBAAkB,QAAI,uBAAU,YAAa;AACrE,QAAM,CAAE,YAAY,aAAc,QAAI,uBAAU,YAAa;AAE7D,QAAM,aAAS,qBAAgD,IAAK;AAEpE,8BAAW,MAAM;AAChB,WAAO,MAAM;AACZ,aAAO,SAAS,SAAS;AAAA,IAC1B;AAAA,EACD,GAAG,CAAC,CAAE;AAEN,QAAM,wBAAoB;AAAA,IACzB,CAAE,QAAiB;AAClB,aAAO,SAAS,SAAS;AACzB,aAAO,UAAU,SAAU,MAAM;AAChC,0BAAmB,GAAI;AAAA,MACxB,GAAG,KAAM;AACT,aAAO,QAAQ;AAAA,IAChB;AAAA,IACA,CAAE,KAAM;AAAA,EACT;AAEA,QAAM,eAAe,CAAE,QAAiB;AACvC,kBAAe,GAAI;AACnB,sBAAmB,GAAI;AAAA,EACxB;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;","names":[]}
package/dist/index.mjs CHANGED
@@ -33,9 +33,75 @@ var ensureError = (error) => {
33
33
  }
34
34
  return new Error(`Unexpected non-error thrown: ${message}`, { cause });
35
35
  };
36
+
37
+ // src/use-debounce-state.ts
38
+ import { useCallback, useEffect, useRef, useState } from "react";
39
+
40
+ // src/debounce.ts
41
+ function debounce(fn, wait) {
42
+ let timer = null;
43
+ const cancel = () => {
44
+ if (!timer) {
45
+ return;
46
+ }
47
+ clearTimeout(timer);
48
+ timer = null;
49
+ };
50
+ const flush = (...args) => {
51
+ cancel();
52
+ fn(...args);
53
+ };
54
+ const run = (...args) => {
55
+ cancel();
56
+ timer = setTimeout(() => {
57
+ fn(...args);
58
+ timer = null;
59
+ }, wait);
60
+ };
61
+ const pending = () => !!timer;
62
+ run.flush = flush;
63
+ run.cancel = cancel;
64
+ run.pending = pending;
65
+ return run;
66
+ }
67
+
68
+ // src/use-debounce-state.ts
69
+ function useDebounceState(options = {}) {
70
+ const { delay = 300, initialValue = "" } = options;
71
+ const [debouncedValue, setDebouncedValue] = useState(initialValue);
72
+ const [inputValue, setInputValue] = useState(initialValue);
73
+ const runRef = useRef(null);
74
+ useEffect(() => {
75
+ return () => {
76
+ runRef.current?.cancel?.();
77
+ };
78
+ }, []);
79
+ const debouncedSetValue = useCallback(
80
+ (val) => {
81
+ runRef.current?.cancel?.();
82
+ runRef.current = debounce(() => {
83
+ setDebouncedValue(val);
84
+ }, delay);
85
+ runRef.current();
86
+ },
87
+ [delay]
88
+ );
89
+ const handleChange = (val) => {
90
+ setInputValue(val);
91
+ debouncedSetValue(val);
92
+ };
93
+ return {
94
+ debouncedValue,
95
+ inputValue,
96
+ handleChange,
97
+ setInputValue
98
+ };
99
+ }
36
100
  export {
37
101
  ElementorError,
38
102
  createError,
39
- ensureError
103
+ debounce,
104
+ ensureError,
105
+ useDebounceState
40
106
  };
41
107
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/errors/elementor-error.ts","../src/errors/create-error.ts","../src/errors/ensure-error.ts"],"sourcesContent":["export type ElementorErrorOptions = {\n\tcause?: Error[ 'cause' ];\n\tcontext?: Record< string, unknown > | null;\n\tcode: string;\n};\n\nexport class ElementorError extends Error {\n\treadonly context: ElementorErrorOptions[ 'context' ];\n\treadonly code: ElementorErrorOptions[ 'code' ];\n\n\tconstructor( message: string, { code, context = null, cause = null }: ElementorErrorOptions ) {\n\t\tsuper( message, { cause } );\n\t\tthis.context = context;\n\t\tthis.code = code;\n\t}\n}\n","import { ElementorError, type ElementorErrorOptions } from './elementor-error';\n\nexport type CreateErrorParams = {\n\tcode: ElementorErrorOptions[ 'code' ];\n\tmessage: string;\n};\n\nexport const createError = < T extends ElementorErrorOptions[ 'context' ] >( { code, message }: CreateErrorParams ) => {\n\treturn class extends ElementorError {\n\t\tconstructor( { cause, context }: { cause?: ElementorErrorOptions[ 'cause' ]; context?: T } = {} ) {\n\t\t\tsuper( message, { cause, code, context } );\n\t\t}\n\t};\n};\n","export const ensureError = ( error: unknown ) => {\n\tif ( error instanceof Error ) {\n\t\treturn error;\n\t}\n\n\tlet message: string;\n\tlet cause: unknown = null;\n\n\ttry {\n\t\tmessage = JSON.stringify( error );\n\t} catch ( e ) {\n\t\tcause = e;\n\t\tmessage = 'Unable to stringify the thrown value';\n\t}\n\treturn new Error( `Unexpected non-error thrown: ${ message }`, { cause } );\n};\n"],"mappings":";AAMO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAChC;AAAA,EACA;AAAA,EAET,YAAa,SAAiB,EAAE,MAAM,UAAU,MAAM,QAAQ,KAAK,GAA2B;AAC7F,UAAO,SAAS,EAAE,MAAM,CAAE;AAC1B,SAAK,UAAU;AACf,SAAK,OAAO;AAAA,EACb;AACD;;;ACRO,IAAM,cAAc,CAAkD,EAAE,MAAM,QAAQ,MAA0B;AACtH,SAAO,cAAc,eAAe;AAAA,IACnC,YAAa,EAAE,OAAO,QAAQ,IAA+D,CAAC,GAAI;AACjG,YAAO,SAAS,EAAE,OAAO,MAAM,QAAQ,CAAE;AAAA,IAC1C;AAAA,EACD;AACD;;;ACbO,IAAM,cAAc,CAAE,UAAoB;AAChD,MAAK,iBAAiB,OAAQ;AAC7B,WAAO;AAAA,EACR;AAEA,MAAI;AACJ,MAAI,QAAiB;AAErB,MAAI;AACH,cAAU,KAAK,UAAW,KAAM;AAAA,EACjC,SAAU,GAAI;AACb,YAAQ;AACR,cAAU;AAAA,EACX;AACA,SAAO,IAAI,MAAO,gCAAiC,OAAQ,IAAI,EAAE,MAAM,CAAE;AAC1E;","names":[]}
1
+ {"version":3,"sources":["../src/errors/elementor-error.ts","../src/errors/create-error.ts","../src/errors/ensure-error.ts","../src/use-debounce-state.ts","../src/debounce.ts"],"sourcesContent":["export type ElementorErrorOptions = {\n\tcause?: Error[ 'cause' ];\n\tcontext?: Record< string, unknown > | null;\n\tcode: string;\n};\n\nexport class ElementorError extends Error {\n\treadonly context: ElementorErrorOptions[ 'context' ];\n\treadonly code: ElementorErrorOptions[ 'code' ];\n\n\tconstructor( message: string, { code, context = null, cause = null }: ElementorErrorOptions ) {\n\t\tsuper( message, { cause } );\n\t\tthis.context = context;\n\t\tthis.code = code;\n\t}\n}\n","import { ElementorError, type ElementorErrorOptions } from './elementor-error';\n\nexport type CreateErrorParams = {\n\tcode: ElementorErrorOptions[ 'code' ];\n\tmessage: string;\n};\n\nexport const createError = < T extends ElementorErrorOptions[ 'context' ] >( { code, message }: CreateErrorParams ) => {\n\treturn class extends ElementorError {\n\t\tconstructor( { cause, context }: { cause?: ElementorErrorOptions[ 'cause' ]; context?: T } = {} ) {\n\t\t\tsuper( message, { cause, code, context } );\n\t\t}\n\t};\n};\n","export const ensureError = ( error: unknown ) => {\n\tif ( error instanceof Error ) {\n\t\treturn error;\n\t}\n\n\tlet message: string;\n\tlet cause: unknown = null;\n\n\ttry {\n\t\tmessage = JSON.stringify( error );\n\t} catch ( e ) {\n\t\tcause = e;\n\t\tmessage = 'Unable to stringify the thrown value';\n\t}\n\treturn new Error( `Unexpected non-error thrown: ${ message }`, { cause } );\n};\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport type * as React from 'react';\n\nimport { debounce } from './debounce';\n\nexport type UseDebounceStateOptions = {\n\tdelay?: number;\n\tinitialValue?: string;\n};\n\nexport type UseDebounceStateResult = {\n\tdebouncedValue: string;\n\tinputValue: string;\n\thandleChange: ( val: string ) => void;\n\tsetInputValue: React.Dispatch< React.SetStateAction< string > >;\n};\n\nexport function useDebounceState( options: UseDebounceStateOptions = {} ): UseDebounceStateResult {\n\tconst { delay = 300, initialValue = '' } = options;\n\n\tconst [ debouncedValue, setDebouncedValue ] = useState( initialValue );\n\tconst [ inputValue, setInputValue ] = useState( initialValue );\n\n\tconst runRef = useRef< ReturnType< typeof debounce > | null >( null );\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\trunRef.current?.cancel?.();\n\t\t};\n\t}, [] );\n\n\tconst debouncedSetValue = useCallback(\n\t\t( val: string ) => {\n\t\t\trunRef.current?.cancel?.();\n\t\t\trunRef.current = debounce( () => {\n\t\t\t\tsetDebouncedValue( val );\n\t\t\t}, delay );\n\t\t\trunRef.current();\n\t\t},\n\t\t[ delay ]\n\t);\n\n\tconst handleChange = ( val: string ) => {\n\t\tsetInputValue( val );\n\t\tdebouncedSetValue( val );\n\t};\n\n\treturn {\n\t\tdebouncedValue,\n\t\tinputValue,\n\t\thandleChange,\n\t\tsetInputValue,\n\t};\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function debounce< TArgs extends any[] >( fn: ( ...args: TArgs ) => void, wait: number ) {\n\tlet timer: ReturnType< typeof setTimeout > | null = null;\n\n\tconst cancel = () => {\n\t\tif ( ! timer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tclearTimeout( timer );\n\t\ttimer = null;\n\t};\n\n\tconst flush = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\tfn( ...args );\n\t};\n\n\tconst run = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\ttimer = setTimeout( () => {\n\t\t\tfn( ...args );\n\n\t\t\ttimer = null;\n\t\t}, wait );\n\t};\n\n\tconst pending = () => !! timer;\n\n\trun.flush = flush;\n\trun.cancel = cancel;\n\trun.pending = pending;\n\n\treturn run;\n}\n"],"mappings":";AAMO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAChC;AAAA,EACA;AAAA,EAET,YAAa,SAAiB,EAAE,MAAM,UAAU,MAAM,QAAQ,KAAK,GAA2B;AAC7F,UAAO,SAAS,EAAE,MAAM,CAAE;AAC1B,SAAK,UAAU;AACf,SAAK,OAAO;AAAA,EACb;AACD;;;ACRO,IAAM,cAAc,CAAkD,EAAE,MAAM,QAAQ,MAA0B;AACtH,SAAO,cAAc,eAAe;AAAA,IACnC,YAAa,EAAE,OAAO,QAAQ,IAA+D,CAAC,GAAI;AACjG,YAAO,SAAS,EAAE,OAAO,MAAM,QAAQ,CAAE;AAAA,IAC1C;AAAA,EACD;AACD;;;ACbO,IAAM,cAAc,CAAE,UAAoB;AAChD,MAAK,iBAAiB,OAAQ;AAC7B,WAAO;AAAA,EACR;AAEA,MAAI;AACJ,MAAI,QAAiB;AAErB,MAAI;AACH,cAAU,KAAK,UAAW,KAAM;AAAA,EACjC,SAAU,GAAI;AACb,YAAQ;AACR,cAAU;AAAA,EACX;AACA,SAAO,IAAI,MAAO,gCAAiC,OAAQ,IAAI,EAAE,MAAM,CAAE;AAC1E;;;ACfA,SAAS,aAAa,WAAW,QAAQ,gBAAgB;;;ACClD,SAAS,SAAiC,IAAgC,MAAe;AAC/F,MAAI,QAAgD;AAEpD,QAAM,SAAS,MAAM;AACpB,QAAK,CAAE,OAAQ;AACd;AAAA,IACD;AAEA,iBAAc,KAAM;AACpB,YAAQ;AAAA,EACT;AAEA,QAAM,QAAQ,IAAK,SAAiB;AACnC,WAAO;AAEP,OAAI,GAAG,IAAK;AAAA,EACb;AAEA,QAAM,MAAM,IAAK,SAAiB;AACjC,WAAO;AAEP,YAAQ,WAAY,MAAM;AACzB,SAAI,GAAG,IAAK;AAEZ,cAAQ;AAAA,IACT,GAAG,IAAK;AAAA,EACT;AAEA,QAAM,UAAU,MAAM,CAAC,CAAE;AAEzB,MAAI,QAAQ;AACZ,MAAI,SAAS;AACb,MAAI,UAAU;AAEd,SAAO;AACR;;;ADnBO,SAAS,iBAAkB,UAAmC,CAAC,GAA4B;AACjG,QAAM,EAAE,QAAQ,KAAK,eAAe,GAAG,IAAI;AAE3C,QAAM,CAAE,gBAAgB,iBAAkB,IAAI,SAAU,YAAa;AACrE,QAAM,CAAE,YAAY,aAAc,IAAI,SAAU,YAAa;AAE7D,QAAM,SAAS,OAAgD,IAAK;AAEpE,YAAW,MAAM;AAChB,WAAO,MAAM;AACZ,aAAO,SAAS,SAAS;AAAA,IAC1B;AAAA,EACD,GAAG,CAAC,CAAE;AAEN,QAAM,oBAAoB;AAAA,IACzB,CAAE,QAAiB;AAClB,aAAO,SAAS,SAAS;AACzB,aAAO,UAAU,SAAU,MAAM;AAChC,0BAAmB,GAAI;AAAA,MACxB,GAAG,KAAM;AACT,aAAO,QAAQ;AAAA,IAChB;AAAA,IACA,CAAE,KAAM;AAAA,EACT;AAEA,QAAM,eAAe,CAAE,QAAiB;AACvC,kBAAe,GAAI;AACnB,sBAAmB,GAAI;AAAA,EACxB;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@elementor/utils",
3
3
  "description": "This package contains utility functions that are being used across the Elementor packages",
4
- "version": "0.3.1",
4
+ "version": "0.5.0",
5
5
  "private": false,
6
6
  "author": "Elementor Team",
7
7
  "homepage": "https://elementor.com/",
@@ -39,6 +39,9 @@
39
39
  "build": "tsup --config=../../tsup.build.ts",
40
40
  "dev": "tsup --config=../../tsup.dev.ts"
41
41
  },
42
+ "peerDependencies": {
43
+ "react": "^18.3.1"
44
+ },
42
45
  "devDependencies": {
43
46
  "tsup": "^8.3.5"
44
47
  }
@@ -0,0 +1,37 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
+ export function debounce< TArgs extends any[] >( fn: ( ...args: TArgs ) => void, wait: number ) {
3
+ let timer: ReturnType< typeof setTimeout > | null = null;
4
+
5
+ const cancel = () => {
6
+ if ( ! timer ) {
7
+ return;
8
+ }
9
+
10
+ clearTimeout( timer );
11
+ timer = null;
12
+ };
13
+
14
+ const flush = ( ...args: TArgs ) => {
15
+ cancel();
16
+
17
+ fn( ...args );
18
+ };
19
+
20
+ const run = ( ...args: TArgs ) => {
21
+ cancel();
22
+
23
+ timer = setTimeout( () => {
24
+ fn( ...args );
25
+
26
+ timer = null;
27
+ }, wait );
28
+ };
29
+
30
+ const pending = () => !! timer;
31
+
32
+ run.flush = flush;
33
+ run.cancel = cancel;
34
+ run.pending = pending;
35
+
36
+ return run;
37
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export { ElementorError, createError, ensureError } from './errors';
2
2
  export type { ElementorErrorOptions, CreateErrorParams } from './errors';
3
+ export { useDebounceState, type UseDebounceStateOptions, type UseDebounceStateResult } from './use-debounce-state';
4
+ export { debounce } from './debounce';
@@ -0,0 +1,54 @@
1
+ import { useCallback, useEffect, useRef, useState } from 'react';
2
+ import type * as React from 'react';
3
+
4
+ import { debounce } from './debounce';
5
+
6
+ export type UseDebounceStateOptions = {
7
+ delay?: number;
8
+ initialValue?: string;
9
+ };
10
+
11
+ export type UseDebounceStateResult = {
12
+ debouncedValue: string;
13
+ inputValue: string;
14
+ handleChange: ( val: string ) => void;
15
+ setInputValue: React.Dispatch< React.SetStateAction< string > >;
16
+ };
17
+
18
+ export function useDebounceState( options: UseDebounceStateOptions = {} ): UseDebounceStateResult {
19
+ const { delay = 300, initialValue = '' } = options;
20
+
21
+ const [ debouncedValue, setDebouncedValue ] = useState( initialValue );
22
+ const [ inputValue, setInputValue ] = useState( initialValue );
23
+
24
+ const runRef = useRef< ReturnType< typeof debounce > | null >( null );
25
+
26
+ useEffect( () => {
27
+ return () => {
28
+ runRef.current?.cancel?.();
29
+ };
30
+ }, [] );
31
+
32
+ const debouncedSetValue = useCallback(
33
+ ( val: string ) => {
34
+ runRef.current?.cancel?.();
35
+ runRef.current = debounce( () => {
36
+ setDebouncedValue( val );
37
+ }, delay );
38
+ runRef.current();
39
+ },
40
+ [ delay ]
41
+ );
42
+
43
+ const handleChange = ( val: string ) => {
44
+ setInputValue( val );
45
+ debouncedSetValue( val );
46
+ };
47
+
48
+ return {
49
+ debouncedValue,
50
+ inputValue,
51
+ handleChange,
52
+ setInputValue,
53
+ };
54
+ }