From 039c0d49d58b402448a9a340552c3a83e4bc6fd2 Mon Sep 17 00:00:00 2001 From: Brandon Oxendine Date: Wed, 9 Sep 2026 13:12:51 +0200 Subject: [PATCH] Stop layout shifts when clicking on commits in patch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit scrollToFilesTop scrolled the port down to hide the patch chrome, with a smooth animation. Clicking a commit while already at the top of the diff pulled the header off screen for no reason. Co-authored-by: Rūdolfs Ošiņš --- src/components/Changes.svelte | 6 ++++++ src/components/PierreDiff.svelte | 20 ++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/components/Changes.svelte b/src/components/Changes.svelte index e3b18c7cd..08a269932 100644 --- a/src/components/Changes.svelte +++ b/src/components/Changes.svelte @@ -407,7 +407,13 @@ // auto-selects its only commit — so an effect could not tell "the reader // picked a commit" from "the revision changed underneath them", and scrolled // on both. + // + // The tick comes first so that the selection has reached the effect that + // loads the diff: `diffReady` is read the moment it is awaited, and reading + // it any earlier gets the promise for the diff being navigated away from. async function scrollToDiff() { + await tick(); + await diffReady; await tick(); diffView?.scrollToFilesTop(); } diff --git a/src/components/PierreDiff.svelte b/src/components/PierreDiff.svelte index 6fcf446be..48993bc2a 100644 --- a/src/components/PierreDiff.svelte +++ b/src/components/PierreDiff.svelte @@ -373,7 +373,7 @@ // Scroll to a logical position in the scroll content, where 0 is the top of // the `header`. Pierre reuses a paged scroll scaffold, so the logical position // is not the container's `scrollTop` and has to go through `scrollTo`. - export function scrollToPosition( + function scrollToPosition( position: number, behavior: "smooth" | "instant" = "smooth", ): void { @@ -392,12 +392,24 @@ return headerEl.getBoundingClientRect().height - reserve; } - // Scroll the chrome out of the way. + // Bring the top of the files column into view, and only ever come back up to + // it: a caller asking for the files top wants the diff's first lines on + // screen, and they already are whenever the port sits above that point. + // Scrolling down from there would pull the chrome off screen on the reader's + // behalf, which nothing about picking a commit asks for. Instantly, too — an + // animated correction runs for half a second against a diff that may still be + // loading underneath it, and each adjustment it makes on the way reads as the + // view jumping again. export function scrollToFilesTop(): void { const position = filesTopPosition(); - if (position !== undefined) { - scrollToPosition(position); + if (position === undefined) { + return; } + const current = view?.getScrollTop(); + if (current !== undefined && current <= position) { + return; + } + scrollToPosition(position, "instant"); } // Nothing that targets a file should scroll the chrome back into view: the