UNPKG

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