1313import { announce } from '@react-aria/live-announcer' ;
1414import { ariaHideOutside } from '@react-aria/overlays' ;
1515import { DragEndEvent , DragItem , DropActivateEvent , DropEnterEvent , DropEvent , DropExitEvent , DropItem , DropOperation , DropTarget as DroppableCollectionTarget , FocusableElement } from '@react-types/shared' ;
16- import { getActiveElement , isVirtualClick , isVirtualPointerEvent , nodeContains } from '@react-aria/utils' ;
16+ import { getActiveElement , getEventTarget , isVirtualClick , isVirtualPointerEvent , nodeContains } from '@react-aria/utils' ;
1717import { getDragModality , getTypes } from './utils' ;
1818import type { LocalizedStringFormatter } from '@internationalized/string' ;
1919import { RefObject , useEffect , useState } from 'react' ;
@@ -243,7 +243,7 @@ class DragSession {
243243 this . cancelEvent ( e ) ;
244244
245245 if ( e . key === 'Enter' ) {
246- if ( e . altKey || nodeContains ( this . getCurrentActivateButton ( ) , e . target as Node ) ) {
246+ if ( e . altKey || nodeContains ( this . getCurrentActivateButton ( ) , getEventTarget ( e ) as Node ) ) {
247247 this . activate ( this . currentDropTarget , this . currentDropItem ) ;
248248 } else {
249249 this . drop ( ) ;
@@ -257,25 +257,26 @@ class DragSession {
257257
258258 onFocus ( e : FocusEvent ) : void {
259259 let activateButton = this . getCurrentActivateButton ( ) ;
260- if ( e . target === activateButton ) {
260+ let eventTarget = getEventTarget ( e ) ;
261+ if ( eventTarget === activateButton ) {
261262 // TODO: canceling this breaks the focus ring. Revisit when we support tabbing.
262263 this . cancelEvent ( e ) ;
263264 return ;
264265 }
265266
266267 // Prevent focus events, except to the original drag target.
267- if ( e . target !== this . dragTarget . element ) {
268+ if ( eventTarget !== this . dragTarget . element ) {
268269 this . cancelEvent ( e ) ;
269270 }
270271
271272 // Ignore focus events on the window/document (JSDOM). Will be handled in onBlur, below.
272- if ( ! ( e . target instanceof HTMLElement ) || e . target === this . dragTarget . element ) {
273+ if ( ! ( eventTarget instanceof HTMLElement ) || eventTarget === this . dragTarget . element ) {
273274 return ;
274275 }
275276
276277 let dropTarget =
277- this . validDropTargets . find ( target => target . element === e . target as HTMLElement ) ||
278- this . validDropTargets . find ( target => nodeContains ( target . element , e . target as HTMLElement ) ) ;
278+ this . validDropTargets . find ( target => target . element === eventTarget ) ||
279+ this . validDropTargets . find ( target => nodeContains ( target . element , eventTarget ) ) ;
279280
280281 if ( ! dropTarget ) {
281282 // if (e.target === activateButton) {
@@ -289,7 +290,7 @@ class DragSession {
289290 return ;
290291 }
291292
292- let item = dropItems . get ( e . target as HTMLElement ) ;
293+ let item = dropItems . get ( eventTarget ) ;
293294 if ( dropTarget ) {
294295 this . setCurrentDropTarget ( dropTarget , item ) ;
295296 }
@@ -302,7 +303,7 @@ class DragSession {
302303 return ;
303304 }
304305
305- if ( e . target !== this . dragTarget . element ) {
306+ if ( getEventTarget ( e ) !== this . dragTarget . element ) {
306307 this . cancelEvent ( e ) ;
307308 }
308309
@@ -321,15 +322,16 @@ class DragSession {
321322 this . cancelEvent ( e ) ;
322323 if ( isVirtualClick ( e ) || this . isVirtualClick ) {
323324 let dropElements = dropItems . values ( ) ;
324- let item = [ ...dropElements ] . find ( item => item . element === e . target as HTMLElement || nodeContains ( item . activateButtonRef ?. current , e . target as HTMLElement ) ) ;
325- let dropTarget = this . validDropTargets . find ( target => nodeContains ( target . element , e . target as HTMLElement ) ) ;
325+ let eventTarget = getEventTarget ( e ) as HTMLElement ;
326+ let item = [ ...dropElements ] . find ( item => item . element === eventTarget || nodeContains ( item . activateButtonRef ?. current , eventTarget ) ) ;
327+ let dropTarget = this . validDropTargets . find ( target => nodeContains ( target . element , eventTarget ) ) ;
326328 let activateButton = item ?. activateButtonRef ?. current ?? dropTarget ?. activateButtonRef ?. current ;
327- if ( nodeContains ( activateButton , e . target as HTMLElement ) && dropTarget ) {
329+ if ( nodeContains ( activateButton , eventTarget ) && dropTarget ) {
328330 this . activate ( dropTarget , item ) ;
329331 return ;
330332 }
331333
332- if ( e . target === this . dragTarget . element ) {
334+ if ( getEventTarget ( e ) === this . dragTarget . element ) {
333335 this . cancel ( ) ;
334336 return ;
335337 }
@@ -350,7 +352,8 @@ class DragSession {
350352
351353 cancelEvent ( e : Event ) : void {
352354 // Allow focusin and focusout on the drag target so focus ring works properly.
353- if ( ( e . type === 'focusin' || e . type === 'focusout' ) && ( e . target === this . dragTarget ?. element || e . target === this . getCurrentActivateButton ( ) ) ) {
355+ let eventTarget = getEventTarget ( e ) ;
356+ if ( ( e . type === 'focusin' || e . type === 'focusout' ) && ( eventTarget === this . dragTarget ?. element || eventTarget === this . getCurrentActivateButton ( ) ) ) {
354357 return ;
355358 }
356359
0 commit comments