|
1 | 1 | import { useActiveVersion } from '@docusaurus/plugin-content-docs/client'; |
2 | 2 | import { Link } from "react-router-dom"; |
3 | 3 | import ThemedImage from '@theme/ThemedImage' |
| 4 | +import { useRef, useEffect, useState } from 'react'; |
4 | 5 |
|
5 | 6 | export const GetExternalPath = () => { |
6 | 7 | return process.env.NODE_ENV === 'development' ? "" : "https://github.com/nanos-world/docs/raw/master/external"; |
7 | 8 | } |
8 | 9 |
|
9 | | -// External Video from Github |
10 | | -export const VideoExternal = ({ path, noplay, controls = true, className, style }) => ( |
11 | | - <video key={path} controls={controls} allowFullScreen={true} preload={"none"} autoPlay={!noplay} loop={!noplay} muted={!noplay} className={className} style={style}> |
12 | | - <source src={`${ GetExternalPath() }/videos${ path }`} /> |
13 | | - </video> |
14 | | -) |
| 10 | +// External Video from Github (with lazy load) |
| 11 | +export const VideoExternal = ({ path, noplay, controls = true, className, style }) => { |
| 12 | + const videoRef = useRef(null); |
| 13 | + const [isVisible, setIsVisible] = useState(false); |
| 14 | + const src = `${ GetExternalPath() }/videos${ path }`; |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + // Create an Intersection Observer |
| 18 | + const observer = new IntersectionObserver( |
| 19 | + ([entry]) => { |
| 20 | + // If the video is intersecting with the viewport |
| 21 | + if (entry.isIntersecting) { |
| 22 | + setIsVisible(true); |
| 23 | + observer.disconnect(); // Stop observing once visible |
| 24 | + } |
| 25 | + }, |
| 26 | + { |
| 27 | + root: null, // observe against the viewport |
| 28 | + rootMargin: '0px', |
| 29 | + threshold: 0.1, // trigger when 10% of the video is visible |
| 30 | + } |
| 31 | + ); |
| 32 | + |
| 33 | + if (videoRef.current) { |
| 34 | + observer.observe(videoRef.current); |
| 35 | + } |
| 36 | + |
| 37 | + // Cleanup function |
| 38 | + return () => { |
| 39 | + if (videoRef.current && observer) { |
| 40 | + observer.unobserve(videoRef.current); |
| 41 | + } |
| 42 | + }; |
| 43 | + }, []); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + if (isVisible && videoRef.current) { |
| 47 | + // Set the source and load the video |
| 48 | + videoRef.current.src = src; |
| 49 | + videoRef.current.play(); |
| 50 | + } |
| 51 | + }, [isVisible, src]); |
| 52 | + |
| 53 | + return ( |
| 54 | + <video |
| 55 | + ref={videoRef} |
| 56 | + loop={!noplay} |
| 57 | + autoPlay={!noplay} |
| 58 | + muted={!noplay} |
| 59 | + className={className} |
| 60 | + style={style} |
| 61 | + controls={controls} |
| 62 | + allowFullScreen={true} |
| 63 | + playsInline |
| 64 | + > |
| 65 | + Your browser does not support the video tag. |
| 66 | + </video> |
| 67 | + ); |
| 68 | +}; |
15 | 69 |
|
16 | 70 | // External Image from Github |
17 | 71 | export const ImageExternal = ({ path, className }) => ( |
@@ -42,7 +96,7 @@ export const getActiveVersionPath = () => { |
42 | 96 |
|
43 | 97 | // Function to convert PascalCase string to kebab-case |
44 | 98 | export const getKebabFromPascal = (str) => { |
45 | | - return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); |
| 99 | + return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); |
46 | 100 | }; |
47 | 101 |
|
48 | 102 | // Getter to get the current version path - for links |
|
0 commit comments