@purpurds/countdown 0.0.1

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.
@@ -0,0 +1,67 @@
1
+ import React, { useCallback, useEffect, useRef, useState } from "react";
2
+ import { Paragraph } from "@purpurds/paragraph";
3
+ import c from "classnames/bind";
4
+
5
+ import { type CounterLabels } from "./countdown.types";
6
+ import styles from "./counter.module.scss";
7
+ import { getDiffs, getNext, inMs } from "./utils";
8
+
9
+ const cx = c.bind(styles);
10
+
11
+ const rootClassName = "purpur-countdown-counter";
12
+
13
+ interface CounterProps {
14
+ end: number;
15
+ size: "md" | "lg";
16
+ tag: "days" | "hours" | "minutes" | "seconds";
17
+ counterLabels: CounterLabels;
18
+ }
19
+
20
+ const intervals: Map<string, ReturnType<typeof setInterval>> = new Map();
21
+
22
+ export const Counter = ({ tag, size, end, counterLabels }: CounterProps) => {
23
+ const ref = useRef<HTMLHeadingElement>(null);
24
+ const id = `${tag}-interval`;
25
+
26
+ const [currentTime, setCurrentTime] = useState(getDiffs(end)[tag]);
27
+ const [nextTime, setNextTime] = useState(getNext(currentTime, tag));
28
+ const [animationKey, setAnimationKey] = useState(0);
29
+
30
+ const checktime = useCallback(() => {
31
+ const diffs = getDiffs(end);
32
+ const next = getNext(diffs[tag], tag);
33
+
34
+ setNextTime(next);
35
+ setCurrentTime(diffs[tag]);
36
+ }, [end, tag]);
37
+
38
+ useEffect(() => {
39
+ intervals.set(id, setInterval(checktime, inMs.second));
40
+ return () => clearInterval(intervals.get(id)!);
41
+ }, [checktime, id]);
42
+
43
+ useEffect(() => {
44
+ setAnimationKey((prev) => prev + 1);
45
+ }, [currentTime]);
46
+
47
+ const counterTag = counterLabels[tag];
48
+
49
+ return (
50
+ <div
51
+ className={cx(rootClassName)}
52
+ role="group"
53
+ aria-hidden="true"
54
+ data-testid={`counter-${tag}`}
55
+ >
56
+ <Paragraph
57
+ className={cx(`${rootClassName}__number`, `${rootClassName}--size-${size}`)}
58
+ ref={ref}
59
+ data-tick={nextTime}
60
+ key={animationKey}
61
+ >
62
+ {currentTime}
63
+ </Paragraph>
64
+ <Paragraph className={cx(`${rootClassName}__label`)}>{counterTag}</Paragraph>
65
+ </div>
66
+ );
67
+ };
@@ -0,0 +1,4 @@
1
+ declare module "*.scss" {
2
+ const styles: { [className: string]: string };
3
+ export default styles;
4
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,45 @@
1
+ export function getDiffs(endtime: number) {
2
+ const now = new Date().getTime();
3
+ const diffInMs = Math.abs(endtime - now);
4
+
5
+ let seconds = 0,
6
+ minutes = 0,
7
+ hours = 0,
8
+ days = 0;
9
+
10
+ if (diffInMs >= inMs.day) {
11
+ days = Math.floor(diffInMs / inMs.day);
12
+ }
13
+ if (diffInMs >= inMs.hour) {
14
+ hours = Math.floor((diffInMs % inMs.day) / inMs.hour);
15
+ }
16
+ if (diffInMs >= inMs.minute) {
17
+ minutes = Math.floor((diffInMs % inMs.hour) / inMs.minute);
18
+ }
19
+ if (diffInMs >= inMs.second) {
20
+ seconds = Math.floor((diffInMs % inMs.minute) / inMs.second);
21
+ }
22
+
23
+ return {
24
+ seconds,
25
+ minutes,
26
+ hours,
27
+ days,
28
+ };
29
+ }
30
+
31
+ export function getNext(time: number, tag: string) {
32
+ let r = 0;
33
+ if (tag === "seconds") r = time - 1 >= 0 ? time - 1 : 59;
34
+ if (tag === "minutes") r = time - 1 >= 0 ? time - 1 : 59;
35
+ if (tag === "hours") r = time - 1 >= 0 ? time - 1 : 23;
36
+ if (tag === "days") r = time - 1 >= 0 ? time - 1 : 0;
37
+ return r;
38
+ }
39
+
40
+ export const inMs = {
41
+ day: 24 * 60 * 60 * 1000,
42
+ hour: 60 * 60 * 1000,
43
+ minute: 60 * 1000,
44
+ second: 1000,
45
+ };