forked from widse/react-toast
-
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.
- Loading branch information
Showing
11 changed files
with
182 additions
and
46 deletions.
There are no files selected for viewing
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
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,12 @@ | ||
import { TOAST_TYPE } from "./atoms"; | ||
import ToastContainer from "./ToastContainer"; | ||
|
||
const ToastA = () => { | ||
return ( | ||
<ToastContainer toastType={TOAST_TYPE.toastA}> | ||
<div className="Toast">Toast A</div> | ||
</ToastContainer> | ||
); | ||
}; | ||
|
||
export default ToastA; |
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,59 @@ | ||
import { ReactNode, useEffect } from "react"; | ||
import { createPortal } from "react-dom"; | ||
import { AnimatePresence, motion } from "framer-motion"; | ||
import { ToastType } from "./atoms"; | ||
import useToastList from "./useToastList"; | ||
|
||
interface Props { | ||
children: ReactNode; | ||
toastType: ToastType; | ||
config?: { | ||
duration: number; | ||
}; | ||
} | ||
|
||
const ToastContainer = ({children,toastType,config = { duration: 3000 }}: Props) => { | ||
const ref = document.querySelector("#toast-root"); | ||
const { duration } = config; | ||
const { toastList, close } = useToastList(); | ||
|
||
const isShown = toastList.includes(toastType); | ||
|
||
useEffect(() => { | ||
if (!isShown) { | ||
return undefined; | ||
} | ||
|
||
const timer = setTimeout(() => { | ||
close(toastType); | ||
}, duration); | ||
|
||
return () => { | ||
clearTimeout(timer); | ||
}; | ||
}, [close, duration, isShown, toastType]); | ||
|
||
if (!ref) { | ||
return null; | ||
} | ||
|
||
return createPortal( | ||
<AnimatePresence> | ||
{isShown && ( | ||
<motion.div | ||
key={toastType} | ||
layout | ||
initial={{ opacity: 0.5, y: 24 }} | ||
animate={{ opacity: 1, y: 0 }} | ||
exit={{ opacity: 0.5, y: 12 }} | ||
transition={{ ease: "easeOut" }} | ||
> | ||
{children} | ||
</motion.div> | ||
)} | ||
</AnimatePresence>, | ||
ref | ||
); | ||
}; | ||
|
||
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,14 @@ | ||
import { atom } from "recoil"; | ||
|
||
export const TOAST_TYPE = { | ||
toastA: "toastA", | ||
toastB: "toastB", | ||
toastC: "toastC", | ||
} as const; | ||
|
||
export type ToastType = keyof typeof TOAST_TYPE; | ||
|
||
export const ToastList = atom<ToastType[]>({ | ||
key: "ToastList", | ||
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,34 @@ | ||
import { useCallback } from "react"; | ||
import { useRecoilState } from "recoil"; | ||
import { ToastList, ToastType } from "./atoms"; | ||
|
||
const useToastList = () => { | ||
const [toastList, setToastList] = useRecoilState<ToastType[]>(ToastList); | ||
|
||
const show = useCallback( | ||
(toastType: ToastType) => { | ||
setToastList((prev) => prev.concat(toastType)); | ||
}, | ||
[setToastList] | ||
); | ||
|
||
const close = useCallback( | ||
(toastType: ToastType) => { | ||
setToastList((prev) => prev.filter((t) => t !== toastType)); | ||
}, | ||
[setToastList] | ||
); | ||
|
||
const closeAll = () => { | ||
setToastList([]); | ||
}; | ||
|
||
return { | ||
toastList, | ||
show, | ||
close, | ||
closeAll, | ||
}; | ||
}; | ||
|
||
export default useToastList; |
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 |
---|---|---|
|
@@ -1304,6 +1304,18 @@ | |
resolved "https://registry.yarnpkg.com/@emotion/hash/-/hash-0.9.1.tgz#4ffb0055f7ef676ebc3a5a91fb621393294e2f43" | ||
integrity sha512-gJB6HLm5rYwSLI6PQa+X1t5CFGrv1J1TWG+sOyMCeKz2ojaj6Fnl/rZEspogG+cvqbt4AE/2eIyD2QfLKTBNlQ== | ||
|
||
"@emotion/is-prop-valid@^0.8.2": | ||
version "0.8.8" | ||
resolved "https://registry.yarnpkg.com/@emotion/is-prop-valid/-/is-prop-valid-0.8.8.tgz#db28b1c4368a259b60a97311d6a952d4fd01ac1a" | ||
integrity sha512-u5WtneEAr5IDG2Wv65yhunPSMLIpuKsbuOktRojfrEiEvRyC85LgPMZI63cr7NUqT8ZIGdSVg8ZKGxIug4lXcA== | ||
dependencies: | ||
"@emotion/memoize" "0.7.4" | ||
|
||
"@emotion/[email protected]": | ||
version "0.7.4" | ||
resolved "https://registry.yarnpkg.com/@emotion/memoize/-/memoize-0.7.4.tgz#19bf0f5af19149111c40d98bb0cf82119f5d9eeb" | ||
integrity sha512-Ja/Vfqe3HpuzRsG1oBtWTHk2PGZ7GR+2Vz5iYGelAw8dx32K0y7PjVuxK6z1nMpZOqAFsRUPCkK1YjJ56qJlgw== | ||
|
||
"@emotion/memoize@^0.8.1": | ||
version "0.8.1" | ||
resolved "https://registry.yarnpkg.com/@emotion/memoize/-/memoize-0.8.1.tgz#c1ddb040429c6d21d38cc945fe75c818cfb68e17" | ||
|
@@ -4830,6 +4842,15 @@ fraction.js@^4.3.6: | |
resolved "https://registry.yarnpkg.com/fraction.js/-/fraction.js-4.3.7.tgz#06ca0085157e42fda7f9e726e79fefc4068840f7" | ||
integrity sha512-ZsDfxO51wGAXREY55a7la9LScWpwv9RxIrYABrlvOFBlH/ShPnrtsXeuUIfXKKOVicNxQ+o8JTbJvjS4M89yew== | ||
|
||
framer-motion@^10.16.16: | ||
version "10.16.16" | ||
resolved "https://registry.yarnpkg.com/framer-motion/-/framer-motion-10.16.16.tgz#a10a03e1190a717109163cfff212a84c8ad11b0c" | ||
integrity sha512-je6j91rd7NmUX7L1XHouwJ4v3R+SO4umso2LUcgOct3rHZ0PajZ80ETYZTajzEXEl9DlKyzjyt4AvGQ+lrebOw== | ||
dependencies: | ||
tslib "^2.4.0" | ||
optionalDependencies: | ||
"@emotion/is-prop-valid" "^0.8.2" | ||
|
||
[email protected]: | ||
version "0.5.2" | ||
resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7" | ||
|
@@ -5046,6 +5067,11 @@ gzip-size@^6.0.0: | |
dependencies: | ||
duplexer "^0.1.2" | ||
|
||
[email protected]: | ||
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/hamt_plus/-/hamt_plus-1.0.2.tgz#e21c252968c7e33b20f6a1b094cd85787a265601" | ||
integrity sha512-t2JXKaehnMb9paaYA7J0BX8QQAY8lwfQ9Gjf4pg/mk4krt+cmwmU652HOoWonf+7+EQV97ARPMhhVgU1ra2GhA== | ||
|
||
handle-thing@^2.0.0: | ||
version "2.0.1" | ||
resolved "https://registry.yarnpkg.com/handle-thing/-/handle-thing-2.0.1.tgz#857f79ce359580c340d43081cc648970d0bb234e" | ||
|
@@ -8011,6 +8037,13 @@ readdirp@~3.6.0: | |
dependencies: | ||
picomatch "^2.2.1" | ||
|
||
recoil@^0.7.7: | ||
version "0.7.7" | ||
resolved "https://registry.yarnpkg.com/recoil/-/recoil-0.7.7.tgz#c5f2c843224384c9c09e4a62c060fb4c1454dc8e" | ||
integrity sha512-8Og5KPQW9LwC577Vc7Ug2P0vQshkv1y3zG3tSSkWMqkWSwHmE+by06L8JtnGocjW6gcCvfwB3YtrJG6/tWivNQ== | ||
dependencies: | ||
hamt_plus "1.0.2" | ||
|
||
recursive-readdir@^2.2.2: | ||
version "2.2.3" | ||
resolved "https://registry.yarnpkg.com/recursive-readdir/-/recursive-readdir-2.2.3.tgz#e726f328c0d69153bcabd5c322d3195252379372" | ||
|
@@ -9053,7 +9086,7 @@ tslib@^1.8.1: | |
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" | ||
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg== | ||
|
||
tslib@^2.0.3: | ||
tslib@^2.0.3, tslib@^2.4.0: | ||
version "2.6.2" | ||
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.2.tgz#703ac29425e7b37cd6fd456e92404d46d1f3e4ae" | ||
integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q== | ||
|