Skip to content

Commit

Permalink
fix(vidstack): avoid conflicting keys with native media element
Browse files Browse the repository at this point in the history
closes #788
  • Loading branch information
mihar-22 committed Mar 20, 2023
1 parent df97a0d commit 480dced
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/vidstack/src/player/element/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CustomElementHost, onAttach } from 'maverick.js/element';
import { DOMEvent, isKeyboardClick, listenEvent } from 'maverick.js/std';

import { MediaContext, useMedia } from '../media/context';
import { isHTMLMediaElement } from '../media/providers/type-check';
import type { MediaKeyShortcuts } from '../media/types';
import type { MediaPlayerProps } from './types';

Expand All @@ -19,7 +20,7 @@ export const MEDIA_KEY_SHORTCUTS: MediaKeyShortcuts = {

const MODIFIER_KEYS = new Set(['Shift', 'Alt', 'Meta', 'Control']),
BUTTON_SELECTORS = 'button, [role="button"]',
IGNORE_SELECTORS = 'video, input, textarea, select, [contenteditable], [role^="menuitem"]';
IGNORE_SELECTORS = 'input, textarea, select, [contenteditable], [role^="menuitem"]';

export function useKeyboard(
{ $player, $store: $media, ariaKeys, remote }: MediaContext,
Expand All @@ -46,10 +47,17 @@ export function useKeyboard(
});
}

function onPreventVideoKeys(event: KeyboardEvent) {
if (isHTMLMediaElement(event.target) && getMatchingMethod(event)) {
event.preventDefault();
}
}

effect(() => {
if (!$active()) return;
listenEvent(target, 'keyup', onKeyUp);
listenEvent(target, 'keydown', onKeyDown);
listenEvent(target, 'keydown', onPreventVideoKeys, { capture: true });
});

let seekTotal;
Expand Down Expand Up @@ -91,6 +99,7 @@ export function useKeyboard(

if (method?.startsWith('seek')) {
event.preventDefault();
event.stopPropagation();
if (timeSlider) {
forwardTimeKeyEvent(event);
timeSlider = null;
Expand Down Expand Up @@ -123,13 +132,15 @@ export function useKeyboard(

if (!method && /[0-9]/.test(event.key) && !sliderFocused) {
event.preventDefault();
event.stopPropagation();
remote.seek(($media.duration / 10) * Number(event.key), event);
return;
}

if (!method || (/volume|seek/.test(method) && sliderFocused)) return;

event.preventDefault();
event.stopPropagation();

switch (method) {
case 'seekForward':
Expand Down
5 changes: 5 additions & 0 deletions packages/vidstack/src/player/media/providers/type-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ export function isHTMLAudioElement(element: unknown): element is HTMLAudioElemen
export function isHTMLVideoElement(element: unknown): element is HTMLVideoElement {
return !__SERVER__ && element instanceof HTMLVideoElement;
}

/** @see {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement} */
export function isHTMLMediaElement(element: unknown): element is HTMLMediaElement {
return isHTMLAudioElement(element) || isHTMLVideoElement(element);
}

0 comments on commit 480dced

Please sign in to comment.