File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed
Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ import {
5151 defaultNumericNotationStyle ,
5252 defaultSquareStyle ,
5353} from './defaults' ;
54+ import { RightClickCancelSensor } from './RightClickCancelSensor' ;
5455
5556type 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
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments