react-spring#useSprings TypeScript Examples
The following examples show how to use
react-spring#useSprings.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: array-supercontrol.tsx From utopia with MIT License | 5 votes |
export function useArraySuperControl<T>(
values: ReadonlyArray<T>,
onSubmitValue: (newValue: ReadonlyArray<T>, transient?: boolean | undefined) => void,
rowHeight: number,
reversed: boolean = false,
) {
const localDraggingIndex = React.useRef(values.map((_, index) => index))
const [springs, setSprings] = useSprings(
values.length,
getStyleForArrayDragItem(localDraggingIndex.current, reversed, rowHeight),
[values],
)
React.useEffect(() => {
localDraggingIndex.current = values.map((_, index) => index)
setSprings(getStyleForArrayDragItem(localDraggingIndex.current, reversed, rowHeight))
}, [values, setSprings, reversed, rowHeight])
const bind = useDrag(({ args: [currentlyDraggingOriginalIndex], down, movement: [, y] }) => {
const currentlyDraggingTemporaryIndex = localDraggingIndex.current.indexOf(
currentlyDraggingOriginalIndex,
)
const newCurrentlyDraggingTemporaryIndex = clamp(
Math.round((currentlyDraggingOriginalIndex * rowHeight + (reversed ? -y : y)) / rowHeight),
0,
values.length - 1,
)
localDraggingIndex.current = move(
currentlyDraggingTemporaryIndex,
newCurrentlyDraggingTemporaryIndex,
localDraggingIndex.current,
)
setSprings(
getStyleForArrayDragItem(localDraggingIndex.current, reversed, rowHeight, down, {
currentlyDraggingOriginalIndex: currentlyDraggingOriginalIndex as number,
currentlyDraggingTemporaryIndex,
y,
}),
)
// Ensure to only change the ordering if it has actually changed.
if (!down && currentlyDraggingOriginalIndex !== newCurrentlyDraggingTemporaryIndex) {
const newValue = move(
currentlyDraggingOriginalIndex as number,
newCurrentlyDraggingTemporaryIndex,
[...values],
)
onSubmitValue(newValue)
}
})
return {
bind,
springs,
}
}