From b068b814f2e76cc8a66ecdff9cf8c778844e1bd0 Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Thu, 8 Dec 2022 20:49:13 +0530 Subject: [PATCH] refactor: allow opting out of toast pause on window blur behavior --- packages/toast/src/ToastTimer.tsx | 12 ++++++++---- packages/toast/src/toastStore.ts | 3 ++- packages/toast/src/types.ts | 1 + 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/toast/src/ToastTimer.tsx b/packages/toast/src/ToastTimer.tsx index 190c1afbd..483448cc8 100644 --- a/packages/toast/src/ToastTimer.tsx +++ b/packages/toast/src/ToastTimer.tsx @@ -75,8 +75,10 @@ export const ToastTimer: FunctionComponent = ({ 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 = ({ 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, diff --git a/packages/toast/src/toastStore.ts b/packages/toast/src/toastStore.ts index ba676842a..c3875799a 100644 --- a/packages/toast/src/toastStore.ts +++ b/packages/toast/src/toastStore.ts @@ -61,10 +61,11 @@ export const addToast = action(toastStore, 'addToast', (store: WritableAtom