Skip to content

Commit 95ff517

Browse files
committed
fix: Respect Sortable.Flex alignItems before items are measured
Sortable.Flex put `alignSelf: 'flex-start'` on every item to stop them stretching (stretched items would be measured at the wrong size, which breaks the absolute layout). But `alignSelf` overrides the container's `alignItems`, so a user-provided `alignItems` (e.g. 'center') was ignored until the component switched to its absolute layout after measuring, causing a visible jump with differently-sized items. Prevent stretching on the container instead: fall back to `alignItems: 'flex-start'` only when no specific `alignItems` is set (or it's 'stretch'), and drop the per-item `alignSelf` override. The provided `alignItems` now applies from the first render.
1 parent 92fe9da commit 95ff517

1 file changed

Lines changed: 5 additions & 10 deletions

File tree

packages/react-native-sortables/src/components/SortableFlex.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { memo, useMemo } from 'react';
22
import type { ViewStyle } from 'react-native';
3-
import { StyleSheet } from 'react-native';
43
import { runOnUI, useAnimatedStyle } from 'react-native-reanimated';
54

65
import { DEFAULT_SORTABLE_FLEX_PROPS } from '../constants';
@@ -224,7 +223,11 @@ function SortableFlexComponent({
224223
}
225224
: {
226225
alignContent,
227-
alignItems,
226+
// Avoid the default `stretch` so items keep their measured size
227+
alignItems:
228+
alignItems === undefined || alignItems === 'stretch'
229+
? 'flex-start'
230+
: alignItems,
228231
flexDirection,
229232
flexWrap,
230233
justifyContent
@@ -235,17 +238,9 @@ function SortableFlexComponent({
235238
<SortableContainer
236239
{...rest}
237240
containerStyle={[baseContainerStyle, animatedContainerStyle]}
238-
itemStyle={styles.styleOverride}
239241
onLayout={runOnUI(handleContainerMeasurement)}
240242
/>
241243
);
242244
}
243245

244-
const styles = StyleSheet.create({
245-
styleOverride: {
246-
// This is needed to prevent items from stretching (which is default behavior)
247-
alignSelf: 'flex-start'
248-
}
249-
});
250-
251246
export default SortableFlex;

0 commit comments

Comments
 (0)