Skip to content

Commit 63f554a

Browse files
committed
feat: listen shortcut keys on root element
1 parent d7a6924 commit 63f554a

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

packages/griffith/src/components/Player.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import useBoolean from '../hooks/useBoolean'
4848
import useMount from '../hooks/useMount'
4949
import useHandler from '../hooks/useHandler'
5050
import usePlayerShortcuts from './usePlayerShortcuts'
51+
5152
const CONTROLLER_HIDE_DELAY = 3000
5253

5354
// 被 Provider 包装后的属性
@@ -138,7 +139,7 @@ const InnerPlayer: React.FC<InnerPlayerProps> = ({
138139
}) => {
139140
const {emitEvent, subscribeAction} = useContext(InternalMessageContext)
140141
const {currentSrc} = useContext(VideoSourceContext)
141-
const rootRef = useRef<HTMLDivElement>(null)
142+
const [root, setRoot] = useState<HTMLDivElement | null>(null)
142143
const videoRef = useRef<{
143144
root: HTMLVideoElement
144145
seek(currentTime: number): void
@@ -368,7 +369,7 @@ const InnerPlayer: React.FC<InnerPlayerProps> = ({
368369
const onExit = () => {
369370
return emitEvent(EVENTS.EXIT_FULLSCREEN)
370371
}
371-
BigScreen?.toggle(rootRef.current!, onEnter, onExit)
372+
BigScreen?.toggle(root!, onEnter, onExit)
372373
}
373374
})
374375

@@ -463,6 +464,7 @@ const InnerPlayer: React.FC<InnerPlayerProps> = ({
463464
})
464465

465466
usePlayerShortcuts({
467+
root,
466468
prevVolumeRef,
467469
isPlaying,
468470
volume,
@@ -603,7 +605,9 @@ const InnerPlayer: React.FC<InnerPlayerProps> = ({
603605
onMouseDown={handleMouseDown}
604606
onMouseUp={handleMouseUp}
605607
onMouseMove={handleShowController}
606-
ref={rootRef}
608+
ref={setRoot}
609+
tabIndex={-1}
610+
aria-label={title}
607611
>
608612
<div className={css(styles.video)}>
609613
<Video

packages/griffith/src/components/usePlayerShortcuts.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {useActionToastDispatch} from './ActionToast'
77
import VideoSourceContext from '../contexts/VideoSourceContext'
88

99
type Options = {
10+
root: HTMLDivElement | null
1011
prevVolumeRef: React.MutableRefObject<number>
1112
isPlaying: boolean
1213
isPageFullScreen: boolean
@@ -22,6 +23,7 @@ type Options = {
2223
}
2324

2425
const usePlayerShortcuts = ({
26+
root,
2527
prevVolumeRef,
2628
isPlaying,
2729
isPageFullScreen,
@@ -156,13 +158,14 @@ const usePlayerShortcuts = ({
156158
})
157159

158160
useEffect(() => {
159-
if (standalone) {
160-
document.addEventListener('keydown', handleKeyDown)
161+
const el = standalone ? document.body : root
162+
if (el) {
163+
el.addEventListener('keydown', handleKeyDown)
161164
return () => {
162-
document.removeEventListener('keydown', handleKeyDown)
165+
el.removeEventListener('keydown', handleKeyDown)
163166
}
164167
}
165-
}, [handleKeyDown, standalone])
168+
}, [handleKeyDown, root, standalone])
166169
}
167170

168171
export default usePlayerShortcuts

0 commit comments

Comments
 (0)