Skip to content

Commit

Permalink
hide ControlsBar when video doesn't have focus or mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
joshfarrant committed Dec 17, 2024
1 parent ec28371 commit 52dfae2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
8 changes: 7 additions & 1 deletion packages/react/src/VideoPlayer/VideoPlayer.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,14 @@
left: 0;
width: 100%;
background: var(--brand-videoPlayer-controls-bgColor);
padding: var(--base-size-16) var(--base-size-24);
padding: var(--base-size-12) var(--base-size-16);
pointer-events: all;
opacity: 1;
}

.VideoPlayer__controlsBar--fade {
transition: opacity var(--brand-animation-duration-default) var(--brand-animation-easing-default);
opacity: 0;
}

.VideoPlayer__controls:focus,
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/VideoPlayer/VideoPlayer.module.css.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare const styles: {
readonly "VideoPlayer__controls": string;
readonly "VideoPlayer__controls--hidden": string;
readonly "VideoPlayer__controlsBar": string;
readonly "VideoPlayer__controlsBar--fade": string;
readonly "VideoPlayer__iconControl": string;
readonly "VideoPlayer__tooltip": string;
readonly "VideoPlayer__seek": string;
Expand Down
52 changes: 40 additions & 12 deletions packages/react/src/VideoPlayer/VideoPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useRef, forwardRef, useContext, type HTMLProps, type FunctionComponent} from 'react'
import React, {useEffect, useState, useRef, forwardRef, useContext, type HTMLProps, type FunctionComponent} from 'react'
import clsx from 'clsx'
import {Text} from '../Text'
import {type AnimateProps} from '../animation'
Expand Down Expand Up @@ -63,7 +63,37 @@ const Root = ({
const useVideoContext = useVideo()
const {ccEnabled, isPlaying, ref, togglePlaying} = useVideoContext

const hideControls = !isPlaying && !showControlsWhenPaused
const [hasFocusOrMouse, setHasFocusOrMouse] = useState(false)

useEffect(() => {
const videoWrapper = videoWrapperRef.current

if (!videoWrapper) {
return
}

const showControls = () => {
setHasFocusOrMouse(true)
}

const hideControls = () => {
setHasFocusOrMouse(false)
}

videoWrapper.addEventListener('mousemove', showControls)
videoWrapper.addEventListener('mouseleave', hideControls)
videoWrapper.addEventListener('focusin', showControls)
videoWrapper.addEventListener('focusout', hideControls)

return () => {
videoWrapper.removeEventListener('mousemove', showControls)
videoWrapper.removeEventListener('mouseleave', hideControls)
videoWrapper.removeEventListener('focusin', showControls)
videoWrapper.removeEventListener('focusout', hideControls)
}
}, [videoWrapperRef])

const showControls = hasFocusOrMouse || (showControlsWhenPaused && !isPlaying)

return (
<div className={styles.VideoPlayer__container} ref={videoWrapperRef}>
Expand Down Expand Up @@ -93,16 +123,14 @@ const Root = ({
</button>
<div className={styles.VideoPlayer__controls}>
{ccEnabled && <Captions />}
{!hideControls && (
<ControlsBar>
{showPlayPauseButton && <PlayPauseButton />}
{showSeekControl && <SeekControl />}
{showCCButton && <CCButton />}
{showMuteButton && <MuteButton />}
{showVolumeControl && !isSmall && <VolumeControl />}
{showFullScreenButton && <FullScreenButton />}
</ControlsBar>
)}
<ControlsBar className={clsx(!showControls && styles['VideoPlayer__controlsBar--fade'])}>
{showPlayPauseButton && <PlayPauseButton />}
{showSeekControl && <SeekControl />}
{showCCButton && <CCButton />}
{showMuteButton && <MuteButton />}
{showVolumeControl && !isSmall && <VolumeControl />}
{showFullScreenButton && <FullScreenButton />}
</ControlsBar>
</div>
</div>
)
Expand Down

0 comments on commit 52dfae2

Please sign in to comment.