diff --git a/packages/main/cypress/specs/Dialog.cy.tsx b/packages/main/cypress/specs/Dialog.cy.tsx index 686ad9cef953..5867fc8e1b88 100644 --- a/packages/main/cypress/specs/Dialog.cy.tsx +++ b/packages/main/cypress/specs/Dialog.cy.tsx @@ -1790,4 +1790,45 @@ describe("Native drag-and-drop in draggable dialogs", () => { expect(preventDefaultCalled).to.be.true; }); }); + + it("initial focus", () => { + cy.mount( + <> + +
+ +
+
+ + ); + + cy.get("#dialogId") + .should("be.visible"); + + cy.get("#innerInput") + .should("be.focused"); + }); + + it("initial focus prevented", () => { + cy.mount( + <> + +
+ +
+
+ + ); + + cy.get("#dialogId") + .should("be.visible"); + + cy.get("#innerInput") + .should("not.be.focused"); + }); }); diff --git a/packages/main/src/Popup.ts b/packages/main/src/Popup.ts index 297addb09bfb..684fd44cab12 100644 --- a/packages/main/src/Popup.ts +++ b/packages/main/src/Popup.ts @@ -253,6 +253,7 @@ abstract class Popup extends UI5Element { _focusedElementBeforeOpen?: HTMLElement | null; _opened = false; _open = false; + _isEnteredInDom = false; _resizeHandlerRegistered = false; constructor() { @@ -290,6 +291,7 @@ abstract class Popup extends UI5Element { } this.tabIndex = -1; + this._isEnteredInDom = true; this.handleOpenOnEnterDOM(); @@ -310,6 +312,8 @@ abstract class Popup extends UI5Element { this._removeOpenedPopup(); } + this._isEnteredInDom = false; + this._deregisterResizeHandler(); this._detachBrowserEvents(); deregisterUI5Element(this); @@ -341,7 +345,7 @@ abstract class Popup extends UI5Element { } async openPopup() { - if (this._opened) { + if (!this._isEnteredInDom && this._opened) { return; } diff --git a/packages/main/test/pages/DialogInitiallyOpenPreventFocus.html b/packages/main/test/pages/DialogInitiallyOpenPreventFocus.html new file mode 100644 index 000000000000..9302d83a4de7 --- /dev/null +++ b/packages/main/test/pages/DialogInitiallyOpenPreventFocus.html @@ -0,0 +1,40 @@ + + + + + + + + Dialog + + + + + + + + + + + + + +