Skip to content

Commit

Permalink
Fix swipeable swiping to left when no renderRightActions is set (#3145
Browse files Browse the repository at this point in the history
)

## Description

Fixed `Swipeable` being able to be opened to the left when no right
element is present.
This was caused by incorrectly set `rightOffset`, equal to `0` when no
elements were present, even though it should've been set to `rowWidth`

## Test plan

- Open any swipeable example in the example app
- Remove the `renderRightActions` prop
- See how before this fix, `Swipeable` could've still been opened to the
left, and now it can't be.
  • Loading branch information
latekvo authored Oct 11, 2024
1 parent 3c3d87b commit 4b8bcf5
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 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,28 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
const overshootLeftProp = overshootLeft;
const overshootRightProp = overshootRight;

const calculateCurrentOffset = useCallback(() => {
const updateRightElementWidth = () => {
'worklet';
if (rowState.value === 1) {
return leftWidth.value;
} else if (rowState.value === -1) {
return -rowWidth.value - rightOffset.value;
if (rightOffset.value === null) {
rightOffset.value = rowWidth.value;
}
return 0;
rightWidth.value = Math.max(0, rowWidth.value - rightOffset.value);
};

const calculateCurrentOffset = useCallback(() => {
'worklet';
updateRightElementWidth();
return rowState.value === 1
? leftWidth.value
: rowState.value === -1
? -rowWidth.value - rightOffset.value!
: 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 +455,6 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
},
openRight() {
'worklet';
rightWidth.value = rowWidth.value - rightOffset.value;
animateRow(calculateCurrentOffset(), -rightWidth.value);
},
reset() {
Expand Down Expand Up @@ -520,7 +528,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

0 comments on commit 4b8bcf5

Please sign in to comment.