Skip to content

Commit

Permalink
feat(ReactPlayer): video aspect ratio & object-fit (#1019)
Browse files Browse the repository at this point in the history
* feat(ReactPlayer): video aspect ratio & object-fit

* fix: schemas
  • Loading branch information
PahaN47 authored Sep 20, 2024
1 parent 9e571b4 commit 925ace9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/components/Media/Video/Video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const Video = (props: VideoAllProps) => {
playButton,
ariaLabel,
customControlsOptions,
contain,
} = video;

return (
Expand All @@ -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}
/>
);
}, [
Expand Down
9 changes: 9 additions & 0 deletions src/components/ReactPlayer/ReactPlayer.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 22 additions & 3 deletions src/components/ReactPlayer/ReactPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface ReactPlayerBlockProps
onClickPreview?: () => void;
height?: number;
ratio?: number;
autoRatio?: boolean;
children?: React.ReactNode;
}

Expand Down Expand Up @@ -87,6 +88,8 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
height,
ariaLabel,
ratio,
autoRatio,
contain,
} = props;

const {
Expand All @@ -113,6 +116,7 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
const [playedPercent, setPlayedPercent] = useState<number>(0);
const [currentHeight, setCurrentHeight] = useState(height);
const [width, setWidth] = useState<number>(0);
const [actualRatio, setActualRatio] = useState<number>();
const [muted, setMuted] = useState<boolean>(mute);
const [started, setStarted] = useState(autoPlay);
const [ended, setEnded] = useState<boolean>(false);
Expand Down Expand Up @@ -202,7 +206,11 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
parseFloat(paddingRight);

setWidth(newWidth);
setCurrentHeight(Math.floor(getHeight(newWidth, ratio)));
setCurrentHeight(
Math.floor(
getHeight(newWidth, ratio ?? (autoRatio ? actualRatio : undefined)),
),
);
}
}, 200);

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

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

const onReady = useCallback((player: ReactPlayer) => {
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);

Expand Down Expand Up @@ -365,6 +383,7 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
{
wrapper: !currentHeight,
controls,
contain,
},
className,
)}
Expand All @@ -390,7 +409,7 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
progressInterval={FPS}
onClickPreview={handleClickPreview}
onStart={onStart}
onReady={setPlayerRef}
onReady={onReady}
onPlay={onPlay}
onPause={
autoPlay && customControlsType !== CustomControlsType.WithMuteButton
Expand Down
3 changes: 2 additions & 1 deletion src/models/constructor-items/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export interface MediaVideoProps extends AnalyticsEventsBase {
controls?: MediaVideoControlsType;
customControlsOptions?: CustomControlsOptions;
ariaLabel?: string;
contain?: boolean;
}

// links
Expand Down Expand Up @@ -248,7 +249,7 @@ export type ThemedMediaVideoProps = ThemeSupporting<MediaVideoProps>;
export interface MediaComponentVideoProps extends AnalyticsEventsBase {
video: MediaVideoProps;
height?: number;
ratio?: number;
ratio?: number | 'auto';
previewImg?: string;
}

Expand Down
6 changes: 5 additions & 1 deletion src/schema/validators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ export const VideoProps = {
ariaLabel: {
type: 'string',
},
contain: {
type: 'boolean',
},
},
};

Expand Down Expand Up @@ -624,7 +627,8 @@ export const MediaProps = {
anyOf: [AnalyticsEventSchema, {type: 'array', items: AnalyticsEventSchema}],
},
ratio: {
type: 'number',
type: ['number', 'string'],
pattern: '^auto$',
},
iframe: {
...IframeProps,
Expand Down
1 change: 1 addition & 0 deletions styles/variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ $animationDuration: 300ms;

//colors
$videoPlayButtonGrey: #eff2f8;
$videoPlayerBg: #000;
$secondary: var(--g-color-text-secondary);
$lightSecondary: var(--g-color-text-light-secondary);

Expand Down

0 comments on commit 925ace9

Please sign in to comment.