@@ -4,9 +4,8 @@ import type {
44 ReactiveControllerHost ,
55} from 'lit' ;
66import type { Ref } from 'lit/directives/ref.js' ;
7- import { findElementFromEventPath } from '../util.js' ;
87
9- type DragDropCallback = ( ) => unknown ;
8+ import { findElementFromEventPath } from '../util.js' ;
109
1110type DragEnterCallback = ( target : Element ) => unknown ;
1211
@@ -16,9 +15,18 @@ type DragCallbackParams = {
1615 event : PointerEvent ;
1716} ;
1817
18+ type State = {
19+ initial : DOMRect ;
20+ current : DOMRect ;
21+ offset : {
22+ dx : number ;
23+ dy : number ;
24+ } ;
25+ } ;
26+
1927// TODO: Start providing callback params for the hooks and type them
2028
21- type DragDropConfig = {
29+ type DragConfig = {
2230 /** Whether the drag and drop feature is enabled for the current host. */
2331 enabled ?: boolean ;
2432 /**
@@ -62,7 +70,7 @@ type DragDropConfig = {
6270 /** Callback invoked at the beginning of a drag operation. */
6371 dragStart ?: DragCallback ;
6472 /** Callback invoked while dragging the target element. */
65- dragMove ?: DragDropCallback ;
73+ dragMove ?: DragCallback ;
6674
6775 dragEnter ?: DragEnterCallback ;
6876
@@ -71,22 +79,23 @@ type DragDropConfig = {
7179 dragOver ?: DragEnterCallback ;
7280
7381 /** Callback invoked during a drop operation. */
74- dragEnd ?: DragDropCallback ;
82+ dragEnd ?: DragCallback ;
7583 /** Callback invoked when a drag and drop is cancelled */
76- dragCancel ?: DragDropCallback ;
84+ dragCancel ?: DragCallback ;
7785} ;
7886
7987const additionalEvents = [ 'pointermove' , 'lostpointercapture' ] as const ;
8088
81- class DragDropController implements ReactiveController {
89+ class DragController implements ReactiveController {
8290 private _host : ReactiveControllerHost & LitElement ;
83- private _config : DragDropConfig = {
91+ private _config : DragConfig = {
8492 enabled : true ,
8593 mode : 'deferred' ,
8694 snapToCursor : false ,
8795 } ;
8896
89- private _dragOffset = { dx : 0 , dy : 0 } ;
97+ private _state ! : State ;
98+
9099 private _previousMatch ! : Element | null ;
91100
92101 private _id = - 1 ;
@@ -145,10 +154,7 @@ class DragDropController implements ReactiveController {
145154 return Boolean ( this . _config . enabled ) ;
146155 }
147156
148- constructor (
149- host : ReactiveControllerHost & LitElement ,
150- config ?: DragDropConfig
151- ) {
157+ constructor ( host : ReactiveControllerHost & LitElement , config ?: DragConfig ) {
152158 this . _host = host ;
153159 this . _host . addController ( this ) ;
154160 this . setConfig ( config ) ;
@@ -174,10 +180,13 @@ class DragDropController implements ReactiveController {
174180 const rect = this . _host . getBoundingClientRect ( ) ;
175181
176182 this . _id = pointerId ;
177-
178- this . _dragOffset = {
179- dx : rect . x - clientX ,
180- dy : rect . y - clientY ,
183+ this . _state = {
184+ initial : rect ,
185+ current : structuredClone ( rect ) ,
186+ offset : {
187+ dx : rect . x - clientX ,
188+ dy : rect . y - clientY ,
189+ } ,
181190 } ;
182191 }
183192
@@ -231,8 +240,10 @@ class DragDropController implements ReactiveController {
231240
232241 private _setPosition ( x : number , y : number ) {
233242 const { top, left } = this . _layer . getBoundingClientRect ( ) ;
234- const posX = this . _hasSnapping ? x - left : x - left + this . _dragOffset . dx ;
235- const posY = this . _hasSnapping ? y - top : y - top + this . _dragOffset . dy ;
243+ const { offset } = this . _state ;
244+
245+ const posX = this . _hasSnapping ? x - left : x - left + offset . dx ;
246+ const posY = this . _hasSnapping ? y - top : y - top + offset . dy ;
236247
237248 this . _dragItem . style . transform = `translate(${ posX } px,${ posY } px)` ;
238249 }
@@ -242,19 +253,17 @@ class DragDropController implements ReactiveController {
242253 return ;
243254 }
244255
245- this . _ghost = this . _config . ghost
246- ? this . _config . ghost . call ( this . _host )
247- : createDefaultDragGhost ( this . _host . getBoundingClientRect ( ) ) ;
256+ this . _ghost =
257+ this . _config . ghost ? .call ( this . _host ) ??
258+ createDefaultDragGhost ( this . _host . getBoundingClientRect ( ) ) ;
248259
249260 this . _setPosition ( clientX , clientY ) ;
250261 this . _layer . append ( this . _ghost ) ;
251262 }
252263
253264 private _removeGhost ( ) : void {
254- if ( this . _ghost ) {
255- this . _ghost . remove ( ) ;
256- this . _ghost = null ;
257- }
265+ this . _ghost ?. remove ( ) ;
266+ this . _ghost = null ;
258267 }
259268
260269 private _shouldSkip ( event : PointerEvent ) : boolean {
@@ -308,7 +317,7 @@ class DragDropController implements ReactiveController {
308317 }
309318
310319 /** Updates the drag and drop controller configuration. */
311- public setConfig ( value ?: DragDropConfig ) : void {
320+ public setConfig ( value ?: DragConfig ) : void {
312321 Object . assign ( this . _config , value ) ;
313322 }
314323
@@ -378,9 +387,9 @@ function createDefaultDragGhost(rect: DOMRect): HTMLElement {
378387/**
379388 * Adds a drag and drop controller to the given host
380389 */
381- export function addDragDropController (
390+ export function addDragController (
382391 host : ReactiveControllerHost & LitElement ,
383- config ?: DragDropConfig
384- ) : DragDropController {
385- return new DragDropController ( host , config ) ;
392+ config ?: DragConfig
393+ ) : DragController {
394+ return new DragController ( host , config ) ;
386395}
0 commit comments