-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(player): implement volume adjusting feature (#32)
- Loading branch information
Showing
4 changed files
with
150 additions
and
0 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
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,34 @@ | ||
import styled from "@emotion/styled"; | ||
|
||
export const Control = styled.div` | ||
max-width: 0; | ||
width: ${({ theme }) => theme.spacing(10)}; | ||
margin-right: ${({ theme }) => theme.spacing(1.5)}; | ||
box-sizing: border-box; | ||
opacity: 0; | ||
transition: ${({ theme }) => | ||
theme.transitions.create(["opacity"], { | ||
duration: theme.transitions.duration.shortest, | ||
})}; | ||
&:has(:active) { | ||
opacity: 1; | ||
max-width: ${({ theme }) => theme.spacing(10)}; | ||
} | ||
`; | ||
|
||
export const Root = styled.div` | ||
display: flex; | ||
align-items: center; | ||
&:hover { | ||
${Control} { | ||
max-width: ${({ theme }) => theme.spacing(10)}; | ||
opacity: 1; | ||
} | ||
} | ||
`; |
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,73 @@ | ||
import React from "react"; | ||
|
||
import { Slider } from "ui"; | ||
|
||
import { IconButton } from "@mui/material"; | ||
import VolumeUpRoundedIcon from "@mui/icons-material/VolumeUpRounded"; | ||
import VolumeOffIcon from "@mui/icons-material/VolumeOff"; | ||
|
||
import { Control, Root } from "@components/VolumeControl.styles"; | ||
import { usePlayer } from "@components/Player/context"; | ||
|
||
export function VolumeControl() { | ||
const player = usePlayer(); | ||
const [muted, setMuted] = React.useState(player.muted); | ||
const [volume, setVolume] = React.useState(player.volume); | ||
|
||
React.useEffect(() => { | ||
setVolume(player.volume); | ||
}, [player.volume]); | ||
|
||
const handleChange = React.useCallback( | ||
(value: number) => { | ||
player.setVolume(value); | ||
setVolume(value); | ||
|
||
if (value === 0) { | ||
setMuted(true); | ||
} else if (muted) { | ||
setMuted(false); | ||
} | ||
}, | ||
[player, muted], | ||
); | ||
const handleChangeEnd = React.useCallback( | ||
(value: number) => { | ||
player.setVolume(value, true); | ||
setVolume(value); | ||
|
||
if (value === 0) { | ||
setMuted(true); | ||
} else if (muted) { | ||
setMuted(false); | ||
} | ||
}, | ||
[player, muted], | ||
); | ||
|
||
const handleClick = React.useCallback(() => { | ||
const isMuted = player.muted; | ||
|
||
player.setMuted(!isMuted); | ||
setMuted(!isMuted); | ||
setVolume(isMuted ? player.volume : 0); | ||
}, [player]); | ||
|
||
return ( | ||
<Root> | ||
<Control> | ||
<Slider | ||
value={muted ? 0 : volume} | ||
min={0} | ||
max={1} | ||
onValueChange={handleChange} | ||
onValueChangeEnd={handleChangeEnd} | ||
/> | ||
</Control> | ||
<IconButton size="small" onClick={handleClick}> | ||
{!muted && <VolumeUpRoundedIcon fontSize="small" />} | ||
{muted && <VolumeOffIcon fontSize="small" />} | ||
</IconButton> | ||
</Root> | ||
); | ||
} |