Stop layout shifts when clicking on commits in patch

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ņš <rudolfs@osins.org>
This commit is contained in:
Brandon Oxendine
2026-09-09 14:21:43 +02:00
committed by Rūdolfs Ošiņš
co-authored by Rūdolfs Ošiņš
parent 7dc1d4d94d
commit 039c0d49d5
2 changed files with 22 additions and 4 deletions
+6
View File
@@ -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();
}
+16 -4
View File
@@ -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