From e18e67b5128a218797b757a7695362c655049f81 Mon Sep 17 00:00:00 2001 From: Infinite Date: Wed, 8 Jan 2020 10:54:28 +0100 Subject: [PATCH] Toast --- ui/v2.5/src/components/Shared/Toast.tsx | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 ui/v2.5/src/components/Shared/Toast.tsx diff --git a/ui/v2.5/src/components/Shared/Toast.tsx b/ui/v2.5/src/components/Shared/Toast.tsx new file mode 100644 index 000000000..6ba4ca1e8 --- /dev/null +++ b/ui/v2.5/src/components/Shared/Toast.tsx @@ -0,0 +1,53 @@ +import React, { useState, useContext, createContext } from 'react'; +import { Toast } from 'react-bootstrap'; + +interface IToast { + header?: string; + content: JSX.Element|string; + delay?: number; +} +interface IActiveToast extends IToast { + id: number; +} + +let toastID = 0; +const ToastContext = createContext<(item:IToast) => void>(() => {}); + +export const ToastProvider: React.FC = ({children}) => { + const [toasts, setToasts] = useState([]); + + const removeToast = (id:number) => ( + setToasts(toasts.filter(item => item.id !== id)) + ); + + const toastItems = toasts.map(toast => ( + removeToast(toast.id)} autohide delay={toast.delay ?? 5000}> + + + { toast.header ?? 'Stash' } + + + {toast.content} + + )); + + const addToast = (toast:IToast) => ( + setToasts([...toasts, { ...toast, id: toastID++ }]) + ); + + return ( + + {children} +
+ { toastItems } +
+
+ ) +} + +const useToasts = () => { + const setToast = useContext(ToastContext); + return setToast; +} + +export default useToasts;