Skip to content

Commit

Permalink
fix draggable with custom hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrth25 committed Nov 6, 2023
1 parent 40ee62a commit 3bdf475
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/containers/questions-list/QuestionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import Box from '@mui/material/Box'

import Question from '~/components/question/Question'
import useDroppable from '~/hooks/use-droppable'

import { styles } from '~/containers/questions-list/QuestionsList.styles'
import { Question as QuestionInterface } from '~/types'
Expand All @@ -21,6 +22,8 @@ interface QuestionsListProps {
}

const QuestionsList: FC<QuestionsListProps> = ({ items, setItems }) => {
const { enabled } = useDroppable()

const reorder = (
list: QuestionInterface[],
startIndex: number,
Expand Down Expand Up @@ -64,13 +67,16 @@ const QuestionsList: FC<QuestionsListProps> = ({ items, setItems }) => {

return (
<DragDropContext onDragEnd={onDragEnd}>
<Droppable droppableId='draggable'>
{(provided: DroppableProvided) => (
<Box {...provided.droppableProps} ref={provided.innerRef}>
{questionsList}
</Box>
)}
</Droppable>
{enabled && (
<Droppable droppableId='draggable'>
{(provided: DroppableProvided) => (
<Box {...provided.droppableProps} ref={provided.innerRef}>
{questionsList}
{provided.placeholder}
</Box>
)}
</Droppable>
)}
</DragDropContext>
)
}
Expand Down
18 changes: 18 additions & 0 deletions src/hooks/use-droppable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useState, useEffect } from 'react'

const useDroppable = () => {
const [enabled, setEnabled] = useState(false)

useEffect(() => {
const animation = requestAnimationFrame(() => setEnabled(true))

return () => {
cancelAnimationFrame(animation)
setEnabled(false)
}
}, [])

return { enabled }
}

export default useDroppable

0 comments on commit 3bdf475

Please sign in to comment.