forked from cloudinary-community/next-cloudinary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideo.js
More file actions
85 lines (76 loc) · 2.08 KB
/
Video.js
File metadata and controls
85 lines (76 loc) · 2.08 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
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
import { getUrlParamsFromString } from '../../lib/parse';
import { useEffect, useRef, useState } from 'react';
import styles from './Video.module.scss';
export const Video = ({
className,
title,
url,
width = 560,
height = 315,
}) => {
const videoId = getUrlParamsFromString(url).find(({ key }) => key === 'v')?.value;
const [isVideoVisible, setIsVideoVisible] = useState(false);
const videoContainerRef = useRef();
useEffect(() => {
const options = {
root: null,
rootMargin: '0px',
threshold: 0.5, // Adjust this threshold as needed
};
let observer;
if (videoContainerRef.current) {
observer = new IntersectionObserver(handleIntersection, options);
observer.observe(videoContainerRef.current);
}
return () => {
if (observer && videoContainerRef.current) {
observer.unobserve(videoContainerRef.current);
}
};
}, []);
const handleIntersection = (entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
setIsVideoVisible(true);
}
});
};
return (
<figure className={styles.video}>
{isVideoVisible ? (
<div
ref={videoContainerRef}
className={styles.videoContainer}
style={{
paddingTop: `${(height / width) * 100}%`,
}}
>
<iframe
title={title}
width={width}
height={height}
src={`https://www.youtube.com/embed/${videoId}?feature=oembed`}
frameBorder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
/>
</div>
) : (
<div
ref={videoContainerRef}
style={{
paddingTop: `${(height / width) * 100}%`,
// width: '100%',
// height: '100%',
}}
></div>
)}
<figcaption>
<a href={url} rel="noopener" target="_blank">
View on YouTube
</a>
</figcaption>
</figure>
);
};
export default Video;