@mui/material#SnackbarCloseReason TypeScript Examples

The following examples show how to use @mui/material#SnackbarCloseReason. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Message.tsx    From your_spotify with GNU General Public License v3.0 6 votes vote down vote up
export default function Message() {
  const message = useSelector(selectMessage);
  const [open, setOpen] = useState(false);

  const onClose = useCallback(
    (_: Event | React.SyntheticEvent<any, Event>, reason: SnackbarCloseReason) => {
      if (reason === 'clickaway') {
        return;
      }
      setOpen(false);
    },
    [],
  );

  useEffect(() => {
    setOpen(true);
  }, [message]);

  if (!message) {
    return null;
  }

  return (
    <Snackbar
      open={open}
      autoHideDuration={2000}
      onClose={onClose}
      anchorOrigin={{ horizontal: 'center', vertical: 'top' }}>
      <Alert onClose={() => setOpen(false)} level={message.level} message={message.message} />
    </Snackbar>
  );
}
Example #2
Source File: Snackbar.tsx    From frontend with MIT License 6 votes vote down vote up
function SnackBar() {
  const { getAlerts } = AlertStore

  const handleSnackBarClose = (
    _event: React.SyntheticEvent | Event,
    reason: SnackbarCloseReason,
  ) => {
    if (reason === 'clickaway') {
      return
    }
    AlertStore.hide()
  }
  const handleClose = () => AlertStore.hide()

  return (
    <>
      {getAlerts.map(({ id, show, duration, type, message }) => {
        return (
          <Snackbar
            key={id}
            open={show}
            anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
            autoHideDuration={duration}
            onClose={handleSnackBarClose}>
            <Alert severity={type} onClose={handleClose}>
              {message}
            </Alert>
          </Snackbar>
        )
      })}
    </>
  )
}