From eb5ea0748cae19cd2b8e218b801832cc839cdbdc Mon Sep 17 00:00:00 2001 From: Austin Wood Date: Tue, 12 Dec 2017 15:10:35 -0800 Subject: [PATCH] [fixed] check if focusLaterElements is empty before popping MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/helpers/focusManager.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/helpers/focusManager.js b/src/helpers/focusManager.js index 9d00ebb2..0a8accd8 100644 --- a/src/helpers/focusManager.js +++ b/src/helpers/focusManager.js @@ -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(