-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathHeader.tsx
194 lines (178 loc) · 7.78 KB
/
Header.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import React, {useContext} from 'react';
import {useUniqId} from '@gravity-ui/uikit';
import {Button, HTML, Media, RouterLink} from '../../components';
import HeaderBreadcrumbs from '../../components/HeaderBreadcrumbs/HeaderBreadcrumbs';
import {getMediaImage} from '../../components/Media/Image/utils';
import YFMWrapper from '../../components/YFMWrapper/YFMWrapper';
import {MobileContext} from '../../context/mobileContext';
import {useTheme} from '../../context/theme';
import {Col, Grid, Row} from '../../grid';
import {ClassNameProps, HeaderBlockBackground, HeaderBlockProps} from '../../models';
import {block, getThemedValue} from '../../utils';
import {getImageSize, getTitleSizes, titleWithImageSizes} from './utils';
import './Header.scss';
const b = block('header-block');
type HeaderBlockFullProps = HeaderBlockProps & ClassNameProps;
interface BackgroundProps {
background: HeaderBlockBackground;
isMobile: boolean;
}
const Background = ({background, isMobile}: BackgroundProps) => {
const {url, image, fullWidthMedia, video, color} = background;
const imageObject = url ? getMediaImage(url) : image;
const renderMedia = !isMobile || (typeof image === 'object' && 'mobile' in image);
return (
<div
className={b('background', {media: true, 'full-width-media': fullWidthMedia})}
style={{backgroundColor: color}}
>
{renderMedia && (
<Media
{...background}
className={b('background-media')}
imageClassName={b('image')}
videoClassName={b('video')}
isBackground={true}
parallax={false}
video={isMobile ? undefined : video}
image={imageObject}
/>
)}
</div>
);
};
interface FullWidthBackgroundProps {
background: HeaderBlockBackground;
}
const FullWidthBackground = ({background}: FullWidthBackgroundProps) => (
<div
className={b('background', {['full-width']: true})}
style={{backgroundColor: background?.color}}
/>
);
export const HeaderBlock = (props: React.PropsWithChildren<HeaderBlockFullProps>) => {
const {
title,
overtitle,
description,
buttons,
image,
video,
width = 'm',
imageSize,
offset = 'default',
background,
theme: textTheme = 'light',
verticalOffset = 'm',
className,
breadcrumbs,
status,
renderTitle,
children,
mediaView = 'full',
} = props;
const isMobile = useContext(MobileContext);
const theme = useTheme();
const hasRightSideImage = Boolean(image || video);
const curImageSize = imageSize || getImageSize(width);
const titleSizes = hasRightSideImage ? titleWithImageSizes(curImageSize) : getTitleSizes(width);
let curVerticalOffset = verticalOffset;
if (hasRightSideImage && !verticalOffset) {
curVerticalOffset = 'm';
}
const backgroundThemed = background && getThemedValue(background, theme);
const imageThemed = image && getThemedValue(image, theme);
const videoThemed = video && getThemedValue(video, theme);
const fullWidth = backgroundThemed?.fullWidth || backgroundThemed?.fullWidthMedia;
const titleId = useUniqId();
return (
<header
className={b(
{
['has-media']: hasRightSideImage,
['full-width']: fullWidth,
['media-view']: mediaView,
['controls-view']: textTheme,
},
className,
)}
>
{backgroundThemed && fullWidth && <FullWidthBackground background={backgroundThemed} />}
{backgroundThemed && <Background background={backgroundThemed} isMobile={isMobile} />}
<Grid containerClass={b('container-fluid')}>
{breadcrumbs && (
<Row className={b('breadcrumbs')}>
<Col>
<HeaderBreadcrumbs {...breadcrumbs} theme={textTheme} />
</Col>
</Row>
)}
<Row>
<Col reset className={b('content-wrapper')}>
<Row>
<Col
className={b('content', {
offset,
theme: textTheme,
'vertical-offset': curVerticalOffset,
})}
>
<Col sizes={titleSizes} className={b('content-inner')}>
{overtitle && (
<div className={b('overtitle')}>
<HTML>{overtitle}</HTML>
</div>
)}
<h1 className={b('title')} id={titleId}>
{status}
{renderTitle ? renderTitle(title) : <HTML>{title}</HTML>}
</h1>
{description && (
<div className={b('description', {theme: textTheme})}>
<YFMWrapper
content={description}
modifiers={{
constructor: true,
constructorTheme: textTheme,
}}
/>
</div>
)}
{buttons && (
<div className={b('buttons')} data-qa="header-buttons">
{buttons.map((button, index) => (
<RouterLink href={button.url} key={index}>
<Button
key={index}
className={b('button')}
size="xl"
extraProps={{
'aria-describedby': titleId,
...button.extraProps,
}}
{...button}
/>
</RouterLink>
))}
</div>
)}
{children}
</Col>
</Col>
</Row>
{hasRightSideImage && (
<Media
className={b('media', {[curImageSize]: true})}
videoClassName={b('video')}
imageClassName={b('image')}
video={videoThemed}
image={imageThemed}
/>
)}
</Col>
</Row>
</Grid>
</header>
);
};
export default HeaderBlock;