Skip to content

Commit

Permalink
fix: working with youtube player api (#815)
Browse files Browse the repository at this point in the history
* fix: working with youtube player api
  • Loading branch information
Kyzyl-ool authored Feb 14, 2024
1 parent 52890dd commit a5267eb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"@types/sanitize-html": "2.6.1",
"@types/uuid": "^9.0.0",
"@types/webpack-env": "^1.18.1",
"@types/youtube-player": "^5.5.11",
"autoprefixer": "^10.4.14",
"babel-loader": "^8.3.0",
"css-loader": "^5.2.7",
Expand Down
11 changes: 9 additions & 2 deletions src/components/ReactPlayer/ReactPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {block} from '../../utils';
import CustomBarControls from './CustomBarControls';
import i18n from './i18n';
import {checkYoutubeVideos} from './utils';
import {isYoutubePlayerInstance} from './utils/youtube';

import './ReactPlayer.scss';

Expand Down Expand Up @@ -137,8 +138,14 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
return;
}

const videoInstance = playerRef.getInternalPlayer() as HTMLVideoElement;
const {play, pause, addEventListener} = videoInstance;
let play, pause, addEventListener;
const videoInstance = playerRef.getInternalPlayer();
if (isYoutubePlayerInstance(videoInstance)) {
({pauseVideo: pause, playVideo: play, addEventListener} = videoInstance);
} else {
// it is assumed that `videoInstance` is HTMLVideoElement by default
({play, pause, addEventListener} = videoInstance);
}

// eslint-disable-next-line consistent-return
return {
Expand Down
7 changes: 7 additions & 0 deletions src/components/ReactPlayer/utils/youtube.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type {YouTubePlayer} from 'youtube-player/dist/types';

export const isYoutubePlayerInstance = (
playerInstance: Record<string, unknown> | YouTubePlayer,
): playerInstance is YouTubePlayer => {
return Boolean(playerInstance['pauseVideo'] && playerInstance['playVideo']);
};

0 comments on commit a5267eb

Please sign in to comment.