From 925ace9b9846499aa94d7c4d34d9837b8da25cb8 Mon Sep 17 00:00:00 2001 From: Pavel Kovirshin <45898580+PahaN47@users.noreply.github.com> Date: Fri, 20 Sep 2024 17:48:12 +0300 Subject: [PATCH] feat(ReactPlayer): video aspect ratio & object-fit (#1019) * feat(ReactPlayer): video aspect ratio & object-fit * fix: schemas --- src/components/Media/Video/Video.tsx | 5 ++++- src/components/ReactPlayer/ReactPlayer.scss | 9 ++++++++ src/components/ReactPlayer/ReactPlayer.tsx | 25 ++++++++++++++++++--- src/models/constructor-items/common.ts | 3 ++- src/schema/validators/common.ts | 6 ++++- styles/variables.scss | 1 + 6 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/components/Media/Video/Video.tsx b/src/components/Media/Video/Video.tsx index a2d829f22..c921102d0 100644 --- a/src/components/Media/Video/Video.tsx +++ b/src/components/Media/Video/Video.tsx @@ -85,6 +85,7 @@ const Video = (props: VideoAllProps) => { playButton, ariaLabel, customControlsOptions, + contain, } = video; return ( @@ -104,7 +105,9 @@ const Video = (props: VideoAllProps) => { height={height} ariaLabel={ariaLabel} customControlsOptions={customControlsOptions} - ratio={ratio} + ratio={ratio === 'auto' ? undefined : ratio} + autoRatio={ratio === 'auto'} + contain={contain} /> ); }, [ diff --git a/src/components/ReactPlayer/ReactPlayer.scss b/src/components/ReactPlayer/ReactPlayer.scss index 2fb0a7dbc..61cc7669d 100644 --- a/src/components/ReactPlayer/ReactPlayer.scss +++ b/src/components/ReactPlayer/ReactPlayer.scss @@ -4,6 +4,11 @@ $block: '.#{$ns}ReactPlayer'; #{$block} { + video { + background-color: var(--pc-color-video-player-bg, $videoPlayerBg); + object-fit: cover; + } + &__wrapper { position: relative; // Player ratio: 100 / (1280 / 720) @@ -67,6 +72,10 @@ $block: '.#{$ns}ReactPlayer'; } } + &_contain video { + object-fit: contain; + } + @media only screen and (max-width: map-get($gridBreakpoints, 'sm')) { &__button_text { font-size: 20px; diff --git a/src/components/ReactPlayer/ReactPlayer.tsx b/src/components/ReactPlayer/ReactPlayer.tsx index 1cd2e1ca8..d4bd129fe 100644 --- a/src/components/ReactPlayer/ReactPlayer.tsx +++ b/src/components/ReactPlayer/ReactPlayer.tsx @@ -59,6 +59,7 @@ export interface ReactPlayerBlockProps onClickPreview?: () => void; height?: number; ratio?: number; + autoRatio?: boolean; children?: React.ReactNode; } @@ -87,6 +88,8 @@ export const ReactPlayerBlock = React.forwardRef(0); const [currentHeight, setCurrentHeight] = useState(height); const [width, setWidth] = useState(0); + const [actualRatio, setActualRatio] = useState(); const [muted, setMuted] = useState(mute); const [started, setStarted] = useState(autoPlay); const [ended, setEnded] = useState(false); @@ -202,7 +206,11 @@ export const ReactPlayerBlock = React.forwardRef { window.removeEventListener('resize', updateSize); }; - }, [ratio]); + }, [actualRatio, autoRatio, ratio]); const playEvents = useMemo( () => eventsArray?.filter((e: AnalyticsEvent) => e.type === PredefinedEventTypes.Play), @@ -317,6 +325,16 @@ export const ReactPlayerBlock = React.forwardRef { + setPlayerRef(player); + const videoElement = player.getInternalPlayer(); + const videoWidth = videoElement.videoWidth as number | undefined; + const videoHeight = videoElement.videoHeight as number | undefined; + if (videoWidth && videoHeight) { + setActualRatio(videoHeight / videoWidth); + } + }, []); + const onProgress = useCallback((progress: PlayerPropgress) => { setPlayedPercent(progress.played); @@ -365,6 +383,7 @@ export const ReactPlayerBlock = React.forwardRef; export interface MediaComponentVideoProps extends AnalyticsEventsBase { video: MediaVideoProps; height?: number; - ratio?: number; + ratio?: number | 'auto'; previewImg?: string; } diff --git a/src/schema/validators/common.ts b/src/schema/validators/common.ts index f90896c31..1c66b4446 100644 --- a/src/schema/validators/common.ts +++ b/src/schema/validators/common.ts @@ -184,6 +184,9 @@ export const VideoProps = { ariaLabel: { type: 'string', }, + contain: { + type: 'boolean', + }, }, }; @@ -624,7 +627,8 @@ export const MediaProps = { anyOf: [AnalyticsEventSchema, {type: 'array', items: AnalyticsEventSchema}], }, ratio: { - type: 'number', + type: ['number', 'string'], + pattern: '^auto$', }, iframe: { ...IframeProps, diff --git a/styles/variables.scss b/styles/variables.scss index 51bbd8390..c8682cf53 100644 --- a/styles/variables.scss +++ b/styles/variables.scss @@ -37,6 +37,7 @@ $animationDuration: 300ms; //colors $videoPlayButtonGrey: #eff2f8; +$videoPlayerBg: #000; $secondary: var(--g-color-text-secondary); $lightSecondary: var(--g-color-text-light-secondary);