Skip to content

Commit 32bffab

Browse files
author
Juli Ovechkina
authored
Merge pull request #71 from gravity-ui/yuberdysheva/fix-player-height
fix(Tabs): player video height
2 parents 792be1b + a372ddb commit 32bffab

File tree

6 files changed

+45
-23
lines changed

6 files changed

+45
-23
lines changed

src/blocks/Media/__stories__/Media.stories.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
MediaProps,
1010
} from '../../../models';
1111
import Media from '../Media';
12-
import {PageConstructor} from '../../../containers/PageConstructor/PageConstructor';
12+
import {PageConstructor} from '../../../containers/PageConstructor';
1313

1414
import data from './data.json';
1515

@@ -133,7 +133,10 @@ const DefaultArgs = {
133133
};
134134

135135
Default.args = DefaultArgs as MediaBlockProps;
136-
ImageSlider.args = DefaultArgs as MediaBlockProps;
136+
ImageSlider.args = {
137+
...DefaultArgs,
138+
...data.imageSlider.content,
139+
} as MediaBlockProps;
137140
Video.args = DefaultArgs as MediaBlockProps;
138141
DataLens.args = {
139142
...DefaultArgs,

src/blocks/Tabs/Tabs.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {Fragment, useContext, useState} from 'react';
1+
import React, {Fragment, useContext, useRef, useState} from 'react';
22

33
import {block, getThemedValue} from '../../utils';
44
import {Row, Col, GridColumnOrderClasses} from '../../grid';
@@ -11,6 +11,7 @@ import {ThemeValueContext} from '../../context/theme/ThemeValueContext';
1111
import {getMediaImage} from '../../components/Media/Image/utils';
1212
import ButtonTabs, {ButtonTabsItemProps} from '../../components/ButtonTabs/ButtonTabs';
1313
import {Content} from '../../sub-blocks';
14+
import {getHeight} from '../../components/VideoBlock/VideoBlock';
1415

1516
import './Tabs.scss';
1617

@@ -31,6 +32,9 @@ export const TabsBlock = ({
3132
const tabs: ButtonTabsItemProps[] = items.map(({tabName}) => ({title: tabName, id: tabName}));
3233
const activeTabData = items.find(({tabName}) => tabName === activeTab);
3334
const isReverse = direction === 'content-media';
35+
const ref = useRef<HTMLDivElement>(null);
36+
const mediaWidth = ref?.current?.offsetWidth;
37+
const mediaHeight = mediaWidth && getHeight(mediaWidth);
3438

3539
let imageProps;
3640

@@ -78,14 +82,17 @@ export const TabsBlock = ({
7882
}}
7983
className={b('col', {centered: centered})}
8084
>
81-
{activeTabData?.media && (
82-
<Media
83-
{...getThemedValue(activeTabData.media, theme)}
84-
key={activeTab}
85-
className={b('media')}
86-
playVideo={play}
87-
/>
88-
)}
85+
<div ref={ref}>
86+
{activeTabData?.media && (
87+
<Media
88+
{...getThemedValue(activeTabData.media, theme)}
89+
key={activeTab}
90+
className={b('media')}
91+
playVideo={play}
92+
height={mediaHeight}
93+
/>
94+
)}
95+
</div>
8996
{imageProps && (
9097
<Fragment>
9198
<FullScreenImage {...imageProps} imageClassName={b('image')} />

src/components/Media/Media.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export const Media = (props: MediaAllProps) => {
8484
record={youtube}
8585
attributes={{color: 'white', rel: '0'}}
8686
previewImg={previewImg}
87+
height={height}
8788
/>
8889
);
8990
}

src/components/Media/Video/Video.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,18 @@ const Video = (props: VideoAllProps) => {
7979
playButton={playButton || commonPlayButton}
8080
customBarControlsClassName={customBarControlsClassName}
8181
metrika={metrika}
82+
height={height}
8283
/>
8384
);
8485
}, [
8586
video,
86-
metrika,
87+
height,
88+
videoClassName,
8789
previewImg,
8890
playVideo,
89-
videoClassName,
9091
commonPlayButton,
9192
customBarControlsClassName,
93+
metrika,
9294
]);
9395

9496
const defaultVideoBlock = useMemo(() => {

src/components/ReactPlayer/ReactPlayer.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface ReactPlayerBlockProps
4242
customBarControlsClassName?: string;
4343
showPreview?: boolean;
4444
onClickPreview?: () => void;
45+
height?: number;
4546
children?: React.ReactNode;
4647
}
4748

@@ -67,6 +68,7 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
6768
showPreview,
6869
onClickPreview,
6970
metrika: videoMetrika,
71+
height,
7072
} = props;
7173

7274
const {
@@ -86,7 +88,7 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
8688
const [playerRef, setPlayerRef] = useState<ReactPlayer>();
8789
const [isPlaying, setIsPlaying] = useState(autoPlay);
8890
const [playedPercent, setPlayedPercent] = useState<number>(0);
89-
const [height, setHeight] = useState<number>(0);
91+
const [currentHeight, setCurrentHeight] = useState(height);
9092
const [width, setWidth] = useState<number>(0);
9193
const [muted, setMuted] = useState<boolean>(mute);
9294
const [started, setStarted] = useState(autoPlay);
@@ -141,7 +143,7 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
141143
parseFloat(paddingRight);
142144

143145
setWidth(newWidth);
144-
setHeight(Math.floor(getHeight(newWidth)));
146+
setCurrentHeight(Math.floor(getHeight(newWidth)));
145147
}
146148
}, 200);
147149

