Skip to content

Commit 1eaeb35

Browse files
committed
fix touch drag in browser DevTools responsive mode
Closes #403 Two fixes: 1. Always register touch event listeners instead of gating behind isTouchDevice (which is evaluated once at import time and misses DevTools touch simulation toggled after page load). 2. Guard preventDefault() with e.cancelable check to avoid errors when the browser marks simulated touch events as non-cancelable.
1 parent 5a40c8e commit 1eaeb35

1 file changed

Lines changed: 5 additions & 16 deletions

File tree

packages/lib/src/index.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
type Position,
1717
type AvatarEditorConfig,
1818
isPassiveSupported,
19-
isTouchDevice,
2019
} from '@react-avatar-editor/core'
2120

2221
export interface Props extends AvatarEditorConfig {
@@ -270,7 +269,7 @@ const AvatarEditor = forwardRef<AvatarEditorRef, Props>((props, ref) => {
270269
return
271270
}
272271

273-
e.preventDefault()
272+
if (e.cancelable) e.preventDefault()
274273

275274
const mousePositionX =
276275
'targetTouches' in e ? e.targetTouches[0].pageX : e.clientX
@@ -315,24 +314,14 @@ const AvatarEditor = forwardRef<AvatarEditorRef, Props>((props, ref) => {
315314
const options = isPassiveSupported() ? { passive: false } : false
316315
document.addEventListener('mousemove', handleDocumentMouseMove, options)
317316
document.addEventListener('mouseup', handleDocumentMouseUp, options)
318-
319-
if (isTouchDevice) {
320-
document.addEventListener('touchmove', handleDocumentMouseMove, options)
321-
document.addEventListener('touchend', handleDocumentMouseUp, options)
322-
}
317+
document.addEventListener('touchmove', handleDocumentMouseMove, options)
318+
document.addEventListener('touchend', handleDocumentMouseUp, options)
323319

324320
return () => {
325321
document.removeEventListener('mousemove', handleDocumentMouseMove, false)
326322
document.removeEventListener('mouseup', handleDocumentMouseUp, false)
327-
328-
if (isTouchDevice) {
329-
document.removeEventListener(
330-
'touchmove',
331-
handleDocumentMouseMove,
332-
false,
333-
)
334-
document.removeEventListener('touchend', handleDocumentMouseUp, false)
335-
}
323+
document.removeEventListener('touchmove', handleDocumentMouseMove, false)
324+
document.removeEventListener('touchend', handleDocumentMouseUp, false)
336325
}
337326
}, [])
338327

0 commit comments

Comments
 (0)