import React, { FC, useEffect, useState } from 'react'; import { useEvent } from '@anton.bobrov/react-hooks'; import cn from 'classnames'; import { prefixedClasNames } from '../../utils/prefixedClassNames'; import { IVideoPlayerProps } from './types'; import { VideoPlayerVimeo } from './Vimeo'; import { VideoPlayerYoutube } from './YouTube'; import { VideoPlayerMp4 } from './Mp4'; /** * VideoPlayer component for creating HTML5 video players as well as * embedding YouTube and Vimeo videos. * * @link See examples https://antonbobrov.github.io/react-kit/?path=/docs/video-videoplayer--docs * * @requires Requires styles: `@import '~@anton.bobrov/react-components/lib/styles/components/VideoPlayer';` */ export const VideoPlayer: FC = ({ className, style, source, src, id, onLoad: onLoadProp, youtubeProps, vimeoProps, }) => { const [isLoaded, setIsLoaded] = useState(false); const onLoad = useEvent(onLoadProp); useEffect(() => { if (isLoaded) { onLoad?.(); } }, [isLoaded, onLoad]); const loadedClassName = prefixedClasNames('loaded'); const classNames = cn( prefixedClasNames('video-player'), isLoaded && loadedClassName, className, ); if (source === 'mp4' && !!src) { return (
setIsLoaded(true)} />
); } if (['yt', 'youtube'].includes(source) && !!id) { return (
setIsLoaded(true)} />
); } if (['vm', 'vimeo'].includes(source) && !!id) { return (
setIsLoaded(true)} />
); } return (
Unexpected video source
); }; export * from './Mp4'; export * from './Mp4/types'; export * from './Vimeo'; export * from './Vimeo/types'; export * from './YouTube'; export * from './YouTube/types';