-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathvideo.js
More file actions
52 lines (46 loc) · 1.38 KB
/
video.js
File metadata and controls
52 lines (46 loc) · 1.38 KB
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
import { useRef, useCallback, useEffect } from 'react'
import { useInView } from 'react-intersection-observer'
import 'intersection-observer'
export default ({ src, caption, ratio }) => {
const [inViewRef, inView] = useInView({
threshold: 1,
})
const videoRef = useRef()
const setRefs = useCallback(
(node) => {
// Ref's from useRef needs to have the node assigned to `current`
videoRef.current = node
// Callback refs, like the one from `useInView`, is a function that takes the node as an argument
inViewRef(node)
if (node) {
node.addEventListener('click', function () {
if (this.paused) {
this.play()
} else {
this.pause()
}
})
}
},
[inViewRef]
)
useEffect(() => {
if (!videoRef || !videoRef.current) {
return
}
if (inView) {
videoRef.current.play()
} else {
videoRef.current.pause()
}
}, [inView])
return (
<div style={{ position: 'relative', margin: '2rem 1rem' }}>
<div style={{ paddingBottom: ratio * 100 + '%' }}/>
<video style={{ position: 'absolute', top: 0, left: 0 }} loop muted autoPlay playsInline ref={setRefs}>
<source src={src} type="video/mp4" />
</video>
{caption && <figcaption style={{ fontSize: '.9rem', textAlign: 'center' }}>{caption}</figcaption>}
</div>
)
}