1- import React , { Fragment , useRef , useState } from 'react' ;
1+ import React , { Fragment , useCallback , useRef , useState } from 'react' ;
22
33import { useUniqId } from '@gravity-ui/uikit' ;
44
@@ -12,9 +12,10 @@ import {getHeight} from '../../components/VideoBlock/VideoBlock';
1212import { useTheme } from '../../context/theme' ;
1313import { Col , GridColumnOrderClasses , Row } from '../../grid' ;
1414import { TabsBlockProps } from '../../models' ;
15- import { Content } from '../../sub-blocks' ;
1615import { block , getThemedValue } from '../../utils' ;
1716
17+ import TabsTextContent from './TabsTextContent/TabsTextContent' ;
18+
1819import './Tabs.scss' ;
1920
2021const b = block ( 'tabs-block' ) ;
@@ -37,15 +38,32 @@ export const TabsBlock = ({
3738 const isReverse = direction === 'content-media' ;
3839 const ref = useRef < HTMLDivElement > ( null ) ;
3940 const mediaWidth = ref ?. current ?. offsetWidth ;
40- const mediaHeight = mediaWidth && getHeight ( mediaWidth ) ;
4141 const captionId = useUniqId ( ) ;
42-
42+ const themedMedia = getThemedValue ( activeTabData ?. media , theme ) ;
43+ const hasNoImage = ! themedMedia ?. image || ! activeTabData ?. image ;
44+ const mediaVideoHeight = hasNoImage && mediaWidth && getHeight ( mediaWidth ) ;
45+ const [ minImageHeight , setMinImageHeight ] = useState ( ref ?. current ?. offsetHeight ) ;
46+ // TODO remove property support activeTabData?.image. Use only activeTabData?.media?.image
4347 let imageProps ;
4448
49+ const handleImageHeight = useCallback ( ( ) => {
50+ setMinImageHeight ( ref ?. current ?. offsetHeight ) ;
51+ } , [ ] ) ;
52+
53+ const onSelectTab = (
54+ id : string | null ,
55+ e : React . MouseEvent < HTMLButtonElement | HTMLAnchorElement > ,
56+ ) => {
57+ setActiveTab ( id ) ;
58+ handleImageHeight ( ) ;
59+ e . currentTarget . scrollIntoView ( { inline : 'center' , behavior : 'smooth' , block : 'nearest' } ) ;
60+ } ;
61+
4562 if ( activeTabData ) {
46- const themedImage = getThemedValue ( activeTabData . image , theme ) ;
63+ const themedImage = getThemedValue ( activeTabData ? .image , theme ) ;
4764
4865 imageProps = themedImage && getMediaImage ( themedImage ) ;
66+
4967 if ( activeTabData . caption && imageProps ) {
5068 Object . assign ( imageProps , {
5169 'aria-describedby' : captionId ,
@@ -57,29 +75,14 @@ export const TabsBlock = ({
5775 const showText = Boolean ( activeTabData ?. text ) ;
5876
5977 const textContent = activeTabData && showText && (
60- < Col
61- sizes = { { all : 12 , md : showMedia ? 4 : 8 } }
62- className = { b ( 'content' , { centered : centered } ) }
63- >
64- < div
65- className = { b ( 'content-wrapper' , {
66- margin : Boolean ( ( activeTabData ?. media || imageProps ) && ! isReverse ) ,
67- } ) }
68- >
69- < Content
70- title = { activeTabData . title }
71- text = { activeTabData . text }
72- additionalInfo = { activeTabData . additionalInfo }
73- size = { contentSize }
74- links = { [
75- ...( activeTabData . link ? [ activeTabData . link ] : [ ] ) ,
76- ...( activeTabData . links || [ ] ) ,
77- ] }
78- buttons = { activeTabData . buttons }
79- colSizes = { { all : 12 } }
80- />
81- </ div >
82- </ Col >
78+ < TabsTextContent
79+ showMedia = { showMedia }
80+ data = { activeTabData }
81+ imageProps = { imageProps ? imageProps : undefined }
82+ isReverse = { isReverse }
83+ contentSize = { contentSize }
84+ centered = { centered }
85+ />
8386 ) ;
8487
8588 const mediaContent = showMedia && (
@@ -91,15 +94,18 @@ export const TabsBlock = ({
9194 } }
9295 className = { b ( 'col' , { centered : centered } ) }
9396 >
94- < div ref = { ref } >
97+ < div style = { { minHeight : mediaVideoHeight || minImageHeight } } >
9598 { activeTabData ?. media && (
96- < Media
97- { ...getThemedValue ( activeTabData . media , theme ) }
98- key = { activeTab }
99- className = { b ( 'media' ) }
100- playVideo = { play }
101- height = { mediaHeight }
102- />
99+ < div ref = { ref } >
100+ < Media
101+ { ...getThemedValue ( activeTabData . media , theme ) }
102+ key = { activeTab }
103+ className = { b ( 'media' ) }
104+ playVideo = { play }
105+ height = { mediaVideoHeight || undefined }
106+ onImageLoad = { handleImageHeight }
107+ />
108+ </ div >
103109 ) }
104110 </ div >
105111 { imageProps && (
@@ -115,20 +121,12 @@ export const TabsBlock = ({
115121 </ Col >
116122 ) ;
117123
118- const onSelectTab = (
119- id : string | null ,
120- e : React . MouseEvent < HTMLButtonElement | HTMLAnchorElement > ,
121- ) => {
122- setActiveTab ( id ) ;
123- e . currentTarget . scrollIntoView ( { inline : 'center' , behavior : 'smooth' , block : 'nearest' } ) ;
124- } ;
125-
126124 return (
127125 < AnimateBlock className = { b ( ) } onScroll = { ( ) => setPlay ( true ) } animate = { animated } >
128126 < Title
129127 title = { title }
130128 subtitle = { description }
131- className = { b ( 'block- title' , { centered : centered } ) }
129+ className = { b ( 'title' , { centered : centered } ) }
132130 />
133131 < Row >
134132 < Col sizes = { tabsColSizes } >
0 commit comments