Skip to content

Commit 925ace9

Browse files
authored
feat(ReactPlayer): video aspect ratio & object-fit (#1019)
* feat(ReactPlayer): video aspect ratio & object-fit * fix: schemas
1 parent 9e571b4 commit 925ace9

File tree

6 files changed

+43
-6
lines changed

6 files changed

+43
-6
lines changed

src/components/Media/Video/Video.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const Video = (props: VideoAllProps) => {
8585
playButton,
8686
ariaLabel,
8787
customControlsOptions,
88+
contain,
8889
} = video;
8990

9091
return (
@@ -104,7 +105,9 @@ const Video = (props: VideoAllProps) => {
104105
height={height}
105106
ariaLabel={ariaLabel}
106107
customControlsOptions={customControlsOptions}
107-
ratio={ratio}
108+
ratio={ratio === 'auto' ? undefined : ratio}
109+
autoRatio={ratio === 'auto'}
110+
contain={contain}
108111
/>
109112
);
110113
}, [

src/components/ReactPlayer/ReactPlayer.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
$block: '.#{$ns}ReactPlayer';
55

66
#{$block} {
7+
video {
8+
background-color: var(--pc-color-video-player-bg, $videoPlayerBg);
9+
object-fit: cover;
10+
}
11+
712
&__wrapper {
813
position: relative;
914
// Player ratio: 100 / (1280 / 720)
@@ -67,6 +72,10 @@ $block: '.#{$ns}ReactPlayer';
6772
}
6873
}
6974

75+
&_contain video {
76+
object-fit: contain;
77+
}
78+
7079
@media only screen and (max-width: map-get($gridBreakpoints, 'sm')) {
7180
&__button_text {
7281
font-size: 20px;

src/components/ReactPlayer/ReactPlayer.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export interface ReactPlayerBlockProps
5959
onClickPreview?: () => void;
6060
height?: number;
6161
ratio?: number;
62+
autoRatio?: boolean;
6263
children?: React.ReactNode;
6364
}
6465

@@ -87,6 +88,8 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
8788
height,
8889
ariaLabel,
8990
ratio,
91+
autoRatio,
92+
contain,
9093
} = props;
9194

9295
const {
@@ -113,6 +116,7 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
113116
const [playedPercent, setPlayedPercent] = useState<number>(0);
114117
const [currentHeight, setCurrentHeight] = useState(height);
115118
const [width, setWidth] = useState<number>(0);
119+
const [actualRatio, setActualRatio] = useState<number>();
116120
const [muted, setMuted] = useState<boolean>(mute);
117121
const [started, setStarted] = useState(autoPlay);
118122
const [ended, setEnded] = useState<boolean>(false);
@@ -202,7 +206,11 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
202206
parseFloat(paddingRight);
203207

204208
setWidth(newWidth);
205-
setCurrentHeight(Math.floor(getHeight(newWidth, ratio)));
209+
setCurrentHeight(
210+
Math.floor(
211+
getHeight(newWidth, ratio ?? (autoRatio ? actualRatio : undefined)),
212+
),
213+
);
206214
}
207215
}, 200);
208216

@@ -211,7 +219,7 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
211219
return () => {
212220
window.removeEventListener('resize', updateSize);
213221
};
214-
}, [ratio]);
222+
}, [actualRatio, autoRatio, ratio]);
215223

216224
const playEvents = useMemo(
217225
() => eventsArray?.filter((e: AnalyticsEvent) => e.type === PredefinedEventTypes.Play),
@@ -317,6 +325,16 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
317325
}
318326
}, [changeMute, controls, customControlsType, ended, isPlaying, muted]);
319327

328+
const onReady = useCallback((player: ReactPlayer) => {
329+
setPlayerRef(player);
330+
const videoElement = player.getInternalPlayer();
331+
const videoWidth = videoElement.videoWidth as number | undefined;
332+
const videoHeight = videoElement.videoHeight as number | undefined;
333+
if (videoWidth && videoHeight) {
334+
setActualRatio(videoHeight / videoWidth);
335+
}
336+
}, []);
337+
320338
const onProgress = useCallback((progress: PlayerPropgress) => {
321339
setPlayedPercent(progress.played);
322340

@@ -365,6 +383,7 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
365383
{
366384
wrapper: !currentHeight,
367385
controls,
386+
contain,
368387
},
369388
className,
370389
)}
@@ -390,7 +409,7 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
390409
progressInterval={FPS}
391410
onClickPreview={handleClickPreview}
392411
onStart={onStart}
393-
onReady={setPlayerRef}
412+
onReady={onReady}
394413
onPlay={onPlay}
395414
onPause={
396415
autoPlay && customControlsType !== CustomControlsType.WithMuteButton

src/models/constructor-items/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export interface MediaVideoProps extends AnalyticsEventsBase {
176176
controls?: MediaVideoControlsType;
177177
customControlsOptions?: CustomControlsOptions;
178178
ariaLabel?: string;
179+
contain?: boolean;
179180
}
180181

181182
// links
@@ -248,7 +249,7 @@ export type ThemedMediaVideoProps = ThemeSupporting<MediaVideoProps>;
248249
export interface MediaComponentVideoProps extends AnalyticsEventsBase {
249250
video: MediaVideoProps;
250251
height?: number;
251-
ratio?: number;
252+
ratio?: number | 'auto';
252253
previewImg?: string;
253254
}
254255

src/schema/validators/common.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ export const VideoProps = {
184184
ariaLabel: {
185185
type: 'string',
186186
},
187+
contain: {
188+
type: 'boolean',
189+
},
187190
},
188191
};
189192

@@ -624,7 +627,8 @@ export const MediaProps = {
624627
anyOf: [AnalyticsEventSchema, {type: 'array', items: AnalyticsEventSchema}],
625628
},
626629
ratio: {
627-
type: 'number',
630+
type: ['number', 'string'],
631+
pattern: '^auto$',
628632
},
629633
iframe: {
630634
...IframeProps,

styles/variables.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ $animationDuration: 300ms;
3737

3838
//colors
3939
$videoPlayButtonGrey: #eff2f8;
40+
$videoPlayerBg: #000;
4041
$secondary: var(--g-color-text-secondary);
4142
$lightSecondary: var(--g-color-text-light-secondary);
4243

0 commit comments

Comments
 (0)