A platform for community discussion. Free, open, simple.
FIX: CLS jumpiness in post-stream when ?page=N (#25034)
Natalie Tay12131c8e21a9
diff --git a/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js b/app/assets/javascripts/discourse/app/components/scrolling-post-stream.jsindex d4635a4f8f1..ed6c637f7d1 100644--- a/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js+++ b/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js@@ -181,10 +181,7 @@ export default MountWidget.extend({const first = posts.objectAt(onscreen[0]);if (this._topVisible !== first) {this._topVisible = first;- const elem = postsNodes.item(onscreen[0]);- const elemId = elem.id;- const elemPos = domUtils.position(elem);- const distToElement = elemPos?.top || 0;+ const elemId = postsNodes.item(onscreen[0]).id;const topRefresh = () => {refresh(() => {@@ -194,16 +191,30 @@ export default MountWidget.extend({return;}- const position = domUtils.position(refreshedElem);- const top = position.top - distToElement;- document.documentElement.scroll({ top, left: 0 });+ // The getOffsetTop function calculates the total offset distance of+ // an element from the top of the document. Unlike element.offsetTop+ // which only returns the offset relative to its nearest positioned+ // ancestor, this function recursively accumulates the offsetTop+ // of an element and all of its offset parents (ancestors).+ // This ensures the total distance is measured from the very top of+ // the document, accounting for any nested elements and their+ // respective offsets.+ const getOffsetTop = (element) => {+ if (!element) {+ return 0;+ }+ return element.offsetTop + getOffsetTop(element.offsetParent);+ };++ const top = getOffsetTop(refreshedElem) - offsetCalculator();+ window.scrollTo({ top });// This seems weird, but somewhat infrequently a rerender// will cause the browser to scroll to the top of the document// in Chrome. This makes sure the scroll works correctly if that// happens.schedule("afterRender", () => {- document.documentElement.scroll({ top, left: 0 });+ window.scrollTo({ top });});});};