Skip to content

Use unpack style for SortableListItem component #2546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/components/sortableList/SortableListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {useDidUpdate} from 'hooks';
import SortableListContext from './SortableListContext';
import usePresenter from './usePresenter';
import {HapticService, HapticType} from '../../services';
import {StyleUtils} from 'utils';
export interface InternalSortableListItemProps {
index: number;
}
Expand Down Expand Up @@ -49,6 +50,17 @@ const SortableListItem = (props: Props) => {
const translateY = useSharedValue<number>(0);

const isDragging = useSharedValue(false);

const draggedItemShadow = useSharedValue(StyleUtils.unpackStyle({
...Shadows.sh30.bottom,
...Shadows.sh30.top
}));

const defaultItemShadow = useSharedValue(StyleUtils.unpackStyle({
shadowColor: Colors.transparent,
elevation: 0
}));

const tempTranslateY = useSharedValue<number>(0);
const tempItemsOrder = useSharedValue<string[]>(itemsOrder.value);

Expand Down Expand Up @@ -143,14 +155,8 @@ const SortableListItem = (props: Props) => {
const zIndex = isDragging.value ? 100 : withTiming(0, animationConfig);
const opacity = isDragging.value ? 0.95 : 1;
const shadow = isDragging.value
? {
...Shadows.sh30.bottom,
...Shadows.sh30.top
}
: {
shadowColor: Colors.transparent,
elevation: 0
};
? draggedItemShadow.value
: defaultItemShadow.value;

return {
backgroundColor: Colors.$backgroundDefault, // required for elevation to work in Android
Expand Down
7 changes: 0 additions & 7 deletions src/incubator/Slider/SliderPresenter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {StyleProp, ViewStyle, StyleSheet} from 'react-native';
import {SharedValue, interpolate} from 'react-native-reanimated';
import {SliderProps} from '../../components/slider';

Expand Down Expand Up @@ -56,12 +55,6 @@ export function validateValues(props: SliderProps) {
}
}

export function unpackStyle(style?: StyleProp<ViewStyle>) {
if (style) {
return JSON.parse(JSON.stringify(StyleSheet.flatten(style)));
}
}

export function getStepInterpolated(trackWidth: number, minimumValue: number, maximumValue: number, stepXValue: SharedValue<number>) {
'worklet';
const outputRange = [0, trackWidth];
Expand Down
8 changes: 4 additions & 4 deletions src/incubator/Slider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {useSharedValue, useAnimatedStyle, runOnJS, useAnimatedReaction, withTimi
import {forwardRef, ForwardRefInjectedProps, Constants} from '../../commons/new';
import {extractAccessibilityProps} from '../../commons/modifiers';
import {Colors, Spacings} from '../../style';
import {StyleUtils} from 'utils';
import View from '../../components/view';
import {SliderProps} from '../../components/slider';
import {
validateValues,
getOffsetForValue,
getValueForOffset,
unpackStyle,
getStepInterpolated
} from './SliderPresenter';
import Thumb from './Thumb';
Expand Down Expand Up @@ -99,12 +99,12 @@ const Slider = React.memo((props: Props) => {
const defaultThumbStyle: StyleProp<ViewStyle> = useMemo(() => [
styles.thumb, {backgroundColor: disabled ? Colors.$backgroundDisabled : thumbTintColor}
], [disabled, thumbTintColor]);
const _thumbStyle = useSharedValue(unpackStyle(thumbStyle || defaultThumbStyle));
const _activeThumbStyle = useSharedValue(unpackStyle(activeThumbStyle));
const _thumbStyle = useSharedValue(StyleUtils.unpackStyle(thumbStyle || defaultThumbStyle, {flatten: true}));
const _activeThumbStyle = useSharedValue(StyleUtils.unpackStyle(activeThumbStyle, {flatten: true}));

useEffect(() => {
if (!thumbStyle) {
_thumbStyle.value = unpackStyle(defaultThumbStyle);
_thumbStyle.value = StyleUtils.unpackStyle(defaultThumbStyle, {flatten: true});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [defaultThumbStyle, thumbStyle]);
Expand Down
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as TextUtils from './textUtils';
import * as StyleUtils from './styleUtils';

export {TextUtils};
export {TextUtils, StyleUtils};
11 changes: 11 additions & 0 deletions src/utils/styleUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {StyleProp, ViewStyle, StyleSheet} from 'react-native';

interface UnpackStyleOptions {
flatten?: boolean;
}

export function unpackStyle(style?: StyleProp<ViewStyle>, options: UnpackStyleOptions = {}) {
if (style) {
return JSON.parse(JSON.stringify(options.flatten ? StyleSheet.flatten(style) : style));
}
}