-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added `useToasts` hook, refactored into using it internally in `ToastProvider`.
- Loading branch information
Showing
6 changed files
with
124 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,39 @@ | ||
import React, { | ||
ReactElement, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
import { ToastEventType } from "../toast"; | ||
import React, { ReactElement } from "react"; | ||
import ReactDOM from "react-dom"; | ||
import { useToasts } from "../useToasts"; | ||
|
||
export type ToastProviderProps<T> = { | ||
renderToasts: (props: { | ||
toasts: (T & { id: string })[]; | ||
onRemoveToast: (id: string) => void; | ||
}) => ReactElement; | ||
removeToastsAfterMs?: number; | ||
}; | ||
|
||
let incr = 0; | ||
const genId = () => { | ||
incr += 1; | ||
return `toast-${incr}`; | ||
onToastAdded?: (toast: T) => void; | ||
portal?: undefined | HTMLElement; | ||
}; | ||
|
||
export function ToastProvider<T extends Record<string, any>>( | ||
props: ToastProviderProps<T> | ||
) { | ||
const mounted = useRef(true); | ||
const [toasts, setToasts] = useState<(T & { id: string })[]>( | ||
[] | ||
); | ||
useEffect(() => { | ||
mounted.current = true; | ||
const listener = (ev: Event) => { | ||
const event = ev as Event & { detail?: T }; | ||
if (event.detail) { | ||
const toastId = genId(); | ||
setToasts((prev) => { | ||
if (event.detail) { | ||
return [...prev, { ...event.detail, id: toastId }]; | ||
} else { | ||
return prev; | ||
} | ||
}); | ||
if (props.removeToastsAfterMs) { | ||
setTimeout(() => { | ||
if (mounted.current) { | ||
setToasts((prev) => | ||
prev.filter((item) => item.id !== toastId) | ||
); | ||
} | ||
}, props.removeToastsAfterMs); | ||
} | ||
} | ||
}; | ||
document.addEventListener(ToastEventType, listener); | ||
return () => { | ||
document.removeEventListener(ToastEventType, listener); | ||
mounted.current = false; | ||
}; | ||
}, [props.removeToastsAfterMs]); | ||
const { toasts, onRemoveToast } = useToasts({ | ||
removeToastsAfterMs: props.removeToastsAfterMs, | ||
onToastAdded: props.onToastAdded, | ||
}); | ||
|
||
if (props.portal) { | ||
return ReactDOM.createPortal( | ||
<props.renderToasts | ||
toasts={toasts} | ||
onRemoveToast={onRemoveToast} | ||
/>, | ||
props.portal | ||
); | ||
} | ||
|
||
return ( | ||
<props.renderToasts | ||
toasts={toasts} | ||
onRemoveToast={(id: string) => { | ||
setToasts((prev) => | ||
prev.filter((item) => item.id !== id) | ||
); | ||
}} | ||
onRemoveToast={onRemoveToast} | ||
/> | ||
); | ||
} |
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,56 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
import { ToastEventType } from "./toast"; | ||
import { genId } from "./utils"; | ||
|
||
export type UseToastsOpts<T> = { | ||
onToastAdded?: (toast: T & { id: string }) => void; | ||
removeToastsAfterMs?: number; | ||
}; | ||
|
||
/** Listens to all toasts and stores them in a list */ | ||
export function useToasts<T>(opts: UseToastsOpts<T> = {}) { | ||
const mounted = useRef(true); | ||
const [toasts, setToasts] = useState<(T & { id: string })[]>( | ||
[] | ||
); | ||
useEffect(() => { | ||
mounted.current = true; | ||
const listener = (ev: Event) => { | ||
const event = ev as Event & { detail?: T }; | ||
if (event.detail) { | ||
const toastId = genId(); | ||
setToasts((prev) => { | ||
if (event.detail) { | ||
const newToast = { ...event.detail, id: toastId }; | ||
opts.onToastAdded?.(newToast); | ||
return [...prev, { ...event.detail, id: toastId }]; | ||
} else { | ||
return prev; | ||
} | ||
}); | ||
if (opts.removeToastsAfterMs) { | ||
setTimeout(() => { | ||
if (mounted.current) { | ||
setToasts((prev) => | ||
prev.filter((item) => item.id !== toastId) | ||
); | ||
} | ||
}, opts.removeToastsAfterMs); | ||
} | ||
} | ||
}; | ||
document.addEventListener(ToastEventType, listener); | ||
return () => { | ||
document.removeEventListener(ToastEventType, listener); | ||
mounted.current = false; | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [opts.removeToastsAfterMs, opts.onToastAdded]); | ||
|
||
return { | ||
toasts, | ||
onRemoveToast: (id: string) => { | ||
setToasts((prev) => prev.filter((item) => item.id !== id)); | ||
}, | ||
}; | ||
} |
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 @@ | ||
let incr = 0; | ||
export const genId = () => { | ||
incr += 1; | ||
return `toast-${incr}`; | ||
}; |