-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleCard.tsx
More file actions
116 lines (110 loc) · 2.73 KB
/
Copy pathSimpleCard.tsx
File metadata and controls
116 lines (110 loc) · 2.73 KB
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
import React from 'react';
import classNames from 'classnames';
import {
Button,
ButtonPresetTheme,
ButtonVariant,
IconAngleRight,
IconSize,
} from 'hds-react';
import styles from '../pageModules.module.scss';
import colorStyles from '../../styles/background.module.scss';
import {
getColor,
getIconName,
getTextFromHtml,
isWhiteText,
} from '../../utils/string';
import { Icon } from './Icon';
import { useConfig } from '../../configProvider/useConfig';
type CardProps = {
title?: string;
description?: string;
backgroundColor?: string;
icon?: string;
linkTitle?: string;
linkTarget?: string;
linkUrl?: string;
direction?: 'vertical' | 'horisontal';
};
export function SimpleCard({
title,
description,
backgroundColor,
icon,
linkTitle,
linkTarget,
linkUrl,
direction = 'vertical',
}: CardProps) {
const {
utils: { redirectToUrl },
} = useConfig();
const openInNewTab = (url: string): void => {
const newWindow = window.open(url, '_blank', 'noopener,noreferrer');
if (newWindow) newWindow.opener = null;
};
const handleClick = () => {
if (linkUrl) {
if (linkTarget === '_blank') {
openInNewTab(linkUrl);
} else {
redirectToUrl(linkUrl);
}
}
};
return (
<div
className={classNames(
styles.cardWrapper,
backgroundColor &&
colorStyles[`background${getColor(backgroundColor)}`],
backgroundColor &&
isWhiteText(backgroundColor) &&
colorStyles.whiteText,
direction && styles[direction],
)}
>
{icon && (
<div
className={classNames(
styles.cardIconWrapper,
direction && styles[direction],
)}
>
<Icon name={getIconName(icon)} />
</div>
)}
<div className={styles.cardContent}>
{title && <div className={styles.title}>{title}</div>}
{description && (
<div className={styles.description}>
{getTextFromHtml(description)}
</div>
)}
{linkTitle && linkUrl && (
<div
className={classNames(
styles.button,
backgroundColor &&
isWhiteText(backgroundColor) &&
colorStyles.whiteButton,
)}
>
<Button
style={direction === 'vertical' ? { width: '100%' } : {}}
variant={ButtonVariant.Secondary}
theme={ButtonPresetTheme.Black}
onClick={handleClick}
iconEnd={
<IconAngleRight aria-hidden="true" size={IconSize.Small} />
}
>
{linkTitle}
</Button>
</div>
)}
</div>
</div>
);
}