|
| 1 | +import * as React from 'react'; |
| 2 | +import {ContentSize, TabsBlockItem} from '../../../models'; |
| 3 | +import {ProjectSettingsContext} from '../../../context/projectSettingsContext'; |
| 4 | +import {block, getThemedValue} from '../../../utils'; |
| 5 | +import {useTheme} from '../../../context/theme'; |
| 6 | +import {getHeight} from '../../../components/VideoBlock/VideoBlock'; |
| 7 | +import {getMediaImage} from '../../../components/Media/Image/utils'; |
| 8 | +import TabsTextContent from '../TabsTextContent/TabsTextContent'; |
| 9 | +import {Col, GridColumnOrderClasses, Row} from '../../../grid'; |
| 10 | +import Media from '../../../components/Media/Media'; |
| 11 | +import {mergeVideoMicrodata} from '../../../utils/microdata'; |
| 12 | +import {FullscreenImage, YFMWrapper} from '../../../components'; |
| 13 | +import {useUniqId} from '@gravity-ui/uikit'; |
| 14 | + |
| 15 | +import './TabContent.scss'; |
| 16 | + |
| 17 | +const b = block('tab-content'); |
| 18 | + |
| 19 | +export interface TabContentProps { |
| 20 | + tabData: TabsBlockItem; |
| 21 | + isActive: boolean; |
| 22 | + isReverse: boolean; |
| 23 | + contentSize: ContentSize; |
| 24 | + centered?: boolean; |
| 25 | + play: boolean; |
| 26 | + getTabElementId?: (tabId: string) => string; |
| 27 | + getTabContentElementId?: (tabId: string) => string; |
| 28 | +} |
| 29 | + |
| 30 | +export const TabContent = ({ |
| 31 | + tabData, |
| 32 | + isActive, |
| 33 | + isReverse, |
| 34 | + contentSize, |
| 35 | + centered, |
| 36 | + play, |
| 37 | + getTabElementId, |
| 38 | + getTabContentElementId, |
| 39 | +}: TabContentProps) => { |
| 40 | + const {tabName} = tabData; |
| 41 | + |
| 42 | + const mediaContainerRef = React.useRef<HTMLDivElement>(null); |
| 43 | + const theme = useTheme(); |
| 44 | + const {renderInvisibleBlocks} = React.useContext(ProjectSettingsContext); |
| 45 | + |
| 46 | + const captionId = useUniqId(); |
| 47 | + |
| 48 | + const mediaWidth = mediaContainerRef?.current?.offsetWidth; |
| 49 | + const [minImageHeight, setMinImageHeight] = React.useState( |
| 50 | + mediaContainerRef?.current?.offsetHeight, |
| 51 | + ); |
| 52 | + |
| 53 | + const shouldRender = renderInvisibleBlocks || isActive; |
| 54 | + |
| 55 | + const themedImage = getThemedValue(tabData.image, theme); |
| 56 | + const themedMedia = getThemedValue(tabData.media, theme); |
| 57 | + |
| 58 | + const hasNoImage = !themedMedia?.image && !tabData.image; |
| 59 | + const mediaVideoHeight = hasNoImage && mediaWidth && getHeight(mediaWidth); |
| 60 | + |
| 61 | + // TODO remove property support activeTabData?.image. Use only activeTabData?.media?.image |
| 62 | + const imageProps = React.useMemo(() => { |
| 63 | + const imagePropsResult = themedImage && getMediaImage(themedImage); |
| 64 | + |
| 65 | + if (tabData.caption && imagePropsResult) { |
| 66 | + Object.assign(imagePropsResult, { |
| 67 | + 'aria-describedby': captionId, |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + return imagePropsResult; |
| 72 | + }, [captionId, tabData.caption, themedImage]); |
| 73 | + |
| 74 | + const handleImageHeight = React.useCallback(() => { |
| 75 | + if (minImageHeight !== mediaContainerRef?.current?.offsetHeight) { |
| 76 | + setMinImageHeight(mediaContainerRef?.current?.offsetHeight); |
| 77 | + } |
| 78 | + }, [minImageHeight]); |
| 79 | + |
| 80 | + React.useEffect(() => { |
| 81 | + handleImageHeight(); |
| 82 | + }, [isActive, handleImageHeight]); |
| 83 | + |
| 84 | + if (!shouldRender) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + const showMedia = isActive && Boolean(tabData.media || imageProps); |
| 89 | + const showText = Boolean(tabData.text); |
| 90 | + const border = tabData.border || 'shadow'; |
| 91 | + |
| 92 | + const textContent = showText && ( |
| 93 | + <TabsTextContent |
| 94 | + showMedia={showMedia} |
| 95 | + data={tabData} |
| 96 | + imageProps={imageProps || undefined} |
| 97 | + isReverse={isReverse} |
| 98 | + contentSize={contentSize} |
| 99 | + centered={centered} |
| 100 | + /> |
| 101 | + ); |
| 102 | + |
| 103 | + const mediaContent = showMedia && ( |
| 104 | + <Col |
| 105 | + sizes={{all: 12, md: 8}} |
| 106 | + orders={{ |
| 107 | + all: GridColumnOrderClasses.Last, |
| 108 | + md: GridColumnOrderClasses.First, |
| 109 | + }} |
| 110 | + className={b('col', {centered: centered})} |
| 111 | + > |
| 112 | + {tabData.media && ( |
| 113 | + <div style={{minHeight: mediaVideoHeight || minImageHeight}}> |
| 114 | + <div ref={mediaContainerRef}> |
| 115 | + <Media |
| 116 | + {...mergeVideoMicrodata(getThemedValue(tabData.media, theme), { |
| 117 | + name: tabData.tabName, |
| 118 | + description: tabData.caption ? tabData.caption : undefined, |
| 119 | + })} |
| 120 | + key={tabName} |
| 121 | + className={b('media', {border})} |
| 122 | + playVideo={play} |
| 123 | + height={mediaVideoHeight || undefined} |
| 124 | + onImageLoad={handleImageHeight} |
| 125 | + /> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + )} |
| 129 | + {imageProps && ( |
| 130 | + <React.Fragment> |
| 131 | + <FullscreenImage {...imageProps} imageClassName={b('image', {border})} /> |
| 132 | + </React.Fragment> |
| 133 | + )} |
| 134 | + {tabData.caption && ( |
| 135 | + <p className={b('caption')} id={captionId}> |
| 136 | + <YFMWrapper |
| 137 | + content={tabData.caption} |
| 138 | + modifiers={{constructor: true}} |
| 139 | + id={captionId} |
| 140 | + /> |
| 141 | + </p> |
| 142 | + )} |
| 143 | + </Col> |
| 144 | + ); |
| 145 | + |
| 146 | + return ( |
| 147 | + <Row |
| 148 | + key={tabName} |
| 149 | + className={b('row', {reverse: isReverse, hidden: !isActive})} |
| 150 | + id={getTabContentElementId?.(tabName)} |
| 151 | + role="tabpanel" |
| 152 | + ariaProps={{ |
| 153 | + 'aria-labelledby': getTabElementId?.(tabName), |
| 154 | + }} |
| 155 | + > |
| 156 | + {mediaContent} |
| 157 | + {textContent} |
| 158 | + </Row> |
| 159 | + ); |
| 160 | +}; |
0 commit comments