Skip to content

Commit

Permalink
fix: cannot drag on the edge of the bookmark
Browse files Browse the repository at this point in the history
  • Loading branch information
foray1010 committed May 18, 2019
1 parent 60e1cfa commit 1ad980e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
21 changes: 11 additions & 10 deletions src/js/popup/components/dragAndDrop/DragAndDropConsumer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ const useDragEvents = ({
onDragOver: (evt: React.MouseEvent<HTMLElement>, responseEvent: ResponseEvent) => void
onDragStart: (evt: React.MouseEvent<HTMLElement>, responseEvent: ResponseEvent) => void
}) => {
const {activeKey, pendingKey, setActiveKey, setPendingKey, unsetAllKeys} = React.useContext(
DragAndDropContext
)

const [mouseCoordinate, setMouseCoordinate] = React.useState({
x: 0,
y: 0
})
const {
activeKey,
mouseCoordinate,
pendingKey,
setActiveKey,
setMouseCoordinate,
setPendingKey,
unsetAllKeys
} = React.useContext(DragAndDropContext)

return {
handleDragOver: React.useCallback(
Expand Down Expand Up @@ -65,7 +66,7 @@ const useDragEvents = ({
y: evt.clientY
})
},
[itemKey, setPendingKey]
[itemKey, setMouseCoordinate, setPendingKey]
),
handleMouseUp: React.useCallback(() => {
unsetAllKeys()
Expand Down Expand Up @@ -114,7 +115,7 @@ const DragAndDropConsumer = (props: Props) => {
})

const isDragging = context.activeKey !== null
const isPending = context.pendingKey === props.itemKey
const isPending = context.pendingKey !== null
return (
<div
className={props.className}
Expand Down
12 changes: 12 additions & 0 deletions src/js/popup/components/dragAndDrop/DragAndDropContext.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import * as React from 'react'

type MouseCoordinate = Readonly<{
x: number
y: number
}>

export interface DragAndDropContextType {
activeKey: string | null
mouseCoordinate: MouseCoordinate
pendingKey: string | null
setActiveKey: (activeKey: string) => void
setMouseCoordinate: (mouseCoordinate: MouseCoordinate) => void
setPendingKey: (pendingKey: string) => void
unsetAllKeys: () => void
}
export default React.createContext<DragAndDropContextType>({
activeKey: null,
mouseCoordinate: {
x: 0,
y: 0
},
pendingKey: null,
setActiveKey: () => {},
setMouseCoordinate: () => {},
setPendingKey: () => {},
unsetAllKeys: () => {}
})
12 changes: 10 additions & 2 deletions src/js/popup/components/dragAndDrop/DragAndDropProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import * as React from 'react'
import useEventListener from '../../hooks/useEventListener'
import DragAndDropContext, {DragAndDropContextType} from './DragAndDropContext'

const useDragAndDropContextState = () => {
const useDragAndDropContextState = (): DragAndDropContextType => {
const [activeKey, setActiveKeyState] = React.useState<DragAndDropContextType['activeKey']>(null)
const [mouseCoordinate, setMouseCoordinate] = React.useState<
DragAndDropContextType['mouseCoordinate']
>({
x: 0,
y: 0
})
const [pendingKey, setPendingKeyState] = React.useState<DragAndDropContextType['pendingKey']>(
null
)
Expand All @@ -25,12 +31,14 @@ const useDragAndDropContextState = () => {
return React.useMemo(
() => ({
activeKey,
mouseCoordinate,
pendingKey,
setActiveKey,
setMouseCoordinate,
setPendingKey,
unsetAllKeys
}),
[activeKey, pendingKey, setActiveKey, setPendingKey, unsetAllKeys]
[activeKey, mouseCoordinate, pendingKey, setActiveKey, setPendingKey, unsetAllKeys]
)
}

Expand Down

0 comments on commit 1ad980e

Please sign in to comment.