@@ -6,7 +6,12 @@ import type {
66import type { Ref } from 'lit/directives/ref.js' ;
77
88import { getDefaultLayer } from '../../resize-container/default-ghost.js' ;
9- import { findElementFromEventPath , getRoot , roundByDPR } from '../util.js' ;
9+ import {
10+ findElementFromEventPath ,
11+ getRoot ,
12+ isLTR ,
13+ roundByDPR ,
14+ } from '../util.js' ;
1015
1116type DragCallback = ( parameters : DragCallbackParameters ) => unknown ;
1217type DragCancelCallback = ( state : DragState ) => unknown ;
@@ -21,13 +26,20 @@ type State = {
2126 current : DOMRect ;
2227 position : { x : number ; y : number } ;
2328 offset : { x : number ; y : number } ;
29+ pointerState : {
30+ initial : { initialX : number ; initialY : number } ;
31+ current : { currentX : number ; currentY : number } ;
32+ direction : Direction ;
33+ } ;
2434} ;
2535
2636type DragState = State & {
2737 ghost : HTMLElement | null ;
2838 element : Element | null ;
2939} ;
3040
41+ type Direction = 'start' | 'end' | 'bottom' | 'top' ;
42+
3143type DragControllerConfiguration = {
3244 /** Whether the drag feature is enabled for the current host. */
3345 enabled ?: boolean ;
@@ -157,13 +169,30 @@ class DragController implements ReactiveController {
157169 }
158170
159171 private get _stateParameters ( ) : DragState {
172+ this . _state . pointerState . direction = this . _trackPointerMovement ( ) ;
173+
160174 return {
161175 ...this . _state ,
162176 ghost : this . _ghost ,
163177 element : this . _matchedElement ,
164178 } ;
165179 }
166180
181+ private _trackPointerMovement ( ) : Direction {
182+ const { initialX, initialY } = this . _state . pointerState . initial ;
183+ const { currentX, currentY } = this . _state . pointerState . current ;
184+ const deltaX = currentX - initialX ;
185+ const deltaY = currentY - initialY ;
186+ const LTR = isLTR ( this . _host ) ;
187+
188+ const isHorizontalMove = Math . abs ( deltaX ) >= Math . abs ( deltaY ) ;
189+
190+ if ( isHorizontalMove ) {
191+ return ( LTR ? deltaX >= 0 : deltaX <= 0 ) ? 'end' : 'start' ;
192+ }
193+ return deltaY >= 0 ? 'bottom' : 'top' ;
194+ }
195+
167196 constructor (
168197 host : ReactiveControllerHost & LitElement ,
169198 options ?: DragControllerConfiguration
@@ -249,7 +278,11 @@ class DragController implements ReactiveController {
249278 this . _createDragGhost ( ) ;
250279 this . _updatePosition ( event ) ;
251280
252- const parameters = { event, state : this . _stateParameters } ;
281+ const parameters = {
282+ event,
283+ state : this . _stateParameters ,
284+ } ;
285+
253286 if ( this . _options . start ?. call ( this . _host , parameters ) === false ) {
254287 this . dispose ( ) ;
255288 return ;
@@ -267,7 +300,20 @@ class DragController implements ReactiveController {
267300 this . _updatePosition ( event ) ;
268301 this . _updateMatcher ( event ) ;
269302
270- const parameters = { event, state : this . _stateParameters } ;
303+ this . _state . pointerState . initial = {
304+ initialX : this . _state . pointerState . current . currentX ,
305+ initialY : this . _state . pointerState . current . currentY ,
306+ } ;
307+ this . _state . pointerState . current = {
308+ currentX : event . clientX ,
309+ currentY : event . clientY ,
310+ } ;
311+
312+ const parameters = {
313+ event,
314+ state : this . _stateParameters ,
315+ } ;
316+
271317 this . _options . move ?. call ( this . _host , parameters ) ;
272318
273319 this . _assignPosition ( this . _dragItem ) ;
@@ -313,6 +359,11 @@ class DragController implements ReactiveController {
313359 current : structuredClone ( rect ) ,
314360 position,
315361 offset,
362+ pointerState : {
363+ initial : { initialX : clientX , initialY : clientY } ,
364+ current : { currentX : clientX , currentY : clientY } ,
365+ direction : 'end' ,
366+ } ,
316367 } ;
317368 }
318369
0 commit comments