Skip to content

Commit 7e45dcd

Browse files
fix(mobile): chat code blocks colour diff, kotlin, sql and other languages again (#33680)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 2163d41 commit 7e45dcd

9 files changed

Lines changed: 442 additions & 89 deletions

‎apps/mobile/src/components/ui/AndroidAnchoredMenu.android.tsx‎

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { Pressable, ScrollView, View } from "react-native";
66
import { useKeyboardState } from "react-native-keyboard-controller";
77

88
import { useHardwareBack } from "../../lib/backHandler";
9-
import { deriveMenuHorizontalPlacement } from "../../lib/menu-placement";
9+
import {
10+
deriveMenuHorizontalPlacement,
11+
deriveMenuVerticalPlacement,
12+
} from "../../lib/menu-placement";
1013
import { WindowHingeContext } from "../../native/window-hinge-context";
1114
import { OverlayPortal } from "./OverlayPortal";
1215
import { useAndroidControlSizing } from "./useAndroidControlSizing";
@@ -41,10 +44,9 @@ export type AndroidAnchoredMenuProps = {
4144
};
4245

4346
export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) {
44-
const { scale, menuWidth: desiredMenuWidth } = useAndroidControlSizing();
47+
const { scale, menuWidth: desiredMenuWidth, menuItemHeight } = useAndroidControlSizing();
4548
const [anchor, setAnchor] = useState<AnchorSnapshot | null>(null);
4649
const [path, setPath] = useState<readonly MenuAction[]>([]);
47-
const [rootHeight, setRootHeight] = useState<number | null>(null);
4850
const [overlay, setOverlay] = useState<OverlayFrame | null>(null);
4951
const windowHinge = use(WindowHingeContext);
5052
// State, not a ref: `open` reaches render-prop children, and the compiler
@@ -58,7 +60,6 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) {
5860
setAnchor(null);
5961
setPath([]);
6062
setOverlay(null);
61-
setRootHeight(null);
6263
};
6364

6465
const open = () => {
@@ -70,7 +71,6 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) {
7071
const measureOverlay = () => {
7172
overlayRef.current?.measureInWindow((x, y, width, height) => {
7273
setOverlay({ x, y, width, height });
73-
setRootHeight(height);
7474
});
7575
};
7676

@@ -110,17 +110,19 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) {
110110
margin: SCREEN_MARGIN,
111111
hinge: windowHinge && { x: windowHinge.x - overlay.x, width: windowHinge.width },
112112
});
113-
// This in-window menu preserves keyboard focus; subtract the IME area so its items remain tappable.
114-
const usableBottom =
115-
overlay === null ? 0 : overlay.height - (keyboardVisible ? keyboardHeight : 0);
116-
const spaceBelow =
113+
const verticalPlacement =
117114
local === null || overlay === null
118-
? 0
119-
: usableBottom - (local.y + local.height) - ANCHOR_GAP - SCREEN_MARGIN;
120-
const spaceAbove = local === null ? 0 : local.y - ANCHOR_GAP - SCREEN_MARGIN;
121-
const opensDown = spaceBelow >= 280 || spaceBelow >= spaceAbove;
122-
const maxHeight = Math.min(opensDown ? spaceBelow : spaceAbove, 480);
123-
const placeable = local !== null && rootHeight !== null;
115+
? null
116+
: deriveMenuVerticalPlacement({
117+
anchorY: local.y,
118+
anchorHeight: local.height,
119+
overlayHeight: overlay.height,
120+
// This in-window menu preserves keyboard focus; subtract the IME area so its items remain tappable.
121+
keyboardInset: keyboardVisible ? keyboardHeight : 0,
122+
gap: ANCHOR_GAP,
123+
margin: SCREEN_MARGIN,
124+
minHeight: 2 * menuItemHeight + 14 * scale,
125+
});
124126

