Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add click sound effect #17

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added public/click.wav
Binary file not shown.
2 changes: 1 addition & 1 deletion src/components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Header() {
></Button>
<Button
variant="ghost"
colorScheme="red"
colorScheme="gray"
leftIcon={<SettingsIcon />}
iconSpacing={0}
></Button>
Expand Down
28 changes: 23 additions & 5 deletions src/components/PomodoroActions.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import { Button, Flex } from "@chakra-ui/react";
import { FaPlay, FaPause, FaUndoAlt } from "react-icons/fa";
import { FaPause, FaPlay, FaUndoAlt } from "react-icons/fa";

import useAudio from "../hooks/useAudio";
import useTimer from "../hooks/useTimer";

function PomodoroActions() {
const { handleStart, handleStop, handleRestart } = useTimer();
const { toggle } = useAudio("/click.wav");

const handleClickStart = () => {
toggle();
handleStart();
};

const handleClickRestart = () => {
toggle();
handleRestart();
};

const handleClickStop = () => {
toggle();
handleStop();
};

return (
<Flex justifyContent="center">
Expand All @@ -13,26 +31,26 @@ function PomodoroActions() {
leftIcon={<FaUndoAlt />}
iconSpacing={0}
fontSize="2rem"
onClick={handleRestart}
onClick={handleClickRestart}
></Button>
<Button
variant="ghost"
color="brand.base"
leftIcon={<FaPlay />}
iconSpacing={0}
fontSize="2rem"
onClick={handleStart}
onClick={handleClickStart}
></Button>
<Button
variant="ghost"
color="brand.base"
leftIcon={<FaPause />}
iconSpacing={0}
fontSize="2rem"
onClick={handleStop}
onClick={handleClickStop}
></Button>
</Flex>
);
}

export default PomodoroActions;
export default PomodoroActions;
23 changes: 23 additions & 0 deletions src/hooks/useAudio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useEffect, useState } from "react";

const useAudio = (url) => {
const [audio] = useState(new Audio(url));
const [playing, setPlaying] = useState(false);

const toggle = () => setPlaying(!playing);

useEffect(() => {
playing ? audio.play() : audio.pause();
}, [audio, playing]);

useEffect(() => {
audio.addEventListener("ended", () => setPlaying(false));
return () => {
audio.removeEventListener("ended", () => setPlaying(false));
};
}, [audio]);

return { playing, toggle };
};

export default useAudio;
Loading