From f130b3dbba7e36ea1f2698cb616ef892335facaf Mon Sep 17 00:00:00 2001 From: Ken Sugiura Date: Wed, 4 Sep 2019 22:58:24 -0400 Subject: [PATCH] Fix issue with `returnFocusOnDeactivate` not working when `active` prop is changed --- src/focus-trap-react.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/focus-trap-react.js b/src/focus-trap-react.js index 5701ec60..4a0354dd 100644 --- a/src/focus-trap-react.js +++ b/src/focus-trap-react.js @@ -48,7 +48,18 @@ class FocusTrap extends React.Component { const returnFocus = returnFocusOnDeactivate || false; const config = { returnFocus }; this.focusTrap.deactivate(config); + if ( + returnFocusOnDeactivate !== false && + this.previouslyFocusedElement && + this.previouslyFocusedElement.focus + ) { + this.previouslyFocusedElement.focus(); + } } else if (!prevProps.active && this.props.active) { + if (typeof document !== 'undefined') { + this.previouslyFocusedElement = document.activeElement; + } + this.focusTrap.activate(); }