Skip to content

Commit

Permalink
chore: release v2.2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
childrentime committed Mar 30, 2023
1 parent 3435fc7 commit 78fba64
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/core/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,9 @@
* fix bug in useDarkMode
* delete option ignoreDefault in useLocalStorage and useSessionStorage
* add option csrData in useLocalStorage and useSessionStorage

### 2.2.4(March 30, 2023)

### Core

* add useCountDown Hook
3 changes: 3 additions & 0 deletions packages/core/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import useReducedMotion from "./useReduceMotion";
import useScrollIntoView from "./useScrollIntoView";
import useSticky from "./useSticky";
import useAsyncEffect from "./useAsyncEffect";
import useCountDown from "./useCountDown";

export {
usePrevious,
Expand Down Expand Up @@ -162,6 +163,7 @@ export {
useScrollIntoView,
useSticky,
useAsyncEffect,
useCountDown,
};

export * from "./useToggle";
Expand Down Expand Up @@ -245,3 +247,4 @@ export * from "./useReduceMotion";
export * from "./useScrollIntoView";
export * from "./useSticky";
export * from "./useAsyncEffect";
export * from "./useCountDown";
48 changes: 48 additions & 0 deletions packages/core/hooks/useCountDown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useEffect, useState } from "react";
import useInterval from "./useInterval";

const padZero = (time: number): string => {
return `${time}`.length < 2 ? `0${time}` : `${time}`;
};

export const getHMSTime = (timeDiff: number): [string, string, string] => {
if (timeDiff <= 0) {
return ["00", "00", "00"];
}
if (timeDiff > 100 * 3600) {
return ["99", "59", "59"];
}
const hour = Math.floor(timeDiff / 3600);
const minute = Math.floor((timeDiff - hour * 3600) / 60);
const second = timeDiff - hour * 3600 - minute * 60;
return [padZero(hour), padZero(minute), padZero(second)];
};

const useCountDown = (
time: number,
format: (number) => [string, string, string] = getHMSTime,
callback?: () => void
) => {
const [remainTime, setRemainTime] = useState(time);
const [delay, setDelay] = useState<number | null>(1000);

useInterval(() => {
if (remainTime <= 0) {
setDelay(null);
return;
}
setRemainTime(remainTime - 1);
}, delay);

useEffect(() => {
if (time > 0 && remainTime <= 0) {
callback && callback();
}
}, [callback, remainTime, time]);

const [hour, minute, secoud] = format(remainTime);

return [hour, minute, secoud] as const;
};

export default useCountDown;
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reactuses/core",
"version": "2.2.3",
"version": "2.2.4",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
Expand Down

0 comments on commit 78fba64

Please sign in to comment.