Skip to content

Commit 5b44d84

Browse files
rodrigoB-CBclaude
andcommitted
fix(mobile): stop Android user bubbles with code blocks from overlapping
On Android a user message containing a fenced code block draws its paragraphs on top of each other inside a hugely over-tall bubble, and the overflow paints over the following rows in the feed. The bubble sizes itself from its content (`maxWidth`, no `width`), so a child whose intrinsic width is unbounded — the code block's horizontal ScrollView — forces a clamp. Android positions the bubble's children during the unclamped intrinsic pass, where the surrounding paragraphs collapse to a single line, and never repositions them once the width is clamped back to `maxWidth`. Measured on a Pixel 8, the paragraph is laid out 612px tall while the code block that follows it is placed 28px below the paragraph's *top* — an overlap of 584px. Pinning the bubble's width removes the intrinsic pass, which is the same reason review-comment bubbles already carry an explicit width. Applies to GFM tables too, which use the same horizontally scrollable renderer. iOS is unaffected: it renders markdown through the native SelectableMarkdownText view, which has no nested block children. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 45d9aa9 commit 5b44d84

3 files changed

Lines changed: 74 additions & 1 deletion

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import Animated, { FadeIn, FadeInUp, type SharedValue } from "react-native-reani
4949
import { useThemeColor } from "../../lib/useThemeColor";
5050
import { useFontFamily } from "../../lib/useFontFamily";
5151
import { copyTextWithHaptic } from "../../lib/copyTextWithHaptic";
52+
import { hasWideMarkdownBlock } from "../../lib/wideMarkdownBlocks";
5253
import {
5354
hasNativeSelectableMarkdownText,
5455
SelectableMarkdownText,
@@ -876,6 +877,12 @@ function renderFeedEntry(
876877
const timestampLabel = formatMessageTime(isUser ? message.createdAt : message.updatedAt);
877878
const attachments = message.attachments ?? [];
878879
const hasReviewCommentContext = message.text.includes("<review_comment");
880+
// A bubble that sizes itself from its content cannot lay out a block whose
881+
// intrinsic width overflows `maxWidth`: Android positions the bubble's
882+
// children during the unclamped pass and never moves them once the width
883+
// is clamped, so the paragraphs around the block end up drawn on top of
884+
// each other. Pinning the width removes that pass.
885+
const hasWideBlock = hasWideMarkdownBlock(message.text);
879886
const assistantTurnStillInProgress =
880887
message.role === "assistant" &&
881888
props.unsettledTurnId !== null &&
@@ -898,7 +905,11 @@ function renderFeedEntry(
898905
style={{
899906
backgroundColor: userBubbleColor,
900907
maxWidth: props.userBubbleMaxWidth,
901-
...(hasReviewCommentContext ? { width: props.reviewCommentBubbleWidth } : null),
908+
...(hasReviewCommentContext
909+
? { width: props.reviewCommentBubbleWidth }
910+
: hasWideBlock
911+
? { width: props.userBubbleMaxWidth }
912+
: null),
902913
}}
903914
>
904915
{message.text.trim().length > 0 ? (
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { hasWideMarkdownBlock } from "./wideMarkdownBlocks";
4+
5+
describe("hasWideMarkdownBlock", () => {
6+
it("ignores prose, inline code, and emphasis", () => {
7+
expect(hasWideMarkdownBlock("just a message")).toBe(false);
8+
expect(hasWideMarkdownBlock("I found it in `secteurs_intervention` earlier")).toBe(false);
9+
expect(hasWideMarkdownBlock("a | b in a sentence")).toBe(false);
10+
expect(hasWideMarkdownBlock("an em dash — and a rule\n\n---\n")).toBe(false);
11+
});
12+
13+
it("detects fenced code blocks", () => {
14+
expect(hasWideMarkdownBlock("before\n```\ncode\n```\nafter")).toBe(true);
15+
expect(hasWideMarkdownBlock("before\n```ts\ncode\n```")).toBe(true);
16+
expect(hasWideMarkdownBlock("before\n~~~\ncode\n~~~")).toBe(true);
17+
expect(hasWideMarkdownBlock(" ```\ncode\n```")).toBe(true);
18+
});
19+
20+
it("detects GFM tables", () => {
21+
expect(hasWideMarkdownBlock("| a | b |\n| --- | --- |\n| 1 | 2 |")).toBe(true);
22+
expect(hasWideMarkdownBlock("a | b\n:-- | --:\n1 | 2")).toBe(true);
23+
});
24+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Detects markdown that the JS renderer draws as a standalone block View
3+
* wrapping a horizontal ScrollView — fenced code blocks and GFM tables.
4+
*
5+
* Those blocks report an intrinsic width equal to their widest line, which is
6+
* effectively unbounded. A user bubble sizes itself from its content
7+
* (`maxWidth` with no `width`), so Android lays the bubble's children out
8+
* during the unclamped intrinsic pass — where the surrounding paragraphs
9+
* collapse to a single line — and never repositions them once the width is
10+
* clamped back to `maxWidth`. The result is siblings drawn on top of each
11+
* other inside an over-tall bubble. Pinning the bubble's width removes the
12+
* intrinsic pass entirely, which is the same reason review-comment bubbles
13+
* already carry an explicit width.
14+
*
15+
* Indented (four-space) code blocks are deliberately not detected: they are
16+
* vanishingly rare in chat input and the check would fire on ordinary nested
17+
* list continuations.
18+
*/
19+
20+
const FENCED_CODE_BLOCK = /^ {0,3}(?:```|~~~)/m;
21+
22+
function isTableDelimiterRow(line: string): boolean {
23+
const trimmed = line.trim();
24+
if (!trimmed.includes("|") || !trimmed.includes("-")) {
25+
return false;
26+
}
27+
return /^[|\-: \t]+$/.test(trimmed);
28+
}
29+
30+
export function hasWideMarkdownBlock(text: string): boolean {
31+
if (FENCED_CODE_BLOCK.test(text)) {
32+
return true;
33+
}
34+
if (!text.includes("|")) {
35+
return false;
36+
}
37+
return text.split("\n").some(isTableDelimiterRow);
38+
}

0 commit comments

Comments
 (0)