Skip to content

Commit 75581d0

Browse files
fix: image layout issue
1 parent 59764ca commit 75581d0

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

packages/pluggableWidgets/image-native/src/components/BackgroundImage.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReactNode, FunctionComponent, useState, useCallback } from "react";
1+
import { ReactNode, FunctionComponent, useState, useCallback, useRef, useEffect } from "react";
22
import { ImageStyle, LayoutChangeEvent, View } from "react-native";
33
import { extractStyles } from "@mendix/pluggable-widgets-tools";
44
import { ResizeModeEnum } from "../../typings/ImageProps";
@@ -24,12 +24,29 @@ export const BackgroundImage: FunctionComponent<BackgroundImageProps> = props =>
2424
const [dimensions, setDimensions] = useState<DimensionsType>();
2525
const { source, initialDimensions, children, opacity, styles, name } = props;
2626
const [svgProps] = extractStyles(styles.image as ImageStyle, ["width", "height"]);
27+
const dimensionsResolved = !!((dimensions?.width || svgProps?.width) && (dimensions?.height || svgProps?.height));
28+
const containerLayoutRef = useRef<{ width: number; height: number } | null>(null);
29+
2730
const onLayoutSetDimensionsCallback = useCallback(
28-
({ nativeEvent: { layout } }: LayoutChangeEvent) =>
29-
onLayoutSetDimensions(layout.width, layout.height, setDimensions, initialDimensions),
31+
({ nativeEvent: { layout } }: LayoutChangeEvent) => {
32+
containerLayoutRef.current = { width: layout.width, height: layout.height };
33+
onLayoutSetDimensions(layout.width, layout.height, setDimensions, initialDimensions);
34+
},
3035
[initialDimensions]
3136
);
3237

38+
// Re-compute dimensions when initialDimensions arrives after onLayout already fired (static images only)
39+
useEffect(() => {
40+
if (!dimensionsResolved && initialDimensions && containerLayoutRef.current) {
41+
onLayoutSetDimensions(
42+
containerLayoutRef.current.width,
43+
containerLayoutRef.current.height,
44+
setDimensions,
45+
initialDimensions
46+
);
47+
}
48+
}, [initialDimensions, dimensionsResolved]);
49+
3350
return (
3451
<View
3552
testID={`${name}$ImageBackgroundView`}
@@ -43,7 +60,7 @@ export const BackgroundImage: FunctionComponent<BackgroundImageProps> = props =>
4360
width: svgProps?.width ?? dimensions?.width ?? "100%",
4461
height: svgProps?.height ?? dimensions?.height ?? "100%"
4562
},
46-
styles.container
63+
dimensionsResolved ? styles.container : { opacity: 0 }
4764
]}
4865
>
4966
<View

packages/pluggableWidgets/image-native/src/components/__tests__/__snapshots__/Image.spec.tsx.snap

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ exports[`Widget Dynamic Image SVG renders the structure as a background image 1`
123123
"width": "100%",
124124
},
125125
{
126-
"alignItems": "center",
127-
"justifyContent": "center",
126+
"opacity": 0,
128127
},
129128
]
130129
}
@@ -158,8 +157,7 @@ exports[`Widget Dynamic Image SVG renders the structure as a background image wi
158157
"width": "100%",
159158
},
160159
{
161-
"alignItems": "center",
162-
"justifyContent": "center",
160+
"opacity": 0,
163161
},
164162
]
165163
}
@@ -193,8 +191,7 @@ exports[`Widget Dynamic Image SVG renders the structure as a background image wi
193191
"width": "100%",
194192
},
195193
{
196-
"alignItems": "center",
197-
"justifyContent": "center",
194+
"opacity": 0,
198195
},
199196
]
200197
}

0 commit comments

Comments
 (0)