Skip to content

Commit e6cddad

Browse files
authored
Merge pull request #2921 from Taal91/feature/improve-mouse-event-simulation
Replaced initMouseEvent with MouseEvent constructor and added composed: true
2 parents 88ce70e + 0fe2a02 commit e6cddad

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

src/dd-touch.ts

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import { DDManager } from './dd-manager';
1111
* /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
1212
*/
1313
export const isTouch: boolean = typeof window !== 'undefined' && typeof document !== 'undefined' &&
14-
( 'ontouchstart' in document
15-
|| 'ontouchstart' in window
16-
// || !!window.TouchEvent // true on Windows 10 Chrome desktop so don't use this
17-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
18-
|| ((window as any).DocumentTouch && document instanceof (window as any).DocumentTouch)
19-
|| navigator.maxTouchPoints > 0
20-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
21-
|| (navigator as any).msMaxTouchPoints > 0
22-
);
14+
( 'ontouchstart' in document
15+
|| 'ontouchstart' in window
16+
// || !!window.TouchEvent // true on Windows 10 Chrome desktop so don't use this
17+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
18+
|| ((window as any).DocumentTouch && document instanceof (window as any).DocumentTouch)
19+
|| navigator.maxTouchPoints > 0
20+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
21+
|| (navigator as any).msMaxTouchPoints > 0
22+
);
2323

2424
// interface TouchCoord {x: number, y: number};
2525

@@ -51,26 +51,23 @@ function simulateMouseEvent(e: TouchEvent, simulatedType: string) {
5151
// Prevent "Ignored attempt to cancel a touchmove event with cancelable=false" errors
5252
if (e.cancelable) e.preventDefault();
5353

54-
const touch = e.changedTouches[0], simulatedEvent = document.createEvent('MouseEvents');
55-
56-
// Initialize the simulated mouse event using the touch event's coordinates
57-
simulatedEvent.initMouseEvent(
58-
simulatedType, // type
59-
true, // bubbles
60-
true, // cancelable
61-
window, // view
62-
1, // detail
63-
touch.screenX, // screenX
64-
touch.screenY, // screenY
65-
touch.clientX, // clientX
66-
touch.clientY, // clientY
67-
false, // ctrlKey
68-
false, // altKey
69-
false, // shiftKey
70-
false, // metaKey
71-
0, // button
72-
null // relatedTarget
73-
);
54+
const touch = e.changedTouches[0], simulatedEvent = new MouseEvent(simulatedType, {
55+
bubbles: true,
56+
composed: true,
57+
cancelable: true,
58+
view: window,
59+
detail: 1,
60+
screenX: touch.screenX,
61+
screenY: touch.screenY,
62+
clientX: touch.clientX,
63+
clientY: touch.clientY,
64+
ctrlKey: false,
65+
altKey: false,
66+
shiftKey: false,
67+
metaKey: false,
68+
button: 0,
69+
relatedTarget: null
70+
});
7471

7572
// Dispatch the simulated event to the target element
7673
e.target.dispatchEvent(simulatedEvent);

0 commit comments

Comments
 (0)