-
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
1 parent
3386728
commit b0172cf
Showing
9 changed files
with
170 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { useEffect } from "react"; | ||
import clsx from "clsx"; | ||
|
||
interface ToastProps { | ||
title?: string; | ||
body: string; | ||
onClose: () => void; | ||
type?: "success" | "error" | "warning" | "info"; | ||
} | ||
|
||
const Toast: React.FC<ToastProps> = ({ | ||
title, | ||
body, | ||
onClose, | ||
type = "info", | ||
}) => { | ||
useEffect(() => { | ||
const timer = setTimeout(onClose, 5000); | ||
return () => clearTimeout(timer); | ||
}, [onClose]); | ||
|
||
const getBackgroundColor = () => { | ||
switch (type) { | ||
case "success": | ||
return "bg-green-500"; | ||
case "error": | ||
return "bg-red-500"; | ||
case "warning": | ||
return "bg-yellow-500"; | ||
case "info": | ||
default: | ||
return "bg-blue-500"; | ||
} | ||
}; | ||
|
||
return ( | ||
<div | ||
className={clsx( | ||
"fixed top-4 right-4 text-white p-4 rounded shadow-lg", | ||
getBackgroundColor() | ||
)} | ||
> | ||
{title && <strong className="block text-lg">{title}</strong>} | ||
<div> | ||
<p className={clsx(title && "mt-2")}>{body}</p> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Toast; |
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,13 @@ | ||
import { createContext } from "react"; | ||
|
||
interface ToastContextType { | ||
addToast: ( | ||
title: string | undefined, | ||
messages: string | string[], | ||
type?: "success" | "error" | "warning" | "info" | ||
) => void; | ||
} | ||
|
||
export const ToastContext = createContext<ToastContextType | undefined>( | ||
undefined | ||
); |
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,52 @@ | ||
import { ReactNode, useState } from "react"; | ||
import { ToastContext } from "./ToastContext"; | ||
import Toast from "../components/Toast"; | ||
|
||
interface ToastMessage { | ||
id: number; | ||
title?: string; | ||
message: string; | ||
type: "success" | "error" | "warning" | "info"; | ||
} | ||
|
||
export const ToastProvider: React.FC<{ children: ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const [toasts, setToasts] = useState<ToastMessage[]>([]); | ||
|
||
const addToast = ( | ||
title: string | undefined, | ||
messages: string | string[], | ||
type: "success" | "error" | "warning" | "info" = "info" | ||
) => { | ||
const messageBody = Array.isArray(messages) | ||
? messages.join(", ") | ||
: messages; | ||
|
||
setToasts((prevToasts) => [ | ||
...prevToasts, | ||
{ id: Date.now() + Math.random(), title, message: messageBody, type }, | ||
]); | ||
}; | ||
|
||
const removeToast = (id: number) => { | ||
setToasts((prevToasts) => prevToasts.filter((toast) => toast.id !== id)); | ||
}; | ||
|
||
return ( | ||
<ToastContext.Provider value={{ addToast }}> | ||
{children} | ||
<div className="fixed top-4 right-4 space-y-2"> | ||
{toasts.map((toast) => ( | ||
<Toast | ||
key={toast.id} | ||
title={toast.title} | ||
body={toast.message} | ||
type={toast.type} | ||
onClose={() => removeToast(toast.id)} | ||
/> | ||
))} | ||
</div> | ||
</ToastContext.Provider> | ||
); | ||
}; |
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,10 @@ | ||
import { useContext } from "react"; | ||
import { ToastContext } from "./ToastContext"; | ||
|
||
export const useToast = () => { | ||
const context = useContext(ToastContext); | ||
if (!context) { | ||
throw new Error("useToast must be used within a ToastProvider"); | ||
} | ||
return context; | ||
}; |
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