Skip to content

Commit 3a65e51

Browse files
committed
clarify difficult code
1 parent fd5ed10 commit 3a65e51

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

packages/@react-aria/toast/src/useToastRegion.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,29 @@ export function useToastRegion<T>(props: AriaToastRegionProps, state: ToastState
5858
prevVisibleToasts.current = state.visibleToasts;
5959
return;
6060
}
61-
// Get a list of all removed toasts by index.
62-
let removedToasts = prevVisibleToasts.current
61+
// Get a list of all toasts by index and add info if they are removed.
62+
let allToasts = prevVisibleToasts.current
6363
.map((t, i) => ({
6464
...t,
6565
i,
6666
isRemoved: !state.visibleToasts.some(t2 => t.key === t2.key)
6767
}));
6868

69-
let removedToastIndex = removedToasts.findIndex(t => t.i === focusedToast.current);
69+
let removedFocusedToastIndex = allToasts.findIndex(t => t.i === focusedToast.current);
7070

7171
// If the focused toast was removed, focus the next or previous toast.
72-
if (removedToastIndex > -1) {
72+
if (removedFocusedToastIndex > -1) {
7373
let i = 0;
7474
let nextToast;
7575
let prevToast;
76-
while (i <= removedToastIndex) {
77-
if (!removedToasts[i].isRemoved) {
76+
while (i <= removedFocusedToastIndex) {
77+
if (!allToasts[i].isRemoved) {
7878
prevToast = Math.max(0, i - 1);
7979
}
8080
i++;
8181
}
82-
while (i < removedToasts.length) {
83-
if (!removedToasts[i].isRemoved) {
82+
while (i < allToasts.length) {
83+
if (!allToasts[i].isRemoved) {
8484
nextToast = i - 1;
8585
break;
8686
}
@@ -92,6 +92,7 @@ export function useToastRegion<T>(props: AriaToastRegionProps, state: ToastState
9292
prevToast = 0;
9393
}
9494

95+
// prioritize going to newer toasts
9596
if (prevToast >= 0 && prevToast < toasts.current.length) {
9697
focusWithoutScrolling(toasts.current[prevToast]);
9798
} else if (nextToast >= 0 && nextToast < toasts.current.length) {

0 commit comments

Comments
 (0)