Skip to content

Commit eb5ea07

Browse files
Austin Wooddiasbruno
Austin Wood
authored andcommitted
[fixed] check if focusLaterElements is empty before popping
Without a check to see if the focusLaterElements array is empty, we end up calling `pop()` on an empty array—resulting in an undefined value—and inadvertently jump to the `catch` block when we attempt to call `focus()` on `undefined`. This ensures that if there are no existing elements to return focus to once our modal is closed (this happens often in a testing env), we do not throw a TypeError by accident.
1 parent 33e1fe1 commit eb5ea07

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Diff for: src/helpers/focusManager.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ export function markForFocusLater() {
3737
export function returnFocus() {
3838
let toFocus = null;
3939
try {
40-
toFocus = focusLaterElements.pop();
41-
toFocus.focus();
40+
if (focusLaterElements.length !== 0) {
41+
toFocus = focusLaterElements.pop();
42+
toFocus.focus();
43+
}
4244
return;
4345
} catch (e) {
4446
console.warn(

0 commit comments

Comments
 (0)