All files / lib/components/Toast index.tsx

0% Statements 0/20
0% Branches 0/2
0% Functions 0/8
0% Lines 0/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                                                                                                                                                                                                                                               
/* eslint-disable jsx-a11y/click-events-have-key-events */
import React, { useEffect } from 'react';
import { css, keyframes } from '@emotion/react';
 
interface ToastRenderData {
  text: string;
}
 
export function useToast() {
  const [isOpen, setIsOpen] = React.useState(false);
  const [renderData, setRenderData] = React.useState<ToastRenderData>();
 
  const onClose = () => setIsOpen(false);
 
  const openToast = (data: ToastRenderData) => {
    setRenderData(data);
    setIsOpen(true);
  };
 
  const toastProps = React.useMemo(
    () => ({
      ...renderData,
      onClose,
      isOpen
    }),
    [renderData, isOpen, setIsOpen]
  );
 
  return {
    props: toastProps,
    openToast
  };
}
 
interface ToastProps {
  isOpen: boolean;
  onClose: () => void;
  text: string;
}
 
export function Toast({ isOpen, onClose, text }: ToastProps) {
  useEffect(() => {
    const timer = setTimeout(() => {
      onClose();
    }, 3000);
 
    return () => {
      clearTimeout(timer);
    };
  }, [isOpen]);
 
  return isOpen ? (
    <div css={toastWrapper}>
      <div css={toastBody}>
        <div css={toastText}>{text}</div>
      </div>
    </div>
  ) : null;
}
 
const toastAnimation = keyframes`
  0% {
    opacity: 0;
    transform: translateY(10px);
  }
  20% {
    opacity: 1;
    transform: translateY(0);
  }
  80% {
    opacity: 1;
    transform: translateY(0);
  }
  100% {
    opacity: 0;
    transform: translateY(10px);
  }
`;
 
const toastWrapper = css`
  position: fixed;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  top: 0;
  right: 0;
  bottom: 20px;
  left: 0;
  padding: 0 16px;
 
  z-index: 10000;
 
  animation: ${toastAnimation} 3s ease-in-out;
`;
 
const toastBody = css`
  display: flex;
  flex-direction: column;
  justify-content: center;
 
  width: 168px;
  height: 36px;
 
  border-radius: 16px;
  background: #363636;
 
  box-shadow: 0 10px 130px 0 rgba(0, 0, 0, 0.15), 0 4px 20px 0 rgba(0, 0, 0, 0.15);
`;
 
const toastText = css`
  color: #ffffff;
  text-align: center;
 
  font-family: Noto Sans KR sans-serif;
  font-size: 14px;
  font-style: normal;
  font-weight: 400;
  line-height: 20px;
`;