sudo-archive/src/components/video/VideoPlayer.tsx

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-01-08 14:37:16 +00:00
import { forwardRef, useContext, useRef } from "react";
import { VideoPlayerContext, VideoPlayerContextProvider } from "./VideoContext";
2023-01-08 12:15:32 +00:00
interface VideoPlayerProps {
autoPlay?: boolean;
2023-01-08 12:15:32 +00:00
children?: React.ReactNode;
}
const VideoPlayerInternals = forwardRef<
HTMLVideoElement,
{ autoPlay: boolean }
>((props, ref) => {
2023-01-08 12:15:32 +00:00
const video = useContext(VideoPlayerContext);
return (
<video
ref={ref}
autoPlay={props.autoPlay}
playsInline
className="h-full w-full"
>
2023-01-08 12:15:32 +00:00
{video.source ? <source src={video.source} type="video/mp4" /> : null}
</video>
);
});
export function VideoPlayer(props: VideoPlayerProps) {
const playerRef = useRef<HTMLVideoElement | null>(null);
2023-01-08 15:23:42 +00:00
const playerWrapperRef = useRef<HTMLDivElement | null>(null);
2023-01-08 12:15:32 +00:00
return (
2023-01-08 15:23:42 +00:00
<VideoPlayerContextProvider player={playerRef} wrapper={playerWrapperRef}>
<div
className="relative aspect-video w-full bg-black"
ref={playerWrapperRef}
>
<VideoPlayerInternals
autoPlay={props.autoPlay ?? false}
ref={playerRef}
/>
2023-01-08 15:23:42 +00:00
<div className="absolute inset-0">{props.children}</div>
</div>
2023-01-08 12:15:32 +00:00
</VideoPlayerContextProvider>
);
}