@@ -292,13 +294,17 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
292294
);
293295

294296
return (
295-
<div className={b({wrapper: !height}, className)} ref={ref} onClick={handleClick}>
297+
<div
298+
className={b({wrapper: !currentHeight}, className)}
299+
ref={ref}
300+
onClick={handleClick}
301+
>
296302
<ReactPlayer
297303
className={b('player')}
298304
url={src}
299305
muted={muted}
300306
controls={controls === MediaVideoControlsType.Default}
301-
height={height || '100%'}
307+
height={currentHeight || '100%'}
302308
width={width || '100%'}
303309
light={previewImgUrl}
304310
playing={isPlaying}

src/components/VideoBlock/VideoBlock.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function getVideoSrc(stream?: string, record?: string) {
4040
return src;
4141
}
4242

43-
function getHeight(width: number): number {
43+
export function getHeight(width: number): number {
4444
return (width / 16) * 9;
4545
}
4646

@@ -52,16 +52,17 @@ export interface VideoBlockProps {
5252
className?: string;
5353
previewImg?: string;
5454
playButton?: React.ReactNode;
55+
height?: number;
5556
}
5657

5758
const VideoBlock = (props: VideoBlockProps) => {
58-
const {stream, record, attributes, className, id, previewImg, playButton} = props;
59+
const {stream, record, attributes, className, id, previewImg, playButton, height} = props;
5960
const src = getVideoSrc(stream, record);
6061
const ref = useRef<HTMLDivElement>(null);
6162
const iframeRef = useRef<HTMLIFrameElement>();
6263
const [hidePreview, setHidePreview] = useState(false);
6364
const norender = (!stream && !record) || !src;
64-
const [height, setHeight] = useState<number>();
65+
const [currentHeight, setCurrentHeight] = useState(height || undefined);
6566
const fullId = `${iframeId}-${id || src}`;
6667
const onPreviewClick = useCallback(() => {
6768
if (iframeRef.current) {
@@ -76,15 +77,17 @@ const VideoBlock = (props: VideoBlockProps) => {
7677

7778
useEffect(() => {
7879
const updateSize = _.debounce(() => {
79-
setHeight(ref.current ? Math.round(getHeight(ref.current.offsetWidth)) : undefined);
80+
setCurrentHeight(
81+
ref.current ? Math.round(getHeight(ref.current.offsetWidth)) : undefined,
82+
);
8083
}, 100);
8184

8285
updateSize();
8386
window.addEventListener('resize', updateSize);
8487
return () => {
8588
window.removeEventListener('resize', updateSize);
8689
};
87-
}, []);
90+
}, [height]);
8891

8992
useEffect(() => {
9093
if (norender) {
@@ -119,7 +122,7 @@ const VideoBlock = (props: VideoBlockProps) => {
119122
}
120123

121124
return (
122-
<div className={b(null, className)} ref={ref} style={{height}}>
125+
<div className={b(null, className)} ref={ref} style={{height: currentHeight}}>
123126
{previewImg && !hidePreview && (
124127
<div className={b('preview')} onClick={onPreviewClick}>
125128
<Image src={previewImg} className={b('image')} />

0 commit comments

Comments
 (0)