fix(desktop): single-owner anchored scroll for dynamic loading (#1115)

Signed-off-by: Tyler Longwell <tlongwell@squareup.com>
Signed-off-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub17jjz49l9jjmhhk7cac63j8yt9z555n9cw8vk7v5jz4vzw4ppld5qgj57cc <f4a42a97e594b77bdbd8ee35191c8b28a94a4cb871d96f32921558275421fb68@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co>
This commit is contained in:
Tyler
2026-06-18 20:40:05 -04:00
committed by GitHub
parent 4d2eb52a6c
commit 047db4214e
25 changed files with 2849 additions and 841 deletions

View File

@@ -144,6 +144,32 @@ function aspectRatioFromDim(dim?: string): number | undefined {
return width / height;
}
/**
* Parse a NIP-92 `dim` value ("WxH") into intrinsic pixel dimensions. Used to
* stamp explicit `width`/`height` attributes on inline images so the browser
* reserves aspect-ratio-correct layout space *before* the image decodes. This
* is what keeps the timeline from jumping when a tall image loads late — the
* row's height is known at first paint instead of growing from ~0 on load.
*/
function dimensionsFromDim(
dim?: string,
): { width: number; height: number } | undefined {
if (!dim) return undefined;
const match = dim.match(/^(\d+)x(\d+)$/i);
if (!match) return undefined;
const width = Number(match[1]);
const height = Number(match[2]);
if (
!Number.isFinite(width) ||
!Number.isFinite(height) ||
width <= 0 ||
height <= 0
) {
return undefined;
}
return { width, height };
}
function isInsideHiddenSpoiler(element: Element): boolean {
return (
element.closest('.buzz-spoiler[data-spoiler][data-revealed="false"]') !==
@@ -1004,10 +1030,12 @@ function ImageZoomOverlay({
*/
function ImageBlock({
alt,
dim,
resolvedSrc,
src,
}: {
alt: string | undefined;
dim?: string;
resolvedSrc: string | undefined;
src: string | undefined;
}) {
@@ -1093,6 +1121,12 @@ function ImageBlock({
const closeMenu = React.useCallback(() => setMenu(null), []);
useDismissImageContextMenu(Boolean(menu), closeMenu);
// Intrinsic dimensions from the NIP-92 `dim` tag, stamped as width/height
// attributes so the browser reserves aspect-ratio space before the image
// decodes. Without this the row grows from ~0 on load and shoves the
// timeline — the exact reflow the anchored-scroll restore then has to fight.
const intrinsicDimensions = dimensionsFromDim(dim);
const handleContextMenu = (e: React.MouseEvent) => {
e.preventDefault();
if (isInsideHiddenSpoiler(e.currentTarget)) return;
@@ -1153,9 +1187,11 @@ function ImageBlock({
alt={alt}
className="block max-h-64 max-w-sm rounded-xl object-contain"
data-spoiler-media-size={hiddenSpoilerMediaSize ? "" : undefined}
height={intrinsicDimensions?.height}
ref={imageRef}
src={resolvedSrc}
style={spoilerMediaStyle}
width={intrinsicDimensions?.width}
onLoad={(event) => updateSpoilerMediaSize(event.currentTarget)}
/>
</button>
@@ -1860,9 +1896,15 @@ function createMarkdownComponents(
</span>
);
}
const entry = src ? imetaByUrl?.get(src) : undefined;
return (
<span data-block-media="" className="block">
<ImageBlock alt={alt} resolvedSrc={resolvedSrc} src={src} />
<ImageBlock
alt={alt}
dim={entry?.dim}
resolvedSrc={resolvedSrc}
src={src}
/>
</span>
);
},
@@ -2094,7 +2136,7 @@ function MarkdownInner({
(link: ParsedMessageLink) => {
// Always route through `goChannel` with `messageId` set: the channel
// route already handles scroll-into-view + highlight via
// `useTimelineScrollManager` + `getEventById` backfill, and works for
// `useAnchoredScroll` + `getEventById` backfill, and works for
// both stream-message replies and forum threads. Detecting "the thread
// root is a forum post" up front would require an event lookup we don't
// currently have synchronously; the brief explicitly allows skipping