Skip to content

Commit

Permalink
Rich Text: Avoid activeElement focus call (#20594)
Browse files Browse the repository at this point in the history
* Rich Text: Avoid activeElement focus call

* Rich Text: Restore focus, accounting for null activeElement

* Rich Text: Verify activeElement instanceof HTMLElement
  • Loading branch information
aduth authored Mar 2, 2020
1 parent e01b562 commit 59da7c7
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/rich-text/src/to-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,5 +302,24 @@ export function applySelection( { startPath, endPath }, current ) {
}

selection.addRange( range );
activeElement.focus();

// This function is not intended to cause a shift in focus. Since the above
// selection manipulations may shift focus, ensure that focus is restored to
// its previous state. `activeElement` can be `null` or the body element if
// there is no focus, which is accounted for here in the explicit `blur` to
// restore to a state of non-focus.
if ( activeElement !== document.activeElement ) {
// The `instanceof` checks protect against edge cases where the focused
// element is not of the interface HTMLElement (does not have a `focus`
// or `blur` property).
//
// See: https://github.com/Microsoft/TypeScript/issues/5901#issuecomment-431649653
if ( activeElement ) {
if ( activeElement instanceof window.HTMLElement ) {
activeElement.focus();
}
} else if ( document.activeElement instanceof window.HTMLElement ) {
document.activeElement.blur();
}
}
}

0 comments on commit 59da7c7

Please sign in to comment.