From 2c33c4094d3f68f8bf1fd3347c04c41c94ec2c32 Mon Sep 17 00:00:00 2001 From: David Schnurr Date: Wed, 8 Jan 2020 10:21:43 -0800 Subject: [PATCH] Fix for defocusing text inputs #315 --- lib/utils/domFns.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/utils/domFns.js b/lib/utils/domFns.js index 6963da72..00b51ba9 100644 --- a/lib/utils/domFns.js +++ b/lib/utils/domFns.js @@ -158,14 +158,21 @@ export function addUserSelectStyles(doc: ?Document) { } export function removeUserSelectStyles(doc: ?Document) { + if (!doc) return; try { - if (doc && doc.body) removeClassName(doc.body, 'react-draggable-transparent-selection'); + if (doc.body) removeClassName(doc.body, 'react-draggable-transparent-selection'); // $FlowIgnore: IE if (doc.selection) { // $FlowIgnore: IE doc.selection.empty(); } else { - window.getSelection().removeAllRanges(); // remove selection caused by scroll + // Remove selection caused by scroll, unless it's a focused input + // (we use doc.defaultView in case we're in an iframe) + const win = (doc.defaultView || window).getSelection(); + const selection = win; + if (selection && selection.type !== 'Caret') { + selection.removeAllRanges(); + } } } catch (e) { // probably IE