125127
const onPressItem = (action: MenuAction) => {
126128
if ((action.subactions?.length ?? 0) > 0) {
@@ -167,7 +169,7 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) {
167169
onLayout={measureOverlay}
168170
>
169171
<Pressable accessible={false} className="absolute inset-0" onPress={close} />
170-
{!placeable || local === null ? null : !anchor.keyboardWasVisible ? (
172+
{local === null ? null : !anchor.keyboardWasVisible ? (
171173
<MenuPopup
172174
// Compose aligns to the trigger's start if it fits; hinge-safe triggers keep menus on their side.
173175
anchor={windowHinge ? { ...local, x: left, width: menuWidth } : local}
@@ -185,12 +187,9 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) {
185187
className="absolute overflow-hidden bg-card-alt shadow-md"
186188
style={{
187189
left,
188-
maxHeight,
189190
width: menuWidth,
190191
borderRadius: 4 * scale,
191-
...(opensDown
192-
? { top: local.y + local.height + ANCHOR_GAP }
193-
: { bottom: (rootHeight ?? 0) - local.y + ANCHOR_GAP }),
192+
...verticalPlacement,
194193
}}
195194
>
196195
{/* Compose DropdownMenu takes focus; in-window editor menus preserve the keyboard. */}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { deriveMenuVerticalPlacement } from "./menu-placement";
4+
5+
const base = { gap: 6, margin: 12, minHeight: 110 };
6+
7+
describe("deriveMenuVerticalPlacement", () => {
8+
it.each([
9+
{
10+
name: "anchor taller than the keyboard-free area stays on screen",
11+
input: { anchorY: -100, anchorHeight: 1000, overlayHeight: 800, keyboardInset: 300 },
12+
expected: { maxHeight: 476, top: 12 },
13+
},
14+
{
15+
name: "room below opens down",
16+
input: { anchorY: 100, anchorHeight: 40, overlayHeight: 800, keyboardInset: 300 },
17+
expected: { maxHeight: 342, top: 146 },
18+
},
19+
{
20+
name: "room above opens up",
21+
input: { anchorY: 400, anchorHeight: 40, overlayHeight: 800, keyboardInset: 300 },
22+
expected: { maxHeight: 382, bottom: 406 },
23+
},
24+
])("$name", ({ input, expected }) => {
25+
expect(deriveMenuVerticalPlacement({ ...base, ...input })).toEqual(expected);
26+
});
27+
});

‎apps/mobile/src/lib/menu-placement.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,31 @@ export function deriveMenuHorizontalPlacement(input: {
2323
const left = Math.min(Math.max(preferredLeft, start + input.margin), end - width - input.margin);
2424
return { left, width };
2525
}
26+
27+
/** In the overlay's coordinates; `bottom` is measured up from the overlay's bottom edge. */
28+
export function deriveMenuVerticalPlacement(input: {
29+
readonly anchorY: number;
30+
readonly anchorHeight: number;
31+
readonly overlayHeight: number;
32+
readonly keyboardInset: number;
33+
readonly gap: number;
34+
readonly margin: number;
35+
readonly minHeight: number;
36+
}) {
37+
const usableBottom = input.overlayHeight - input.keyboardInset;
38+
const spaceBelow = usableBottom - (input.anchorY + input.anchorHeight) - input.gap - input.margin;
39+
const spaceAbove = input.anchorY - input.gap - input.margin;
40+
const opensDown = spaceBelow >= 280 || spaceBelow >= spaceAbove;
41+
const maxHeight = Math.min(opensDown ? spaceBelow : spaceAbove, 480);
42+
if (maxHeight < input.minHeight) {
43+
// Neither side fits (e.g. a code block taller than the screen): overlap the anchor instead.
44+
const top = Math.max(
45+
input.margin,
46+
Math.min(input.anchorY, usableBottom - input.margin - input.minHeight),
47+
);
48+
return { maxHeight: Math.min(usableBottom - input.margin - top, 480), top };
49+
}
50+
return opensDown
51+
? { maxHeight, top: input.anchorY + input.anchorHeight + input.gap }
52+
: { maxHeight, bottom: input.overlayHeight - input.anchorY + input.gap };
53+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import type { ReactNode } from "react";
2+
import { Pressable, StyleSheet, Text, View } from "react-native";
3+
4+
import { SymbolView, type AppSymbolName } from "../components/ui/AppSymbol";
5+
import { themeColorWithAlpha } from "../lib/mobileTheme";
6+
import type { NativeMarkdownTextStyle } from "./SelectableMarkdownText.types";
7+
8+
/**
9+
* The native code block's border and header, for a fence drawn in React once it closes.
10+
* The block slot already applies the code block's outer margins.
11+
*/
12+
export function CodeBlockFrame(props: {
13+
readonly label: string;
14+
readonly textStyle: NativeMarkdownTextStyle;
15+
readonly actions: ReactNode;
16+
readonly children: ReactNode;
17+
}) {
18+
const { textStyle } = props;
19+
return (
20+
<View
21+
style={[
22+
styles.block,
23+
{
24+
backgroundColor: textStyle.codeBlockBackgroundColor,
25+
borderColor: textStyle.dividerColor,
26+
},
27+
]}
28+
>
29+
<View
30+
style={[
31+
styles.header,
32+
{ borderBottomColor: themeColorWithAlpha(textStyle.codeColor, 0.2) },
33+
]}
34+
>
35+
<Text
36+
style={[
37+
styles.label,
38+
{
39+
color: themeColorWithAlpha(textStyle.codeColor, 0.6),
40+
fontSize: textStyle.fontSize * 0.9 * 0.85,
41+
},
42+
]}
43+
>
44+
{props.label}
45+
</Text>
46+
<View style={styles.actions}>{props.actions}</View>
47+
</View>
48+
{props.children}
49+
</View>
50+
);
51+
}
52+
53+
/** A header icon; `active` draws it at full strength, as the native wrap toggle when on. */
54+
export function CodeBlockHeaderButton(props: {
55+
readonly icon: AppSymbolName;
56+
readonly label: string;
57+
readonly textStyle: NativeMarkdownTextStyle;
58+
readonly active?: boolean;
59+
readonly onPress: () => void;
60+
}) {
61+
return (
62+
<Pressable
63+
accessibilityRole="button"
64+
accessibilityLabel={props.label}
65+
hitSlop={10}
66+
onPress={props.onPress}
67+
>
68+
<SymbolView
69+
name={props.icon}
70+
size={14}
71+
tintColor={
72+
props.active
73+
? props.textStyle.codeColor
74+
: themeColorWithAlpha(props.textStyle.codeColor, 0.6)
75+
}
76+
type="monochrome"
77+
/>
78+
</Pressable>
79+
);
80+
}
81+
82+
const styles = StyleSheet.create({
83+
block: {
84+
borderRadius: 8,
85+
borderWidth: StyleSheet.hairlineWidth,
86+
overflow: "hidden",
87+
},
88+
header: {
89+
minHeight: 32,
90+
paddingHorizontal: 12,
91+
flexDirection: "row",
92+
alignItems: "center",
93+
justifyContent: "space-between",
94+
borderBottomWidth: StyleSheet.hairlineWidth,
95+
},
96+
label: { fontWeight: "500" },
97+
actions: { flexDirection: "row", alignItems: "center", gap: 14 },
98+
});

‎apps/mobile/src/native/EnrichedMarkdownText.tsx‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
type MarkdownLinkOccurrence,
2121
type MarkdownStyle,
2222
} from "react-native-enriched-markdown";
23+
import { codeFenceRenderer } from "./codeBlockMarkdown";
24+
import { HighlightedCodeBlock } from "./HighlightedCodeBlock";
2325
import { MermaidCodeBlock } from "./MermaidCodeBlock";
2426
import type { SelectableMarkdownTextProps } from "./SelectableMarkdownText.types";
2527

@@ -29,6 +31,7 @@ import { useEnrichedLinkVariants } from "./useEnrichedLinkVariants";
2931
import { useRetryingImageSource } from "./useRetryingImageSource";
3032
import * as Dialog from "../components/ui/Dialog";
3133
import { copyTextWithHaptic } from "../lib/copyTextWithHaptic";
34+
import type { HighlightScheme } from "../lib/highlights";
3235
import {
3336
ENRICHED_INLINE_FILE_LINK_REGEX,
3437
enrichedTextLinkRegex,
@@ -166,19 +169,25 @@ function enrichedStyle(
166169
};
167170
}
168171

169-
/** Diagram and math fences, as web draws them. A fence streams as native code until it closes. */
172+
/**
173+
* Diagram and math fences, as web draws them, and code enriched-markdown cannot colour.
174+
* Diagrams and math stay native until the library reports them complete; fallback code keeps one block throughout.
175+
*/
170176
function codeBlockElement(
171177
block: MarkdownCodeBlock,
172178
textStyle: SelectableMarkdownTextProps["textStyle"],
173179
markdownStyle: MarkdownStyle,
174180
mathRenderingEnabled: boolean,
181+
scheme: HighlightScheme,
175182
): ReactElement | undefined {
176-
if (!block.isComplete) return undefined;
177-
if (block.language === "mermaid") {
178-
return <MermaidCodeBlock code={block.code} textStyle={textStyle} />;
183+
const renderer = codeFenceRenderer(block.language, mathRenderingEnabled);
184+
if (renderer.kind === "mermaid") {
185+
return block.isComplete ? (
186+
<MermaidCodeBlock code={block.code} textStyle={textStyle} />
187+
) : undefined;
179188
}
180-
if (block.language === "math" && mathRenderingEnabled) {
181-
return (
189+
if (renderer.kind === "math") {
190+
return block.isComplete ? (
182191
<EnrichedMarkdownText
183192
// A blank line would end the paragraph, and so the math, early.
184193
markdown={`$$\n${block.code.replace(/\n\s*\n/g, "\n").trim()}\n$$`}
@@ -190,6 +199,19 @@ function codeBlockElement(
190199
selectionColor={textStyle.selectionColor}
191200
selectionHandleColor={textStyle.selectionHandleColor}
192201
/>
202+
) : undefined;
203+
}
204+
if (renderer.kind === "highlight" && markdownStyle.codeBlock) {
205+
return (
206+
<HighlightedCodeBlock
207+
code={block.code}
208+
fence={block.language}
209+
language={renderer.language}
210+
complete={block.isComplete}
211+
scheme={scheme}
212+
textStyle={textStyle}
213+
codeStyle={markdownStyle.codeBlock}
214+
/>
193215
);
194216
}
195217
return undefined;
@@ -347,7 +369,13 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
347369
}
348370
: undefined,
349371
code: (block: MarkdownCodeBlock) =>
350-
codeBlockElement(block, props.textStyle, baseMarkdownStyle, mathRenderingEnabled),
372+
codeBlockElement(
373+
block,
374+
props.textStyle,
375+
baseMarkdownStyle,
376+
mathRenderingEnabled,
377+
themeAppearance,
378+
),
351379
};
352380
const resolveNativeImageSource = useRetryingImageSource(resolveImageSource);
353381
const renderMarkdown = (openAndroidMenu?: () => void) => {

0 commit comments

Comments
 (0)