44 * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55 */
66import { AriaModalController } from '@vaadin/a11y-base/src/aria-modal-controller.js' ;
7+ import { FocusRestorationController } from '@vaadin/a11y-base/src/focus-restoration-controller.js' ;
78import { FocusTrapController } from '@vaadin/a11y-base/src/focus-trap-controller.js' ;
89import { getDeepActiveElement } from '@vaadin/a11y-base/src/focus-utils.js' ;
910import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js' ;
@@ -51,6 +52,7 @@ export const OverlayFocusMixin = (superClass) =>
5152
5253 this . __ariaModalController = new AriaModalController ( this ) ;
5354 this . __focusTrapController = new FocusTrapController ( this ) ;
55+ this . __focusRestorationController = new FocusRestorationController ( ) ;
5456 }
5557
5658 /** @protected */
@@ -59,6 +61,7 @@ export const OverlayFocusMixin = (superClass) =>
5961
6062 this . addController ( this . __ariaModalController ) ;
6163 this . addController ( this . __focusTrapController ) ;
64+ this . addController ( this . __focusRestorationController ) ;
6265 }
6366
6467 /**
@@ -73,18 +76,18 @@ export const OverlayFocusMixin = (superClass) =>
7376 }
7477
7578 if ( this . restoreFocusOnClose && this . _shouldRestoreFocus ( ) ) {
76- this . __restoreFocus ( ) ;
79+ this . __focusRestorationController . restoreFocus ( ) ;
7780 }
7881 }
7982
8083 /**
81- * Store previously focused node when the overlay starts to open.
84+ * Save the previously focused node when the overlay starts to open.
8285 *
8386 * @protected
8487 */
85- _storeFocus ( ) {
88+ _saveFocus ( ) {
8689 if ( this . restoreFocusOnClose ) {
87- this . __storeFocus ( ) ;
90+ this . __focusRestorationController . saveFocus ( this . restoreFocusNode ) ;
8891 }
8992 }
9093
@@ -136,30 +139,4 @@ export const OverlayFocusMixin = (superClass) =>
136139 }
137140 return n === this ;
138141 }
139-
140- /** @private */
141- __storeFocus ( ) {
142- // Store the focused node.
143- this . __restoreFocusNode = getDeepActiveElement ( ) ;
144- }
145-
146- /** @private */
147- __restoreFocus ( ) {
148- // Use restoreFocusNode if specified, otherwise fallback to the node
149- // which was focused before opening the overlay.
150- const restoreFocusNode = this . restoreFocusNode || this . __restoreFocusNode ;
151- if ( restoreFocusNode ) {
152- if ( getDeepActiveElement ( ) === document . body ) {
153- // In Firefox and Safari, focusing the restoreFocusNode synchronously
154- // doesn't work as expected when the overlay is closing on outside click.
155- // These browsers force focus to move to the body element and retain it
156- // there until the next event loop iteration.
157- setTimeout ( ( ) => restoreFocusNode . focus ( ) ) ;
158- } else {
159- restoreFocusNode . focus ( ) ;
160- }
161-
162- this . __restoreFocusNode = null ;
163- }
164- }
165142 } ;
0 commit comments