-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use a more stable drag end handling
Instead of triggering the animated reaction through a shared value update we save the handler in an array and call it when the item is released. This way we remove hacks to avoid reanimated batching.
- Loading branch information
Showing
6 changed files
with
44 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,37 @@ | ||
import {useAnimatedReaction} from 'react-native-reanimated'; | ||
import {useEffect} from 'react'; | ||
|
||
import {useContext} from './useContext'; | ||
import {ReorderableCellContext, ReorderableListContext} from '../contexts'; | ||
|
||
export const useReorderableDragEnd = ( | ||
onEnd: (from: number, to: number) => void, | ||
) => { | ||
const {currentIndex} = useContext(ReorderableListContext); | ||
const {releasedIndex, index} = useContext(ReorderableCellContext); | ||
|
||
useAnimatedReaction( | ||
() => releasedIndex.value === index, | ||
newValue => { | ||
if (newValue) { | ||
onEnd(index, currentIndex.value); | ||
const {dragEndHandlers} = useContext(ReorderableListContext); | ||
const {index} = useContext(ReorderableCellContext); | ||
|
||
useEffect(() => { | ||
dragEndHandlers.modify(value => { | ||
'worklet'; | ||
|
||
if (!Array.isArray(value[index])) { | ||
value[index] = []; | ||
} | ||
}, | ||
[onEnd], | ||
); | ||
|
||
value[index].push(onEnd); | ||
|
||
return value; | ||
}); | ||
|
||
return () => { | ||
dragEndHandlers.modify(value => { | ||
'worklet'; | ||
|
||
if (Array.isArray(value[index])) { | ||
value[index] = value[index].filter(x => x.name !== onEnd.name); | ||
} | ||
|
||
return value; | ||
}); | ||
}; | ||
}, [index, dragEndHandlers, onEnd]); | ||
}; |