Skip to content

Commit 8189614

Browse files
committed
feat: right click whilst dragging will now cancel drag
1 parent 440040d commit 8189614

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/ChessboardProvider.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
defaultNumericNotationStyle,
5252
defaultSquareStyle,
5353
} from './defaults';
54+
import { RightClickCancelSensor } from './RightClickCancelSensor';
5455

5556
type Defined<T> = T extends undefined ? never : T;
5657

@@ -613,6 +614,7 @@ export function ChessboardProvider({
613614
useSensor(KeyboardSensor),
614615
useSensor(TouchSensor),
615616
useSensor(MouseSensor),
617+
useSensor(RightClickCancelSensor),
616618
);
617619

618620
// collision detection that first tries pointer-based detection and then falls back to rectangle intersection for keyboards

src/RightClickCancelSensor.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { PointerSensor, PointerSensorProps } from '@dnd-kit/core';
2+
3+
/**
4+
* A custom PointerSensor that listens for right-clicks during a drag
5+
* and cancels the active drag operation.
6+
*
7+
* Works by listening to the "contextmenu" event on window.
8+
*/
9+
export class RightClickCancelSensor extends PointerSensor {
10+
private handleContextMenu = (event: MouseEvent) => {
11+
event.preventDefault();
12+
// @ts-expect-error: Accessing private props to call onCancel
13+
if (this.props && typeof this.props.onCancel === 'function') {
14+
// @ts-expect-error: Accessing private props to call onCancel
15+
this.props.onCancel();
16+
}
17+
};
18+
19+
constructor(props: PointerSensorProps) {
20+
super(props);
21+
window.addEventListener('contextmenu', this.handleContextMenu, {
22+
passive: false,
23+
});
24+
}
25+
26+
teardown() {
27+
window.removeEventListener('contextmenu', this.handleContextMenu);
28+
}
29+
}

0 commit comments

Comments
 (0)