UNPKG

@mui/material

Version:

Quickly build beautiful React apps. MUI is a simple and customizable component library to build faster, beautiful, and more accessible React applications. Follow your own design system, or start with Material Design.

280 lines (260 loc) 8.8 kB
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; import _extends from "@babel/runtime/helpers/esm/extends"; var _CloseIcon; const _excluded = ["action", "children", "className", "closeText", "color", "icon", "iconMapping", "onClose", "role", "severity", "variant"]; import * as React from 'react'; import PropTypes from 'prop-types'; import clsx from 'clsx'; import { unstable_composeClasses as composeClasses } from '@mui/base'; 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'; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; 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 _extends({}, theme.typography.body2, { 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: /*#__PURE__*/_jsx(SuccessOutlinedIcon, { fontSize: "inherit" }), warning: /*#__PURE__*/_jsx(ReportProblemOutlinedIcon, { fontSize: "inherit" }), error: /*#__PURE__*/_jsx(ErrorOutlineIcon, { fontSize: "inherit" }), info: /*#__PURE__*/_jsx(InfoOutlinedIcon, { fontSize: "inherit" }) }; const Alert = /*#__PURE__*/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' } = props, other = _objectWithoutPropertiesLoose(props, _excluded); const ownerState = _extends({}, props, { color, severity, variant }); const classes = useUtilityClasses(ownerState); return /*#__PURE__*/_jsxs(AlertRoot, _extends({ role: role, elevation: 0, ownerState: ownerState, className: clsx(classes.root, className), ref: ref }, other, { children: [icon !== false ? /*#__PURE__*/_jsx(AlertIcon, { ownerState: ownerState, className: classes.icon, children: icon || iconMapping[severity] || defaultIconMapping[severity] }) : null, /*#__PURE__*/_jsx(AlertMessage, { ownerState: ownerState, className: classes.message, children: children }), action != null ? /*#__PURE__*/_jsx(AlertAction, { className: classes.action, children: action }) : null, action == null && onClose ? /*#__PURE__*/_jsx(AlertAction, { ownerState: ownerState, className: classes.action, children: /*#__PURE__*/_jsx(IconButton, { size: "small", "aria-label": closeText, title: closeText, color: "inherit", onClick: onClose, children: _CloseIcon || (_CloseIcon = /*#__PURE__*/_jsx(CloseIcon, { fontSize: "small" })) }) }) : null] })); }); process.env.NODE_ENV !== "production" ? 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.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]), /** * The variant to use. * @default 'standard' */ variant: PropTypes /* @typescript-to-proptypes-ignore */ .oneOfType([PropTypes.oneOf(['filled', 'outlined', 'standard']), PropTypes.string]) } : void 0; export default Alert;