-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor logic to prevent breaking changes in useToastState #6270
Changes from 2 commits
fbd9172
b83af04
6ffd4f5
1e7184a
fbde481
344b124
a69022e
d812221
2edead2
02cf240
6ca5d84
ead9919
735fcab
9026e59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,19 +134,15 @@ export class ToastQueue<T> { | |
} | ||
} | ||
|
||
if (toast.priority) { | ||
this.queue.splice(low, 0, toast); | ||
} else { | ||
this.queue.unshift(toast); | ||
} | ||
this.queue.splice(low, 0, toast); | ||
|
||
toast.animation = low < this.maxVisibleToasts ? 'entering' : 'queued'; | ||
let i = this.maxVisibleToasts; | ||
while (i < this.queue.length) { | ||
this.queue[i++].animation = 'queued'; | ||
} | ||
|
||
this.updateVisibleToasts(); | ||
this.updateVisibleToasts({action: 'add'}); | ||
return toastKey; | ||
} | ||
|
||
|
@@ -159,30 +155,26 @@ export class ToastQueue<T> { | |
if (index >= 0) { | ||
this.queue[index].onClose?.(); | ||
this.queue.splice(index, 1); | ||
this.visibleToasts.find(t => t.key === key).animation = 'exiting'; | ||
} | ||
|
||
this.updateVisibleToasts(index); | ||
this.updateVisibleToasts({action: 'close', key}); | ||
} | ||
|
||
/** Removes a toast from the visible toasts after an exiting animation. */ | ||
remove(key: string) { | ||
let index = this.queue.findIndex(t => t.key === key); | ||
this.visibleToasts = this.visibleToasts.filter(t => t.key !== key); | ||
this.updateVisibleToasts(index); | ||
this.updateVisibleToasts({action: 'remove', key}); | ||
} | ||
|
||
private updateVisibleToasts(oldIndex = -1) { | ||
private updateVisibleToasts(options: {action: 'add' | 'close' | 'remove', key?: string}) { | ||
let {action, key} = options; | ||
let toasts = this.queue.slice(0, this.maxVisibleToasts); | ||
if (this.hasExitAnimation) { | ||
let prevToasts: QueuedToast<T>[] = this.visibleToasts | ||
.filter(t => !toasts.some(t2 => t.key === t2.key)) | ||
.map(t => ({...t, animation: 'exiting'})); | ||
|
||
if (oldIndex !== -1) { | ||
toasts.splice(oldIndex, 0, prevToasts?.[0]); | ||
} | ||
|
||
this.visibleToasts = toasts; | ||
if (action === 'remove') { | ||
this.visibleToasts = this.visibleToasts.filter(t => t.key !== key); | ||
} else if (action === 'close' && this.hasExitAnimation) { | ||
// Cause a rerender to happen for exit animation | ||
this.visibleToasts = this.visibleToasts.map(t => t); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One change in logic here is that previously, if the number of visible toasts exceeded the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh didn't realize that was a case here. Will revisit how we apply the |
||
} else { | ||
this.visibleToasts = toasts; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a breaking change or was
this.queue.unshift(toast);
a bug we hadn't found?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reverts the logic to what is currently released
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I just remembered that and was coming back to delete it, but you responded.