Skip to content

A platform for community discussion. Free, open, simple.

FIX: Nested view single thread scrolling issues (#40195)

Mark VanLandinghamf8204acfa542
diff --git a/app/assets/stylesheets/common/nested-view.scss b/app/assets/stylesheets/common/nested-view.scss
index c562de52337..0bb78ec80d5 100644
--- a/app/assets/stylesheets/common/nested-view.scss
+++ b/app/assets/stylesheets/common/nested-view.scss
@@ -787,6 +787,9 @@
border: 1px solid var(--tertiary-low);
border-radius: var(--d-border-radius);
font-size: var(--font-down-1);
+ position: sticky;
+ top: var(--header-offset, 0);
+ z-index: 9;
}
&__banner-icon {
diff --git a/frontend/discourse/app/components/nested/context-view.gjs b/frontend/discourse/app/components/nested/context-view.gjs
index 970a011bafb..d4309eb53a1 100644
--- a/frontend/discourse/app/components/nested/context-view.gjs
+++ b/frontend/discourse/app/components/nested/context-view.gjs
@@ -36,10 +36,15 @@ export default class NestedContextView extends Component {
#nextTimer = null;
#retryTimer = null;
#highlightTimer = null;
+ #lastScrollKey = null;
constructor() {
super(...arguments);
- this.appEvents.on("nested:scroll-to-target", this, this.scheduleScroll);
+ this.appEvents.on(
+ "nested:scroll-to-target",
+ this,
+ this.forceScheduleScroll
+ );
}
willDestroy() {
@@ -48,7 +53,11 @@ export default class NestedContextView extends Component {
cancel(this.#nextTimer);
cancel(this.#retryTimer);
clearTimeout(this.#highlightTimer);
- this.appEvents.off("nested:scroll-to-target", this, this.scheduleScroll);
+ this.appEvents.off(
+ "nested:scroll-to-target",
+ this,
+ this.forceScheduleScroll
+ );
this.viewportTracker.destroy();
}
@@ -56,6 +65,21 @@ export default class NestedContextView extends Component {
// scroll wins. Fresh attempt counter per invocation.
@action
scheduleScroll() {
+ this.#scheduleScroll();
+ }
+
+ @action
+ forceScheduleScroll() {
+ this.#scheduleScroll({ force: true });
+ }
+
+ #scheduleScroll({ force = false } = {}) {
+ const scrollKey = this.#scrollKey;
+ if (!force && scrollKey === this.#lastScrollKey) {
+ return;
+ }
+
+ this.#lastScrollKey = scrollKey;
cancel(this.#nextTimer);
cancel(this.#retryTimer);
clearTimeout(this.#highlightTimer);
@@ -63,6 +87,10 @@ export default class NestedContextView extends Component {
this.#nextTimer = next(this, this.#scrollToTarget);
}
+ get #scrollKey() {
+ return `${this.args.targetPostNumber}:${this.args.contextChain?.post?.id}`;
+ }
+
get flatViewUrl() {
return getURL(`/t/${this.args.topic.slug}/${this.args.topic.id}?flat=1`);
}
@@ -90,7 +118,14 @@ export default class NestedContextView extends Component {
2000
);
}
- target.scrollIntoView({ behavior: "smooth", block: "center" });
+ const headerOffset = this.header.headerOffset || 0;
+ const banner = document.querySelector(".nested-context-view__banner");
+ const bannerHeight = banner ? banner.offsetHeight : 0;
+ const rect = target.getBoundingClientRect();
+ window.scrollTo({
+ top: window.scrollY + rect.top - headerOffset - bannerHeight,
+ behavior: "smooth",
+ });
} else if (this.#scrollAttempts < this.#maxScrollAttempts) {
// Target may not be in the DOM yet (async child rendering).
this.#scrollAttempts++;
diff --git a/frontend/discourse/app/components/nested/post.gjs b/frontend/discourse/app/components/nested/post.gjs
index a2e65adb7b6..b3423678725 100644
--- a/frontend/discourse/app/components/nested/post.gjs
+++ b/frontend/discourse/app/components/nested/post.gjs
@@ -52,6 +52,11 @@ export default class NestedPost extends Component {
}
const rect = element.getBoundingClientRect();
window.scrollTo(0, window.scrollY + rect.top - anchor.offsetFromTop);
+
+ // Defer the event to avoid backtracking re-render errors during the render phase
+ Promise.resolve().then(() => {
+ this.appEvents.trigger("nested-replies:scroll-restored");
+ });
});
@tracked _childWasCreated = false;
diff --git a/frontend/discourse/app/controllers/nested.js b/frontend/discourse/app/controllers/nested.js
index 46c92782886..b006330fbd2 100644
--- a/frontend/discourse/app/controllers/nested.js
+++ b/frontend/discourse/app/controllers/nested.js
@@ -401,6 +401,11 @@ export default class NestedController extends Controller {
this,
this.#onPostUnregistered
);
+ this.appEvents.on(
+ "nested-replies:scroll-restored",
+ this,
+ this.#onScrollRestored
+ );
this.#postEventsSubscribed = true;
// Register the OP post directly since it's not rendered by NestedPost
@@ -430,6 +435,11 @@ export default class NestedController extends Controller {
this,
this.#onPostUnregistered
);
+ this.appEvents.off(
+ "nested-replies:scroll-restored",
+ this,
+ this.#onScrollRestored
+ );
this.#postEventsSubscribed = false;
}
if (this.#messageBusChannel) {
@@ -451,6 +461,10 @@ export default class NestedController extends Controller {
}
}
+ #onScrollRestored() {
+ this.scrollAnchor = null;
+ }
+
@bind
_onMessage(data, globalId, messageId) {
if (messageId != null) {