-
Notifications
You must be signed in to change notification settings - Fork 11
/
advanced-data-card.tsx
431 lines (404 loc) · 16 KB
/
advanced-data-card.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// https://www.figma.com/file/35iS2dwoogulJZPgFGKmCX/Explora%C3%A7%C3%A3o-Data-Card-%2F-Slots?type=design&node-id=443-32508&mode=design&t=wwoi2TRLVsjwGYAb-0
'use client';
import * as React from 'react';
import {Boxed} from '../boxed';
import Stack from '../stack';
import * as styles from './advanced-data-card.css';
import * as mediaStyles from '../image.css';
import Divider from '../divider';
import {Text2, Text} from '../text';
import * as textProps from '../text-props';
import {vars} from '../skins/skin-contract.css';
import Box from '../box';
import Touchable from '../touchable';
import classNames from 'classnames';
import {CardActionsGroup, useInnerText} from '../card';
import {useTheme} from '../hooks';
import {getPrefixedDataAttributes} from '../utils/dom';
import Inline from '../inline';
import {applyCssVars} from '../utils/css';
import Tag from '../tag';
import {isBiggerHeading} from '../utils/headings';
import type {PressHandler} from '../touchable';
import type {ExclusifyUnion} from '../utils/utility-types';
import type {CardAction} from '../card';
import type StackingGroup from '../stacking-group';
import type Image from '../image';
import type {ButtonPrimary, ButtonLink} from '../button';
import type {DataAttributes, HeadingType, TrackingEvent} from '../utils/types';
import type {RendersNullableElement} from '../utils/renders-element';
import type {
HighlightedValueBlock,
InformationBlock,
ProgressBlock,
RowBlock,
SimpleBlock,
ValueBlock,
} from './blocks';
type TouchableProps = {
trackingEvent?: TrackingEvent | ReadonlyArray<TrackingEvent>;
role?: string;
} & ExclusifyUnion<
| {
href: string | undefined;
newTab?: boolean;
}
| {
to: string | undefined;
newTab?: boolean;
fullPageOnWebView?: boolean;
}
| {
onPress: PressHandler | undefined;
}
>;
type TouchableCard<T> = T & TouchableProps;
type MaybeTouchableCard<T> = ExclusifyUnion<TouchableCard<T> | T>;
type CardContentProps = {
headline?: string | RendersNullableElement<typeof Tag>;
headlineRef?: (instance: HTMLElement | null) => void;
pretitle?: string;
pretitleAs?: HeadingType;
pretitleLinesMax?: number;
title?: string;
titleAs: HeadingType;
titleLinesMax?: number;
subtitle?: string;
subtitleLinesMax?: number;
description?: string;
descriptionLinesMax?: number;
};
const CardContent = ({
headline,
headlineRef,
pretitle,
pretitleAs,
pretitleLinesMax,
title,
titleAs,
titleLinesMax,
subtitle,
subtitleLinesMax,
description,
descriptionLinesMax,
}: CardContentProps) => {
const {textPresets} = useTheme();
return (
/** using flex instead of nested Stacks, this way we can rearrange texts so the DOM structure makes more sense for screen reader users */
<div className={styles.flexColumn}>
{isBiggerHeading(titleAs, pretitleAs) ? (
<>
{title && (
<div style={{paddingBottom: subtitle || description ? 4 : 0}}>
<Text
{...textProps.text4}
truncate={titleLinesMax}
weight={textPresets.cardTitle.weight}
as={titleAs}
hyphens="auto"
>
{title}
</Text>
</div>
)}
{headline && (
<div
ref={headlineRef}
style={{
order: -2,
paddingBottom: pretitle || title || subtitle || description ? 4 : 0,
}}
>
{typeof headline === 'string' ? <Tag type="promo">{headline}</Tag> : headline}
</div>
)}
{pretitle && (
<div style={{order: -1, paddingBottom: title || subtitle || description ? 4 : 0}}>
<Text2 truncate={pretitleLinesMax} as={pretitleAs} regular hyphens="auto">
{pretitle}
</Text2>
</div>
)}
</>
) : (
<>
{pretitle && (
<div style={{paddingBottom: title || subtitle || description ? 4 : 0}}>
<Text2 truncate={pretitleLinesMax} as={pretitleAs} regular hyphens="auto">
{pretitle}
</Text2>
</div>
)}
{headline && (
<div
ref={headlineRef}
style={{
order: -1,
paddingBottom: pretitle || title || subtitle || description ? 4 : 0,
}}
>
{typeof headline === 'string' ? <Tag type="promo">{headline}</Tag> : headline}
</div>
)}
{title && (
<div style={{paddingBottom: subtitle || description ? 4 : 0}}>
<Text
{...textProps.text4}
truncate={titleLinesMax}
weight={textPresets.cardTitle.weight}
as={titleAs}
hyphens="auto"
>
{title}
</Text>
</div>
)}
</>
)}
{subtitle && (
<div style={{paddingBottom: description ? 4 : 0}}>
<Text2 truncate={subtitleLinesMax} as="div" regular hyphens="auto">
{subtitle}
</Text2>
</div>
)}
{description && (
<Text2
truncate={descriptionLinesMax}
as="div"
regular
color={vars.colors.textSecondary}
hyphens="auto"
>
{description}
</Text2>
)}
</div>
);
};
type CardFooterProps = {
button?: RendersNullableElement<typeof ButtonPrimary>;
footerImage?: RendersNullableElement<typeof Image>;
footerText?: string;
footerTextLinesMax?: number;
buttonLink?: RendersNullableElement<typeof ButtonLink>;
};
const CardFooter = ({button, footerImage, footerText, footerTextLinesMax, buttonLink}: CardFooterProps) => {
const hasButton = !!button;
const hasFooterImage = !!footerImage;
const hasFooterText = !!footerText;
const hasButtonLink = !!buttonLink;
const hasAllItens = hasButton && (hasFooterImage || hasFooterText) && hasButtonLink;
const maxWidth = hasButtonLink && !hasAllItens ? '178px' : '';
return (
<>
<div className={styles.divider}>
<Divider />
</div>
<div
className={classNames(
styles.actions,
styles.actionsVariants[hasAllItens ? 'mobile' : 'default']
)}
>
{hasButton && (
<div className={classNames(styles.marginRightButton, styles.buttonMobile)}>{button}</div>
)}
<div
className={classNames(
styles.footerDirection,
hasAllItens ? styles.marginRightAuto : styles.withPaddingTop
)}
>
{hasFooterImage && (
<div className={styles.footerImage}>
<div
style={applyCssVars({
[mediaStyles.vars.mediaBorderRadius]: vars.borderRadii.mediaSmall,
})}
>
{footerImage}
</div>
</div>
)}
{hasFooterText && (
<div style={{maxWidth}} className={styles.footerText}>
<Text2 truncate={footerTextLinesMax} regular>
{footerText}
</Text2>
</div>
)}
</div>
{hasButtonLink && (
<div
className={classNames(
styles.adjustButtonLink,
hasAllItens ? styles.button : styles.buttonMobile
)}
>
{buttonLink}
</div>
)}
</div>
</>
);
};
type AllowedExtra =
| typeof StackingGroup
| typeof HighlightedValueBlock
| typeof InformationBlock
| typeof ProgressBlock
| typeof RowBlock
| typeof SimpleBlock
| typeof ValueBlock;
type AdvancedDataCardProps = MaybeTouchableCard<{
stackingGroup?: RendersNullableElement<typeof StackingGroup>;
headline?: RendersNullableElement<typeof Tag>;
pretitle?: string;
pretitleLinesMax?: number;
pretitleAs?: HeadingType;
title?: string;
titleLinesMax?: number;
titleAs?: HeadingType;
subtitle?: string;
subtitleLinesMax?: number;
description?: string;
descriptionLinesMax?: number;
extra?: ReadonlyArray<RendersNullableElement<AllowedExtra>>;
extraDividerPadding?: 8 | 16 | 24;
noExtraDivider?: boolean;
button?: RendersNullableElement<typeof ButtonPrimary>;
footerImage?: RendersNullableElement<typeof Image>;
footerText?: string;
footerTextLinesMax?: number;
buttonLink?: RendersNullableElement<typeof ButtonLink>;
dataAttributes?: DataAttributes;
actions?: ReadonlyArray<CardAction | React.ReactElement>;
'aria-label'?: string;
onClose?: () => void;
}>;
export const AdvancedDataCard = React.forwardRef<HTMLDivElement, AdvancedDataCardProps>(
(
{
stackingGroup,
headline,
pretitle,
pretitleAs,
pretitleLinesMax,
title,
titleAs = 'h3',
titleLinesMax,
subtitle,
subtitleLinesMax,
description,
descriptionLinesMax,
extra,
extraDividerPadding = 24,
noExtraDivider = false,
button,
footerImage,
footerText,
footerTextLinesMax,
buttonLink,
dataAttributes,
actions,
'aria-label': ariaLabelProp,
onClose,
...touchableProps
},
ref
) => {
const isTouchable = !!touchableProps.href || !!touchableProps.onPress || !!touchableProps.to;
const footerProps = {button, footerImage, footerText, footerTextLinesMax, buttonLink};
const hasFooter = !!button || !!footerImage || !!footerText || !!buttonLink;
const hasExtras = !!extra?.length;
const topActionsCount = (actions?.length || 0) + (onClose ? 1 : 0);
const {text: headlineText, ref: headlineRef} = useInnerText();
const {text: extraText, ref: extraRef} = useInnerText();
const ariaLabel =
ariaLabelProp ||
(isBiggerHeading(titleAs, pretitleAs)
? [title, headlineText, pretitle, subtitle, description, extraText]
: [pretitle, headlineText, title, subtitle, description, extraText]
)
.filter(Boolean)
.join(' ');
return (
<section
className={styles.container}
{...getPrefixedDataAttributes(dataAttributes, 'AdvancedDataCard')}
ref={ref}
aria-label={isTouchable ? undefined : ariaLabel}
>
<Boxed className={styles.dataCard} width="100%" height="100%" minHeight={styles.MIN_HEIGHT}>
<Touchable
maybe
{...touchableProps}
aria-label={isTouchable ? ariaLabel : undefined}
className={styles.touchable}
>
{isTouchable && <div className={styles.touchableCardHoverOverlay} />}
<div
className={classNames(
styles.cardContentStyle,
!hasFooter && !hasExtras ? styles.minHeight : ''
)}
aria-hidden={isTouchable}
>
<Box paddingTop={8}>
<Inline space={0}>
<Stack space={8}>
{stackingGroup}
<CardContent
headline={headline}
headlineRef={headlineRef}
pretitle={pretitle}
pretitleAs={pretitleAs}
pretitleLinesMax={pretitleLinesMax}
title={title}
titleAs={titleAs}
titleLinesMax={titleLinesMax}
subtitle={subtitle}
subtitleLinesMax={subtitleLinesMax}
description={description}
descriptionLinesMax={descriptionLinesMax}
/>
</Stack>
{/** Hack to avoid content from rendering on top of the top action buttons */}
{!stackingGroup && (
<div
style={applyCssVars({
[styles.vars.topActionsCount]: String(topActionsCount),
})}
className={styles.topActionsWithoutIcon}
/>
)}
</Inline>
</Box>
</div>
<div style={{flexGrow: 1}} />
{hasExtras && (
<div className={styles.extra} ref={extraRef}>
{extra.map((item, index) => {
return (
<div key={index}>
<div className={styles.paddingX}>{item}</div>
{index + 1 !== extra.length && (
<Box paddingY={extraDividerPadding}>
{!noExtraDivider && <Divider />}
</Box>
)}
</div>
);
})}
</div>
)}
</Touchable>
{hasFooter && <CardFooter {...footerProps} />}
</Boxed>
<CardActionsGroup actions={actions} onClose={onClose} />
</section>
);
}
);
export default AdvancedDataCard;