Skip to content
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

Fix swipeable swiping to left when no renderRightActions is set #3145

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Changes from 1 commit
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
19 changes: 14 additions & 5 deletions src/components/ReanimatedSwipeable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
const rowWidth = useSharedValue<number>(0);
const leftWidth = useSharedValue<number>(0);
const rightWidth = useSharedValue<number>(0);
const rightOffset = useSharedValue<number>(0);
const rightOffset = useSharedValue<number | null>(null);

const leftActionTranslate = useSharedValue<number>(0);
const rightActionTranslate = useSharedValue<number>(0);
Expand Down Expand Up @@ -266,19 +266,29 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
const overshootLeftProp = overshootLeft;
const overshootRightProp = overshootRight;

const updateRightElementWidth = () => {
'worklet';
if (rightOffset.value === null) {
rightOffset.value = rowWidth.value;
}
rightWidth.value = Math.max(0, rowWidth.value - rightOffset.value);
};

const calculateCurrentOffset = useCallback(() => {
'worklet';
updateRightElementWidth();
if (rowState.value === 1) {
return leftWidth.value;
} else if (rowState.value === -1) {
return -rowWidth.value - rightOffset.value;
return -rowWidth.value - rightOffset.value!;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd think about changing it to use ternary operator:

      return rowState.value === 1
        ? leftWidth.value
        : rowState.value === -1
        ? -rowWidth.value - rightOffset.value!
        : 0;

But it has 3 options, so it may look a bit messy. It's up to you to decide 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks ok, done in 5e8ded8

}
return 0;
}, [leftWidth, rightOffset, rowState, rowWidth]);

const updateAnimatedEvent = () => {
'worklet';
rightWidth.value = Math.max(0, rowWidth.value - rightOffset.value);

updateRightElementWidth();

const overshootLeft = overshootLeftProp ?? leftWidth.value > 0;
const overshootRight = overshootRightProp ?? rightWidth.value > 0;
Expand Down Expand Up @@ -446,7 +456,6 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
},
openRight() {
'worklet';
rightWidth.value = rowWidth.value - rightOffset.value;
m-bert marked this conversation as resolved.
Show resolved Hide resolved
animateRow(calculateCurrentOffset(), -rightWidth.value);
},
reset() {
Expand Down Expand Up @@ -520,7 +529,7 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
const { velocityX } = event;
userDrag.value = event.translationX;

rightWidth.value = rowWidth.value - rightOffset.value;
updateRightElementWidth();

const leftThreshold = leftThresholdProp ?? leftWidth.value / 2;
const rightThreshold = rightThresholdProp ?? rightWidth.value / 2;
Expand Down
Loading