Skip to content

Commit 7aeb162

Browse files
committed
hide controls on inactivity
1 parent 9b96b0f commit 7aeb162

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

.changeset/hot-laws-hug.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
- Improved contrast of play overlay focus styles.
88
- Improved contrast of controls and title.
99
- The title bar now hides while the video is playing.
10+
- The controls bar now hides when the cursor or keyboard focus leaves the video player, or after a few seconds of inactivity, and reappears when the cursor or keyboard focus returns.

packages/react/src/VideoPlayer/VideoPlayer.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,29 @@ const Root = ({
6363
const useVideoContext = useVideo()
6464
const {ccEnabled, isPlaying, ref, togglePlaying} = useVideoContext
6565

66-
const [hasFocusOrMouse, setHasFocusOrMouse] = useState(false)
66+
const [isInteracting, setIsInteracting] = useState(false)
6767

6868
useEffect(() => {
6969
const videoWrapper = videoWrapperRef.current
70+
let hideControlsTimeout: NodeJS.Timeout
71+
const inactivityTimeout = 3000
7072

7173
if (!videoWrapper) {
7274
return
7375
}
7476

7577
const showControls = () => {
76-
setHasFocusOrMouse(true)
78+
setIsInteracting(true)
79+
80+
clearTimeout(hideControlsTimeout)
81+
82+
hideControlsTimeout = setTimeout(() => {
83+
setIsInteracting(false)
84+
}, inactivityTimeout)
7785
}
7886

7987
const hideControls = () => {
80-
setHasFocusOrMouse(false)
88+
setIsInteracting(false)
8189
}
8290

8391
videoWrapper.addEventListener('mousemove', showControls)
@@ -90,10 +98,12 @@ const Root = ({
9098
videoWrapper.removeEventListener('mouseleave', hideControls)
9199
videoWrapper.removeEventListener('focusin', showControls)
92100
videoWrapper.removeEventListener('focusout', hideControls)
101+
102+
clearTimeout(hideControlsTimeout)
93103
}
94104
}, [videoWrapperRef])
95105

96-
const showControls = hasFocusOrMouse || (showControlsWhenPaused && !isPlaying)
106+
const showControls = isInteracting || (showControlsWhenPaused && !isPlaying)
97107

98108
return (
99109
<div className={styles.VideoPlayer__container} ref={videoWrapperRef}>

0 commit comments

Comments
 (0)