Skip to content

Commit 983af6a

Browse files
dimovpetarilhan007
authored andcommitted
fix(ui5-dialog): correctly restore body scrolling on ESC (#3696)
Body scrolling is now blocked only on first call of blockBodyScrolling and restored on the last call of unblockBodyScrolling. Fixes #3690
1 parent e61f46f commit 983af6a

File tree

4 files changed

+66
-24
lines changed

4 files changed

+66
-24
lines changed

packages/main/src/Popup.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { isTabPrevious } from "@ui5/webcomponents-base/dist/Keys.js";
99
import { getNextZIndex, getFocusedElement, isFocusedElementWithinNode } from "@ui5/webcomponents-base/dist/util/PopupUtils.js";
1010
import PopupTemplate from "./generated/templates/PopupTemplate.lit.js";
1111
import PopupBlockLayer from "./generated/templates/PopupBlockLayerTemplate.lit.js";
12-
import { getOpenedPopups, addOpenedPopup, removeOpenedPopup } from "./popup-utils/OpenedPopupsRegistry.js";
12+
import { addOpenedPopup, removeOpenedPopup } from "./popup-utils/OpenedPopupsRegistry.js";
1313

1414
// Styles
1515
import styles from "./generated/themes/Popup.css.js";
@@ -150,6 +150,8 @@ const createBlockingStyle = () => {
150150

151151
createBlockingStyle();
152152

153+
let bodyScrollingBlockers = 0;
154+
153155
/**
154156
* @class
155157
* <h3 class="comment-api-title">Overview</h3>
@@ -237,6 +239,12 @@ class Popup extends UI5Element {
237239
* @protected
238240
*/
239241
static blockBodyScrolling() {
242+
bodyScrollingBlockers++;
243+
244+
if (bodyScrollingBlockers !== 1) {
245+
return;
246+
}
247+
240248
if (window.pageYOffset > 0) {
241249
document.body.style.top = `-${window.pageYOffset}px`;
242250
}
@@ -248,6 +256,12 @@ class Popup extends UI5Element {
248256
* @protected
249257
*/
250258
static unblockBodyScrolling() {
259+
bodyScrollingBlockers--;
260+
261+
if (bodyScrollingBlockers !== 0) {
262+
return;
263+
}
264+
251265
document.body.classList.remove("ui5-popup-scroll-blocker");
252266
window.scrollTo(0, -parseFloat(document.body.style.top));
253267
document.body.style.top = "";
@@ -422,12 +436,9 @@ class Popup extends UI5Element {
422436
return;
423437
}
424438

425-
const openedPopups = getOpenedPopups();
426439
if (this.isModal) {
427440
this._blockLayerHidden = true;
428-
if (openedPopups.length === 1) {
429-
Popup.unblockBodyScrolling();
430-
}
441+
Popup.unblockBodyScrolling();
431442
}
432443

433444
this.hide();

packages/main/src/popup-utils/OpenedPopupsRegistry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const _keydownListener = event => {
3535
}
3636

3737
if (isEscape(event)) {
38-
openedRegistry.pop().instance.close(true);
38+
openedRegistry[openedRegistry.length - 1].instance.close(true);
3939
}
4040
};
4141

packages/main/test/pages/Dialog.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
<body>
2828
<ui5-checkbox text="Compact size" id="cbCompact"></ui5-checkbox>
29+
<ui5-checkbox text="Scrollable page" id="cbScrollable"></ui5-checkbox>
2930
<br>
3031
<ui5-button id="btnOpenDialog">Open Streched Dialog</ui5-button>
3132
<br>
@@ -309,7 +310,7 @@
309310
<br>
310311
<br>
311312

312-
<ui5-button id='popbtn'>Open Popover</ui5-button>
313+
<ui5-button id="dangerDialogBtn">Open Dialog</ui5-button>
313314

314315

315316

@@ -356,11 +357,17 @@
356357

357358
<ui5-dialog id="empty-dialog">Empty</ui5-dialog>
358359

360+
<span id="scrollHelper" style="display: none; margin-top: 1000px;">SCroll Helper</span>
361+
359362
<script>
360363
cbCompact.addEventListener("change", function () {
361364
document.body.classList.toggle("ui5-content-density-compact", cbCompact.checked);
362365
});
363366

367+
cbScrollable.addEventListener("change", function () {
368+
scrollHelper.style.display = cbScrollable.checked ? "block" : "none";
369+
});
370+
364371
let preventClosing = true;
365372

366373
btnOpenDialog.addEventListener("click", function () {

packages/main/test/specs/Dialog.spec.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,29 +167,53 @@ describe("Acc", () => {
167167
});
168168
});
169169

170-
describe("Multiple dialogs page scroll", () => {
171-
before(() => {
172-
browser.url(`http://localhost:${PORT}/test-resources/pages/Dialog.html`);
173-
});
170+
describe("Page scrolling", () => {
171+
before(() => {
172+
browser.url(`http://localhost:${PORT}/test-resources/pages/Dialog.html`);
173+
});
174174

175-
it("tests multiple dialogs page scrolling", () => {
176-
const preventButtonBefore = browser.$("#prevent");
175+
it("tests that page scrolling is blocked and restored", () => {
176+
browser.$("#cbScrollable").click();
177+
const offsetHeightBefore = browser.$("body").getProperty("offsetHeight");
177178

178-
browser.setWindowSize(400, 400);
179-
preventButtonBefore.scrollIntoView();
179+
browser.$("#btnOpenDialog").click();
180180

181-
const offsetBefore = preventButtonBefore.getLocation('y');
181+
assert.ok(browser.$("body").getProperty("offsetHeight") < offsetHeightBefore, "Body scrolling is blocked");
182182

183-
preventButtonBefore.click();
183+
browser.$("#btnCloseDialog").click();
184184

185-
browser.keys("Escape");
186-
const confirmButton = browser.$("#yes");
187-
confirmButton.click();
185+
assert.strictEqual(browser.$("body").getProperty("offsetHeight"), offsetHeightBefore, "Body scrolling is restored");
186+
browser.$("#cbScrollable").click();
187+
});
188188

189-
browser.setTimeout({ script: 5000 });
190-
const offsetAfter = preventButtonBefore.getLocation('y');
189+
it("test page scrolling is restored after close with ESC", () => {
190+
browser.$("#cbScrollable").click();
191+
const offsetHeightBefore = browser.$("body").getProperty("offsetHeight");
191192

192-
assert.strictEqual(offsetBefore, offsetAfter, "No vertical page scrolling when multiple dialogs are closed");
193-
});
193+
browser.$("#btnOpenDialog").click();
194+
browser.keys("Escape");
195+
assert.strictEqual(browser.$("body").getProperty("offsetHeight"), offsetHeightBefore, "Body scrolling is restored");
196+
197+
browser.$("#cbScrollable").click();
198+
});
199+
200+
it("tests multiple dialogs page scrolling", () => {
201+
const preventButtonBefore = browser.$("#prevent");
194202

203+
browser.setWindowSize(400, 400);
204+
preventButtonBefore.scrollIntoView();
205+
206+
const offsetBefore = preventButtonBefore.getLocation('y');
207+
208+
preventButtonBefore.click();
209+
210+
browser.keys("Escape");
211+
const confirmButton = browser.$("#yes");
212+
confirmButton.click();
213+
214+
browser.setTimeout({ script: 5000 });
215+
const offsetAfter = preventButtonBefore.getLocation('y');
216+
217+
assert.strictEqual(offsetBefore, offsetAfter, "No vertical page scrolling when multiple dialogs are closed");
218+
});
195219
});

0 commit comments

Comments
 (0)