-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create countdown component
- Loading branch information
Showing
8 changed files
with
144 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
.countdown { | ||
position: relative; | ||
display: inline-block; | ||
} | ||
|
||
.countdown_button { | ||
background-color: #4caf50; | ||
color: white; | ||
border: none; | ||
padding: 15px 32px; | ||
text-align: center; | ||
font-size: 16px; | ||
cursor: pointer; | ||
} | ||
|
||
.countdown_blur { | ||
filter: blur(5px); | ||
background-color: rgba(33, 33, 33, 0.6); | ||
text-align: center; | ||
font-size: 20px; | ||
display: inline-block; | ||
z-index: 1000; | ||
} | ||
|
||
.countdown_time { | ||
position: absolute; | ||
width: 100%; | ||
height: 100%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
white-space: nowrap; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
font-size: calc(6px + 30%); | ||
z-index: 5; | ||
color: ivory; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
||
import { Button } from '~/Components/Button'; | ||
import { Countdown } from './Countdown'; | ||
|
||
export default { | ||
title: 'Components/Countdown', | ||
component: Countdown, | ||
} as ComponentMeta<typeof Countdown>; | ||
|
||
const Template: ComponentStory<typeof Countdown> = function (args) { | ||
return ( | ||
<Countdown {...args}> | ||
<Button theme="green">he he he haw, Guilty</Button> | ||
</Countdown> | ||
); | ||
}; | ||
|
||
export const Basic = Template.bind({}); | ||
Basic.args = { | ||
targetDate: new Date(new Date(new Date().getTime() + 60 * 60 * 24 * 1000)), | ||
}; | ||
|
||
export const Short = Template.bind({}); | ||
Short.args = { | ||
targetDate: new Date(new Date(new Date().getTime() + 60 * 1000 * 5)), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import classNames from 'classnames'; | ||
import { SECOND_MILLIS } from '~/constants'; | ||
import { Children } from '~/types'; | ||
import styles from './Countdown.module.scss'; | ||
import { calculateTimeLeft, hasReachedTargetDate } from './utils'; | ||
|
||
type CountdownProps = { | ||
targetDate: Date; | ||
children?: Children; | ||
}; | ||
|
||
export function Countdown({ targetDate, children }: CountdownProps) { | ||
const [timeLeft, setTimeLeft] = useState<string>(''); | ||
const [isTimeOut, setIsTimeOut] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
const updateTimer = setInterval(() => { | ||
const newTimeLeft = calculateTimeLeft(targetDate); | ||
setTimeLeft(newTimeLeft); | ||
if (hasReachedTargetDate(targetDate)) setIsTimeOut(true); | ||
}, SECOND_MILLIS); | ||
|
||
return () => clearInterval(updateTimer); | ||
}, [targetDate]); | ||
|
||
if (isTimeOut) return <>{children}</>; | ||
|
||
return ( | ||
<div className={classNames(styles.countdown)}> | ||
<div className={styles.countdown_time}>{timeLeft}</div> | ||
<div className={styles.countdown_blur}>{children}</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Countdown } from './Countdown'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { DAY_MILLIS, HOUR_MILLIS, MINUTE_MILLIS, SECOND_MILLIS } from '~/constants'; | ||
|
||
export function calculateTimeLeft(targetDate: Date): string { | ||
const now = new Date(); | ||
const difference = targetDate.getTime() - now.getTime(); | ||
|
||
if (difference <= 0) return '0m 0s'; | ||
|
||
const days = Math.floor(difference / DAY_MILLIS); | ||
const hours = Math.floor((difference % DAY_MILLIS) / HOUR_MILLIS); | ||
const minutes = Math.floor((difference % HOUR_MILLIS) / MINUTE_MILLIS); | ||
const seconds = Math.floor((difference % MINUTE_MILLIS) / SECOND_MILLIS); | ||
|
||
if (days > 0) return `${days}d ${hours}h`; | ||
if (difference < 60 * SECOND_MILLIS) return `${seconds}s`; | ||
if (difference < 10 * MINUTE_MILLIS) return `${minutes}m ${seconds}s`; | ||
if (difference < 60 * MINUTE_MILLIS) return `${minutes}m`; | ||
return `${hours}h ${minutes}m`; | ||
} | ||
|
||
export function hasReachedTargetDate(targetDate: Date): boolean { | ||
const now = new Date(); | ||
return now.getTime() >= targetDate.getTime(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters