Skip to content

Commit

Permalink
refactor: restyle toast
Browse files Browse the repository at this point in the history
  • Loading branch information
KimiaMontazeri committed Nov 27, 2023
1 parent a3bde45 commit 9da223a
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions frontend/src/components/toast/Toast.jsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
import {useCallback} from "react";
import {Alert, Snackbar} from "@mui/material";
import { useCallback } from 'react';
import { Close } from '@mui/icons-material';
import { IconButton, Snackbar, SnackbarContent } from '@mui/material';
import Slide from '@mui/material/Slide';

export default function Toast({
duration = 6000,
vertical = "top", //'top', 'bottom'
horizontal = "right", //'left', 'center', 'right'
message = "fill me",
alertType = "success",//'error', 'warning', 'info', 'success',
open,
setOpen,
}) {
const CloseButton = ({ onClick }) => (
<IconButton onClick={onClick}>
<Close />
</IconButton>
);

const alertTypeColorMapping = {
error: 'var(--error-color)',
warning: 'var(--warn-color)',
info: 'var(--info-color)',
success: 'var(--success-color)',
};

const onClose = useCallback(() => {
setOpen(false)
}, [])
export default function Toast({
duration = 6000,
vertical = 'top', //'top', 'bottom'
horizontal = 'right', //'left', 'center', 'right'
message = 'fill me',
alertType = 'success', //'error', 'warning', 'info', 'success',
open,
setOpen,
}) {
const onClose = useCallback(() => {
setOpen(false);
}, []);

return (
<Snackbar
autoHideDuration={duration}
open={open}
onClose={onClose}
anchorOrigin={{vertical, horizontal}}
TransitionComponent={(props) => <Slide {...props} direction={"right"}/>}
>
<Alert
sx={{
width: "100%",
}}
onClose={onClose}
severity={alertType}>
{message}
</Alert>
</Snackbar>
)
}
return (
<Snackbar
autoHideDuration={duration}
open={open}
onClose={onClose}
anchorOrigin={{ vertical, horizontal }}
TransitionComponent={(props) => <Slide {...props} direction={'right'} />}
action={<CloseButton />}
>
<SnackbarContent
message={message}
action={<CloseButton onClick={() => setOpen(false)} />}
sx={{ bgcolor: alertTypeColorMapping[alertType], color: 'var(--light-text-color)' }}
/>
</Snackbar>
);
}

0 comments on commit 9da223a

Please sign in to comment.