Skip to content

Commit a109558

Browse files
authored
feat(ui5-popup): custom popups work with focusable elements in the shadow root (#1844)
1 parent 08fc5f3 commit a109558

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

packages/base/src/util/FocusableElements.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import isNodeHidden from "./isNodeHidden.js";
22
import isNodeClickable from "./isNodeClickable.js";
33

4+
const isFocusTrap = el => {
5+
return el.hasAttribute("data-ui5-focus-trap");
6+
};
7+
48
const getFirstFocusableElement = container => {
59
if (!container || isNodeHidden(container)) {
610
return null;
@@ -19,7 +23,10 @@ const getLastFocusableElement = container => {
1923

2024
const findFocusableElement = (container, forward) => {
2125
let child;
22-
if (container.assignedNodes && container.assignedNodes()) {
26+
27+
if (container.shadowRoot) {
28+
child = forward ? container.shadowRoot.firstChild : container.shadowRoot.lastChild;
29+
} else if (container.assignedNodes && container.assignedNodes()) {
2330
const assignedElements = container.assignedNodes();
2431
child = forward ? assignedElements[0] : assignedElements[assignedElements.length - 1];
2532
} else {
@@ -36,7 +43,7 @@ const findFocusableElement = (container, forward) => {
3643
return null;
3744
}
3845

39-
if (child.nodeType === 1 && !isNodeHidden(child)) {
46+
if (child.nodeType === 1 && !isNodeHidden(child) && !isFocusTrap(child)) {
4047
if (isNodeClickable(child)) {
4148
return (child && typeof child.focus === "function") ? child : null;
4249
}

packages/main/src/Popup.hbs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
{{> beforeHeader}}
44

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

77
{{> header}}
88

@@ -12,7 +12,7 @@
1212

1313
{{> footer}}
1414

15-
<span class="last-fe" tabindex="0" @focusin={{forwardToFirst}}></span>
15+
<span class="last-fe" data-ui5-focus-trap tabindex="0" @focusin={{forwardToFirst}}></span>
1616

1717
</section>
1818

packages/main/src/Popup.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,11 @@ class Popup extends UI5Element {
311311
}
312312

313313
/**
314-
* Sets "inline-block" display to the popup
314+
* Sets "block" display to the popup
315315
* @protected
316316
*/
317317
show() {
318-
this.style.display = "inline-block";
318+
this.style.display = "block";
319319
}
320320

321321

packages/main/test/pages/Dialog.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
<ui5-button id="modals-open">Open Multiple modals</ui5-button>
3838
<br>
3939
<br>
40+
<ui5-button id="empty-open">Open empty dialog (no focusable element)</ui5-button>
41+
<br><br>
4042

4143
<ui5-block-layer></ui5-block-layer>
4244

@@ -198,6 +200,8 @@
198200
</ui5-list>
199201
</ui5-popover>
200202

203+
<ui5-dialog id="empty-dialog">Empty</ui5-dialog>
204+
201205
<script>
202206

203207
let preventClosing = true;
@@ -248,7 +252,9 @@
248252
bigDanger.addEventListener('click', function (event) {
249253
bigDangerPop.openBy(bigDanger);
250254
});
255+
256+
window["empty-open"].addEventListener("click", function (event) { window["empty-dialog"].open(); });
251257
</script>
252258
</body>
253259

254-
</html>
260+
</html>

0 commit comments

Comments
 (0)