-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Trying new VideoComponent * Remove mouse check * Increased rootMargin
- Loading branch information
Showing
3 changed files
with
120 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
|
||
import { CSSProperties, useEffect, useRef } from "react"; | ||
import { useIsVisible } from "../lib/useIsVisible"; | ||
|
||
|
||
type VideoComponentProps = { | ||
src: string; | ||
poster?: string; | ||
alt?: string; | ||
style?: CSSProperties; | ||
}; | ||
export const VideoComponent = ({ | ||
src, | ||
poster, | ||
style, | ||
alt, | ||
}: VideoComponentProps) => { | ||
const { isVisible, targetRef } = useIsVisible( | ||
{ | ||
root: null, | ||
rootMargin: "500px", | ||
threshold: 0.1, | ||
}, | ||
false, | ||
); | ||
|
||
const videoRef = useRef<HTMLVideoElement>(null); | ||
|
||
useEffect(() => { | ||
const handleVideoPlayback = async () => { | ||
if (isVisible) { | ||
await videoRef.current?.play(); | ||
} else { | ||
videoRef.current?.pause(); | ||
} | ||
}; | ||
|
||
handleVideoPlayback(); | ||
}, [isVisible]); | ||
|
||
return ( | ||
<span | ||
ref={targetRef as any} | ||
style={{ | ||
position: "relative", | ||
minHeight: "50px", | ||
height: "100%", | ||
}} | ||
> | ||
<video | ||
ref={videoRef} | ||
loop | ||
muted | ||
autoPlay={false} | ||
preload="none" | ||
playsInline | ||
poster={poster} | ||
aria-label={alt} | ||
style={{ | ||
objectFit: "contain", | ||
display: "block", | ||
width: "100%", | ||
height: "100%", | ||
...style, | ||
}} | ||
> | ||
<source src={src} type="video/mp4" /> | ||
Your browser does not support the video tag. Please try viewing this | ||
page in a modern browser. | ||
</video> | ||
</span> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
|
||
interface IntersectionObserverOptions { | ||
root?: Element | null; | ||
rootMargin?: string; | ||
threshold?: number | number[]; | ||
} | ||
|
||
export const useIsVisible = ( | ||
options?: IntersectionObserverOptions, | ||
once = false, | ||
) => { | ||
const optionsRef = useRef(options); | ||
const [isVisible, setIsVisible] = useState(false); | ||
const targetRef = useRef<Element | null>(null); | ||
|
||
useEffect(() => { | ||
const observer = new IntersectionObserver((entries) => { | ||
entries.forEach((entry) => { | ||
if (entry.isIntersecting) { | ||
setIsVisible(() => true); | ||
if (once) { | ||
observer.unobserve(entry.target); | ||
observer.disconnect(); | ||
} | ||
} else { | ||
setIsVisible(() => false); | ||
} | ||
}); | ||
}, optionsRef.current); | ||
|
||
if (targetRef.current) { | ||
observer.observe(targetRef.current); | ||
} | ||
|
||
return () => { | ||
if (targetRef.current) { | ||
observer.unobserve(targetRef.current); | ||
} | ||
observer.disconnect(); // Clean up the IntersectionObserver | ||
}; | ||
}, [once]); | ||
|
||
return { isVisible, targetRef }; | ||
}; |