Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 35 additions & 31 deletions app/containers/markdown/__snapshots__/Markdown.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1249,40 +1249,44 @@ exports[`Story Snapshots: Image should match snapshot 1`] = `
}
}
>
<ViewManagerAdapter_ExpoImage
borderColor={4291546833}
containerViewRef={{"current":"Object"}}
contentFit="contain"
contentPosition={
{
"left": "50%",
"top": "50%",
<View>
<ViewManagerAdapter_ExpoImage
aspectRatio={1}
borderColor={4291546833}
containerViewRef={{"current":"Object"}}
contentFit="contain"
contentPosition={
{
"left": "50%",
"top": "50%",
}
}
}
height={300}
nativeViewRef={{"current":"NativeComponent"}}
onError={[Function]}
onLoad={[Function]}
onLoadStart={[Function]}
onProgress={[Function]}
placeholder={[]}
source={
[
maxHeight={300}
nativeViewRef={{"current":"NativeComponent"}}
onError={[Function]}
onLoad={[Function]}
onLoadStart={[Function]}
onProgress={[Function]}
placeholder={[]}
source={
[
{
"uri": "https://play.google.com/intl/en_us/badges/images/badge_new.png",
},
]
}
style={
{
"uri": "https://play.google.com/intl/en_us/badges/images/badge_new.png",
},
]
}
style={
{
"borderColor": "#CBCED1",
"height": 300,
"width": 300,
"aspectRatio": 1,
"borderColor": "#CBCED1",
"maxHeight": 300,
"width": "100%",
}
}
}
transition={null}
width={300}
/>
transition={null}
width="100%"
/>
</View>
</Text>
</Text>
</View>
Expand Down
28 changes: 21 additions & 7 deletions app/containers/markdown/components/Image.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import React, { useState } from 'react';
import { type Image as ImageProps } from '@rocket.chat/message-parser';
import { Image as ExpoImage } from 'expo-image';
import { Image as ExpoImage, type ImageLoadEventData } from 'expo-image';
import { View } from 'react-native';

import { useTheme } from '../../../theme';
import styles from '../styles';
Expand All @@ -9,16 +10,29 @@ interface IImageProps {
value: ImageProps['value'];
}

const MAX_HEIGHT = 300;

const Image = ({ value }: IImageProps) => {
const { colors } = useTheme();
const { src } = value;
const [aspectRatio, setAspectRatio] = useState<number>(1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Initial aspectRatio: 1 causes a visible layout shift on load.

Before onLoad fires, the placeholder renders at full width × up to 300px height (square-capped). For landscape images, once the true ratio is applied, the container height shrinks (e.g. ~300 → ~211 for 16:9), causing a scroll jump in the chat list.

Consider starting with null / undefined and rendering a fixed-height placeholder until the ratio is known:

💡 Proposed mitigation
-const [aspectRatio, setAspectRatio] = useState<number>(1);
+const [aspectRatio, setAspectRatio] = useState<number | undefined>(undefined);
-<View>
-  <ExpoImage
-    style={[styles.inlineImage, { borderColor: colors.strokeLight, aspectRatio, maxHeight: MAX_HEIGHT }]}
-    source={{ uri: encodeURI(src.value) }}
-    contentFit='contain'
-    onLoad={onLoad}
-  />
-</View>
+<View style={{ width: '100%', height: aspectRatio ? undefined : MAX_HEIGHT }}>
+  <ExpoImage
+    style={[styles.inlineImage, { borderColor: colors.strokeLight, aspectRatio, maxHeight: MAX_HEIGHT }]}
+    source={{ uri: encodeURI(src.value) }}
+    contentFit='contain'
+    onLoad={onLoad}
+  />
+</View>

This reserves a consistent MAX_HEIGHT slot before the image loads (no scroll jump), then collapses to the correct aspect-ratio height after onLoad.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/containers/markdown/components/Image.tsx` at line 18, The component
initializes aspectRatio with 1 causing layout shift; change the useState
initialization in the Image component (the aspectRatio / setAspectRatio state
created via useState) to start as null/undefined instead of 1, render a fixed
MAX_HEIGHT placeholder container when aspectRatio is null to reserve space
before onLoad, and then on the image onLoad handler call setAspectRatio with the
measured ratio so the container collapses to the correct height; ensure
MAX_HEIGHT is used consistently in the placeholder and removed when aspectRatio
is set.


const onLoad = (event: ImageLoadEventData) => {
const { height, width } = event.source;
if (width && height) {
setAspectRatio(width / height);
}
};

return (
<ExpoImage
style={[styles.inlineImage, { borderColor: colors.strokeLight }]}
source={{ uri: encodeURI(src.value) }}
contentFit='contain'
/>
<View>
<ExpoImage
style={[styles.inlineImage, { borderColor: colors.strokeLight, aspectRatio, maxHeight: MAX_HEIGHT }]}
source={{ uri: encodeURI(src.value) }}
contentFit='contain'
onLoad={onLoad}
/>
</View>
);
};

Expand Down
3 changes: 1 addition & 2 deletions app/containers/markdown/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ export default StyleSheet.create({
justifyContent: 'flex-start'
},
inlineImage: {
width: 300,
height: 300
width: '100%'
},
codeInline: {
fontSize: 16,
Expand Down