-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackgroundVideo.js
77 lines (69 loc) · 1.88 KB
/
BackgroundVideo.js
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
import React, { Component, Fragment } from 'react'
import ReactDOM from 'react-dom'
import Image from './Image'
import './BackgroundVideo.css'
class BackgroundVideo extends Component {
constructor(props) {
super(props)
this.ref = React.createRef()
}
state = {
playing: false,
mobileWidth: false
}
updateDimensions() {
this.setState({ mobileWidth: window.innerWidth <= 900 })
}
handelPlay() {
this.setState({ playing: true })
ReactDOM.findDOMNode(this.ref.current).removeEventListener(
'playing',
this.handelPlay
)
}
componentDidMount() {
this.updateDimensions()
window.addEventListener('resize', () => this.updateDimensions())
ReactDOM.findDOMNode(this.ref.current).addEventListener('playing', e =>
this.handelPlay(e)
)
}
componentWillUnmount() {
window.removeEventListener('resize', this.updateDimensions)
}
render() {
const { poster, videoTitle, children } = this.props
return (
<Fragment>
{!this.state.mobileWidth && (
<div className={`BackgroundVideo`}>
<video
ref={this.ref}
poster={poster}
className={`BackgroundVideo--video ${
this.state.playing ? 'playing' : ''
} `}
playsInline
autoPlay
muted
loop
preload="auto"
>
{children}
</video>
{videoTitle && (
<div className="BackgroundVideo--videoTitle">{videoTitle}</div>
)}
</div>
)}
{this.state.mobileWidth && (
<Fragment>
<Image background src={poster} alt="Background poster" />
{videoTitle && <h3 className="Poster--videoTitle">{videoTitle}</h3>}
</Fragment>
)}
</Fragment>
)
}
}
export default BackgroundVideo