Skip to content

Commit

Permalink
feat: pause timer when app enters background
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnartorfis committed Sep 8, 2024
1 parent d6e04e7 commit 2a1a2c5
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions docs/docs/Toaster.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ You can provide default styles for all toasts by passing `style` and `className`
| toastOptions | These will act as default options for all toasts. See [toast()](/toast) for all available options. | `{}` |
| gap | Gap between toasts when expanded | `16` |
| icons | Changes the default icons | `-` |
| pauseWhenPageIsHidden | Pauses toast timers when the app enters background. | `-` |
| `swipToDismissDirection` | Swipe direction to dismiss (`left`, `up`). | `up` |
2 changes: 1 addition & 1 deletion docs/docs/sonner.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
| offset |||
| hotkey | 🕸️ ||
| loadingIcon |||
| pauseWhenPageIsHidden | ||
| pauseWhenPageIsHidden | ||
| cn |||

**🕸️: Not applicable for native apps.**
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const App: React.FC = () => {
error: <Text>💥</Text>,
}}
toastOptions={{}}
pauseWhenPageIsHidden
/>
</GestureHandlerRootView>
</SafeAreaProvider>
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const toastDefaultValues: {
dismissible: boolean;
unstyled: boolean;
invert: boolean;
pauseWhenPageIsHidden: boolean;
} = {
duration: 4000,
position: 'top-center',
Expand All @@ -22,4 +23,5 @@ export const toastDefaultValues: {
dismissible: true,
unstyled: false,
invert: false,
pauseWhenPageIsHidden: false,
};
40 changes: 25 additions & 15 deletions src/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const Toast: React.FC<ToastProps> = ({
styles: stylesCtx,
classNames: classNamesCtx,
icons,
pauseWhenPageIsHidden,
} = useToastContext();

const unstyled = unstyledProps ?? unstyledCtx;
Expand All @@ -64,31 +65,40 @@ export const Toast: React.FC<ToastProps> = ({
const isResolvingPromise = React.useRef(false);

const onBackground = React.useCallback(() => {
if (!pauseWhenPageIsHidden) {
return;
}

if (timer.current) {
timeLeftOnceBackgrounded.current =
duration - (Date.now() - timerStart.current!);
clearTimeout(timer.current);
timer.current = undefined;
timerStart.current = undefined;
}
}, [duration]);
}, [duration, pauseWhenPageIsHidden]);

const onForeground = React.useCallback(() => {
if (timeLeftOnceBackgrounded.current) {
if (timeLeftOnceBackgrounded.current > 0) {
timer.current = setTimeout(
() => {
if (!isDragging.current) {
onAutoClose?.(id);
}
},
Math.min(timeLeftOnceBackgrounded.current, 1000) // minimum 1 second to avoid weird behavior
);
} else {
onAutoClose?.(id);
}
if (!pauseWhenPageIsHidden) {
return;
}

if (
timeLeftOnceBackgrounded.current &&
timeLeftOnceBackgrounded.current > 0
) {
timer.current = setTimeout(
() => {
if (!isDragging.current) {
onAutoClose?.(id);
}
},
Math.max(timeLeftOnceBackgrounded.current, 1000) // minimum 1 second to avoid weird behavior
);
} else {
onAutoClose?.(id);
}
}, [id, onAutoClose]);
}, [id, onAutoClose, pauseWhenPageIsHidden]);

useAppStateListener(
React.useMemo(
Expand Down
4 changes: 4 additions & 0 deletions src/toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const ToasterUI: React.FC<ToasterProps> = ({
invert,
toastOptions,
icons,
pauseWhenPageIsHidden,
...props
}) => {
const [toasts, setToasts] = React.useState<ToastProps[]>([]);
Expand Down Expand Up @@ -147,6 +148,8 @@ export const ToasterUI: React.FC<ToasterProps> = ({
styles: toastOptions?.styles ?? {},
classNames: toastOptions?.classNames ?? {},
icons: icons ?? {},
pauseWhenPageIsHidden:
pauseWhenPageIsHidden ?? toastDefaultValues.pauseWhenPageIsHidden,
}),
[
duration,
Expand All @@ -158,6 +161,7 @@ export const ToasterUI: React.FC<ToasterProps> = ({
invert,
toastOptions,
icons,
pauseWhenPageIsHidden,
]
);

Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export type ToasterProps = StyleProps & {
loading?: React.ReactNode;
};
swipToDismissDirection?: ToastSwipeDirection;
pauseWhenPageIsHidden?: boolean;
};

export type AddToastContextHandler = (
Expand All @@ -129,6 +130,7 @@ export type ToasterContextType = Required<
| 'classNames'
| 'icons'
| 'offset'
| 'pauseWhenPageIsHidden'
>
> & {
addToast: AddToastContextHandler;
Expand Down

0 comments on commit 2a1a2c5

Please sign in to comment.