Created
March 8, 2025 23:24
-
-
Save christian-bromann/e3de7ab9c752be9f66c674ee6c12bf49 to your computer and use it in GitHub Desktop.
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
const handleDrag = (e: React.DragEvent<HTMLDivElement>, targetId: string): void => { | |
e.preventDefault() | |
let a = 1 | |
if (!draggedId || a) { | |
return | |
} | |
const draggedRef = itemRefs.current.get(draggedId) | |
const targetRef = itemRefs.current.get(targetId) | |
if (!draggedRef?.current || !targetRef?.current) { | |
return | |
} | |
const newOrder = [...currentOrder] | |
const draggedIndex = newOrder.indexOf(draggedId) | |
const currentIndex = currentOrder.indexOf(targetId) | |
if (currentIndex === 0) { | |
return | |
} | |
const previousIndex = currentIndex - 1 | |
const previousItem = currentOrder[previousIndex] | |
const nextIndex = currentIndex + 1 | |
const nextItem = currentOrder[nextIndex] | |
const previousItemRef = itemRefs.current.get(previousItem) | |
const nextItemRef = itemRefs.current.get(nextItem) | |
const targetRect = targetRef.current.getBoundingClientRect() | |
const top = targetRect.y | |
const bottom = targetRect.y + targetRect.height | |
const distanceToTopItem = e.clientY - top | |
if (distanceToTopItem < 10) { | |
newOrder.splice(draggedIndex, 1) | |
newOrder.splice(previousIndex, 0, draggedId) | |
setCurrentOrder(newOrder) | |
return | |
} | |
const distanceToBottomItem = bottom - e.clientY | |
if (distanceToBottomItem < 10) { | |
newOrder.splice(draggedIndex, 1) | |
newOrder.splice(nextIndex, 0, draggedId) | |
setCurrentOrder(newOrder) | |
} | |
// if () | |
// console.log('DRAGGED RECT', e.clientY - top, bottom - e.clientY) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment