forked from DistinctCodes/AssetsUp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountdownTimer.tsx
More file actions
43 lines (37 loc) · 1.32 KB
/
Copy pathCountdownTimer.tsx
File metadata and controls
43 lines (37 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { useEffect, useState } from 'react';
interface Countdown {
days: number;
hours: number;
minutes: number;
seconds: number;
}
const CountdownTimer = ({ targetDate }: { targetDate: string }) => {
const calculateTimeLeft = (): Countdown => {
const difference = +new Date(targetDate) - +new Date();
const timeLeft: Countdown = {
days: Math.floor(difference / (1000 * 60 * 60 * 24)),
hours: Math.floor((difference / (1000 * 60 * 60)) % 24),
minutes: Math.floor((difference / 1000 / 60) % 60),
seconds: Math.floor((difference / 1000) % 60),
};
return timeLeft;
};
const [timeLeft, setTimeLeft] = useState<Countdown>(calculateTimeLeft());
useEffect(() => {
const timer = setInterval(() => {
setTimeLeft(calculateTimeLeft());
}, 1000);
return () => clearInterval(timer);
}, [targetDate]);
return (
<div className="bg-white/10 backdrop-blur-md p-6 rounded-xl shadow-md text-center text-white space-x-4 flex justify-center glass">
{Object.entries(timeLeft).map(([unit, value]) => (
<div key={unit} className="flex flex-col items-center">
<span className="text-3xl font-bold">{value}</span>
<span className="text-sm uppercase tracking-wide">{unit}</span>
</div>
))}
</div>
);
};
export default CountdownTimer;