UNPKG

14.8 kBJavaScriptView Raw
1'use client';
2
3import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
4import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
5import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
6import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
7import _extends from "@babel/runtime/helpers/esm/extends";
8import * as React from 'react';
9import PropTypes from 'prop-types';
10import clsx from 'clsx';
11import composeClasses from '@mui/utils/composeClasses';
12import { darken, lighten } from '@mui/system/colorManipulator';
13import { styled } from '../zero-styled';
14import { useDefaultProps } from '../DefaultPropsProvider';
15import useSlot from '../utils/useSlot';
16import capitalize from '../utils/capitalize';
17import Paper from '../Paper';
18import alertClasses, { getAlertUtilityClass } from './alertClasses';
19import IconButton from '../IconButton';
20import SuccessOutlinedIcon from '../internal/svg-icons/SuccessOutlined';
21import ReportProblemOutlinedIcon from '../internal/svg-icons/ReportProblemOutlined';
22import ErrorOutlineIcon from '../internal/svg-icons/ErrorOutline';
23import InfoOutlinedIcon from '../internal/svg-icons/InfoOutlined';
24import CloseIcon from '../internal/svg-icons/Close';
25import { jsx as _jsx } from "react/jsx-runtime";
26import { jsxs as _jsxs } from "react/jsx-runtime";
27var useUtilityClasses = function useUtilityClasses(ownerState) {
28 var variant = ownerState.variant,
29 color = ownerState.color,
30 severity = ownerState.severity,
31 classes = ownerState.classes;
32 var slots = {
33 root: ['root', "color".concat(capitalize(color || severity)), "".concat(variant).concat(capitalize(color || severity)), "".concat(variant)],
34 icon: ['icon'],
35 message: ['message'],
36 action: ['action']
37 };
38 return composeClasses(slots, getAlertUtilityClass, classes);
39};
40var AlertRoot = styled(Paper, {
41 name: 'MuiAlert',
42 slot: 'Root',
43 overridesResolver: function overridesResolver(props, styles) {
44 var ownerState = props.ownerState;
45 return [styles.root, styles[ownerState.variant], styles["".concat(ownerState.variant).concat(capitalize(ownerState.color || ownerState.severity))]];
46 }
47})(function (_ref) {
48 var theme = _ref.theme;
49 var getColor = theme.palette.mode === 'light' ? darken : lighten;
50 var getBackgroundColor = theme.palette.mode === 'light' ? lighten : darken;
51 return _extends({}, theme.typography.body2, {
52 backgroundColor: 'transparent',
53 display: 'flex',
54 padding: '6px 16px',
55 variants: [].concat(_toConsumableArray(Object.entries(theme.palette).filter(function (_ref2) {
56 var _ref3 = _slicedToArray(_ref2, 2),
57 value = _ref3[1];
58 return value.main && value.light;
59 }).map(function (_ref4) {
60 var _ref5 = _slicedToArray(_ref4, 1),
61 color = _ref5[0];
62 return {
63 props: {
64 colorSeverity: color,
65 variant: 'standard'
66 },
67 style: _defineProperty({
68 color: theme.vars ? theme.vars.palette.Alert["".concat(color, "Color")] : getColor(theme.palette[color].light, 0.6),
69 backgroundColor: theme.vars ? theme.vars.palette.Alert["".concat(color, "StandardBg")] : getBackgroundColor(theme.palette[color].light, 0.9)
70 }, "& .".concat(alertClasses.icon), theme.vars ? {
71 color: theme.vars.palette.Alert["".concat(color, "IconColor")]
72 } : {
73 color: theme.palette[color].main
74 })
75 };
76 })), _toConsumableArray(Object.entries(theme.palette).filter(function (_ref6) {
77 var _ref7 = _slicedToArray(_ref6, 2),
78 value = _ref7[1];
79 return value.main && value.light;
80 }).map(function (_ref8) {
81 var _ref9 = _slicedToArray(_ref8, 1),
82 color = _ref9[0];
83 return {
84 props: {
85 colorSeverity: color,
86 variant: 'outlined'
87 },
88 style: _defineProperty({
89 color: theme.vars ? theme.vars.palette.Alert["".concat(color, "Color")] : getColor(theme.palette[color].light, 0.6),
90 border: "1px solid ".concat((theme.vars || theme).palette[color].light)
91 }, "& .".concat(alertClasses.icon), theme.vars ? {
92 color: theme.vars.palette.Alert["".concat(color, "IconColor")]
93 } : {
94 color: theme.palette[color].main
95 })
96 };
97 })), _toConsumableArray(Object.entries(theme.palette).filter(function (_ref10) {
98 var _ref11 = _slicedToArray(_ref10, 2),
99 value = _ref11[1];
100 return value.main && value.dark;
101 }).map(function (_ref12) {
102 var _ref13 = _slicedToArray(_ref12, 1),
103 color = _ref13[0];
104 return {
105 props: {
106 colorSeverity: color,
107 variant: 'filled'
108 },
109 style: _extends({
110 fontWeight: theme.typography.fontWeightMedium
111 }, theme.vars ? {
112 color: theme.vars.palette.Alert["".concat(color, "FilledColor")],
113 backgroundColor: theme.vars.palette.Alert["".concat(color, "FilledBg")]
114 } : {
115 backgroundColor: theme.palette.mode === 'dark' ? theme.palette[color].dark : theme.palette[color].main,
116 color: theme.palette.getContrastText(theme.palette[color].main)
117 })
118 };
119 })))
120 });
121});
122var AlertIcon = styled('div', {
123 name: 'MuiAlert',
124 slot: 'Icon',
125 overridesResolver: function overridesResolver(props, styles) {
126 return styles.icon;
127 }
128})({
129 marginRight: 12,
130 padding: '7px 0',
131 display: 'flex',
132 fontSize: 22,
133 opacity: 0.9
134});
135var AlertMessage = styled('div', {
136 name: 'MuiAlert',
137 slot: 'Message',
138 overridesResolver: function overridesResolver(props, styles) {
139 return styles.message;
140 }
141})({
142 padding: '8px 0',
143 minWidth: 0,
144 overflow: 'auto'
145});
146var AlertAction = styled('div', {
147 name: 'MuiAlert',
148 slot: 'Action',
149 overridesResolver: function overridesResolver(props, styles) {
150 return styles.action;
151 }
152})({
153 display: 'flex',
154 alignItems: 'flex-start',
155 padding: '4px 0 0 16px',
156 marginLeft: 'auto',
157 marginRight: -8
158});
159var defaultIconMapping = {
160 success: /*#__PURE__*/_jsx(SuccessOutlinedIcon, {
161 fontSize: "inherit"
162 }),
163 warning: /*#__PURE__*/_jsx(ReportProblemOutlinedIcon, {
164 fontSize: "inherit"
165 }),
166 error: /*#__PURE__*/_jsx(ErrorOutlineIcon, {
167 fontSize: "inherit"
168 }),
169 info: /*#__PURE__*/_jsx(InfoOutlinedIcon, {
170 fontSize: "inherit"
171 })
172};
173var Alert = /*#__PURE__*/React.forwardRef(function Alert(inProps, ref) {
174 var props = useDefaultProps({
175 props: inProps,
176 name: 'MuiAlert'
177 });
178 var action = props.action,
179 children = props.children,
180 className = props.className,
181 _props$closeText = props.closeText,
182 closeText = _props$closeText === void 0 ? 'Close' : _props$closeText,
183 color = props.color,
184 _props$components = props.components,
185 components = _props$components === void 0 ? {} : _props$components,
186 _props$componentsProp = props.componentsProps,
187 componentsProps = _props$componentsProp === void 0 ? {} : _props$componentsProp,
188 icon = props.icon,
189 _props$iconMapping = props.iconMapping,
190 iconMapping = _props$iconMapping === void 0 ? defaultIconMapping : _props$iconMapping,
191 onClose = props.onClose,
192 _props$role = props.role,
193 role = _props$role === void 0 ? 'alert' : _props$role,
194 _props$severity = props.severity,
195 severity = _props$severity === void 0 ? 'success' : _props$severity,
196 _props$slotProps = props.slotProps,
197 slotProps = _props$slotProps === void 0 ? {} : _props$slotProps,
198 _props$slots = props.slots,
199 slots = _props$slots === void 0 ? {} : _props$slots,
200 _props$variant = props.variant,
201 variant = _props$variant === void 0 ? 'standard' : _props$variant,
202 other = _objectWithoutProperties(props, ["action", "children", "className", "closeText", "color", "components", "componentsProps", "icon", "iconMapping", "onClose", "role", "severity", "slotProps", "slots", "variant"]);
203 var ownerState = _extends({}, props, {
204 color: color,
205 severity: severity,
206 variant: variant,
207 colorSeverity: color || severity
208 });
209 var classes = useUtilityClasses(ownerState);
210 var externalForwardedProps = {
211 slots: _extends({
212 closeButton: components.CloseButton,
213 closeIcon: components.CloseIcon
214 }, slots),
215 slotProps: _extends({}, componentsProps, slotProps)
216 };
217 var _useSlot = useSlot('closeButton', {
218 elementType: IconButton,
219 externalForwardedProps: externalForwardedProps,
220 ownerState: ownerState
221 }),
222 _useSlot2 = _slicedToArray(_useSlot, 2),
223 CloseButtonSlot = _useSlot2[0],
224 closeButtonProps = _useSlot2[1];
225 var _useSlot3 = useSlot('closeIcon', {
226 elementType: CloseIcon,
227 externalForwardedProps: externalForwardedProps,
228 ownerState: ownerState
229 }),
230 _useSlot4 = _slicedToArray(_useSlot3, 2),
231 CloseIconSlot = _useSlot4[0],
232 closeIconProps = _useSlot4[1];
233 return /*#__PURE__*/_jsxs(AlertRoot, _extends({
234 role: role,
235 elevation: 0,
236 ownerState: ownerState,
237 className: clsx(classes.root, className),
238 ref: ref
239 }, other, {
240 children: [icon !== false ? /*#__PURE__*/_jsx(AlertIcon, {
241 ownerState: ownerState,
242 className: classes.icon,
243 children: icon || iconMapping[severity] || defaultIconMapping[severity]
244 }) : null, /*#__PURE__*/_jsx(AlertMessage, {
245 ownerState: ownerState,
246 className: classes.message,
247 children: children
248 }), action != null ? /*#__PURE__*/_jsx(AlertAction, {
249 ownerState: ownerState,
250 className: classes.action,
251 children: action
252 }) : null, action == null && onClose ? /*#__PURE__*/_jsx(AlertAction, {
253 ownerState: ownerState,
254 className: classes.action,
255 children: /*#__PURE__*/_jsx(CloseButtonSlot, _extends({
256 size: "small",
257 "aria-label": closeText,
258 title: closeText,
259 color: "inherit",
260 onClick: onClose
261 }, closeButtonProps, {
262 children: /*#__PURE__*/_jsx(CloseIconSlot, _extends({
263 fontSize: "small"
264 }, closeIconProps))
265 }))
266 }) : null]
267 }));
268});
269process.env.NODE_ENV !== "production" ? Alert.propTypes /* remove-proptypes */ = {
270 // ┌────────────────────────────── Warning ──────────────────────────────┐
271 // │ These PropTypes are generated from the TypeScript type definitions. │
272 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
273 // └─────────────────────────────────────────────────────────────────────┘
274 /**
275 * The action to display. It renders after the message, at the end of the alert.
276 */
277 action: PropTypes.node,
278 /**
279 * The content of the component.
280 */
281 children: PropTypes.node,
282 /**
283 * Override or extend the styles applied to the component.
284 */
285 classes: PropTypes.object,
286 /**
287 * @ignore
288 */
289 className: PropTypes.string,
290 /**
291 * Override the default label for the *close popup* icon button.
292 *
293 * For localization purposes, you can use the provided [translations](/material-ui/guides/localization/).
294 * @default 'Close'
295 */
296 closeText: PropTypes.string,
297 /**
298 * The color of the component. Unless provided, the value is taken from the `severity` prop.
299 * It supports both default and custom theme colors, which can be added as shown in the
300 * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
301 */
302 color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['error', 'info', 'success', 'warning']), PropTypes.string]),
303 /**
304 * The components used for each slot inside.
305 *
306 * @deprecated use the `slots` prop instead. This prop will be removed in v7. [How to migrate](/material-ui/migration/migrating-from-deprecated-apis/).
307 *
308 * @default {}
309 */
310 components: PropTypes.shape({
311 CloseButton: PropTypes.elementType,
312 CloseIcon: PropTypes.elementType
313 }),
314 /**
315 * The extra props for the slot components.
316 * You can override the existing props or add new ones.
317 *
318 * @deprecated use the `slotProps` prop instead. This prop will be removed in v7. [How to migrate](/material-ui/migration/migrating-from-deprecated-apis/).
319 *
320 * @default {}
321 */
322 componentsProps: PropTypes.shape({
323 closeButton: PropTypes.object,
324 closeIcon: PropTypes.object
325 }),
326 /**
327 * Override the icon displayed before the children.
328 * Unless provided, the icon is mapped to the value of the `severity` prop.
329 * Set to `false` to remove the `icon`.
330 */
331 icon: PropTypes.node,
332 /**
333 * The component maps the `severity` prop to a range of different icons,
334 * for instance success to `<SuccessOutlined>`.
335 * If you wish to change this mapping, you can provide your own.
336 * Alternatively, you can use the `icon` prop to override the icon displayed.
337 */
338 iconMapping: PropTypes.shape({
339 error: PropTypes.node,
340 info: PropTypes.node,
341 success: PropTypes.node,
342 warning: PropTypes.node
343 }),
344 /**
345 * Callback fired when the component requests to be closed.
346 * When provided and no `action` prop is set, a close icon button is displayed that triggers the callback when clicked.
347 * @param {React.SyntheticEvent} event The event source of the callback.
348 */
349 onClose: PropTypes.func,
350 /**
351 * The ARIA role attribute of the element.
352 * @default 'alert'
353 */
354 role: PropTypes.string,
355 /**
356 * The severity of the alert. This defines the color and icon used.
357 * @default 'success'
358 */
359 severity: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['error', 'info', 'success', 'warning']), PropTypes.string]),
360 /**
361 * The props used for each slot inside.
362 * @default {}
363 */
364 slotProps: PropTypes.shape({
365 closeButton: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
366 closeIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
367 }),
368 /**
369 * The components used for each slot inside.
370 * @default {}
371 */
372 slots: PropTypes.shape({
373 closeButton: PropTypes.elementType,
374 closeIcon: PropTypes.elementType
375 }),
376 /**
377 * The system prop that allows defining system overrides as well as additional CSS styles.
378 */
379 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
380 /**
381 * The variant to use.
382 * @default 'standard'
383 */
384 variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['filled', 'outlined', 'standard']), PropTypes.string])
385} : void 0;
386export default Alert;
\No newline at end of file