UNPKG

9.71 kBJavaScriptView Raw
1import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
2import _extends from "@babel/runtime/helpers/esm/extends";
3
4var _CloseIcon;
5
6const _excluded = ["action", "children", "className", "closeText", "color", "icon", "iconMapping", "onClose", "role", "severity", "variant"];
7import * as React from 'react';
8import PropTypes from 'prop-types';
9import clsx from 'clsx';
10import { unstable_composeClasses as composeClasses } from '@mui/base';
11import { darken, lighten } from '@mui/system';
12import styled from '../styles/styled';
13import useThemeProps from '../styles/useThemeProps';
14import capitalize from '../utils/capitalize';
15import Paper from '../Paper';
16import alertClasses, { getAlertUtilityClass } from './alertClasses';
17import IconButton from '../IconButton';
18import SuccessOutlinedIcon from '../internal/svg-icons/SuccessOutlined';
19import ReportProblemOutlinedIcon from '../internal/svg-icons/ReportProblemOutlined';
20import ErrorOutlineIcon from '../internal/svg-icons/ErrorOutline';
21import InfoOutlinedIcon from '../internal/svg-icons/InfoOutlined';
22import CloseIcon from '../internal/svg-icons/Close';
23import { jsx as _jsx } from "react/jsx-runtime";
24import { jsxs as _jsxs } from "react/jsx-runtime";
25
26const useUtilityClasses = ownerState => {
27 const {
28 variant,
29 color,
30 severity,
31 classes
32 } = ownerState;
33 const slots = {
34 root: ['root', `${variant}${capitalize(color || severity)}`, `${variant}`],
35 icon: ['icon'],
36 message: ['message'],
37 action: ['action']
38 };
39 return composeClasses(slots, getAlertUtilityClass, classes);
40};
41
42const AlertRoot = styled(Paper, {
43 name: 'MuiAlert',
44 slot: 'Root',
45 overridesResolver: (props, styles) => {
46 const {
47 ownerState
48 } = props;
49 return [styles.root, styles[ownerState.variant], styles[`${ownerState.variant}${capitalize(ownerState.color || ownerState.severity)}`]];
50 }
51})(({
52 theme,
53 ownerState
54}) => {
55 const getColor = theme.palette.mode === 'light' ? darken : lighten;
56 const getBackgroundColor = theme.palette.mode === 'light' ? lighten : darken;
57 const color = ownerState.color || ownerState.severity;
58 return _extends({}, theme.typography.body2, {
59 backgroundColor: 'transparent',
60 display: 'flex',
61 padding: '6px 16px'
62 }, color && ownerState.variant === 'standard' && {
63 color: theme.vars ? theme.vars.palette.Alert[`${color}Color`] : getColor(theme.palette[color].light, 0.6),
64 backgroundColor: theme.vars ? theme.vars.palette.Alert[`${color}StandardBg`] : getBackgroundColor(theme.palette[color].light, 0.9),
65 [`& .${alertClasses.icon}`]: theme.vars ? {
66 color: theme.vars.palette.Alert[`${color}IconColor`]
67 } : {
68 color: theme.palette.mode === 'dark' ? theme.palette[color].main : theme.palette[color].light
69 }
70 }, color && ownerState.variant === 'outlined' && {
71 color: theme.vars ? theme.vars.palette.Alert[`${color}Color`] : getColor(theme.palette[color].light, 0.6),
72 border: `1px solid ${(theme.vars || theme).palette[color].light}`,
73 [`& .${alertClasses.icon}`]: theme.vars ? {
74 color: theme.vars.palette.Alert[`${color}IconColor`]
75 } : {
76 color: theme.palette.mode === 'dark' ? theme.palette[color].main : theme.palette[color].light
77 }
78 }, color && ownerState.variant === 'filled' && _extends({
79 fontWeight: theme.typography.fontWeightMedium
80 }, theme.vars ? {
81 color: theme.vars.palette.Alert[`${color}FilledColor`],
82 backgroundColor: theme.vars.palette.Alert[`${color}FilledBg`]
83 } : {
84 backgroundColor: theme.palette.mode === 'dark' ? theme.palette[color].dark : theme.palette[color].main,
85 color: theme.palette.getContrastText(theme.palette.mode === 'dark' ? theme.palette[color].dark : theme.palette[color].main)
86 }));
87});
88const AlertIcon = styled('div', {
89 name: 'MuiAlert',
90 slot: 'Icon',
91 overridesResolver: (props, styles) => styles.icon
92})({
93 marginRight: 12,
94 padding: '7px 0',
95 display: 'flex',
96 fontSize: 22,
97 opacity: 0.9
98});
99const AlertMessage = styled('div', {
100 name: 'MuiAlert',
101 slot: 'Message',
102 overridesResolver: (props, styles) => styles.message
103})({
104 padding: '8px 0',
105 minWidth: 0,
106 overflow: 'auto'
107});
108const AlertAction = styled('div', {
109 name: 'MuiAlert',
110 slot: 'Action',
111 overridesResolver: (props, styles) => styles.action
112})({
113 display: 'flex',
114 alignItems: 'flex-start',
115 padding: '4px 0 0 16px',
116 marginLeft: 'auto',
117 marginRight: -8
118});
119const defaultIconMapping = {
120 success: /*#__PURE__*/_jsx(SuccessOutlinedIcon, {
121 fontSize: "inherit"
122 }),
123 warning: /*#__PURE__*/_jsx(ReportProblemOutlinedIcon, {
124 fontSize: "inherit"
125 }),
126 error: /*#__PURE__*/_jsx(ErrorOutlineIcon, {
127 fontSize: "inherit"
128 }),
129 info: /*#__PURE__*/_jsx(InfoOutlinedIcon, {
130 fontSize: "inherit"
131 })
132};
133const Alert = /*#__PURE__*/React.forwardRef(function Alert(inProps, ref) {
134 const props = useThemeProps({
135 props: inProps,
136 name: 'MuiAlert'
137 });
138
139 const {
140 action,
141 children,
142 className,
143 closeText = 'Close',
144 color,
145 icon,
146 iconMapping = defaultIconMapping,
147 onClose,
148 role = 'alert',
149 severity = 'success',
150 variant = 'standard'
151 } = props,
152 other = _objectWithoutPropertiesLoose(props, _excluded);
153
154 const ownerState = _extends({}, props, {
155 color,
156 severity,
157 variant
158 });
159
160 const classes = useUtilityClasses(ownerState);
161 return /*#__PURE__*/_jsxs(AlertRoot, _extends({
162 role: role,
163 elevation: 0,
164 ownerState: ownerState,
165 className: clsx(classes.root, className),
166 ref: ref
167 }, other, {
168 children: [icon !== false ? /*#__PURE__*/_jsx(AlertIcon, {
169 ownerState: ownerState,
170 className: classes.icon,
171 children: icon || iconMapping[severity] || defaultIconMapping[severity]
172 }) : null, /*#__PURE__*/_jsx(AlertMessage, {
173 ownerState: ownerState,
174 className: classes.message,
175 children: children
176 }), action != null ? /*#__PURE__*/_jsx(AlertAction, {
177 ownerState: ownerState,
178 className: classes.action,
179 children: action
180 }) : null, action == null && onClose ? /*#__PURE__*/_jsx(AlertAction, {
181 ownerState: ownerState,
182 className: classes.action,
183 children: /*#__PURE__*/_jsx(IconButton, {
184 size: "small",
185 "aria-label": closeText,
186 title: closeText,
187 color: "inherit",
188 onClick: onClose,
189 children: _CloseIcon || (_CloseIcon = /*#__PURE__*/_jsx(CloseIcon, {
190 fontSize: "small"
191 }))
192 })
193 }) : null]
194 }));
195});
196process.env.NODE_ENV !== "production" ? Alert.propTypes
197/* remove-proptypes */
198= {
199 // ----------------------------- Warning --------------------------------
200 // | These PropTypes are generated from the TypeScript type definitions |
201 // | To update them edit the d.ts file and run "yarn proptypes" |
202 // ----------------------------------------------------------------------
203
204 /**
205 * The action to display. It renders after the message, at the end of the alert.
206 */
207 action: PropTypes.node,
208
209 /**
210 * The content of the component.
211 */
212 children: PropTypes.node,
213
214 /**
215 * Override or extend the styles applied to the component.
216 */
217 classes: PropTypes.object,
218
219 /**
220 * @ignore
221 */
222 className: PropTypes.string,
223
224 /**
225 * Override the default label for the *close popup* icon button.
226 *
227 * For localization purposes, you can use the provided [translations](/material-ui/guides/localization/).
228 * @default 'Close'
229 */
230 closeText: PropTypes.string,
231
232 /**
233 * The color of the component. Unless provided, the value is taken from the `severity` prop.
234 * It supports both default and custom theme colors, which can be added as shown in the
235 * [palette customization guide](https://mui.com/material-ui/customization/palette/#adding-new-colors).
236 */
237 color: PropTypes
238 /* @typescript-to-proptypes-ignore */
239 .oneOfType([PropTypes.oneOf(['error', 'info', 'success', 'warning']), PropTypes.string]),
240
241 /**
242 * Override the icon displayed before the children.
243 * Unless provided, the icon is mapped to the value of the `severity` prop.
244 * Set to `false` to remove the `icon`.
245 */
246 icon: PropTypes.node,
247
248 /**
249 * The component maps the `severity` prop to a range of different icons,
250 * for instance success to `<SuccessOutlined>`.
251 * If you wish to change this mapping, you can provide your own.
252 * Alternatively, you can use the `icon` prop to override the icon displayed.
253 */
254 iconMapping: PropTypes.shape({
255 error: PropTypes.node,
256 info: PropTypes.node,
257 success: PropTypes.node,
258 warning: PropTypes.node
259 }),
260
261 /**
262 * Callback fired when the component requests to be closed.
263 * When provided and no `action` prop is set, a close icon button is displayed that triggers the callback when clicked.
264 * @param {React.SyntheticEvent} event The event source of the callback.
265 */
266 onClose: PropTypes.func,
267
268 /**
269 * The ARIA role attribute of the element.
270 * @default 'alert'
271 */
272 role: PropTypes.string,
273
274 /**
275 * The severity of the alert. This defines the color and icon used.
276 * @default 'success'
277 */
278 severity: PropTypes.oneOf(['error', 'info', 'success', 'warning']),
279
280 /**
281 * The system prop that allows defining system overrides as well as additional CSS styles.
282 */
283 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
284
285 /**
286 * The variant to use.
287 * @default 'standard'
288 */
289 variant: PropTypes
290 /* @typescript-to-proptypes-ignore */
291 .oneOfType([PropTypes.oneOf(['filled', 'outlined', 'standard']), PropTypes.string])
292} : void 0;
293export default Alert;
\No newline at end of file