|
| 1 | +'use client'; |
| 2 | +import * as React from 'react'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | +import clsx from 'clsx'; |
| 5 | +import { unstable_composeClasses as composeClasses } from '@mui/base/composeClasses'; |
| 6 | +import { emphasize } from '@mui/system'; |
| 7 | +import styled from '@mui/material/styles/styled'; |
| 8 | +import useThemeProps from '@mui/material/styles/useThemeProps'; |
| 9 | +import Paper from '@mui/material/Paper'; |
| 10 | +import { getSnackbarContentUtilityClass } from './snackbarContentClasses'; |
| 11 | + |
| 12 | +const useUtilityClasses = (ownerState) => { |
| 13 | + const { classes } = ownerState; |
| 14 | + |
| 15 | + const slots = { |
| 16 | + root: ['root'], |
| 17 | + action: ['action'], |
| 18 | + message: ['message'], |
| 19 | + }; |
| 20 | + |
| 21 | + return composeClasses(slots, getSnackbarContentUtilityClass, classes); |
| 22 | +}; |
| 23 | + |
| 24 | +const SnackbarContentRoot = styled(Paper, { |
| 25 | + name: 'MuiSnackbarContent', |
| 26 | + slot: 'Root', |
| 27 | + overridesResolver: (props, styles) => styles.root, |
| 28 | +})(({ theme }) => { |
| 29 | + const emphasis = theme.palette.mode === 'light' ? 0.8 : 0.98; |
| 30 | + const backgroundColor = emphasize(theme.palette.background.default, emphasis); |
| 31 | + |
| 32 | + return { |
| 33 | + ...theme.typography.body2, |
| 34 | + color: theme.vars |
| 35 | + ? theme.vars.palette.SnackbarContent.color |
| 36 | + : theme.palette.getContrastText(backgroundColor), |
| 37 | + backgroundColor: theme.vars ? theme.vars.palette.SnackbarContent.bg : backgroundColor, |
| 38 | + display: 'flex', |
| 39 | + alignItems: 'center', |
| 40 | + flexWrap: 'wrap', |
| 41 | + padding: '6px 16px', |
| 42 | + borderRadius: (theme.vars || theme).shape.borderRadius, |
| 43 | + flexGrow: 1, |
| 44 | + [theme.breakpoints.up('sm')]: { |
| 45 | + flexGrow: 'initial', |
| 46 | + minWidth: 288, |
| 47 | + }, |
| 48 | + }; |
| 49 | +}); |
| 50 | + |
| 51 | +const SnackbarContentMessage = styled('div', { |
| 52 | + name: 'MuiSnackbarContent', |
| 53 | + slot: 'Message', |
| 54 | + overridesResolver: (props, styles) => styles.message, |
| 55 | +})({ |
| 56 | + padding: '8px 0', |
| 57 | +}); |
| 58 | + |
| 59 | +const SnackbarContentAction = styled('div', { |
| 60 | + name: 'MuiSnackbarContent', |
| 61 | + slot: 'Action', |
| 62 | + overridesResolver: (props, styles) => styles.action, |
| 63 | +})({ |
| 64 | + display: 'flex', |
| 65 | + alignItems: 'center', |
| 66 | + marginLeft: 'auto', |
| 67 | + paddingLeft: 16, |
| 68 | + marginRight: -8, |
| 69 | +}); |
| 70 | + |
| 71 | +const SnackbarContent = React.forwardRef(function SnackbarContent(inProps, ref) { |
| 72 | + const props = useThemeProps({ props: inProps, name: 'MuiSnackbarContent' }); |
| 73 | + const { action, className, message, role = 'alert', ...other } = props; |
| 74 | + const ownerState = props; |
| 75 | + const classes = useUtilityClasses(ownerState); |
| 76 | + |
| 77 | + return ( |
| 78 | + <SnackbarContentRoot |
| 79 | + role={role} |
| 80 | + square |
| 81 | + elevation={6} |
| 82 | + className={clsx(classes.root, className)} |
| 83 | + ownerState={ownerState} |
| 84 | + ref={ref} |
| 85 | + {...other} |
| 86 | + > |
| 87 | + <SnackbarContentMessage className={classes.message} ownerState={ownerState}> |
| 88 | + {message} |
| 89 | + </SnackbarContentMessage> |
| 90 | + {action ? ( |
| 91 | + <SnackbarContentAction className={classes.action} ownerState={ownerState}> |
| 92 | + {action} |
| 93 | + </SnackbarContentAction> |
| 94 | + ) : null} |
| 95 | + </SnackbarContentRoot> |
| 96 | + ); |
| 97 | +}); |
| 98 | + |
| 99 | +SnackbarContent.propTypes /* remove-proptypes */ = { |
| 100 | + // ----------------------------- Warning -------------------------------- |
| 101 | + // | These PropTypes are generated from the TypeScript type definitions | |
| 102 | + // | To update them edit the d.ts file and run "yarn proptypes" | |
| 103 | + // ---------------------------------------------------------------------- |
| 104 | + /** |
| 105 | + * The action to display. It renders after the message, at the end of the snackbar. |
| 106 | + */ |
| 107 | + action: PropTypes.node, |
| 108 | + /** |
| 109 | + * Override or extend the styles applied to the component. |
| 110 | + */ |
| 111 | + classes: PropTypes.object, |
| 112 | + /** |
| 113 | + * @ignore |
| 114 | + */ |
| 115 | + className: PropTypes.string, |
| 116 | + /** |
| 117 | + * The message to display. |
| 118 | + */ |
| 119 | + message: PropTypes.node, |
| 120 | + /** |
| 121 | + * The ARIA role attribute of the element. |
| 122 | + * @default 'alert' |
| 123 | + */ |
| 124 | + role: PropTypes /* @typescript-to-proptypes-ignore */.string, |
| 125 | + /** |
| 126 | + * The system prop that allows defining system overrides as well as additional CSS styles. |
| 127 | + */ |
| 128 | + sx: PropTypes.oneOfType([ |
| 129 | + PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), |
| 130 | + PropTypes.func, |
| 131 | + PropTypes.object, |
| 132 | + ]), |
| 133 | +}; |
| 134 | + |
| 135 | +export default SnackbarContent; |
0 commit comments