Skip to content

Commit a74cb4b

Browse files
authored
fix: Flex layout with custom alignmnets on web (#401)
## Description It appears that reanimated's animated style might not override js style properties on web. To handle this case properly, we filter out flex props from the style that is passed to the sortable container and use them only in the animated style.
1 parent 891c0b3 commit a74cb4b

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { type ReactElement } from 'react';
22
import { type StyleProp, StyleSheet, type ViewStyle } from 'react-native';
33
import { useAnimatedStyle, useDerivedValue } from 'react-native-reanimated';
44

5-
import { DEFAULT_SORTABLE_FLEX_PROPS } from '../constants';
5+
import {
6+
DEFAULT_SORTABLE_FLEX_PROPS,
7+
EMPTY_OBJECT,
8+
IS_WEB
9+
} from '../constants';
610
import { useDragEndHandler } from '../hooks';
711
import {
812
FLEX_STRATEGIES,
@@ -124,6 +128,28 @@ function SortableFlexInner({
124128
}: SortableFlexInnerProps) {
125129
const { usesAbsoluteLayout } = useCommonValuesContext();
126130

131+
let relativeLayoutStyle: ViewStyle = EMPTY_OBJECT;
132+
let baseContainerStyle = style;
133+
// On web reanimated animated style might not override the plan js style
134+
// so we have to filter out flex properties
135+
if (IS_WEB) {
136+
const {
137+
alignItems,
138+
flexDirection,
139+
flexWrap,
140+
justifyContent,
141+
...restStyle
142+
} = StyleSheet.flatten(style);
143+
144+
baseContainerStyle = restStyle;
145+
relativeLayoutStyle = {
146+
alignItems,
147+
flexDirection,
148+
flexWrap,
149+
justifyContent
150+
};
151+
}
152+
127153
const animatedContainerStyle = useAnimatedStyle(() =>
128154
usesAbsoluteLayout.value
129155
? {
@@ -138,13 +164,13 @@ function SortableFlexInner({
138164
paddingRight: 0,
139165
paddingTop: 0
140166
}
141-
: {}
167+
: relativeLayoutStyle
142168
);
143169

144170
return (
145171
<SortableContainer
146172
{...containerProps}
147-
style={[style, animatedContainerStyle]}>
173+
style={[baseContainerStyle, animatedContainerStyle]}>
148174
{childrenArray.map(([key, child]) => (
149175
<DraggableView
150176
entering={itemEntering ?? undefined}

0 commit comments

Comments
 (0)