Skip to content

Commit 5e5e1ca

Browse files
authored
fix: Invalid call to gesture manager when item is no longer available (#385)
## Description This PR removes invalid call to the gesture manager when the item gets removed before the gesture starts being handled.
1 parent ad03d2b commit 5e5e1ca

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

packages/react-native-sortables/src/components/shared/DraggableView/TeleportedItemCell.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default function TeleportedItemCell({
3131
onMeasure,
3232
teleportedItemId
3333
}: TeleportedItemCellProps) {
34-
const { notifyRendered } = usePortalContext()!;
34+
const { notifyRendered } = usePortalContext() ?? {};
3535

3636
const teleportedItemStyles = useTeleportedItemStyles(
3737
itemKey,
@@ -44,6 +44,10 @@ export default function TeleportedItemCell({
4444
activationAnimationProgress
4545
);
4646

47+
if (!notifyRendered) {
48+
return null;
49+
}
50+
4751
return (
4852
<ItemCell
4953
cellStyle={[baseCellStyle, teleportedItemStyles]}

packages/react-native-sortables/src/providers/shared/DragProvider.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515

1616
import { useHaptics, useStableCallbackValue } from '../../hooks';
1717
import type {
18+
Dimensions,
1819
DragContextType,
1920
OverDrag,
2021
SortableCallbacks,
@@ -233,32 +234,30 @@ const { DragProvider, useDragContext } = createProvider('Drag')<
233234
(
234235
touch: TouchData,
235236
key: string,
236-
activationAnimationProgress: SharedValue<number>,
237-
fail: () => void
237+
position: Vector,
238+
dimensions: Dimensions,
239+
activationAnimationProgress: SharedValue<number>
238240
) => {
239241
'worklet';
240-
const itemPosition = itemPositions.value[key];
241-
const dimensions = itemDimensions.value[key];
242242
const containerMeasurements = measure(containerRef);
243243

244-
if (!itemPosition || !dimensions || !containerMeasurements) {
245-
fail();
244+
if (!position || !dimensions || !containerMeasurements) {
246245
return;
247246
}
248247

249248
activeAnimationProgress.value = 0;
250249
activeItemDropped.value = false;
251250
prevActiveItemKey.value = activeItemKey.value;
252251
activeItemKey.value = key;
253-
activeItemPosition.value = itemPosition;
254-
activeItemDimensions.value = itemDimensions.value[key] ?? null;
252+
activeItemPosition.value = position;
253+
activeItemDimensions.value = dimensions;
255254
dragStartIndex.value = keyToIndex.value[key] ?? -1;
256255
activationState.value = DragActivationState.ACTIVE;
257256

258257
updateLayer?.(LayerState.FOCUSED);
259258
updateStartScrollOffset?.();
260259

261-
let touchedItemPosition = itemPosition;
260+
let touchedItemPosition = position;
262261

263262
// We need to update the custom handle measurements if the custom handle
264263
// is used (touch position is relative to the handle in this case)
@@ -279,8 +278,8 @@ const { DragProvider, useDragContext } = createProvider('Drag')<
279278
touchPosition.value = { x: touchX, y: touchY };
280279
dragStartTouchPosition.value = touchPosition.value;
281280
dragStartItemTouchOffset.value = {
282-
x: touchX - itemPosition.x,
283-
y: touchY - itemPosition.y
281+
x: touchX - position.x,
282+
y: touchY - position.y
284283
};
285284

286285
const hasInactiveAnimation =
@@ -319,8 +318,6 @@ const { DragProvider, useDragContext } = createProvider('Drag')<
319318
inactiveItemOpacity,
320319
inactiveItemScale,
321320
indexToKey,
322-
itemDimensions,
323-
itemPositions,
324321
keyToIndex,
325322
prevActiveItemKey,
326323
stableOnDragStart,
@@ -370,7 +367,21 @@ const { DragProvider, useDragContext } = createProvider('Drag')<
370367
if (absoluteLayoutState.value !== AbsoluteLayoutState.COMPLETE) {
371368
return;
372369
}
373-
handleDragStart(touch, key, activationAnimationProgress, fail);
370+
371+
const position = itemPositions.value[key];
372+
const dimensions = itemDimensions.value[key];
373+
374+
if (!position || !dimensions) {
375+
return;
376+
}
377+
378+
handleDragStart(
379+
touch,
380+
key,
381+
position,
382+
dimensions,
383+
activationAnimationProgress
384+
);
374385
activate();
375386
}, dragActivationDelay.value);
376387
},
@@ -382,6 +393,8 @@ const { DragProvider, useDragContext } = createProvider('Drag')<
382393
currentTouch,
383394
dragActivationDelay,
384395
handleDragStart,
396+
itemDimensions,
397+
itemPositions,
385398
measureContainer,
386399
sortEnabled,
387400
touchStartTouch

0 commit comments

Comments
 (0)