@samitouri / QOS-React / commits / 1095d3f965

Preserve scroll position when restoring focus (#8401)

This misbehavior is particularly egregious because of our current strategy for updating a container in Fiber, but regardless -- if we restore focus to an offscreen node (ex: because it was reordered among its siblings) then we should not scroll the page to reveal it. We could check the `overflow` computed style values before saving the scroll positions but that's unlikely to be any faster.

Ben Alpert committed Nov 23, 2016 at 14:55 UTC 1095d3f965b93936ea1a8c96429be7f1fe0b9973
1 file changed +20
src/renderers/dom/shared/ReactInputSelection.js
+20
@@ -66,7 +66,27 @@ var ReactInputSelection = {
66 priorSelectionRange
67 );
68 }
69 +
70 + // Focusing a node can change the scroll position, which is undesirable
71 + const ancestors = [];
72 + let ancestor = priorFocusedElem;
73 + while ((ancestor = ancestor.parentNode)) {
74 + if (ancestor.nodeType === 1) {
75 + ancestors.push({
76 + element: ancestor,
77 + left: ancestor.scrollLeft,
78 + top: ancestor.scrollTop,
79 + });
80 + }
81 + }
82 +
83 focusNode(priorFocusedElem);
84 +
85 + for (let i = 0; i < ancestors.length; i++) {
86 + const ancestor = ancestors[i];
87 + ancestor.element.scrollLeft = ancestor.left;
88 + ancestor.element.scrollTop = ancestor.top;
89 + }
90 }
91 },
92