Skip to content

Commit

Permalink
Move dismiss logic back to toast
Browse files Browse the repository at this point in the history
- Use `toast.dismiss()` inside th `useToaster` auto-dimiss logic
  • Loading branch information
timolins committed Jan 6, 2021
1 parent 254a0bb commit 4a4760d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
15 changes: 13 additions & 2 deletions src/core/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ValueOrFunction,
resolveValueOrFunction,
} from './types';
import { genId, dismissToast } from './utils';
import { genId } from './utils';
import { dispatch, ActionType } from './store';

type Message = ValueOrFunction<Renderable, Toast>;
Expand Down Expand Up @@ -46,7 +46,18 @@ toast.error = createHandler('error');
toast.success = createHandler('success');
toast.loading = createHandler('loading');

toast.dismiss = (toastId?: string) => dismissToast(toastId);
toast.dismiss = (toastId?: string) => {
dispatch({
type: ActionType.DISMISS_TOAST,
toastId,
});
setTimeout(() => {
dispatch({
type: ActionType.REMOVE_TOAST,
toastId,
});
}, 1000);
};

toast.remove = (toastId?: string) =>
dispatch({ type: ActionType.REMOVE_TOAST, toastId });
Expand Down
6 changes: 3 additions & 3 deletions src/core/use-toaster.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useMemo } from 'react';
import { dispatch, ActionType, useStore } from './store';
import { toast } from './toast';
import { DefaultToastOptions } from './types';
import { dismissToast } from './utils';

export const useToaster = (toastOptions?: DefaultToastOptions) => {
const { toasts, pausedAt } = useStore(toastOptions);
Expand All @@ -19,11 +19,11 @@ export const useToaster = (toastOptions?: DefaultToastOptions) => {

if (durationLeft < 0) {
if (t.visible) {
dismissToast(t.id);
toast.dismiss(t.id);
}
return;
}
return setTimeout(() => dismissToast(t.id), durationLeft);
return setTimeout(() => toast.dismiss(t.id), durationLeft);
});

return () => {
Expand Down
15 changes: 0 additions & 15 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import { dispatch, ActionType } from './store';

export const genId = (() => {
let count = 0;
return () => {
return (++count).toString();
};
})();

export const dismissToast = (toastId?: string) => {
dispatch({
type: ActionType.DISMISS_TOAST,
toastId,
});
setTimeout(() => {
dispatch({
type: ActionType.REMOVE_TOAST,
toastId,
});
}, 1000);
};

1 comment on commit 4a4760d

@vercel
Copy link

@vercel vercel bot commented on 4a4760d Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.