@trackunit/react-components 0.1.41 → 0.1.43

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 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 [activated, setActivated] = useState(true);
2938
- const timeoutRef = useRef();
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
- setActivated(true);
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
- window.clearTimeout(timeoutRef.current);
2944
- setActivated(false);
2948
+ ready.current = null;
2949
+ timeout.current && clearTimeout(timeout.current);
2945
2950
  }, []);
2951
+ // update ref when function changes
2946
2952
  useEffect(() => {
2947
- if (activated) {
2948
- timeoutRef.current = window.setTimeout(() => {
2949
- onTimeout();
2950
- }, duration);
2951
- }
2952
- return () => {
2953
- stopTimeout();
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,14 +1,14 @@
1
1
  {
2
2
  "name": "@trackunit/react-components",
3
- "version": "0.1.41",
3
+ "version": "0.1.43",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "dependencies": {
7
- "@trackunit/css-core": "0.0.66",
8
- "@trackunit/tailwind-styled-components": "0.0.51",
7
+ "@trackunit/css-core": "0.0.67",
8
+ "@trackunit/tailwind-styled-components": "0.0.52",
9
9
  "react": "18.2.0",
10
- "@trackunit/ui-design-tokens": "0.0.53",
11
- "@trackunit/react-core-contexts-test": "0.1.23",
10
+ "@trackunit/ui-design-tokens": "0.0.54",
11
+ "@trackunit/react-core-contexts-test": "0.1.24",
12
12
  "echarts": "5.4.0",
13
13
  "echarts-for-react": "3.0.2",
14
14
  "uuid": "8.3.2",
@@ -16,7 +16,7 @@
16
16
  "date-fns": "2.29.3",
17
17
  "react-day-picker": "8.3.7",
18
18
  "@floating-ui/react": "0.15.1",
19
- "@trackunit/ui-icons": "0.0.56",
19
+ "@trackunit/ui-icons": "0.0.57",
20
20
  "react-browser-hooks": "2.2.4",
21
21
  "react-useportal": "1.0.17",
22
22
  "react-router-dom": "6.4.5",
@@ -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 {};