-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from LellowMellow/feature/231122-add-toast-co…
…mponent Feature(#133): Toast 공용 컴포넌트 구현
- Loading branch information
Showing
16 changed files
with
211 additions
and
794 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { useState, useEffect } from "react"; | ||
import { useRecoilState } from "recoil"; | ||
import { toastListState } from "./toastAtom"; | ||
import { useRecoilValue } from "recoil"; | ||
import usePortal from "./usePortal"; | ||
import ReactDOM from "react-dom"; | ||
import SuccessIcon from "@/assets/svgs/toast/success.svg?react"; | ||
import AlertIcon from "@/assets/svgs/toast/alert.svg?react"; | ||
import DefaultIcon from "@/assets/svgs/toast/default.svg?react"; | ||
import { TOAST_AVAILABLE_TIME, TOAST_ANIMATION_TIME } from "./constants"; | ||
|
||
interface ToastProps { | ||
toastKey: number; | ||
message: string; | ||
type: "alert" | "success" | "default"; | ||
} | ||
|
||
const Toast = ({ toastKey, message, type }: ToastProps) => { | ||
const [animation, setAnimation] = useState(true); | ||
const [toastList, setToastList] = useRecoilState(toastListState); | ||
|
||
const handleClickToast = (id: number) => { | ||
setToastList(() => toastList.filter((toast) => toast.id !== id)); | ||
}; | ||
|
||
useEffect(() => { | ||
const timer = setTimeout(() => { | ||
setAnimation(false); | ||
}, TOAST_AVAILABLE_TIME - TOAST_ANIMATION_TIME); | ||
|
||
return () => clearTimeout(timer); | ||
}, []); | ||
|
||
return ( | ||
<div | ||
key={toastKey} | ||
className={`rounded-xl medium-16 w-full text-grayscale-black px-4 py-4 flex flex-row items-center justify-start gap-3 cursor-pointer shadow-ml ${ | ||
animation ? "toast-fade-in" : "toast-fade-out" | ||
} ${type === "alert" && "bg-alert-10"} ${type === "success" && "bg-boarlog-10"} ${ | ||
type === "default" && "bg-grayscale-lightgray" | ||
}`} | ||
style={{ transform: `translateY(-${top}px)`, transition: "transform 0.5s ease" }} | ||
onClick={() => handleClickToast(toastKey)} | ||
> | ||
{type === "alert" && <AlertIcon className="fill-alert-100 w-5 h-5" />} | ||
{type === "success" && <SuccessIcon className="fill-boarlog-100 w-5 h-5" />} | ||
{type === "default" && <DefaultIcon className="fill-grayscale-black w-5 h-5" />} | ||
|
||
<p className="w-full break-all">{message}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
const ToastContainer = () => { | ||
const toastList = useRecoilValue(toastListState); | ||
const portalRoot = usePortal("toast-portal"); | ||
|
||
return portalRoot | ||
? ReactDOM.createPortal( | ||
<div className="fixed w-11/12 max-w-xs bottom-3 left-1/2 -translate-x-1/2 space-y-3"> | ||
{toastList.map((toast) => ( | ||
<Toast key={toast.id} toastKey={toast.id} message={toast.message} type={toast.type} /> | ||
))} | ||
</div>, | ||
portalRoot | ||
) | ||
: null; | ||
}; | ||
|
||
export default ToastContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const TOAST_AVAILABLE_TIME = 3000; | ||
export const TOAST_ANIMATION_TIME = 500; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { atom } from "recoil"; | ||
import { ToastMessage } from "./toastType"; | ||
|
||
export const toastListState = atom<ToastMessage[]>({ | ||
key: "toastListState", | ||
default: [] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface ToastMessage { | ||
id: number; | ||
message: string; | ||
type: "alert" | "success" | "default"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
const usePortal = (id: string) => { | ||
const rootElementRef = useRef<HTMLElement | null>(null); | ||
|
||
useEffect(() => { | ||
let parentElement = document.querySelector(`#${id}`) as HTMLElement | null; | ||
|
||
// id가 있는 element가 없으면 새로 생성 | ||
if (!parentElement) { | ||
parentElement = document.createElement("div"); | ||
parentElement.setAttribute("id", id); | ||
document.body.appendChild(parentElement); | ||
} | ||
|
||
rootElementRef.current = parentElement; | ||
|
||
return () => { | ||
if (parentElement && !parentElement.childElementCount) { | ||
parentElement.remove(); | ||
} | ||
}; | ||
}, [id]); | ||
|
||
return rootElementRef.current; | ||
}; | ||
|
||
export default usePortal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useRecoilState } from "recoil"; | ||
import { toastListState } from "./toastAtom"; | ||
import { ToastMessage } from "./toastType"; | ||
import { TOAST_AVAILABLE_TIME } from "./constants"; | ||
|
||
interface UseToastProps { | ||
message: string; | ||
type: "alert" | "success" | "default"; | ||
} | ||
|
||
export const useToast = () => { | ||
const [toastList, setToastList] = useRecoilState(toastListState); | ||
|
||
const showToast = ({ message, type }: UseToastProps) => { | ||
const newToast: ToastMessage = { id: Date.now(), message, type }; | ||
setToastList([...toastList, newToast]); | ||
|
||
setTimeout(() => { | ||
setToastList((currentList) => currentList.filter((toast) => toast.id !== newToast.id)); | ||
}, TOAST_AVAILABLE_TIME); | ||
}; | ||
|
||
return showToast; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[build] | ||
ignore = "git diff --quiet HEAD^ HEAD frontend/" | ||
|
||
[[redirects]] | ||
from = "/*" | ||
to = "/index.html" | ||
status = 200 |