refactor: allow opting out of toast pause on window blur behavior
This commit is contained in:
@@ -75,8 +75,10 @@ export const ToastTimer: FunctionComponent<Props> = ({ toast, index }) => {
|
|||||||
toastElement.addEventListener('mouseenter', handleMouseEnter)
|
toastElement.addEventListener('mouseenter', handleMouseEnter)
|
||||||
toastElement.addEventListener('mouseleave', handleMouseLeave)
|
toastElement.addEventListener('mouseleave', handleMouseLeave)
|
||||||
}
|
}
|
||||||
window.addEventListener('focus', handlePageFocus)
|
if (toast.pauseOnWindowBlur) {
|
||||||
window.addEventListener('blur', handlePageBlur)
|
window.addEventListener('focus', handlePageFocus)
|
||||||
|
window.addEventListener('blur', handlePageBlur)
|
||||||
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
clearTimer()
|
clearTimer()
|
||||||
@@ -84,8 +86,10 @@ export const ToastTimer: FunctionComponent<Props> = ({ toast, index }) => {
|
|||||||
toastElement.removeEventListener('mouseenter', handleMouseEnter)
|
toastElement.removeEventListener('mouseenter', handleMouseEnter)
|
||||||
toastElement.removeEventListener('mouseleave', handleMouseLeave)
|
toastElement.removeEventListener('mouseleave', handleMouseLeave)
|
||||||
}
|
}
|
||||||
window.removeEventListener('focus', handlePageFocus)
|
if (toast.pauseOnWindowBlur) {
|
||||||
window.removeEventListener('blur', handlePageBlur)
|
window.removeEventListener('focus', handlePageFocus)
|
||||||
|
window.removeEventListener('blur', handlePageBlur)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [
|
}, [
|
||||||
clearTimer,
|
clearTimer,
|
||||||
|
|||||||
@@ -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.`)
|
If you want to update an existing toast, use the \`updateToast()\` function instead.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const toast = {
|
const toast: Toast = {
|
||||||
...options,
|
...options,
|
||||||
id,
|
id,
|
||||||
dismissed: false,
|
dismissed: false,
|
||||||
|
pauseOnWindowBlur: options.pauseOnWindowBlur ?? true,
|
||||||
}
|
}
|
||||||
|
|
||||||
store.set([...existingToasts, toast])
|
store.set([...existingToasts, toast])
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ type CommonToastProperties = {
|
|||||||
progress?: number
|
progress?: number
|
||||||
autoClose?: boolean
|
autoClose?: boolean
|
||||||
duration?: number
|
duration?: number
|
||||||
|
pauseOnWindowBlur?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Toast = CommonToastProperties & {
|
export type Toast = CommonToastProperties & {
|
||||||
|
|||||||
Reference in New Issue
Block a user