Skip to content

Commit

Permalink
fix(ui5-dialog): fix text selection on chrome
Browse files Browse the repository at this point in the history
WICG/webcomponents#773

On Chrome, clicking on slotted elements (light DOM) in the
dialog (shadow DOM) causes the focus to travel up the light DOM tree to
reach the closes element that is focusable.
Which ends to be the body element as document.activeElement.

This is not the case for FF & Safari, where the focus travels up the
slot, reaching the dialog root which is actually the closest focusable element.

This change introduces a workaround to align the focus behaviour in Chrome
with the other browsers.

Fixes #3466
  • Loading branch information
georgimkv committed Jul 26, 2021
1 parent b8537b5 commit b68bfdf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/main/src/Popup.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
dir="{{effectiveDir}}"
@keydown={{_onkeydown}}
@focusout={{_onfocusout}}
@mouseup={{_onmouseup}}
>

<span class="first-fe" data-ui5-focus-trap tabindex="0" @focusin={{forwardToLast}}></span>
Expand Down
9 changes: 8 additions & 1 deletion packages/main/src/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,17 @@ class Popup extends UI5Element {
}

_onfocusout(e) {
// relatedTarget is the element, which will get focus. If no such element exists, focus the root
// relatedTarget is the element, which will get focus. If no such element exists, focus the root.
if (!e.relatedTarget) {
this._root.tabIndex = -1;
this._shouldFocusRoot = true;
}
}

_onmouseup(e) {
if (document.activeElement !== this && !this.contains(document.activeElement) && this._shouldFocusRoot) {
this._root.focus();
this._shouldFocusRoot = false;
}
}

Expand Down

0 comments on commit b68bfdf

Please sign in to comment.