2017-09-23 22:58:34 +00:00
|
|
|
// @flow
|
2020-08-09 05:53:59 +00:00
|
|
|
import { darken } from "polished";
|
2020-06-20 20:59:15 +00:00
|
|
|
import * as React from "react";
|
2021-01-03 05:09:43 +00:00
|
|
|
import styled, { css } from "styled-components";
|
|
|
|
import { fadeAndScaleIn, pulse } from "shared/styles/animations";
|
2020-08-12 17:49:15 +00:00
|
|
|
import type { Toast as TToast } from "types";
|
2017-09-23 22:58:34 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
onRequestClose: () => void,
|
|
|
|
closeAfterMs: number,
|
2018-05-31 19:07:49 +00:00
|
|
|
toast: TToast,
|
2017-09-23 22:58:34 +00:00
|
|
|
};
|
|
|
|
|
2021-01-03 05:09:43 +00:00
|
|
|
function Toast({ closeAfterMs = 3000, onRequestClose, toast }: Props) {
|
|
|
|
const timeout = React.useRef();
|
|
|
|
const [pulse, setPulse] = React.useState(false);
|
|
|
|
const { action, reoccurring } = toast;
|
2017-09-23 22:58:34 +00:00
|
|
|
|
2021-01-03 05:09:43 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
timeout.current = setTimeout(onRequestClose, toast.timeout || closeAfterMs);
|
2017-09-23 22:58:34 +00:00
|
|
|
|
2021-01-03 05:09:43 +00:00
|
|
|
return () => clearTimeout(timeout.current);
|
|
|
|
}, [onRequestClose, toast, closeAfterMs]);
|
2017-09-23 22:58:34 +00:00
|
|
|
|
2021-01-03 05:09:43 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (reoccurring) {
|
|
|
|
setPulse(reoccurring);
|
2017-09-23 22:58:34 +00:00
|
|
|
|
2021-01-03 05:09:43 +00:00
|
|
|
// must match animation time in css below vvv
|
|
|
|
setTimeout(() => setPulse(false), 250);
|
|
|
|
}
|
|
|
|
}, [reoccurring]);
|
2017-09-23 22:58:34 +00:00
|
|
|
|
2021-01-03 05:09:43 +00:00
|
|
|
const message =
|
|
|
|
typeof toast.message === "string"
|
|
|
|
? toast.message
|
|
|
|
: toast.message.toString();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ListItem $pulse={pulse}>
|
|
|
|
<Container
|
|
|
|
onClick={action ? undefined : onRequestClose}
|
|
|
|
type={toast.type || "success"}
|
|
|
|
>
|
|
|
|
<Message>{message}</Message>
|
|
|
|
{action && (
|
|
|
|
<Action type={toast.type || "success"} onClick={action.onClick}>
|
|
|
|
{action.text}
|
|
|
|
</Action>
|
|
|
|
)}
|
|
|
|
</Container>
|
|
|
|
</ListItem>
|
|
|
|
);
|
2017-09-23 22:58:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 02:11:23 +00:00
|
|
|
const Action = styled.span`
|
|
|
|
display: inline-block;
|
|
|
|
padding: 10px 12px;
|
|
|
|
height: 100%;
|
|
|
|
text-transform: uppercase;
|
|
|
|
font-size: 12px;
|
2020-08-09 01:53:11 +00:00
|
|
|
color: ${(props) => props.theme.toastText};
|
|
|
|
background: ${(props) => darken(0.05, props.theme.toastBackground)};
|
2019-04-18 02:11:23 +00:00
|
|
|
border-top-right-radius: 5px;
|
|
|
|
border-bottom-right-radius: 5px;
|
|
|
|
|
|
|
|
&:hover {
|
2020-08-09 01:53:11 +00:00
|
|
|
background: ${(props) => darken(0.1, props.theme.toastBackground)};
|
2019-04-18 02:11:23 +00:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2021-01-03 05:09:43 +00:00
|
|
|
const ListItem = styled.li`
|
|
|
|
${(props) =>
|
|
|
|
props.$pulse &&
|
|
|
|
css`
|
|
|
|
animation: ${pulse} 250ms;
|
|
|
|
`}
|
|
|
|
`;
|
|
|
|
|
2019-01-10 06:41:06 +00:00
|
|
|
const Container = styled.div`
|
|
|
|
display: inline-block;
|
2017-09-23 22:58:34 +00:00
|
|
|
align-items: center;
|
|
|
|
animation: ${fadeAndScaleIn} 100ms ease;
|
|
|
|
margin: 8px 0;
|
2020-08-09 01:53:11 +00:00
|
|
|
color: ${(props) => props.theme.toastText};
|
|
|
|
background: ${(props) => props.theme.toastBackground};
|
2017-09-23 22:58:34 +00:00
|
|
|
font-size: 15px;
|
|
|
|
border-radius: 5px;
|
|
|
|
cursor: default;
|
|
|
|
|
|
|
|
&:hover {
|
2020-08-09 01:53:11 +00:00
|
|
|
background: ${(props) => darken(0.05, props.theme.toastBackground)};
|
2017-09-23 22:58:34 +00:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Message = styled.div`
|
2019-04-18 02:11:23 +00:00
|
|
|
display: inline-block;
|
|
|
|
padding: 10px 12px;
|
2017-09-23 22:58:34 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
export default Toast;
|