From ade038ce79538b62bcad6d49848bd798a11ebbc4 Mon Sep 17 00:00:00 2001 From: Dmitry Guketlev Date: Mon, 8 Aug 2022 12:05:06 -0700 Subject: [PATCH] fix(MenuSurface): fix focus restoration on nested menu surfaces PiperOrigin-RevId: 466109850 --- packages/mdc-menu-surface/foundation.ts | 28 ++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/mdc-menu-surface/foundation.ts b/packages/mdc-menu-surface/foundation.ts index d78b61fa95f..fbfe5baba87 100644 --- a/packages/mdc-menu-surface/foundation.ts +++ b/packages/mdc-menu-surface/foundation.ts @@ -636,21 +636,21 @@ export class MDCMenuSurfaceFoundation extends * closed. */ private maybeRestoreFocus() { - const isRootFocused = this.adapter.isFocused(); - const ownerDocument = this.adapter.getOwnerDocument ? - this.adapter.getOwnerDocument() : - document; - const childHasFocus = ownerDocument.activeElement && - this.adapter.isElementInContainer(ownerDocument.activeElement); - if (isRootFocused || childHasFocus) { - // Wait before restoring focus when closing the menu surface. This is - // important because if a touch event triggered the menu close, and the - // subsequent mouse event occurs after focus is restored, then the - // restored focus would be lost. - setTimeout(() => { + // Wait before restoring focus when closing the menu surface. This is + // important because if a touch event triggered the menu close, and the + // subsequent mouse event occurs after focus is restored, then the + // restored focus would be lost. + setTimeout(() => { + const isRootFocused = this.adapter.isFocused(); + const ownerDocument = this.adapter.getOwnerDocument ? + this.adapter.getOwnerDocument() : + document; + const childHasFocus = ownerDocument.activeElement && + this.adapter.isElementInContainer(ownerDocument.activeElement); + if (isRootFocused || childHasFocus) { this.adapter.restoreFocus(); - }, numbers.TOUCH_EVENT_WAIT_MS); - } + } + }, numbers.TOUCH_EVENT_WAIT_MS); } private hasBit(corner: Corner, bit: CornerBit): boolean {