refactor: allow opting out of toast pause on window blur behavior

This commit is contained in:
Aman Harwara
2022-12-08 20:49:13 +05:30
parent 51de012236
commit b068b814f2
3 changed files with 11 additions and 5 deletions

View File

@@ -75,8 +75,10 @@ export const ToastTimer: FunctionComponent<Props> = ({ toast, index }) => {
toastElement.addEventListener('mouseenter', handleMouseEnter)
toastElement.addEventListener('mouseleave', handleMouseLeave)
}
window.addEventListener('focus', handlePageFocus)
window.addEventListener('blur', handlePageBlur)
if (toast.pauseOnWindowBlur) {
window.addEventListener('focus', handlePageFocus)
window.addEventListener('blur', handlePageBlur)
}
return () => {
clearTimer()
@@ -84,8 +86,10 @@ export const ToastTimer: FunctionComponent<Props> = ({ toast, index }) => {
toastElement.removeEventListener('mouseenter', handleMouseEnter)
toastElement.removeEventListener('mouseleave', handleMouseLeave)
}
window.removeEventListener('focus', handlePageFocus)
window.removeEventListener('blur', handlePageBlur)
if (toast.pauseOnWindowBlur) {
window.removeEventListener('focus', handlePageFocus)
window.removeEventListener('blur', handlePageBlur)
}
}
}, [
clearTimer,

View File

@@ -61,10 +61,11 @@ export const addToast = action(toastStore, 'addToast', (store: WritableAtom<Toas
If you want to update an existing toast, use the \`updateToast()\` function instead.`)
}
const toast = {
const toast: Toast = {
...options,
id,
dismissed: false,
pauseOnWindowBlur: options.pauseOnWindowBlur ?? true,
}
store.set([...existingToasts, toast])

View File

@@ -12,6 +12,7 @@ type CommonToastProperties = {
progress?: number
autoClose?: boolean
duration?: number
pauseOnWindowBlur?: boolean
}
export type Toast = CommonToastProperties & {