-
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.
- Loading branch information
1 parent
4a42b79
commit 88c00cb
Showing
9 changed files
with
164 additions
and
7 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,43 @@ | ||
import React, { useState } from 'react' | ||
import useInterval from '../hooks/useInterval' | ||
|
||
type Props = { | ||
time: number | ||
onFinish: () => void | ||
} | ||
|
||
const Countdown = ({ time, onFinish }: Props) => { | ||
const [translateLeft, setTranslateLeft] = useState(100) | ||
const [finished, setFinished] = useState(false) | ||
const step = 100 / time | ||
|
||
useInterval( | ||
() => { | ||
let nextTranslateLeft = Math.round(translateLeft - step) | ||
|
||
if (nextTranslateLeft < 0) { | ||
nextTranslateLeft = 0 | ||
|
||
onFinish() | ||
setFinished(true) | ||
} | ||
|
||
setTranslateLeft(nextTranslateLeft) | ||
}, | ||
1000, | ||
!finished, | ||
) | ||
|
||
return ( | ||
<div className="bg-white h-4 relative shadow overflow-hidden"> | ||
<div | ||
className="absolute bg-red-500 left-0 bottom-0 top-0 w-full h-full transition-transform duration-1000 ease-linear" | ||
style={{ | ||
transform: `translateX(-${translateLeft}%)`, | ||
}} | ||
></div> | ||
</div> | ||
) | ||
} | ||
|
||
export default 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
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,32 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
const useAudio = (url: string, volume = 0.5) => { | ||
const sound = new Audio(url) | ||
sound.volume = volume | ||
|
||
const [audio] = useState(sound) | ||
const [playing, setPlaying] = useState(false) | ||
|
||
const toggle = () => setPlaying(!playing) | ||
|
||
useEffect(() => { | ||
if (playing) { | ||
audio.play() | ||
} else { | ||
audio.currentTime = 0 | ||
audio.pause() | ||
} | ||
}, [playing]) | ||
|
||
useEffect(() => { | ||
audio.addEventListener('ended', () => setPlaying(false)) | ||
|
||
return () => { | ||
audio.removeEventListener('ended', () => setPlaying(false)) | ||
} | ||
}, []) | ||
|
||
return { playing, toggle } | ||
} | ||
|
||
export default useAudio |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useEffect, useRef } from 'react' | ||
|
||
export default function useInterval( | ||
callback: () => void, | ||
delay: number, | ||
inRunning: boolean, | ||
) { | ||
const savedCallback = useRef<() => void>() | ||
|
||
useEffect(() => { | ||
savedCallback.current = callback | ||
}) | ||
|
||
useEffect(() => { | ||
function tick() { | ||
if (savedCallback.current) { | ||
savedCallback.current() | ||
} | ||
} | ||
|
||
if (inRunning) { | ||
const id = setInterval(tick, delay) | ||
|
||
return () => clearInterval(id) | ||
} | ||
}, [inRunning]) | ||
} |
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
88c00cb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: