Skip to content

Commit

Permalink
[fixed] check if focusLaterElements is empty before popping
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Austin Wood authored and diasbruno committed Dec 12, 2017
1 parent 33e1fe1 commit eb5ea07
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/helpers/focusManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ export function markForFocusLater() {
export function returnFocus() {
let toFocus = null;
try {
toFocus = focusLaterElements.pop();
toFocus.focus();
if (focusLaterElements.length !== 0) {
toFocus = focusLaterElements.pop();
toFocus.focus();
}
return;
} catch (e) {
console.warn(
Expand Down

0 comments on commit eb5ea07

Please sign in to comment.