Video.js
2.25 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
86
87
88
89
90
import React, { useRef, useEffect, useState } from "react";
import Image from "next/image";
import { cleanImage } from "../services/imageHandling";
const Video = ({ productData }) => {
const mediaRef = useRef(null);
const [isVisible, setIsVisible] = useState(false);
const media = productData?.video;
const isVideo = media?.mime?.startsWith("video/");
const isImage = media?.mime?.startsWith("image/");
// ✅ Strapi default image path
const defaultImage = "/image/default.svg";
useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
setIsVisible(true);
observer.unobserve(entry.target);
}
},
{ threshold: 0.5 }
);
if (mediaRef.current) observer.observe(mediaRef.current);
return () => {
if (mediaRef.current) observer.unobserve(mediaRef.current);
};
}, []);
return (
<section className="video_sec">
<div className="custom_containers">
{/* VIDEO */}
{isVideo && (
<video
ref={mediaRef}
autoPlay
muted
loop
playsInline
className={`w-100 video-animate ${isVisible ? "video-visible" : ""}`}
>
<source src={cleanImage(media?.url)} type={media?.mime || "video/mp4"} />
</video>
)}
{/* IMAGE */}
{isImage && (
<div
ref={mediaRef}
className={`w-100 video-animate ${isVisible ? "video-visible" : ""}`}
>
<Image
src={cleanImage(media?.url)}
alt={media?.alternativeText || "Media"}
width={media?.width || 868}
height={media?.height || 560}
className="w-100"
/>
</div>
)}
{/* DEFAULT IMAGE */}
{!media && (
<div
ref={mediaRef}
className={`w-100 video-animate ${isVisible ? "video-visible" : ""}`}
>
<Image
src={cleanImage()}
alt="Default Image"
width={868}
height={560}
className="w-100"
/>
</div>
)}
</div>
</section>
);
};
export default Video;