UNPKG

12.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 [CloseButtonSlot, closeButtonProps] = useSlot('closeButton', {
191 elementType: IconButton,
192 externalForwardedProps,
193 ownerState
194 });
195 const [CloseIconSlot, closeIconProps] = useSlot('closeIcon', {
196 elementType: CloseIcon,
197 externalForwardedProps,
198 ownerState
199 });
200 return /*#__PURE__*/_jsxs(AlertRoot, {
201 role: role,
202 elevation: 0,
203 ownerState: ownerState,
204 className: clsx(classes.root, className),
205 ref: ref,
206 ...other,
207 children: [icon !== false ? /*#__PURE__*/_jsx(AlertIcon, {
208 ownerState: ownerState,
209 className: classes.icon,
210 children: icon || iconMapping[severity] || defaultIconMapping[severity]
211 }) : null, /*#__PURE__*/_jsx(AlertMessage, {
212 ownerState: ownerState,
213 className: classes.message,
214 children: children
215 }), action != null ? /*#__PURE__*/_jsx(AlertAction, {
216 ownerState: ownerState,
217 className: classes.action,
218 children: action
219 }) : null, action == null && onClose ? /*#__PURE__*/_jsx(AlertAction, {
220 ownerState: ownerState,
221 className: classes.action,
222 children: /*#__PURE__*/_jsx(CloseButtonSlot, {
223 size: "small",
224 "aria-label": closeText,
225 title: closeText,
226 color: "inherit",
227 onClick: onClose,
228 ...closeButtonProps,
229 children: /*#__PURE__*/_jsx(CloseIconSlot, {
230 fontSize: "small",
231 ...closeIconProps
232 })
233 })
234 }) : null]
235 });
236});
237process.env.NODE_ENV !== "production" ? Alert.propTypes /* remove-proptypes */ = {
238 // ┌────────────────────────────── Warning ──────────────────────────────┐
239 // │ These PropTypes are generated from the TypeScript type definitions. │
240 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
241 // └─────────────────────────────────────────────────────────────────────┘
242 /**
243 * The action to display. It renders after the message, at the end of the alert.
244 */
245 action: PropTypes.node,
246 /**
247 * The content of the component.
248 */
249 children: PropTypes.node,
250 /**
251 * Override or extend the styles applied to the component.
252 */
253 classes: PropTypes.object,
254 /**
255 * @ignore
256 */
257 className: PropTypes.string,
258 /**
259 * Override the default label for the *close popup* icon button.
260 *
261 * For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
262 * @default 'Close'
263 */
264 closeText: PropTypes.string,
265 /**
266 * The color of the component. Unless provided, the value is taken from the `severity` prop.
267 * It supports both default and custom theme colors, which can be added as shown in the
268 * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
269 */
270 color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['error', 'info', 'success', 'warning']), PropTypes.string]),
271 /**
272 * The components used for each slot inside.
273 *
274 * @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.
275 *
276 * @default {}
277 */
278 components: PropTypes.shape({
279 CloseButton: PropTypes.elementType,
280 CloseIcon: PropTypes.elementType
281 }),
282 /**
283 * The extra props for the slot components.
284 * You can override the existing props or add new ones.
285 *
286 * @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.
287 *
288 * @default {}
289 */
290 componentsProps: PropTypes.shape({
291 closeButton: PropTypes.object,
292 closeIcon: PropTypes.object
293 }),
294 /**
295 * Override the icon displayed before the children.
296 * Unless provided, the icon is mapped to the value of the `severity` prop.
297 * Set to `false` to remove the `icon`.
298 */
299 icon: PropTypes.node,
300 /**
301 * The component maps the `severity` prop to a range of different icons,
302 * for instance success to `<SuccessOutlined>`.
303 * If you wish to change this mapping, you can provide your own.
304 * Alternatively, you can use the `icon` prop to override the icon displayed.
305 */
306 iconMapping: PropTypes.shape({
307 error: PropTypes.node,
308 info: PropTypes.node,
309 success: PropTypes.node,
310 warning: PropTypes.node
311 }),
312 /**
313 * Callback fired when the component requests to be closed.
314 * When provided and no `action` prop is set, a close icon button is displayed that triggers the callback when clicked.
315 * @param {React.SyntheticEvent} event The event source of the callback.
316 */
317 onClose: PropTypes.func,
318 /**
319 * The ARIA role attribute of the element.
320 * @default 'alert'
321 */
322 role: PropTypes.string,
323 /**
324 * The severity of the alert. This defines the color and icon used.
325 * @default 'success'
326 */
327 severity: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['error', 'info', 'success', 'warning']), PropTypes.string]),
328 /**
329 * The props used for each slot inside.
330 * @default {}
331 */
332 slotProps: PropTypes.shape({
333 closeButton: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
334 closeIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
335 }),
336 /**
337 * The components used for each slot inside.
338 * @default {}
339 */
340 slots: PropTypes.shape({
341 closeButton: PropTypes.elementType,
342 closeIcon: PropTypes.elementType
343 }),
344 /**
345 * The system prop that allows defining system overrides as well as additional CSS styles.
346 */
347 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
348 /**
349 * The variant to use.
350 * @default 'standard'
351 */
352 variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['filled', 'outlined', 'standard']), PropTypes.string])
353} : void 0;
354export default Alert;
\No newline at end of file