Skip to content

Commit 4b85164

Browse files
authored
Use unpack style for SortableListItem component (#2546)
1 parent 5362c5f commit 4b85164

File tree

5 files changed

+31
-20
lines changed

5 files changed

+31
-20
lines changed

src/components/sortableList/SortableListItem.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {useDidUpdate} from 'hooks';
1717
import SortableListContext from './SortableListContext';
1818
import usePresenter from './usePresenter';
1919
import {HapticService, HapticType} from '../../services';
20+
import {StyleUtils} from 'utils';
2021
export interface InternalSortableListItemProps {
2122
index: number;
2223
}
@@ -49,6 +50,17 @@ const SortableListItem = (props: Props) => {
4950
const translateY = useSharedValue<number>(0);
5051

5152
const isDragging = useSharedValue(false);
53+
54+
const draggedItemShadow = useSharedValue(StyleUtils.unpackStyle({
55+
...Shadows.sh30.bottom,
56+
...Shadows.sh30.top
57+
}));
58+
59+
const defaultItemShadow = useSharedValue(StyleUtils.unpackStyle({
60+
shadowColor: Colors.transparent,
61+
elevation: 0
62+
}));
63+
5264
const tempTranslateY = useSharedValue<number>(0);
5365
const tempItemsOrder = useSharedValue<string[]>(itemsOrder.value);
5466

@@ -143,14 +155,8 @@ const SortableListItem = (props: Props) => {
143155
const zIndex = isDragging.value ? 100 : withTiming(0, animationConfig);
144156
const opacity = isDragging.value ? 0.95 : 1;
145157
const shadow = isDragging.value
146-
? {
147-
...Shadows.sh30.bottom,
148-
...Shadows.sh30.top
149-
}
150-
: {
151-
shadowColor: Colors.transparent,
152-
elevation: 0
153-
};
158+
? draggedItemShadow.value
159+
: defaultItemShadow.value;
154160

155161
return {
156162
backgroundColor: Colors.$backgroundDefault, // required for elevation to work in Android

src/incubator/Slider/SliderPresenter.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {StyleProp, ViewStyle, StyleSheet} from 'react-native';
21
import {SharedValue, interpolate} from 'react-native-reanimated';
32
import {SliderProps} from '../../components/slider';
43

@@ -56,12 +55,6 @@ export function validateValues(props: SliderProps) {
5655
}
5756
}
5857

59-
export function unpackStyle(style?: StyleProp<ViewStyle>) {
60-
if (style) {
61-
return JSON.parse(JSON.stringify(StyleSheet.flatten(style)));
62-
}
63-
}
64-
6558
export function getStepInterpolated(trackWidth: number, minimumValue: number, maximumValue: number, stepXValue: SharedValue<number>) {
6659
'worklet';
6760
const outputRange = [0, trackWidth];

src/incubator/Slider/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import {useSharedValue, useAnimatedStyle, runOnJS, useAnimatedReaction, withTimi
55
import {forwardRef, ForwardRefInjectedProps, Constants} from '../../commons/new';
66
import {extractAccessibilityProps} from '../../commons/modifiers';
77
import {Colors, Spacings} from '../../style';
8+
import {StyleUtils} from 'utils';
89
import View from '../../components/view';
910
import {SliderProps} from '../../components/slider';
1011
import {
1112
validateValues,
1213
getOffsetForValue,
1314
getValueForOffset,
14-
unpackStyle,
1515
getStepInterpolated
1616
} from './SliderPresenter';
1717
import Thumb from './Thumb';
@@ -99,12 +99,12 @@ const Slider = React.memo((props: Props) => {
9999
const defaultThumbStyle: StyleProp<ViewStyle> = useMemo(() => [
100100
styles.thumb, {backgroundColor: disabled ? Colors.$backgroundDisabled : thumbTintColor}
101101
], [disabled, thumbTintColor]);
102-
const _thumbStyle = useSharedValue(unpackStyle(thumbStyle || defaultThumbStyle));
103-
const _activeThumbStyle = useSharedValue(unpackStyle(activeThumbStyle));
102+
const _thumbStyle = useSharedValue(StyleUtils.unpackStyle(thumbStyle || defaultThumbStyle, {flatten: true}));
103+
const _activeThumbStyle = useSharedValue(StyleUtils.unpackStyle(activeThumbStyle, {flatten: true}));
104104

105105
useEffect(() => {
106106
if (!thumbStyle) {
107-
_thumbStyle.value = unpackStyle(defaultThumbStyle);
107+
_thumbStyle.value = StyleUtils.unpackStyle(defaultThumbStyle, {flatten: true});
108108
}
109109
// eslint-disable-next-line react-hooks/exhaustive-deps
110110
}, [defaultThumbStyle, thumbStyle]);

src/utils/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import * as TextUtils from './textUtils';
2+
import * as StyleUtils from './styleUtils';
23

3-
export {TextUtils};
4+
export {TextUtils, StyleUtils};

src/utils/styleUtils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {StyleProp, ViewStyle, StyleSheet} from 'react-native';
2+
3+
interface UnpackStyleOptions {
4+
flatten?: boolean;
5+
}
6+
7+
export function unpackStyle(style?: StyleProp<ViewStyle>, options: UnpackStyleOptions = {}) {
8+
if (style) {
9+
return JSON.parse(JSON.stringify(options.flatten ? StyleSheet.flatten(style) : style));
10+
}
11+
}

0 commit comments

Comments
 (0)