/
home-video.component.jsx
108 lines (86 loc) · 2.35 KB
/
home-video.component.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* eslint-disable jsx-a11y/media-has-caption */
import React from 'react';
import PropTypes from 'prop-types';
import Plyr from 'plyr/dist/plyr.polyfilled.min';
import './home-video.scss';
class HomeVideo extends React.PureComponent {
constructor(props) {
super(props);
this.videoReady = false;
this.playVideoWhenReady = false;
this.getVideoRef = this.getVideoRef.bind(this);
this.onEnded = this.onEnded.bind(this);
this.onExitFullScreen = this.onExitFullScreen.bind(this);
this.onEnterFullScreen = this.onEnterFullScreen.bind(this);
this.onReady = this.onReady.bind(this);
}
componentDidMount() {
this.setupPlyr();
this.setupSource(this.props.videoId);
}
componentDidUpdate(prevProps) {
if (prevProps.videoId !== this.props.videoId && this.plyr) {
this.setupSource(this.props.videoId);
}
}
componentWillUnmount() {
this.destroyPlyr();
}
onExitFullScreen() {
this.plyr.pause();
this.plyr.elements.container.classList.remove('-is-fullscreen');
}
onEnterFullScreen() {
this.plyr.elements.container.classList.add('-is-fullscreen');
}
onEnded() {
this.plyr.fullscreen.exit();
}
onReady() {
this.videoReady = true;
}
getVideoRef(el) {
this.videoElement = el;
}
setupPlyr() {
this.plyr = new Plyr(this.videoElement);
this.addEventListeners();
}
setupSource(videoId) {
this.plyr.source = {
type: 'video',
sources: [
{
src: videoId,
provider: 'youtube'
}
]
};
this.plyr.config.youtube.noCookie = true;
}
addEventListeners() {
this.plyr.on('ended', this.onEnded);
this.plyr.on('exitfullscreen', this.onExitFullScreen);
this.plyr.on('enterfullscreen', this.onEnterFullScreen);
this.plyr.on('ready', this.onReady);
}
destroyPlyr() {
if (this.plyr) this.plyr.destroy();
}
play() {
// we cannot schedule firing video later because initiating fullscreen won't work
// as it must be initiated by user gesture, that's why can only play video manually
// when it's ready
if (this.videoReady) {
this.plyr.play();
this.plyr.fullscreen.enter();
}
}
render() {
return <video ref={this.getVideoRef} controls crossOrigin="true" playsInline />;
}
}
HomeVideo.propTypes = {
videoId: PropTypes.string.isRequired
};
export default HomeVideo;