Skip to content

Commit

Permalink
fix: should not trigger dragging when clicking bookmark with a shakin…
Browse files Browse the repository at this point in the history
…g hand (reported by @dlbryant)
  • Loading branch information
foray1010 committed May 18, 2019
1 parent 833527a commit e2e8265
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/js/popup/components/dragAndDrop/DragAndDropConsumer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const useDragEvents = ({
DragAndDropContext
)

const [mouseCoordinate, setMouseCoordinate] = React.useState({
x: 0,
y: 0
})

return {
handleDragOver: React.useCallback(
(evt: React.MouseEvent<HTMLElement>) => {
Expand All @@ -34,19 +39,31 @@ const useDragEvents = ({
(evt: React.MouseEvent<HTMLElement>) => {
if (pendingKey === null) return

const threshold = 5
const xDisplacement = Math.abs(mouseCoordinate.x - evt.clientX)
const yDisplacement = Math.abs(mouseCoordinate.y - evt.clientY)
// prevent triggering drag instead of click due to hand shaking
if (xDisplacement <= threshold && yDisplacement <= threshold) return

setActiveKey(pendingKey)

onDragStart(evt, {
activeKey: pendingKey,
itemKey
})
},
[itemKey, onDragStart, pendingKey, setActiveKey]
[itemKey, mouseCoordinate.x, mouseCoordinate.y, onDragStart, pendingKey, setActiveKey]
),
handleMouseDown: React.useCallback(
(evt: React.MouseEvent<HTMLElement>) => {
if (evt.buttons !== 1) return

setPendingKey(itemKey)

setMouseCoordinate({
x: evt.clientX,
y: evt.clientY
})
},
[itemKey, setPendingKey]
),
Expand Down

0 comments on commit e2e8265

Please sign in to comment.