-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add default video component, add custom controls (#662)
* feat: add default video component, add custom controls
- Loading branch information
Showing
5 changed files
with
125 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@import '../../../styles/variables.scss'; | ||
|
||
$block: '.#{$ns}default-video'; | ||
|
||
#{$block} { | ||
width: 100%; | ||
display: flex; | ||
align-items: center; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import React, {Fragment, useCallback, useImperativeHandle, useRef, useState} from 'react'; | ||
|
||
import {CustomControlsType, MediaVideoControlsType, MediaVideoProps} from '../../models'; | ||
import {block} from '../../utils'; | ||
import {getVideoTypesWithPriority} from '../Media/Video/utils'; | ||
import CustomBarControls from '../ReactPlayer/CustomBarControls'; | ||
|
||
import './DefaultVideo.scss'; | ||
|
||
const b = block('default-video'); | ||
|
||
type DefaultVideoRefType = HTMLVideoElement | undefined; | ||
|
||
interface DefaultVideoProps { | ||
video: MediaVideoProps; | ||
qa?: string; | ||
customBarControlsClassName?: string; | ||
className?: string; | ||
} | ||
|
||
export const DefaultVideo = React.forwardRef<DefaultVideoRefType, DefaultVideoProps>( | ||
(props, ref) => { | ||
const {video, qa, customBarControlsClassName} = props; | ||
const {controls, customControlsOptions, muted: initiallyMuted} = video; | ||
const { | ||
muteButtonShown, | ||
positioning, | ||
type: customControlsType, | ||
} = customControlsOptions || {}; | ||
const [isPaused, setIsPaused] = useState(false); | ||
const [isMuted, setIsMuted] = useState(initiallyMuted); | ||
const videoRef = useRef<HTMLVideoElement>(null); | ||
|
||
// one may not use this hook and work with `ref` variable only, but | ||
// in this case one should support both function type and object type, | ||
// according to ForwardedRef type. | ||
// Currently used way with extra ref and useImperativeHandle is more | ||
// convenient and allows us to work with object typed ref only, | ||
// avoiding typeof ref === 'function' statements | ||
useImperativeHandle( | ||
ref, | ||
() => { | ||
if (!videoRef?.current) { | ||
return undefined; | ||
} | ||
|
||
return videoRef.current; | ||
}, | ||
[videoRef], | ||
); | ||
const onPlayToggle = useCallback(() => { | ||
setIsPaused((value) => { | ||
if (value) { | ||
videoRef?.current?.play(); | ||
} else { | ||
videoRef?.current?.pause(); | ||
} | ||
|
||
return !value; | ||
}); | ||
}, [videoRef]); | ||
const onMuteToggle = useCallback(() => { | ||
setIsMuted((value) => !value); | ||
}, []); | ||
|
||
const onClick = useCallback(() => { | ||
if (customControlsType === CustomControlsType.WithPlayPauseButton) { | ||
onPlayToggle(); | ||
} | ||
}, [onPlayToggle, customControlsType]); | ||
|
||
return ( | ||
<Fragment> | ||
<video | ||
disablePictureInPicture | ||
playsInline | ||
// @ts-ignore | ||
// eslint-disable-next-line react/no-unknown-property | ||
pip="false" | ||
className={b()} | ||
ref={videoRef} | ||
preload="metadata" | ||
muted={isMuted} | ||
aria-label={video.ariaLabel} | ||
onClick={onClick} | ||
> | ||
{getVideoTypesWithPriority(video.src).map(({src, type}, index) => ( | ||
<source key={index} src={src} type={type} data-qa={qa} /> | ||
))} | ||
</video> | ||
|
||
{controls === MediaVideoControlsType.Custom && ( | ||
<CustomBarControls | ||
className={customBarControlsClassName} | ||
type={customControlsType} | ||
isPaused={isPaused} | ||
onPlayClick={onPlayToggle} | ||
muteButtonShown={muteButtonShown} | ||
shown | ||
positioning={positioning} | ||
mute={{ | ||
isMuted: Boolean(isMuted), | ||
changeMute: onMuteToggle, | ||
}} | ||
/> | ||
)} | ||
</Fragment> | ||
); | ||
}, | ||
); | ||
|
||
DefaultVideo.displayName = 'DefaultVideo'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters