forked from bmcmahen/sancho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlert.tsx
199 lines (187 loc) · 4.9 KB
/
Alert.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
/** @jsx jsx */
import { jsx } from "@emotion/core";
import * as React from "react";
import ReachAlert from "@reach/alert";
import { Text } from "./Text";
import { CloseButton } from "./IconButton";
import { LayerElevations } from "./Layer";
import PropTypes from "prop-types";
import { Theme } from "./Theme";
import { useTheme } from "./Theme/Providers";
import { IconWrapper } from "./IconWrapper";
import {
IconInfo,
IconCheckCircle,
IconAlertCircle,
IconAlertOctagon,
IconHelpCircle
} from "./Icons";
const alertIntentions = (theme: Theme) => ({
info: theme.colors.intent.none,
success: theme.colors.intent.success,
question: theme.colors.intent.primary,
danger: theme.colors.intent.danger,
warning: theme.colors.intent.warning
});
export type AlertIntentions =
| "info"
| "success"
| "question"
| "danger"
| "warning";
const icons: { [key in AlertIntentions]: React.ReactNode } = {
info: <IconInfo />,
success: <IconCheckCircle />,
warning: <IconAlertCircle />,
danger: <IconAlertOctagon />,
question: <IconHelpCircle />
};
export interface AlertProps extends React.HTMLAttributes<HTMLDivElement> {
/** If used, a close button will be attached to the alert box. */
onRequestClose?: () => void;
/** Primary text */
title?: string;
/** Secondary text */
subtitle?: string | React.ReactNode;
/** A unique id used for accessibility purposes */
id?: string;
elevation?: LayerElevations;
/** Optionally render children if a title is not specified. Used for custom alerts. */
children?: React.ReactNode;
/** Changes the icon and colour of the alert. */
intent?: AlertIntentions;
component?: React.ReactType<any>;
type?: "polite" | "assertive";
}
/**
* Use an alert to inform users of important information.
* To display an alert in a toast notification, use the toast module.
*/
export const Alert: React.FunctionComponent<AlertProps> = ({
children,
title,
id,
subtitle,
component,
elevation = "xs",
onRequestClose,
intent = "info",
...other
}) => {
const theme = useTheme();
const intentions = React.useMemo(() => alertIntentions(theme), [theme]);
const dark = theme.colors.mode === "dark";
const color = intentions[intent];
let accent = dark ? color.light : color.base;
if (dark && intent === "info") {
accent = color.light;
}
const icon = icons[intent];
const contents = title ? (
<div
className="Alert__title"
css={{ display: "flex", alignItems: "flex-start" }}
>
<div css={{ flex: "0 0 auto", marginTop: "2px" }}>
<IconWrapper size="md" color={accent}>
{icon}
</IconWrapper>
</div>
<div
className="Alert__text-content"
css={{ marginLeft: theme.spaces.md }}
>
<Text
className="Alert__title-text"
id={id}
css={{ margin: 0 }}
variant="h6"
>
{title}
</Text>
{subtitle && (
<Text
className="Alert__title-subtitle"
muted
css={{
fontSize: theme.fontSizes[0]
}}
>
{subtitle}
</Text>
)}
{children}
</div>
</div>
) : (
children
);
const Component = component || ReachAlert;
return (
<Component
className="Alert"
css={{
backgroundColor: dark
? theme.colors.background.tint1
: theme.colors.background.default,
overflow: "hidden",
position: "relative",
boxShadow: theme.shadows[elevation],
borderRadius: theme.radii.md
}}
{...other}
>
<div>
<div
className="Alert__bar"
css={{
width: theme.radii.md,
position: "absolute",
top: 0,
left: 0,
bottom: 0,
backgroundColor: accent
}}
/>
<div
className="Alert__content"
css={{
display: "flex",
alignItems: "flex-start",
padding: `${theme.spaces.md} ${theme.spaces.md}`,
paddingRight: onRequestClose ? "3.5rem" : undefined
}}
>
{contents}
{onRequestClose && (
<CloseButton
css={{
marginTop: "-0.45rem",
right: theme.spaces.sm,
position: "absolute"
}}
onClick={onRequestClose}
/>
)}
</div>
</div>
</Component>
);
};
Alert.displayName = "Alert";
Alert.propTypes = {
onRequestClose: PropTypes.func,
subtitle: PropTypes.string,
title: PropTypes.string,
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
children: PropTypes.node,
intent: PropTypes.oneOf([
"info",
"success",
"warning",
"danger",
"question"
] as AlertIntentions[]),
elevation: PropTypes.oneOf(["xs", "sm", "md", "lg", "xl"]),
component: PropTypes.elementType
};