import clsxm from "@/lib/clsxm"; import ReactPortal from "@/lib/ReactPortal"; import { Button } from "@fluid-design/fluid-ui"; import { AnimatePresence, motion, useReducedMotion } from "framer-motion"; import { useEffect, useId, useMemo, useRef, useState } from "react"; import { HiExternalLink } from "react-icons/hi"; import { MdFullscreen } from "react-icons/md"; import { useBounceTransition, linearTransition, animationSimple, } from "@/lib/useTransitionValues"; type VideoPlayerProps = { src: { light: string; dark: string; }; title?: string; description?: string; href?: string; acitveClassName?: string; className?: string; containerClassName?: string; showTitle?: boolean; showDescription?: boolean; }; function VideoPlayer({ src: { light, dark }, title, description, href, className, acitveClassName, containerClassName, showTitle = false, showDescription = false, }: VideoPlayerProps) { const id = useId(); const videoRefLight = useRef(null); const videoRefDark = useRef(null); const shouldReduceMotion = useReducedMotion(); const bounceTransition = useBounceTransition(); const [isOpen, setIsOpen] = useState(false); const handleOpen = () => setIsOpen(!isOpen); const videoClassName = [ "w-full max-w-2xl rounded-xl", isOpen ? ["aspect-[4/2.8] object-contain", acitveClassName] : "h-full object-cover", className, ]; const videoProps = shouldReduceMotion ? { playsInline: true, loop: false, controls: true, autoPlay: false, muted: true, transition: linearTransition, } : { playsInline: true, loop: true, controls: false, autoPlay: true, muted: true, transition: bounceTransition, }; const videos = ( <> {!isOpen && ( )} ); function handleEscape(e) { if (e.key === "Escape") { setIsOpen(false); } } useEffect(() => { if (isOpen) { // listen for esc key document.addEventListener("keydown", handleEscape); return () => { // remove event listener document.removeEventListener("keydown", handleEscape); }; } }, [isOpen]); // manually pause videos if reduce motion is enabled useEffect(() => { if (shouldReduceMotion) { videoRefLight.current?.pause(); videoRefDark.current?.pause(); // show controls videoRefLight.current?.setAttribute("controls", "true"); videoRefDark.current?.setAttribute("controls", "true"); } }, [shouldReduceMotion]); // manually play videos if reduce motion is enabled and isOpen is true useEffect(() => { if (shouldReduceMotion && isOpen) { videoRefLight.current?.play(); videoRefDark.current?.play(); } }, [shouldReduceMotion, isOpen]); return ( <> {isOpen && ( setIsOpen(false)} > {videos} {/* Info Bar */} {title && (

{title}

{description && (

{description}

)}
{href && (
)} )}
{videos} {showTitle && title && (

{title}

)} {showDescription && description && (

{description}

)}
); } export default VideoPlayer;