import { getGifHeight } from '@giphy/js-util' import { css, cx } from '@emotion/css' import { ComponentProps, h } from 'preact' import { useCallback, useEffect, useState } from 'preact/hooks' import { PauseIcon, PlayIcon } from './controls/play-pause' import { VolumeOffIcon, VolumeOnIcon } from './controls/volume' import ProgressBar from './progress-bar' import Video, { MEDIA_STATE } from './video' const containerCss = css` position: relative; top: 0; left: 0; right: 0; bottom: 0; background: black; ` const volumeCss = css` display: flex; justify-content: center; align-items: center; ` const controlsCss = css` position: absolute; bottom: 10px; right: 10px; left: 10px; display: flex; justify-content: space-between; transition: opacity ease-out 250ms; ` const VideoPlayer = (props: ComponentProps) => { const { width, hideMute, hidePlayPause, hideProgressBar, className, persistentControls } = props const [isHovered, setIsHovered] = useState(false) const [playState, setPlayState] = useState('paused') const [videoEl, _setVideoEl] = useState(null) const [muted, setMuted] = useState(props.muted) const [mutedByBrowser, setMutedByBrowser] = useState(false) const { onStateChange, setVideoEl, onMuted, onUserMuted } = props const height = props.height || getGifHeight(props.gif, width) const combinedSetPlayState = useCallback( (args: MEDIA_STATE) => { onStateChange?.(args) setPlayState(args) }, [onStateChange, setPlayState] ) const combinedOnMuted = useCallback( (args: boolean) => { onMuted?.(args) setMutedByBrowser(args) }, [setMutedByBrowser, onMuted] ) const combinedSetVideoEl = useCallback( (args: HTMLVideoElement) => { setVideoEl?.(args) _setVideoEl(args) }, [setVideoEl, _setVideoEl] ) const toggleMute = () => { if (mutedByBrowser) { setMutedByBrowser(false) setMuted(false) } else { setMuted(!muted) } } useEffect(() => { setMuted(props.muted) }, [props.muted]) return (
setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} >