/* eslint-disable jsx-a11y/media-has-caption */ import { useForwardedRef } from '@anton.bobrov/react-hooks'; import React, { forwardRef, useEffect, useRef, useState } from 'react'; import { IBaseVideoProps } from './types'; import { requestVideoPlay } from './utils/requestVideoPlay'; /** * BaseVideo component for rendering and controlling video playback. * * @link See examples https://antonbobrov.github.io/react-kit/?path=/docs/video-basevideo--docs */ export const BaseVideo = forwardRef( ( { src, children, autoPlay, isPlaying, onLoadedMetadata, ...videoProps }, forwardedRef, ) => { const ref = useForwardedRef(forwardedRef); const [isLoaded, setIsLoaded] = useState(false); const prevIsPlayingRef = useRef(false); useEffect(() => { const isSamePlaying = Boolean(prevIsPlayingRef.current) === Boolean(isPlaying); if (!isLoaded || !ref.current || isSamePlaying) { return undefined; } prevIsPlayingRef.current = Boolean(isPlaying); if (!isPlaying) { ref.current.pause(); return undefined; } const promise = requestVideoPlay(ref.current); promise?.then(() => {}).catch(() => {}); return () => promise?.cancel(); }, [isLoaded, isPlaying, ref]); return ( ); }, ); BaseVideo.displayName = 'BaseVideo'; export * from './utils/requestVideoPlay';