import React from "react"; import FilePlayer, { ReactPlayerProps } from "react-player"; import styled from "styled-components"; import screenfull from "screenfull"; import { VideoPlayerProps, VideoPlayerState } from "./interface"; import { Label, Body } from "@Typography"; import * as tokens from "@tokens"; import { dim } from "@dim"; import { mq } from "@tokensDir/media-query"; import { sizes as screenSizes } from "@tokensDir/media-query"; const VideoControlBar = styled.div` position: absolute; bottom: 0; left: 0; right: 0; visibility: ${({ isActive }: { isActive: boolean }) => isActive ? "visible" : "hidden"}; opacity: ${({ isActive }: { isActive: boolean }) => (isActive ? "1" : "0")}; transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out; background-color: ${tokens.colorGrey40}; height: 40px; display: flex; justify-content: space-around; align-items: center; ${mq.md` height: 70px; `} `; const StartButton = styled.button` background-color: transparent; background-image: url("icons/play-circle-btn.svg"); background-repeat: no-repeat; background-position: center center; background-size: 50%; cursor: pointer; width: 60px; height: 60px; outline: 0; border: none; position: absolute; bottom: 0; left: 50%; transform: translateX(-50%); visibility: ${({ isActive }: { isActive: boolean }) => isActive ? "visible" : "hidden"}; transition: ease-in-out 0.3s background-image; &:hover { transition: ease-in-out 0.3s background-image; background-image: url("icons/play-circle-btn-hover.svg"); } ${mq.md` bottom: 30px; background-size: 100%; `} `; const ProgressBar = styled.div` width: 50%; background-color: ${tokens.colorGrey40}; height: 2px; cursor: pointer; span { background-color: ${tokens.colorWhite}; height: 100%; display: block; } `; const PlayPause = styled.button` border: none; background-color: transparent; cursor: pointer; &:focus { outline: none; border: none; } `; const FullScreen = styled.button` border: none; background-color: transparent; cursor: pointer; &:focus { outline: none; border: none; } `; const ComponentWrapper = styled.div` margin: ${dim(3)} auto ${dim(6)}; `; const VideoWrapper = styled.div` position: relative; ${({ fullwidth }: { fullwidth?: boolean }) => { if (fullwidth) return "width: 100%"; return ` max-width: ${dim(92)} margin: 0 auto; `; }}; `; const VideoDetails = styled.section` margin-top: ${dim(3)}; text-align: center; ${Label} { margin-top: 0; margin-bottom: ${dim(2)}; } `; class VideoPlayer extends React.Component { private videoWrapperRef: React.RefObject; private videoPlayerRef: React.ReactElement | any; static defaultProps: VideoPlayerProps; state: VideoPlayerState = { hasStarted: false, isControlBarActive: false, hasEnded: false, isSeeking: false, playing: false, duration: 0, played: 0, playedPerecent: 0 }; constructor(props: VideoPlayerProps) { super(props); this.onPlay = this.onPlay.bind(this); this.onPause = this.onPause.bind(this); this.onSeekChange = this.onSeekChange.bind(this); this.onSeekMouseDown = this.onSeekMouseDown.bind(this); this.onSeekMouseUp = this.onSeekMouseUp.bind(this); this.onProgress = this.onProgress.bind(this); this.onDuration = this.onDuration.bind(this); this.onFullScreen = this.onFullScreen.bind(this); this.onStart = this.onStart.bind(this); this.onActivate = this.onActivate.bind(this); this.updatePlayerPosition = this.updatePlayerPosition.bind(this); this.hidePlayerBar = this.hidePlayerBar.bind(this); this.createVideoRef = this.createVideoRef.bind(this); this.videoWrapperRef = React.createRef(); this.videoPlayerRef = React.createRef(); } componentDidMount() { this.setState({ playing: this.props.autoPlay || window.innerHeight <= screenSizes.sm }); } componentWillUnmount() { this.updatePlayerPosition(); } updatePlayerPosition(time = 0) { // @ts-ignore if (!this.videoPlayerRef.seekTo) return; this.videoPlayerRef.seekTo(time); this.setState({ played: time, playedPerecent: time * 100 }); } onPlay() { this.setState({ playing: true }); } onPause() { this.setState({ playing: false }); } onSeekChange(e: React.MouseEvent) { const target = e.target as HTMLElement; const time = parseFloat(`${e.nativeEvent.offsetX / target.offsetWidth}`); this.updatePlayerPosition(time); } onSeekMouseDown() { this.setState({ isSeeking: true }); } onSeekMouseUp() { this.setState({ isSeeking: false }); } onProgress(progress: any) { if (!this.state.isSeeking) { return this.setState({ played: progress.played, playedPerecent: (progress.playedSeconds / this.state.duration) * 100 }); } } onDuration(duration: number) { this.setState({ duration }); } onFullScreen() { if (!this.videoWrapperRef.current) return; screenfull && screenfull.request(this.videoWrapperRef.current); } hidePlayerBar(transition: number = 3000) { const _this = this; setTimeout(() => { _this.setState({ isControlBarActive: false }); }, transition); } onStart() { const _this = this; this.setState( { isControlBarActive: true, hasStarted: true }, _this.hidePlayerBar ); } onActivate() { const _this = this; if (this.state.hasStarted) { this.setState( { isControlBarActive: true }, _this.hidePlayerBar ); } } createVideoRef(player: any) { this.videoPlayerRef = player; } render() { const playedAmount = this.state.playedPerecent.toFixed(1); return ( this.setState({ playing: true })} onPause={() => this.setState({ playing: false })} onDuration={this.onDuration} onProgress={this.onProgress} onMouseOver={this.onActivate} url={`${this.props.mediaSource}/mp4_720p?protocol=https`} muted={this.props.muted} loop={this.props.muted} // If muted video plays on loop width="100%" height="100%" config={{ file: { attributes: { poster: this.props.poster || this.props.mediaSource } } }} /> this.onSeekChange(e)} onMouseUp={this.onSeekMouseUp} > {!this.props.fullwidth && this.props.caption && ( {this.props.caption.byline} )} ); } } VideoPlayer.defaultProps = { autoPlay: false, muted: false, fullwidth: false, poster: false, mediaSource: "" }; export default VideoPlayer;