Video.js
1.52 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
import React, { useRef, useEffect, useState } from "react";
import { cleanImage } from "../services/imageHandling";
const Video = ({ productData }) => {
const videoRef = useRef(null);
const [isVisible, setIsVisible] = useState(false);
const media = productData?.video;
useEffect(() => {
// ✅ Guard for missing video OR SSR
if (!media?.url || typeof window === "undefined") return;
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
setIsVisible(true);
if (videoRef.current) {
observer.unobserve(videoRef.current);
}
}
},
{ threshold: 0.5 }
);
if (videoRef.current) {
observer.observe(videoRef.current);
}
return () => {
if (videoRef.current) {
observer.unobserve(videoRef.current);
}
};
}, [media?.url]); // ✅ dependency added (important)
// ✅ AFTER hooks → safe
if (!media?.url) return null;
return (
<section className="video_sec">
<div className="custom_containers">
<video
ref={videoRef}
autoPlay
muted
loop
playsInline
className={`w-100 video-animate ${
isVisible ? "video-visible" : ""
}`}
>
<source
src={cleanImage(media?.url)}
type={media?.mime || "video/mp4"}
/>
Your browser does not support the video tag.
</video>
</div>
</section>
);
};
export default Video;