|
| 1 | +import IconButton from "@material-ui/core/IconButton" |
| 2 | +import Snackbar, { SnackbarProps as MuiSnackbarProps } from "@material-ui/core/Snackbar" |
| 3 | +import { makeStyles } from "@material-ui/core/styles" |
| 4 | +import CloseIcon from "@material-ui/icons/Close" |
| 5 | +import React from "react" |
| 6 | +import { combineClasses } from "../../util/combineClasses" |
| 7 | + |
| 8 | +type EnterpriseSnackbarVariant = "error" | "info" |
| 9 | + |
| 10 | +export interface EnterpriseSnackbarProps extends MuiSnackbarProps { |
| 11 | + /** Called when the snackbar should close, either from timeout or clicking close */ |
| 12 | + onClose: () => void |
| 13 | + /** Variant of snackbar, for theming */ |
| 14 | + variant?: EnterpriseSnackbarVariant |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Wrapper around Material UI's Snackbar component, provides pre-configured |
| 19 | + * themes and convenience props. Coder UI's Snackbars require a close handler, |
| 20 | + * since they always render a close button. |
| 21 | + * |
| 22 | + * Snackbars do _not_ automatically appear in the top-level position when |
| 23 | + * rendered, you'll need to use ReactDom portals or the Material UI Portal |
| 24 | + * component for that. |
| 25 | + * |
| 26 | + * See original component's Material UI documentation here: https://material-ui.com/components/snackbars/ |
| 27 | + */ |
| 28 | +export const EnterpriseSnackbar: React.FC<EnterpriseSnackbarProps> = ({ |
| 29 | + onClose, |
| 30 | + variant = "info", |
| 31 | + ContentProps = {}, |
| 32 | + action, |
| 33 | + ...rest |
| 34 | +}) => { |
| 35 | + const styles = useStyles() |
| 36 | + |
| 37 | + return ( |
| 38 | + <Snackbar |
| 39 | + anchorOrigin={{ |
| 40 | + vertical: "bottom", |
| 41 | + horizontal: "right", |
| 42 | + }} |
| 43 | + {...rest} |
| 44 | + action={ |
| 45 | + <div className={styles.actionWrapper}> |
| 46 | + {action} |
| 47 | + <IconButton onClick={onClose} className={styles.iconButton}> |
| 48 | + <CloseIcon className={variant === "info" ? styles.closeIcon : styles.closeIconError} /> |
| 49 | + </IconButton> |
| 50 | + </div> |
| 51 | + } |
| 52 | + ContentProps={{ |
| 53 | + ...ContentProps, |
| 54 | + className: combineClasses({ |
| 55 | + [styles.snackbarContent]: true, |
| 56 | + [styles.snackbarContentInfo]: variant === "info", |
| 57 | + [styles.snackbarContentError]: variant === "error", |
| 58 | + }), |
| 59 | + }} |
| 60 | + onClose={onClose} |
| 61 | + /> |
| 62 | + ) |
| 63 | +} |
| 64 | + |
| 65 | +const useStyles = makeStyles((theme) => ({ |
| 66 | + actionWrapper: { |
| 67 | + display: "flex", |
| 68 | + alignItems: "center", |
| 69 | + }, |
| 70 | + iconButton: { |
| 71 | + padding: 0, |
| 72 | + }, |
| 73 | + closeIcon: { |
| 74 | + width: 25, |
| 75 | + height: 25, |
| 76 | + color: theme.palette.info.contrastText, |
| 77 | + }, |
| 78 | + closeIconError: { |
| 79 | + width: 25, |
| 80 | + height: 25, |
| 81 | + color: theme.palette.error.contrastText, |
| 82 | + }, |
| 83 | + snackbarContent: { |
| 84 | + borderLeft: `4px solid ${theme.palette.primary.main}`, |
| 85 | + borderRadius: 0, |
| 86 | + padding: `${theme.spacing(1)}px ${theme.spacing(3)}px ${theme.spacing(1)}px ${theme.spacing(2)}px`, |
| 87 | + boxShadow: theme.shadows[6], |
| 88 | + alignItems: "inherit", |
| 89 | + }, |
| 90 | + snackbarContentInfo: { |
| 91 | + backgroundColor: theme.palette.info.main, |
| 92 | + // Use primary color as a highlight |
| 93 | + borderLeftColor: theme.palette.primary.main, |
| 94 | + color: theme.palette.info.contrastText, |
| 95 | + }, |
| 96 | + snackbarContentError: { |
| 97 | + backgroundColor: theme.palette.error.dark, |
| 98 | + borderLeftColor: theme.palette.error.main, |
| 99 | + color: theme.palette.error.contrastText, |
| 100 | + }, |
| 101 | +})) |
0 commit comments