Skip to content

Commit e018b30

Browse files
committed
feat: use aspectRatio style to infer one dimension of images sizes
1 parent 6ca54e9 commit e018b30

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

packages/render-html/src/elements/IMGElementContentLoading.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ import { IMGElementStateLoading } from './img-types';
88
export default function IMGElementContentLoading({
99
dimensions,
1010
alt,
11-
testID,
1211
children
13-
}: PropsWithChildren<
14-
IMGElementStateLoading & { testID?: string }
15-
>): ReactElement {
12+
}: PropsWithChildren<IMGElementStateLoading>): ReactElement {
1613
return (
1714
<View
1815
style={dimensions}
1916
accessibilityRole="image"
2017
accessibilityLabel={alt}
21-
testID={testID}>
18+
testID="image-loading">
2219
{children}
2320
</View>
2421
);

packages/render-html/src/elements/__tests__/IMGElement.test.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,44 @@ describe('IMGElement', () => {
104104
expect(image).toBeTruthy();
105105
expect(StyleSheet.flatten(image.props.style)).toMatchObject(style);
106106
});
107+
it('should combine width with aspectRatio', async () => {
108+
const source = { uri: 'http://via.placeholder.com/640' };
109+
const dimensions = {
110+
width: 320
111+
};
112+
const { findByTestId } = render(
113+
<HTMLImgElement
114+
{...dimensions}
115+
style={{ aspectRatio: 2 }}
116+
source={source}
117+
/>
118+
);
119+
const image = await findByTestId('image-success');
120+
expect(image).toBeTruthy();
121+
expect(StyleSheet.flatten(image.props.style)).toMatchObject({
122+
width: 320,
123+
height: 160
124+
});
125+
});
126+
it('should combine height with aspectRatio', async () => {
127+
const source = { uri: 'http://via.placeholder.com/640' };
128+
const dimensions = {
129+
height: 160
130+
};
131+
const { findByTestId } = render(
132+
<HTMLImgElement
133+
{...dimensions}
134+
style={{ aspectRatio: 2 }}
135+
source={source}
136+
/>
137+
);
138+
const image = await findByTestId('image-success');
139+
expect(image).toBeTruthy();
140+
expect(StyleSheet.flatten(image.props.style)).toMatchObject({
141+
width: 320,
142+
height: 160
143+
});
144+
});
107145
it('should scale down required dimensions to contentWidth prop when appropriate', async () => {
108146
const source = { uri: 'http://via.placeholder.com/640x360' };
109147
const style = {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default function getDimensionsWithAspectRatio(
2+
width: number | null,
3+
height: number | null,
4+
aspectRatio: number | undefined
5+
) {
6+
return {
7+
width: width ?? (aspectRatio && height ? height * aspectRatio : null),
8+
height: height ?? (aspectRatio && width ? width / aspectRatio : null)
9+
};
10+
}

packages/render-html/src/elements/useImageNaturalDimensions.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useState, useMemo, useEffect } from 'react';
22
import { StyleSheet } from 'react-native';
33
import { ImageDimensions } from '../shared-types';
4+
import getDimensionsWithAspectRatio from './getDimensionsWithAspectRatio';
45
import { UseIMGElementStateProps } from './img-types';
56

67
interface IncompleteImageDimensions {
@@ -63,10 +64,11 @@ function deriveSpecifiedDimensionsFromProps({
6364
const heightProp = normalizeSize(height, normalizeOptionsHeight);
6465
const styleWidth = normalizeSize(flatStyle.width, normalizeOptionsWidth);
6566
const styleHeight = normalizeSize(flatStyle.height, normalizeOptionsHeight);
66-
return {
67-
width: styleWidth ?? widthProp,
68-
height: styleHeight ?? heightProp
69-
};
67+
return getDimensionsWithAspectRatio(
68+
styleWidth ?? widthProp,
69+
styleHeight ?? heightProp,
70+
flatStyle.aspectRatio
71+
);
7072
}
7173

7274
export default function useImageNaturalDimensions<

0 commit comments

Comments
 (0)