Skip to content

Commit 22691b1

Browse files
Fix pointer sensor preventing default event behaviour on attachment.
Instead, wait for activation of drag before intercepting relevant events. This avoids unexpected side effects, such as an `<input/>` not receiving focus on click. With this change, also listen for and clear any text selection that happens as a side effect during an active drag operation.
1 parent 4d66638 commit 22691b1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
### Fixed
6+
7+
- Fix pointer sensor preventing default event behaviour on attachment. Instead,
8+
wait for activation of drag before intercepting relevant events. This avoids
9+
unexpected side effects, such as an `<input/>` not receiving focus on click.
10+
11+
With this change, also listen for and clear any text selection that happens as
12+
a side effect during an active drag operation.
13+
14+
Thanks to [@yonathan06](https://github.com/yonathan06) for reporting this
15+
behaviour.
16+
317
## [0.4.0] - 2022-01-09
418

519
### Added

src/create-pointer-sensor.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const createPointerSensor = (id: string | number = "pointer-sensor"): void => {
3434
let activationDraggableId: string | number | null = null;
3535

3636
const attach = (event: PointerEvent, draggableId: string | number): void => {
37-
event.preventDefault();
3837
document.addEventListener("pointermove", onPointerMove);
3938
document.addEventListener("pointerup", onPointerUp);
4039

@@ -53,12 +52,16 @@ const createPointerSensor = (id: string | number = "pointer-sensor"): void => {
5352

5453
document.removeEventListener("pointermove", onPointerMove);
5554
document.removeEventListener("pointerup", onPointerUp);
55+
document.removeEventListener("selectionchange", clearSelection);
5656
};
5757

5858
const onActivate = (): void => {
5959
if (!anySensorActive()) {
6060
sensorStart(id);
6161
dragStart(activationDraggableId!);
62+
63+
clearSelection();
64+
document.addEventListener("selectionchange", clearSelection);
6265
} else if (!isActiveSensor()) {
6366
detach();
6467
}
@@ -90,6 +93,10 @@ const createPointerSensor = (id: string | number = "pointer-sensor"): void => {
9093
sensorEnd();
9194
}
9295
};
96+
97+
const clearSelection = () => {
98+
window.getSelection()?.removeAllRanges();
99+
};
93100
};
94101

95102
export { createPointerSensor };

0 commit comments

Comments
 (0)