diff --git a/node_modules/react-native-draggable-flatlist/lib/typescript/components/DraggableFlatList.d.ts b/node_modules/react-native-draggable-flatlist/lib/typescript/components/DraggableFlatList.d.ts index d3a4f1f..66e5124 100644 --- a/node_modules/react-native-draggable-flatlist/lib/typescript/components/DraggableFlatList.d.ts +++ b/node_modules/react-native-draggable-flatlist/lib/typescript/components/DraggableFlatList.d.ts @@ -16,12 +16,15 @@ declare const _default: (props: Omit, "keyExtractor" | "data keyExtractor: (item: T, index: number) => string; onDragBegin?: ((index: number) => void) | undefined; onDragEnd?: ((params: import("../types").DragEndParams) => void) | undefined; + onDragTerminate?: (() => void) | undefined; onPlaceholderIndexChange?: ((placeholderIndex: number) => void) | undefined; onRelease?: ((index: number) => void) | undefined; onScrollOffsetChange?: ((scrollOffset: number) => void) | undefined; renderItem: import("../types").RenderItem; renderPlaceholder?: import("../types").RenderPlaceholder | undefined; simultaneousHandlers?: React.Ref | React.Ref[] | undefined; + dragGestureHostPresented?: boolean | undefined; + dragGestureRef?: React.MutableRefObject | undefined; outerScrollOffset?: Animated.SharedValue | undefined; onAnimValInit?: ((animVals: { activeCellOffset: import("react-native-reanimated").SharedValue; diff --git a/node_modules/react-native-draggable-flatlist/lib/typescript/components/NestableDraggableFlatList.d.ts b/node_modules/react-native-draggable-flatlist/lib/typescript/components/NestableDraggableFlatList.d.ts index d3c6cbb..a586a09 100644 --- a/node_modules/react-native-draggable-flatlist/lib/typescript/components/NestableDraggableFlatList.d.ts +++ b/node_modules/react-native-draggable-flatlist/lib/typescript/components/NestableDraggableFlatList.d.ts @@ -15,12 +15,15 @@ export declare const NestableDraggableFlatList: (props: Omit string; onDragBegin?: ((index: number) => void) | undefined; onDragEnd?: ((params: import("../types").DragEndParams) => void) | undefined; + onDragTerminate?: (() => void) | undefined; onPlaceholderIndexChange?: ((placeholderIndex: number) => void) | undefined; onRelease?: ((index: number) => void) | undefined; onScrollOffsetChange?: ((scrollOffset: number) => void) | undefined; renderItem: import("../types").RenderItem; renderPlaceholder?: import("../types").RenderPlaceholder | undefined; simultaneousHandlers?: React.Ref | React.Ref[] | undefined; + dragGestureHostPresented?: boolean | undefined; + dragGestureRef?: React.MutableRefObject | undefined; outerScrollOffset?: Animated.SharedValue | undefined; onAnimValInit?: ((animVals: { activeCellOffset: import("react-native-reanimated").SharedValue; diff --git a/node_modules/react-native-draggable-flatlist/lib/typescript/types.d.ts b/node_modules/react-native-draggable-flatlist/lib/typescript/types.d.ts index 2abb4a2..c60dedf 100644 --- a/node_modules/react-native-draggable-flatlist/lib/typescript/types.d.ts +++ b/node_modules/react-native-draggable-flatlist/lib/typescript/types.d.ts @@ -23,12 +23,21 @@ export declare type DraggableFlatListProps = Modify, { keyExtractor: (item: T, index: number) => string; onDragBegin?: (index: number) => void; onDragEnd?: (params: DragEndParams) => void; + /** @internal Used by the nestable wrapper to release its outer-scroll lock on cancellation. */ + onDragTerminate?: () => void; onPlaceholderIndexChange?: (placeholderIndex: number) => void; onRelease?: (index: number) => void; onScrollOffsetChange?: (scrollOffset: number) => void; renderItem: RenderItem; renderPlaceholder?: RenderPlaceholder; simultaneousHandlers?: React.Ref | React.Ref[]; + /** @internal Forces handler reattachment when a retained native host is presented. */ + dragGestureHostPresented?: boolean; + /** + * Ref for the drag pan, so an enclosing gesture can wait for it to fail. The pan then arbitrates + * manually, as with `simultaneousHandlers`: it fails on a move unless a row is armed. + */ + dragGestureRef?: React.MutableRefObject; outerScrollOffset?: Animated.SharedValue; onAnimValInit?: (animVals: ReturnType) => void; itemEnteringAnimation?: AnimateProps["entering"]; diff --git a/node_modules/react-native-draggable-flatlist/src/components/CellRendererComponent.tsx b/node_modules/react-native-draggable-flatlist/src/components/CellRendererComponent.tsx index e4e861a..f485139 100644 --- a/node_modules/react-native-draggable-flatlist/src/components/CellRendererComponent.tsx +++ b/node_modules/react-native-draggable-flatlist/src/components/CellRendererComponent.tsx @@ -8,6 +8,7 @@ import { } from "react-native"; import Animated, { runOnUI, + SharedValue, useAnimatedStyle, useSharedValue, } from "react-native-reanimated"; @@ -40,12 +41,12 @@ function CellRendererComponent(props: Props) { keyExtractor, horizontal, layoutAnimationDisabled, + transformHostKeys, } = useDraggableFlatListContext(); const key = keyExtractor(item, index); const offset = useSharedValue(-1); const size = useSharedValue(-1); - const heldTanslate = useSharedValue(0); const translate = useCellTranslate({ cellOffset: offset, @@ -55,20 +56,6 @@ function CellRendererComponent(props: Props) { const isActive = activeKey === key; - const animStyle = useAnimatedStyle(() => { - // When activeKey becomes null at the end of a drag and the list reorders, - // the animated style may apply before the next paint, causing a flicker. - // Solution is to hold over the last animated value until the next onLayout. - // (Not required in web) - if (translate.value && !isWeb) { - heldTanslate.value = translate.value; - } - const t = activeKey ? translate.value : heldTanslate.value; - return { - transform: [horizontalAnim.value ? { translateX: t } : { translateY: t }], - }; - }, [translate, activeKey]); - const updateCellMeasurements = useStableCallback(() => { const onSuccess: MeasureLayoutOnSuccessCallback = (x, y, w, h) => { if (isWeb && horizontal) x += scrollOffset.value; @@ -99,7 +86,6 @@ function CellRendererComponent(props: Props) { }); const onCellLayout = useStableCallback((e?: LayoutChangeEvent) => { - heldTanslate.value = 0; updateCellMeasurements(); if (onLayout && e) onLayout(e); }); @@ -162,14 +148,58 @@ function CellRendererComponent(props: Props) { ? itemLayoutAnimation : undefined } - style={[props.style, baseStyle, animStyle]} + style={[props.style, baseStyle]} pointerEvents={activeKey ? "none" : "auto"} > - {children} + + {children} + ); } +// Owns the cell's drag translate. The list remounts it, with the cell's whole subtree, when a +// drop moves the cell: the reorder commit then paints the new slot with no translate, because a +// new view has no last animated transform for Reanimated to re-apply. +function CellTransformHost({ + translate, + dragging, + horizontal, + children, +}: { + translate: SharedValue; + dragging: boolean; + horizontal: boolean; + children: React.ReactNode; +}) { + const { horizontalAnim } = useAnimatedValues(); + const animStyle = useAnimatedStyle(() => { + const t = dragging ? translate.value : 0; + return { + transform: [horizontalAnim.value ? { translateX: t } : { translateY: t }], + }; + }, [translate, dragging]); + + return ( + + {children} + + ); +} + +const styles = { + row: { flexDirection: "row" as const }, + column: { flexDirection: "column" as const }, +}; + export default typedMemo(CellRendererComponent); declare global { diff --git a/node_modules/react-native-draggable-flatlist/src/components/DraggableFlatList.tsx b/node_modules/react-native-draggable-flatlist/src/components/DraggableFlatList.tsx index 7c88afc..05618ec 100644 --- a/node_modules/react-native-draggable-flatlist/src/components/DraggableFlatList.tsx +++ b/node_modules/react-native-draggable-flatlist/src/components/DraggableFlatList.tsx @@ -16,9 +16,11 @@ import { FlatList, Gesture, GestureDetector, + State, } from "react-native-gesture-handler"; import Animated, { runOnJS, + runOnUI, useAnimatedReaction, useAnimatedScrollHandler, useSharedValue, @@ -92,6 +94,17 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { activeCellOffset.value = -1; setActiveKey(null); }); + const terminalHandled = useSharedValue(false); + const gestureSessionId = useSharedValue(0); + const gestureSessionActive = useSharedValue(false); + const currentGestureSessionRef = useRef<{ + id: number; + active: boolean; + } | null>(null); + const activeDragRef = useRef<{ + sessionId: number; + index: number; + } | null>(null); const { dragHitSlop = DEFAULT_PROPS.dragHitSlop, @@ -100,6 +113,12 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { } = props; let [activeKey, setActiveKey] = useState(null); + // Reanimated re-applies a view's last animated transform to every React commit, so a moved + // cell would paint once at its new slot plus its old drag translate. The drop gives each + // moved cell a new transform host instead: a fresh view has no animated props to re-apply. + const [transformHostKeys, setTransformHostKeys] = useState< + ReadonlyMap + >(() => new Map()); const [layoutAnimationDisabled, setLayoutAnimationDisabled] = useState( !propsRef.current.enableLayoutAnimationExperimental ); @@ -113,10 +132,26 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { const dataRef = useRef(props.data); const dataHasChanged = - dataRef.current.map(keyExtractor).join("") !== - props.data.map(keyExtractor).join(""); - dataRef.current = props.data; + dataRef.current.map(keyExtractor).join("") !== props.data.map(keyExtractor).join(""); + // Sheet portals can deliver reordered data after the drop callback commits. + // Keep the held transforms until that data arrives, then renew moved hosts + // in the same render as their new slots. if (dataHasChanged) { + const wasDragging = activeKey !== null; + const previousIndices = new Map( + dataRef.current.map((item, index) => [keyExtractor(item, index), index]), + ); + setTransformHostKeys((previous) => { + const next = new Map(); + props.data.forEach((item, index) => { + const key = keyExtractor(item, index); + const generation = previous.get(key) ?? 0; + next.set(key, generation + (wasDragging && previousIndices.get(key) !== index ? 1 : 0)); + }); + return next; + }); + dataRef.current = props.data; + setActiveKey(null); // When data changes make sure `activeKey` is nulled out in the same render pass activeKey = null; InteractionManager.runAfterInteractions(() => { @@ -144,19 +179,88 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { }); }, [props.data, keyExtractor, keyToIndexRef]); + const onDragCancel = useStableCallback((sessionId: number, index: number) => { + if (activeDragRef.current?.sessionId !== sessionId) return; + activeDragRef.current = null; + reset(); + props.onRelease?.(index); + props.onDragTerminate?.(); + }); + + useEffect(() => { + return () => { + const activeDrag = activeDragRef.current; + if (!activeDrag) return; + activeDragRef.current = null; + propsRef.current.onRelease?.(activeDrag.index); + propsRef.current.onDragTerminate?.(); + }; + }, [propsRef]); + + const onGestureBegin = useStableCallback((sessionId: number) => { + currentGestureSessionRef.current = { id: sessionId, active: true }; + }); + + const onGestureFinalize = useStableCallback( + (sessionId: number, committed: boolean) => { + if (currentGestureSessionRef.current?.id === sessionId) { + currentGestureSessionRef.current.active = false; + } + if (!committed && activeDragRef.current?.sessionId === sessionId) { + onDragCancel(sessionId, activeDragRef.current.index); + } + } + ); + + const onDragActivationRejected = useStableCallback((sessionId: number) => { + if (activeDragRef.current?.sessionId === sessionId) { + onDragCancel(sessionId, activeDragRef.current.index); + } + }); + + const activateDragOnUI = ( + sessionId: number, + index: number, + cellOffset: number, + cellSize: number, + hasCellMeasurements: boolean + ) => { + "worklet"; + if ( + gestureSessionId.value !== sessionId || + !gestureSessionActive.value || + terminalHandled.value + ) { + runOnJS(onDragActivationRejected)(sessionId); + return; + } + if (hasCellMeasurements) { + activeCellOffset.value = cellOffset; + activeCellSize.value = cellSize; + } + spacerIndexAnim.value = index; + activeIndexAnim.value = index; + }; + const drag = useStableCallback((activeKey: string) => { - if (disabled.value) return; + const gestureSession = currentGestureSessionRef.current; + if (disabled.value || !gestureSession?.active) return; const index = keyToIndexRef.current.get(activeKey); const cellData = cellDataRef.current.get(activeKey); - if (cellData) { - activeCellOffset.value = cellData.measurements.offset; - activeCellSize.value = cellData.measurements.size; - } const { onDragBegin } = propsRef.current; if (index !== undefined) { - spacerIndexAnim.value = index; - activeIndexAnim.value = index; + runOnUI(activateDragOnUI)( + gestureSession.id, + index, + cellData?.measurements.offset ?? -1, + cellData?.measurements.size ?? -1, + Boolean(cellData) + ); + activeDragRef.current = { + sessionId: gestureSession.id, + index, + }; setActiveKey(activeKey); onDragBegin?.(index); } @@ -218,8 +322,25 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { props.onRelease?.(index); }); + const finalizeCancelledDrag = () => { + "worklet"; + const activeIndex = activeIndexAnim.value; + if (terminalHandled.value || activeIndex < 0) return; + + terminalHandled.value = true; + isTouchActiveNative.value = false; + touchTranslate.value = 0; + activeIndexAnim.value = -1; + spacerIndexAnim.value = -1; + activeCellSize.value = -1; + activeCellOffset.value = -1; + disabled.value = false; + }; + const onDragEnd = useStableCallback( - ({ from, to }: { from: number; to: number }) => { + ({ from, to, sessionId }: { from: number; to: number; sessionId: number }) => { + if (activeDragRef.current?.sessionId !== sessionId) return; + activeDragRef.current = null; const { onDragEnd, data } = props; const newData = [...data]; @@ -230,34 +351,15 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { onDragEnd?.({ from, to, data: newData }); - setActiveKey(null); - } + // A drop in place doesn't change data, so no data-change reset will clear the armed index. + if (from === to) reset(); + }, ); const onPlaceholderIndexChange = useStableCallback((index: number) => { props.onPlaceholderIndexChange?.(index); }); - // Handle case where user ends drag without moving their finger. - useAnimatedReaction( - () => { - return isTouchActiveNative.value; - }, - (cur, prev) => { - if (cur !== prev && !cur) { - const hasMoved = !!touchTranslate.value; - if (!hasMoved && activeIndexAnim.value >= 0 && !disabled.value) { - runOnJS(onRelease)(activeIndexAnim.value); - runOnJS(onDragEnd)({ - from: activeIndexAnim.value, - to: spacerIndexAnim.value, - }); - } - } - }, - [isTouchActiveNative, onDragEnd, onRelease] - ); - useAnimatedReaction( () => { return spacerIndexAnim.value; @@ -271,11 +373,26 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { ); const gestureDisabled = useSharedValue(false); + const touchStartX = useSharedValue(0); + const touchStartY = useSharedValue(0); + const manualPanActivated = useSharedValue(false); + const coordinatesDragWithExternalGesture = Boolean( + props.simultaneousHandlers || props.dragGestureRef + ); - const panGesture = Gesture.Pan() + let panGesture = Gesture.Pan() .onBegin((evt) => { + manualPanActivated.value = false; gestureDisabled.value = disabled.value; if (gestureDisabled.value) return; + const sessionId = gestureSessionId.value + 1; + gestureSessionId.value = sessionId; + gestureSessionActive.value = true; + terminalHandled.value = false; + runOnJS(onGestureBegin)(sessionId); + panGestureState.value = evt.state; + }) + .onStart((evt) => { panGestureState.value = evt.state; }) .onUpdate((evt) => { @@ -286,8 +403,15 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { : evt.translationY; touchTranslate.value = translation; }) - .onEnd((evt) => { + .onEnd((evt, success) => { if (gestureDisabled.value) return; + if ( + !success || + terminalHandled.value || + !gestureSessionActive.value + ) + return; + // Set touch val to current translate val isTouchActiveNative.value = false; const translation = horizontalAnim.value @@ -299,6 +423,7 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { // Only call onDragEnd if actually dragging a cell if (activeIndexAnim.value === -1 || disabled.value) return; + terminalHandled.value = true; disabled.value = true; runOnJS(onRelease)(activeIndexAnim.value); const springTo = placeholderOffset.value - activeCellOffset.value; @@ -309,22 +434,85 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { runOnJS(onDragEnd)({ from: activeIndexAnim.value, to: spacerIndexAnim.value, + sessionId: gestureSessionId.value, }); disabled.value = false; } ); }) - .onTouchesDown(() => { + .onFinalize((evt) => { + manualPanActivated.value = false; + panGestureState.value = evt.state; + runOnJS(onContainerTouchEnd)(); + if (gestureDisabled.value) return; + + const sessionId = gestureSessionId.value; + const committed = terminalHandled.value; + gestureSessionActive.value = false; + if (!committed) finalizeCancelledDrag(); + runOnJS(onGestureFinalize)(sessionId, committed); + }) + .onTouchesDown((event) => { + const touch = event.changedTouches[0]; + if (touch) { + touchStartX.value = touch.absoluteX; + touchStartY.value = touch.absoluteY; + } runOnJS(onContainerTouchStart)(); }) - .onTouchesUp(() => { + .onTouchesMove((event, stateManager) => { + if (!coordinatesDragWithExternalGesture) return; + + // Manual activation arbitrates only while the pan is waiting. Failing or + // reactivating an active recognizer can leave RNGH/Reanimated livelocked. + if (manualPanActivated.value) return; + + const touch = event.changedTouches[0]; + if (!touch || event.numberOfTouches !== 1) { + stateManager.fail(); + return; + } + + const deltaX = touch.absoluteX - touchStartX.value; + const deltaY = touch.absoluteY - touchStartY.value; + const absDeltaX = Math.abs(deltaX); + const absDeltaY = Math.abs(deltaY); + + if (activeIndexAnim.value < 0) { + if (absDeltaX > 6 || absDeltaY > 6) { + stateManager.fail(); + } + return; + } + + if (absDeltaX > 10 && absDeltaX > absDeltaY) { + stateManager.fail(); + } else if (absDeltaY >= activationDistanceProp && absDeltaY > absDeltaX) { + manualPanActivated.value = true; + stateManager.activate(); + } + }) + .onTouchesUp((event, stateManager) => { // Turning this into a worklet causes timing issues. We want it to run // just after the finger lifts. runOnJS(onContainerTouchEnd)(); + // A manually activated iOS pan can remain BEGAN after the last finger + // lifts. Fail it explicitly so onFinalize cancels the armed row. + if (event.numberOfTouches === 0 && panGestureState.value === State.BEGAN) { + stateManager.fail(); + } + }) + .onTouchesCancelled((_event, stateManager) => { + if (panGestureState.value === State.BEGAN) { + stateManager.fail(); + } }); if (dragHitSlop) panGesture.hitSlop(dragHitSlop); - if (activationDistanceProp) { + if (props.dragGestureRef) panGesture.withRef(props.dragGestureRef); + if (coordinatesDragWithExternalGesture) { + panGesture = panGesture.manualActivation(true); + } else if (activationDistanceProp) { const activeOffset = [-activationDistanceProp, activationDistanceProp]; if (props.horizontal) { panGesture.activeOffsetX(activeOffset); @@ -332,6 +520,21 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { panGesture.activeOffsetY(activeOffset); } } + if (props.simultaneousHandlers) { + panGesture.simultaneousWithExternalGesture( + ...(Array.isArray(props.simultaneousHandlers) + ? props.simultaneousHandlers + : [props.simultaneousHandlers]) + ); + } + + // GestureDetector only updates handler config when a retained host reappears + // with the same native tag. Changing the gesture count forces a native + // reattachment without remounting the list subtree. + const detectorGesture = + props.dragGestureHostPresented === false + ? Gesture.Simultaneous(panGesture, Gesture.Manual().enabled(false)) + : panGesture; const onScroll = useStableCallback((scrollOffset: number) => { props.onScrollOffsetChange?.(scrollOffset); @@ -372,9 +575,11 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { keyExtractor={keyExtractor} horizontal={!!props.horizontal} layoutAnimationDisabled={layoutAnimationDisabled} + transformHostKeys={transformHostKeys} > - + ( }); const onListContainerLayout = useStableCallback(async ({ containerRef }) => { - const nodeHandle = findNodeHandle(scrollableRef.current); + const nodeHandle = scrollableRef.current; const onSuccess = (_x: number, y: number) => { listVerticalOffset.value = y; @@ -74,6 +74,10 @@ function NestableDraggableFlatListInner( } ); + const onDragTerminate = useStableCallback(() => { + setOuterScrollEnabled(true); + }); + const onAnimValInit: DraggableFlatListProps["onAnimValInit"] = useStableCallback( (params) => { setListHoverOffset(params.hoverOffset); @@ -95,6 +99,7 @@ function NestableDraggableFlatListInner( outerScrollOffset={outerScrollOffset} onDragBegin={onDragBegin} onDragEnd={onDragEnd} + onDragTerminate={onDragTerminate} onAnimValInit={onAnimValInit} /> ); diff --git a/node_modules/react-native-draggable-flatlist/src/context/draggableFlatListContext.tsx b/node_modules/react-native-draggable-flatlist/src/context/draggableFlatListContext.tsx index 75f7db7..e99faf3 100644 --- a/node_modules/react-native-draggable-flatlist/src/context/draggableFlatListContext.tsx +++ b/node_modules/react-native-draggable-flatlist/src/context/draggableFlatListContext.tsx @@ -5,6 +5,7 @@ type Props = { keyExtractor: (item: T, index: number) => string; horizontal: boolean; layoutAnimationDisabled: boolean; + transformHostKeys: ReadonlyMap; children: React.ReactNode; }; @@ -19,6 +20,7 @@ export default function DraggableFlatListProvider({ keyExtractor, horizontal, layoutAnimationDisabled, + transformHostKeys, children, }: Props) { const value = useMemo( @@ -27,8 +29,9 @@ export default function DraggableFlatListProvider({ keyExtractor, horizontal, layoutAnimationDisabled, + transformHostKeys, }), - [activeKey, keyExtractor, horizontal, layoutAnimationDisabled] + [activeKey, keyExtractor, horizontal, layoutAnimationDisabled, transformHostKeys] ); return ( diff --git a/node_modules/react-native-draggable-flatlist/src/types.ts b/node_modules/react-native-draggable-flatlist/src/types.ts index d6755c8..af70a01 100644 --- a/node_modules/react-native-draggable-flatlist/src/types.ts +++ b/node_modules/react-native-draggable-flatlist/src/types.ts @@ -36,12 +36,21 @@ export type DraggableFlatListProps = Modify< keyExtractor: (item: T, index: number) => string; onDragBegin?: (index: number) => void; onDragEnd?: (params: DragEndParams) => void; + /** @internal Used by the nestable wrapper to release its outer-scroll lock on cancellation. */ + onDragTerminate?: () => void; onPlaceholderIndexChange?: (placeholderIndex: number) => void; onRelease?: (index: number) => void; onScrollOffsetChange?: (scrollOffset: number) => void; renderItem: RenderItem; renderPlaceholder?: RenderPlaceholder; simultaneousHandlers?: React.Ref | React.Ref[]; + /** @internal Forces handler reattachment when a retained native host is presented. */ + dragGestureHostPresented?: boolean; + /** + * Ref for the drag pan, so an enclosing gesture can wait for it to fail. The pan then arbitrates + * manually, as with `simultaneousHandlers`: it fails on a move unless a row is armed. + */ + dragGestureRef?: React.MutableRefObject; outerScrollOffset?: Animated.SharedValue; onAnimValInit?: (animVals: ReturnType) => void; itemEnteringAnimation?: AnimateProps["entering"];