-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add Embed and AutoPlayVideo components to website
- Loading branch information
Showing
5 changed files
with
107 additions
and
15 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
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
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,64 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
|
||
const AutoPlayVideo = ({ | ||
src, | ||
type = 'video/mp4', | ||
...props | ||
}: React.VideoHTMLAttributes<HTMLVideoElement> & { type?: string }) => { | ||
const videoRef = useRef<HTMLVideoElement>(null); | ||
|
||
useEffect(() => { | ||
const videoElement = videoRef.current; | ||
if (!videoElement) return; | ||
|
||
// Ensure IntersectionObserver is supported | ||
if (!('IntersectionObserver' in window)) { | ||
// Fallback to always play if not supported | ||
videoElement.play(); | ||
return; | ||
} | ||
|
||
const observerOptions = { | ||
root: null, // Use the viewport as the container | ||
rootMargin: '0px', | ||
threshold: 0.25, // Play when 25% of the video is visible | ||
}; | ||
|
||
const handleIntersect = entries => { | ||
entries.forEach(entry => { | ||
if (entry.isIntersecting) { | ||
videoElement.play(); | ||
} else { | ||
videoElement.pause(); | ||
} | ||
}); | ||
}; | ||
|
||
const observer = new IntersectionObserver(handleIntersect, observerOptions); | ||
observer.observe(videoElement); | ||
|
||
// Clean up the observer on unmount | ||
return () => { | ||
observer.unobserve(videoElement); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<center> | ||
<video | ||
ref={videoRef} | ||
width="600" | ||
controls | ||
muted | ||
playsInline | ||
loop | ||
{...props} | ||
> | ||
<source src={src} type={type} /> | ||
Your browser does not support the video tag. | ||
</video> | ||
</center> | ||
); | ||
}; | ||
|
||
export default AutoPlayVideo; |
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,35 @@ | ||
import Link from '@docusaurus/Link'; | ||
|
||
// import Refresh from '../../static/img/refresh.svg'; | ||
|
||
export default function Embed({ | ||
src, | ||
repo, | ||
width = '100%', | ||
height = '400', | ||
...props | ||
}: React.IframeHTMLAttributes<HTMLIFrameElement> & { repo?: string }) { | ||
return ( | ||
<div> | ||
{/* <Refresh /> */} | ||
<iframe src={src} width={width} height={height} {...props} /> | ||
<p style={{ textAlign: 'center' }}> | ||
<Link | ||
className="button button--secondary button--sm" | ||
to={src} | ||
target="_blank" | ||
> | ||
Open in new tab | ||
</Link> | ||
| ||
<Link | ||
className="button button--secondary button--sm" | ||
to={repo} | ||
target="_blank" | ||
> | ||
Github | ||
</Link> | ||
</p> | ||
</div> | ||
); | ||
} |