Skip to content

Commit

Permalink
timer countdown creating weird behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
UmungoBungo committed Oct 7, 2024
1 parent 08483cc commit 1bd8a53
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/docs/src/components/Demo/PlayerVolume.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const PlayerVolume: React.FC<{
}> = ({playerRef, updateAudioMute, updateAudioVolume, audioState}) => {
const containerRef = useRef<HTMLDivElement>(null);
const [isHovered, setIsHovered] = useState(false);
const [isDragging, setIsDragging] = useState(false);
const timerRef = useRef<Timer | null>(null);

useEffect(() => {
const {current} = playerRef;
Expand Down Expand Up @@ -42,15 +44,18 @@ export const PlayerVolume: React.FC<{
}, [isHovered]);

const onClick = useCallback(() => {
if (timerRef.current !== null) {
clearTimeout(timerRef.current);
timerRef.current = null;
}

if (audioState.isMuted) {
playerRef.current.unmute();
} else {
playerRef.current.mute();
}
}, [audioState, playerRef]);

const [isDragging, setIsDragging] = useState(false);

const calculatePosition = useCallback((clientY: number) => {
const container = containerRef.current?.getBoundingClientRect();
if (container) {
Expand All @@ -65,6 +70,11 @@ export const PlayerVolume: React.FC<{
const handlePointerDown = useCallback(
(e: React.PointerEvent<HTMLDivElement>) => {
if (e.button !== 0) return; // Only respond to left mouse button
if (timerRef.current !== null) {
clearTimeout(timerRef.current);
timerRef.current = null;
}

setIsDragging(true);
const position = calculatePosition(e.clientY);
if (position === null) return;
Expand Down Expand Up @@ -97,7 +107,7 @@ export const PlayerVolume: React.FC<{

const handlePointerUp = useCallback(() => {
setIsDragging(false);
setTimeout(() => {
timerRef.current = setTimeout(() => {
setIsHovered(false);
}, 1000);
}, []);
Expand Down

0 comments on commit 1bd8a53

Please sign in to comment.