From a026b2390b78a67093a5f08dbfe55ab469c95da5 Mon Sep 17 00:00:00 2001 From: Brian Lambert Date: Mon, 16 Sep 2024 15:14:49 -0700 Subject: [PATCH] Improve how last focused element is determined in PositronModalReactRenderer (#4693) --- .../positronModalReactRenderer.tsx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/vs/workbench/browser/positronModalReactRenderer/positronModalReactRenderer.tsx b/src/vs/workbench/browser/positronModalReactRenderer/positronModalReactRenderer.tsx index 2342627c856..c6866b429b7 100644 --- a/src/vs/workbench/browser/positronModalReactRenderer/positronModalReactRenderer.tsx +++ b/src/vs/workbench/browser/positronModalReactRenderer/positronModalReactRenderer.tsx @@ -121,9 +121,17 @@ export class PositronModalReactRenderer extends Disposable { // Call the base class's constructor. super(); - // Set the last focused element. - const activeElement = DOM.getWindow(options.parent).document.activeElement; - if (activeElement instanceof HTMLElement) { + // Get the active element. + let activeElement: Element | null = null; + if (options.parent) { + activeElement = DOM.getWindow(options.parent).document.activeElement; + } + if (!activeElement) { + activeElement = DOM.getActiveWindow().document.activeElement; + } + + // If the active element is an HTML element, set it as the last focused element. + if (DOM.isHTMLElement(activeElement)) { this._lastFocusedElement = activeElement; }