@mantine/hooks 6.0.2 → 6.0.4
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.
|
@@ -5,27 +5,31 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var React = require('react');
|
|
6
6
|
|
|
7
7
|
function useTimeout(callback, delay, options = { autoInvoke: false }) {
|
|
8
|
+
const callbackRef = React.useRef(null);
|
|
8
9
|
const timeoutRef = React.useRef(null);
|
|
9
|
-
const start = (...callbackParams) => {
|
|
10
|
+
const start = React.useCallback((...callbackParams) => {
|
|
10
11
|
if (!timeoutRef.current) {
|
|
11
12
|
timeoutRef.current = window.setTimeout(() => {
|
|
12
|
-
|
|
13
|
+
callbackRef.current(callbackParams);
|
|
13
14
|
timeoutRef.current = null;
|
|
14
15
|
}, delay);
|
|
15
16
|
}
|
|
16
|
-
};
|
|
17
|
-
const clear = () => {
|
|
17
|
+
}, [delay]);
|
|
18
|
+
const clear = React.useCallback(() => {
|
|
18
19
|
if (timeoutRef.current) {
|
|
19
20
|
window.clearTimeout(timeoutRef.current);
|
|
20
21
|
timeoutRef.current = null;
|
|
21
22
|
}
|
|
22
|
-
};
|
|
23
|
+
}, []);
|
|
24
|
+
React.useEffect(() => {
|
|
25
|
+
callbackRef.current = callback;
|
|
26
|
+
}, [callback]);
|
|
23
27
|
React.useEffect(() => {
|
|
24
28
|
if (options.autoInvoke) {
|
|
25
29
|
start();
|
|
26
30
|
}
|
|
27
31
|
return clear;
|
|
28
|
-
}, [delay]);
|
|
32
|
+
}, [clear, delay, options.autoInvoke, start]);
|
|
29
33
|
return { start, clear };
|
|
30
34
|
}
|
|
31
35
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-timeout.js","sources":["../../src/use-timeout/use-timeout.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"use-timeout.js","sources":["../../src/use-timeout/use-timeout.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\nexport function useTimeout(\n callback: (...callbackParams: any[]) => void,\n delay: number,\n options: { autoInvoke: boolean } = { autoInvoke: false }\n) {\n const callbackRef = useRef<Function>(null);\n const timeoutRef = useRef<number>(null);\n\n const start = useCallback(\n (...callbackParams: any[]) => {\n if (!timeoutRef.current) {\n timeoutRef.current = window.setTimeout(() => {\n callbackRef.current(callbackParams);\n timeoutRef.current = null;\n }, delay);\n }\n },\n [delay]\n );\n\n const clear = useCallback(() => {\n if (timeoutRef.current) {\n window.clearTimeout(timeoutRef.current);\n timeoutRef.current = null;\n }\n }, []);\n\n useEffect(() => {\n callbackRef.current = callback;\n }, [callback]);\n\n useEffect(() => {\n if (options.autoInvoke) {\n start();\n }\n\n return clear;\n }, [clear, delay, options.autoInvoke, start]);\n\n return { start, clear };\n}\n"],"names":["useRef","useCallback","useEffect"],"mappings":";;;;;;AACO,SAAS,UAAU,CAAC,QAAQ,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,EAAE;AAC7E,EAAE,MAAM,WAAW,GAAGA,YAAM,CAAC,IAAI,CAAC,CAAC;AACnC,EAAE,MAAM,UAAU,GAAGA,YAAM,CAAC,IAAI,CAAC,CAAC;AAClC,EAAE,MAAM,KAAK,GAAGC,iBAAW,CAAC,CAAC,GAAG,cAAc,KAAK;AACnD,IAAI,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE;AAC7B,MAAM,UAAU,CAAC,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,MAAM;AACnD,QAAQ,WAAW,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;AAC5C,QAAQ,UAAU,CAAC,OAAO,GAAG,IAAI,CAAC;AAClC,OAAO,EAAE,KAAK,CAAC,CAAC;AAChB,KAAK;AACL,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;AACd,EAAE,MAAM,KAAK,GAAGA,iBAAW,CAAC,MAAM;AAClC,IAAI,IAAI,UAAU,CAAC,OAAO,EAAE;AAC5B,MAAM,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;AAC9C,MAAM,UAAU,CAAC,OAAO,GAAG,IAAI,CAAC;AAChC,KAAK;AACL,GAAG,EAAE,EAAE,CAAC,CAAC;AACT,EAAEC,eAAS,CAAC,MAAM;AAClB,IAAI,WAAW,CAAC,OAAO,GAAG,QAAQ,CAAC;AACnC,GAAG,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;AACjB,EAAEA,eAAS,CAAC,MAAM;AAClB,IAAI,IAAI,OAAO,CAAC,UAAU,EAAE;AAC5B,MAAM,KAAK,EAAE,CAAC;AACd,KAAK;AACL,IAAI,OAAO,KAAK,CAAC;AACjB,GAAG,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC;AAChD,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;AAC1B;;;;"}
|
|
@@ -1,27 +1,31 @@
|
|
|
1
|
-
import { useRef, useEffect } from 'react';
|
|
1
|
+
import { useRef, useCallback, useEffect } from 'react';
|
|
2
2
|
|
|
3
3
|
function useTimeout(callback, delay, options = { autoInvoke: false }) {
|
|
4
|
+
const callbackRef = useRef(null);
|
|
4
5
|
const timeoutRef = useRef(null);
|
|
5
|
-
const start = (...callbackParams) => {
|
|
6
|
+
const start = useCallback((...callbackParams) => {
|
|
6
7
|
if (!timeoutRef.current) {
|
|
7
8
|
timeoutRef.current = window.setTimeout(() => {
|
|
8
|
-
|
|
9
|
+
callbackRef.current(callbackParams);
|
|
9
10
|
timeoutRef.current = null;
|
|
10
11
|
}, delay);
|
|
11
12
|
}
|
|
12
|
-
};
|
|
13
|
-
const clear = () => {
|
|
13
|
+
}, [delay]);
|
|
14
|
+
const clear = useCallback(() => {
|
|
14
15
|
if (timeoutRef.current) {
|
|
15
16
|
window.clearTimeout(timeoutRef.current);
|
|
16
17
|
timeoutRef.current = null;
|
|
17
18
|
}
|
|
18
|
-
};
|
|
19
|
+
}, []);
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
callbackRef.current = callback;
|
|
22
|
+
}, [callback]);
|
|
19
23
|
useEffect(() => {
|
|
20
24
|
if (options.autoInvoke) {
|
|
21
25
|
start();
|
|
22
26
|
}
|
|
23
27
|
return clear;
|
|
24
|
-
}, [delay]);
|
|
28
|
+
}, [clear, delay, options.autoInvoke, start]);
|
|
25
29
|
return { start, clear };
|
|
26
30
|
}
|
|
27
31
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-timeout.js","sources":["../../src/use-timeout/use-timeout.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"use-timeout.js","sources":["../../src/use-timeout/use-timeout.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\nexport function useTimeout(\n callback: (...callbackParams: any[]) => void,\n delay: number,\n options: { autoInvoke: boolean } = { autoInvoke: false }\n) {\n const callbackRef = useRef<Function>(null);\n const timeoutRef = useRef<number>(null);\n\n const start = useCallback(\n (...callbackParams: any[]) => {\n if (!timeoutRef.current) {\n timeoutRef.current = window.setTimeout(() => {\n callbackRef.current(callbackParams);\n timeoutRef.current = null;\n }, delay);\n }\n },\n [delay]\n );\n\n const clear = useCallback(() => {\n if (timeoutRef.current) {\n window.clearTimeout(timeoutRef.current);\n timeoutRef.current = null;\n }\n }, []);\n\n useEffect(() => {\n callbackRef.current = callback;\n }, [callback]);\n\n useEffect(() => {\n if (options.autoInvoke) {\n start();\n }\n\n return clear;\n }, [clear, delay, options.autoInvoke, start]);\n\n return { start, clear };\n}\n"],"names":[],"mappings":";;AACO,SAAS,UAAU,CAAC,QAAQ,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,EAAE;AAC7E,EAAE,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;AACnC,EAAE,MAAM,UAAU,GAAG,MAAM,CAAC,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,EAAE,CAAC,GAAG,cAAc,EAAE,GAAG,EAAE,KAAK,IAAI,EAC5C,KAAK,EAAE,MAAM,EACb,OAAO,GAAE;IAAE,UAAU,EAAE,OAAO,CAAA;CAA0B;+
|
|
1
|
+
{"version":3,"file":"use-timeout.d.ts","sourceRoot":"","sources":["../../src/use-timeout/use-timeout.ts"],"names":[],"mappings":"AAEA,wBAAgB,UAAU,CACxB,QAAQ,EAAE,CAAC,GAAG,cAAc,EAAE,GAAG,EAAE,KAAK,IAAI,EAC5C,KAAK,EAAE,MAAM,EACb,OAAO,GAAE;IAAE,UAAU,EAAE,OAAO,CAAA;CAA0B;+BAMlC,GAAG,EAAE;;EA+B5B"}
|