@trackunit/react-components 0.1.41 → 0.1.42
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/index.js +23 -22
- package/package.json +1 -1
- package/src/hooks/useTimeout.d.ts +3 -7
package/index.js
CHANGED
|
@@ -2928,35 +2928,36 @@ function tryParse(value) {
|
|
|
2928
2928
|
}
|
|
2929
2929
|
|
|
2930
2930
|
/**
|
|
2931
|
-
* The useTimeout hook.
|
|
2932
|
-
*
|
|
2933
2931
|
* @param {UseTimeoutProps} props The props
|
|
2934
|
-
* @returns {{ startTimeout: () => void; stopTimeout: () => void; }} return
|
|
2932
|
+
* @returns {{ startTimeout: () => void; stopTimeout: () => void; isReady: boolean | null}} return
|
|
2935
2933
|
*/
|
|
2936
2934
|
const useTimeout = ({ onTimeout, duration }) => {
|
|
2937
|
-
const
|
|
2938
|
-
const
|
|
2935
|
+
const ready = useRef(false);
|
|
2936
|
+
const timeout = useRef();
|
|
2937
|
+
const callback = useRef(onTimeout);
|
|
2938
|
+
const isReady = useCallback(() => ready.current, []);
|
|
2939
2939
|
const startTimeout = useCallback(() => {
|
|
2940
|
-
|
|
2941
|
-
|
|
2940
|
+
ready.current = false;
|
|
2941
|
+
timeout.current && clearTimeout(timeout.current);
|
|
2942
|
+
timeout.current = setTimeout(() => {
|
|
2943
|
+
ready.current = true;
|
|
2944
|
+
callback.current();
|
|
2945
|
+
}, duration);
|
|
2946
|
+
}, [duration]);
|
|
2942
2947
|
const stopTimeout = useCallback(() => {
|
|
2943
|
-
|
|
2944
|
-
|
|
2948
|
+
ready.current = null;
|
|
2949
|
+
timeout.current && clearTimeout(timeout.current);
|
|
2945
2950
|
}, []);
|
|
2951
|
+
// update ref when function changes
|
|
2946
2952
|
useEffect(() => {
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
return
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
}, [onTimeout, activated, duration, stopTimeout]);
|
|
2956
|
-
return {
|
|
2957
|
-
startTimeout,
|
|
2958
|
-
stopTimeout,
|
|
2959
|
-
};
|
|
2953
|
+
callback.current = onTimeout;
|
|
2954
|
+
}, [onTimeout]);
|
|
2955
|
+
// startTimeout on mount, stopTimeout on unmount
|
|
2956
|
+
useEffect(() => {
|
|
2957
|
+
startTimeout();
|
|
2958
|
+
return stopTimeout;
|
|
2959
|
+
}, [duration, startTimeout, stopTimeout]);
|
|
2960
|
+
return [isReady, stopTimeout, startTimeout];
|
|
2960
2961
|
};
|
|
2961
2962
|
|
|
2962
2963
|
/**
|
package/package.json
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
|
+
export type UseTimeoutReturn = [() => boolean | null, () => void, () => void];
|
|
1
2
|
interface UseTimeoutProps {
|
|
2
3
|
onTimeout: () => void;
|
|
3
4
|
duration: number;
|
|
4
5
|
}
|
|
5
6
|
/**
|
|
6
|
-
* The useTimeout hook.
|
|
7
|
-
*
|
|
8
7
|
* @param {UseTimeoutProps} props The props
|
|
9
|
-
* @returns {{ startTimeout: () => void; stopTimeout: () => void; }} return
|
|
8
|
+
* @returns {{ startTimeout: () => void; stopTimeout: () => void; isReady: boolean | null}} return
|
|
10
9
|
*/
|
|
11
|
-
export declare const useTimeout: ({ onTimeout, duration }: UseTimeoutProps) =>
|
|
12
|
-
startTimeout: () => void;
|
|
13
|
-
stopTimeout: () => void;
|
|
14
|
-
};
|
|
10
|
+
export declare const useTimeout: ({ onTimeout, duration }: UseTimeoutProps) => UseTimeoutReturn;
|
|
15
11
|
export {};
|