fix(web): keep chat media at a stable size while it loads #9938
Mergedjuliusmarmingemerged 6 commits into
main fromrefs/pull/9938/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
- apps/web/src/components/ChatMarkdown.tsx line 1294OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1269,0 +1274,21 @@1274+ CHAT_MARKDOWN_MEDIA_MAX_WIDTH_CLASS_NAME,1275+ CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME,1276+);1277+1278+/**1279+ * Holds a 16:9 slot (or the authored size) until the image has decoded, and1280+ * keeps that slot for the failure state, so a timeline row moves at most once:1281+ * when the natural size arrives. A bare `<img>` is zero height until then.1282+ */1283+function ChatMarkdownImage(props: {1284+ readonly src: string | null;1285+ readonly sourceFailed?: boolean | undefined;1286+ readonly alt: string;1287+ readonly copyMarkdown: string | undefined;1288+ readonly className?: string | undefined;1289+ readonly style?: CSSProperties | undefined;1290+ readonly actionsSource: MediaActionSource;1291+ readonly originalUrl?: string | undefined;1292+ readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;1293+}) {1294+ const [loaded, setLoaded] = useState(false);apps/web/src/components/media/MediaVideoPlayer.tsx line 132OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -127,4 +127,6 @@127127 <span128128 role="alert"129129 className={cn(130- "flex min-h-28 flex-col items-center justify-center gap-3 rounded-lg border border-border/40 bg-muted/40 p-4 text-center text-sm text-muted-foreground",130+ // Same 16:9 slot as the loading and playing states, so a failed or131+ // retried video does not move the rows below it.132+ "flex aspect-video min-h-28 w-full flex-col items-center justify-center gap-3 rounded-lg border border-border/40 bg-muted/40 p-4 text-center text-sm text-muted-foreground",
- apps/web/src/components/ChatMarkdown.tsx line 1470Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1269,0 +1334,21 @@1334+ ) : src !== null ? (1335+ <img1336+ ref={markLoadedIfComplete}1337+ src={src}1338+ alt={props.alt}1339+ decoding="async"1340+ draggable={false}1341+ className={1342+ settled1343+ ? cn(1344+ "block!",1345+ CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME,1346+ CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME,1347+ )1348+ : "invisible absolute inset-0 size-full"1349+ }1350+ style={settled ? props.style : undefined}1351+ onLoad={() => setLoaded(true)}1352+ onError={() => setFailedSrc(src)}1353+ />1354+ ) : null}
- apps/web/src/components/ChatMarkdown.tsx line 405Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -359,0 +367,21 @@367+ * An image that is the only content of its block (optionally wrapped in a368+ * link) is almost always a screenshot or figure, so it gets a reserved slot369+ * while it loads. Images mixed with text or other images — badge rows, icons370+ * in a sentence — stay inline at their natural size, since a placeholder taller371+ * than the image would move the page more than the image itself does.372+ */373+function markStandaloneImages(node: MarkdownImageHastNode) {374+ const children = meaningfulHastChildren(node);375+ // Only a block can make its image standalone; a link that wraps an image376+ // is still inline content of whatever block holds the link.377+ if (children.length === 1 && node.tagName !== "a") {378+ let only = children[0];379+ if (only?.type === "element" && only.tagName === "a") {380+ const linkChildren = meaningfulHastChildren(only);381+ only = linkChildren.length === 1 ? linkChildren[0] : undefined;382+ }383+ if (only?.type === "element" && only.tagName === "img") {384+ only.properties = { ...only.properties, dataStandalone: true };385+ }386+ }387+ node.children?.forEach((child) => {
juliusmarminge merged thiseced382
juliusmarminge closed this
Sign in to join this conversation.




macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed