parent
04e340ad64
commit
562895c073
@ -0,0 +1,33 @@ |
||||
import { useEffect, Ref } from 'preact/hooks'; |
||||
import Sortable from 'sortablejs'; |
||||
import { ReorderListParams } from '../../types'; |
||||
|
||||
export const useSortable = ( |
||||
listContainerRef: Readonly<Ref<HTMLElement>>, |
||||
onListReorder: (params: ReorderListParams) => void, |
||||
) => { |
||||
useEffect(() => { |
||||
if (!listContainerRef.current) { |
||||
return; |
||||
} |
||||
|
||||
const sortable = Sortable.create(listContainerRef.current, { |
||||
onEnd: (event) => { |
||||
if ( |
||||
event.oldIndex === undefined || |
||||
event.newIndex === undefined |
||||
) { |
||||
return; |
||||
} |
||||
|
||||
onListReorder({ |
||||
oldIndex: event.oldIndex, |
||||
newIndex: event.newIndex, |
||||
}); |
||||
}, |
||||
}); |
||||
return () => { |
||||
sortable.destroy(); |
||||
}; |
||||
}, [listContainerRef, onListReorder]); |
||||
}; |
Loading…
Reference in new issue