-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Alert.js
266 lines (250 loc) · 7.95 KB
/
Alert.js
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
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { unstable_composeClasses as composeClasses } from '@mui/core';
import { darken, lighten } from '@mui/system';
import styled from '../styles/styled';
import useThemeProps from '../styles/useThemeProps';
import capitalize from '../utils/capitalize';
import Paper from '../Paper';
import alertClasses, { getAlertUtilityClass } from './alertClasses';
import IconButton from '../IconButton';
import SuccessOutlinedIcon from '../internal/svg-icons/SuccessOutlined';
import ReportProblemOutlinedIcon from '../internal/svg-icons/ReportProblemOutlined';
import ErrorOutlineIcon from '../internal/svg-icons/ErrorOutline';
import InfoOutlinedIcon from '../internal/svg-icons/InfoOutlined';
import CloseIcon from '../internal/svg-icons/Close';
const useUtilityClasses = (ownerState) => {
const { variant, color, severity, classes } = ownerState;
const slots = {
root: ['root', `${variant}${capitalize(color || severity)}`, `${variant}`],
icon: ['icon'],
message: ['message'],
action: ['action'],
};
return composeClasses(slots, getAlertUtilityClass, classes);
};
const AlertRoot = styled(Paper, {
name: 'MuiAlert',
slot: 'Root',
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [
styles.root,
styles[ownerState.variant],
styles[`${ownerState.variant}${capitalize(ownerState.color || ownerState.severity)}`],
];
},
})(({ theme, ownerState }) => {
const getColor = theme.palette.mode === 'light' ? darken : lighten;
const getBackgroundColor = theme.palette.mode === 'light' ? lighten : darken;
const color = ownerState.color || ownerState.severity;
return {
...theme.typography.body2,
borderRadius: theme.shape.borderRadius,
backgroundColor: 'transparent',
display: 'flex',
padding: '6px 16px',
...(color &&
ownerState.variant === 'standard' && {
color: getColor(theme.palette[color].light, 0.6),
backgroundColor: getBackgroundColor(theme.palette[color].light, 0.9),
[`& .${alertClasses.icon}`]: {
color:
theme.palette.mode === 'dark' ? theme.palette[color].main : theme.palette[color].light,
},
}),
...(color &&
ownerState.variant === 'outlined' && {
color: getColor(theme.palette[color].light, 0.6),
border: `1px solid ${theme.palette[color].light}`,
[`& .${alertClasses.icon}`]: {
color:
theme.palette.mode === 'dark' ? theme.palette[color].main : theme.palette[color].light,
},
}),
...(color &&
ownerState.variant === 'filled' && {
color: '#fff',
fontWeight: theme.typography.fontWeightMedium,
backgroundColor:
theme.palette.mode === 'dark' ? theme.palette[color].dark : theme.palette[color].main,
}),
};
});
const AlertIcon = styled('div', {
name: 'MuiAlert',
slot: 'Icon',
overridesResolver: (props, styles) => styles.icon,
})({
marginRight: 12,
padding: '7px 0',
display: 'flex',
fontSize: 22,
opacity: 0.9,
});
const AlertMessage = styled('div', {
name: 'MuiAlert',
slot: 'Message',
overridesResolver: (props, styles) => styles.message,
})({
padding: '8px 0',
});
const AlertAction = styled('div', {
name: 'MuiAlert',
slot: 'Action',
overridesResolver: (props, styles) => styles.action,
})({
display: 'flex',
alignItems: 'flex-start',
padding: '4px 0 0 16px',
marginLeft: 'auto',
marginRight: -8,
});
const defaultIconMapping = {
success: <SuccessOutlinedIcon fontSize="inherit" />,
warning: <ReportProblemOutlinedIcon fontSize="inherit" />,
error: <ErrorOutlineIcon fontSize="inherit" />,
info: <InfoOutlinedIcon fontSize="inherit" />,
};
const Alert = React.forwardRef(function Alert(inProps, ref) {
const props = useThemeProps({ props: inProps, name: 'MuiAlert' });
const {
action,
children,
className,
closeText = 'Close',
color,
icon,
iconMapping = defaultIconMapping,
onClose,
role = 'alert',
severity = 'success',
variant = 'standard',
...other
} = props;
const ownerState = {
...props,
color,
severity,
variant,
};
const classes = useUtilityClasses(ownerState);
return (
<AlertRoot
role={role}
square
elevation={0}
ownerState={ownerState}
className={clsx(classes.root, className)}
ref={ref}
{...other}
>
{icon !== false ? (
<AlertIcon ownerState={ownerState} className={classes.icon}>
{icon || iconMapping[severity] || defaultIconMapping[severity]}
</AlertIcon>
) : null}
<AlertMessage ownerState={ownerState} className={classes.message}>
{children}
</AlertMessage>
{action != null ? <AlertAction className={classes.action}>{action}</AlertAction> : null}
{action == null && onClose ? (
<AlertAction ownerState={ownerState} className={classes.action}>
<IconButton
size="small"
aria-label={closeText}
title={closeText}
color="inherit"
onClick={onClose}
>
<CloseIcon fontSize="small" />
</IconButton>
</AlertAction>
) : null}
</AlertRoot>
);
});
Alert.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The action to display. It renders after the message, at the end of the alert.
*/
action: PropTypes.node,
/**
* The content of the component.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* Override the default label for the *close popup* icon button.
*
* For localization purposes, you can use the provided [translations](/guides/localization/).
* @default 'Close'
*/
closeText: PropTypes.string,
/**
* The main color for the alert. Unless provided, the value is taken from the `severity` prop.
*/
color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['error', 'info', 'success', 'warning']),
PropTypes.string,
]),
/**
* Override the icon displayed before the children.
* Unless provided, the icon is mapped to the value of the `severity` prop.
* Set to `false` to remove the `icon`.
*/
icon: PropTypes.node,
/**
* The component maps the `severity` prop to a range of different icons,
* for instance success to `<SuccessOutlined>`.
* If you wish to change this mapping, you can provide your own.
* Alternatively, you can use the `icon` prop to override the icon displayed.
*/
iconMapping: PropTypes.shape({
error: PropTypes.node,
info: PropTypes.node,
success: PropTypes.node,
warning: PropTypes.node,
}),
/**
* Callback fired when the component requests to be closed.
* When provided and no `action` prop is set, a close icon button is displayed that triggers the callback when clicked.
* @param {React.SyntheticEvent} event The event source of the callback.
*/
onClose: PropTypes.func,
/**
* The ARIA role attribute of the element.
* @default 'alert'
*/
role: PropTypes.string,
/**
* The severity of the alert. This defines the color and icon used.
* @default 'success'
*/
severity: PropTypes.oneOf(['error', 'info', 'success', 'warning']),
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.object,
/**
* The variant to use.
* @default 'standard'
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['filled', 'outlined', 'standard']),
PropTypes.string,
]),
};
export default Alert;