Skip to content

Commit f2f3889

Browse files
authored
fix(ui5-popup): prevent focus on elements below block layer (#2800)
Problem: When element inside the popup, which can't get focus is clicked, the focus goes on the body element. Then any element below the block layer can be focused. Solution: Don't let the focus leave the popup. Fixes: #2626
1 parent 747738e commit f2f3889

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

packages/main/src/Popup.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
dir="{{dir}}"
99
tabindex="-1"
1010
@keydown={{_onkeydown}}
11+
@focusout={{_onfocusout}}
1112
>
1213

1314
<span class="first-fe" data-ui5-focus-trap tabindex="0" @focusin={{forwardToLast}}></span>

packages/main/src/Popup.js

+7
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ class Popup extends UI5Element {
263263
}
264264
}
265265

266+
_onfocusout(e) {
267+
// relatedTarget is the element, which will get focus. If no such element exists, focus the root
268+
if (!e.relatedTarget) {
269+
this._root.focus();
270+
}
271+
}
272+
266273
/**
267274
* Focus trapping
268275
* @private

packages/main/test/pages/Popover.html

+14
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,15 @@
384384

385385
<br>
386386

387+
<ui5-button id="btnOpenPopoverWithDiv">Open Popover with div Inside</ui5-button>
388+
389+
<ui5-popover id="popWithDiv" header-text="Newsletter subscription" modal>
390+
<div id="divContent">I'm a div, which can't get focus. Click me and see where focus goes.</div>
391+
<div slot="footer" class="popover-footer">
392+
<ui5-button design="Emphasized">Subscribe</ui5-button>
393+
</div>
394+
</ui5-popover>
395+
387396
<script>
388397
btn.addEventListener("click", function (event) {
389398
pop.openBy(btn);
@@ -488,6 +497,11 @@
488497

489498
popover.openBy(btnOpenDynamic);
490499
});
500+
501+
btnOpenPopoverWithDiv.addEventListener("click", function (event) {
502+
popWithDiv.openBy(event.target);
503+
});
504+
491505
</script>
492506
</body>
493507

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

+12
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,18 @@ describe("Popover general interaction", () => {
251251
assert.ok(activeElementId, popoverId, "Popover remains focused");
252252
});
253253

254+
it("tests focus when content, which can't be focused is clicked", () => {
255+
browser.url("http://localhost:8080/test-resources/pages/Popover.html");
256+
257+
$("#btnOpenPopoverWithDiv").click();
258+
$("#divContent").click();
259+
260+
const popoverId = "popWithDiv";
261+
const activeElementId = $(browser.getActiveElement()).getAttribute("id");
262+
263+
assert.strictEqual(activeElementId, popoverId, "Popover is focused");
264+
});
265+
254266
it("tests that dynamically created popover is opened", () => {
255267
browser.url("http://localhost:8080/test-resources/pages/Popover.html");
256268

0 commit comments

Comments
 (0)