@@ -48,8 +48,7 @@ import type {CdkDropList} from './drop-list';
4848import { DragDrop } from '../drag-drop' ;
4949import { CDK_DRAG_CONFIG , DragDropConfig , DragStartDelay , DragAxis } from './config' ;
5050import { assertElementNode } from './assertions' ;
51-
52- const DRAG_HOST_CLASS = 'cdk-drag' ;
51+ import { DragDropRegistry } from '../drag-drop-registry' ;
5352
5453/**
5554 * Injection token that can be used to reference instances of `CdkDropList`. It serves as
@@ -63,7 +62,7 @@ export const CDK_DROP_LIST = new InjectionToken<CdkDropList>('CdkDropList');
6362 selector : '[cdkDrag]' ,
6463 exportAs : 'cdkDrag' ,
6564 host : {
66- 'class' : DRAG_HOST_CLASS ,
65+ 'class' : 'cdk-drag' ,
6766 '[class.cdk-drag-disabled]' : 'disabled' ,
6867 '[class.cdk-drag-dragging]' : '_dragRef.isDragging()' ,
6968 } ,
@@ -78,9 +77,9 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
7877 private _changeDetectorRef = inject ( ChangeDetectorRef ) ;
7978 private _selfHandle = inject < CdkDragHandle > ( CDK_DRAG_HANDLE , { optional : true , self : true } ) ;
8079 private _parentDrag = inject < CdkDrag > ( CDK_DRAG_PARENT , { optional : true , skipSelf : true } ) ;
80+ private _dragDropRegistry = inject ( DragDropRegistry ) ;
8181
8282 private readonly _destroyed = new Subject < void > ( ) ;
83- private static _dragInstances : CdkDrag [ ] = [ ] ;
8483 private _handles = new BehaviorSubject < CdkDragHandle [ ] > ( [ ] ) ;
8584 private _previewTemplate : CdkDragPreview | null ;
8685 private _placeholderTemplate : CdkDragPlaceholder | null ;
@@ -240,11 +239,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
240239 zIndex : config ?. zIndex ,
241240 } ) ;
242241 this . _dragRef . data = this ;
243-
244- // We have to keep track of the drag instances in order to be able to match an element to
245- // a drag instance. We can't go through the global registry of `DragRef`, because the root
246- // element could be different.
247- CdkDrag . _dragInstances . push ( this ) ;
242+ this . _dragDropRegistry . registerDirectiveNode ( this . element . nativeElement , this ) ;
248243
249244 if ( config ) {
250245 this . _assignDefaults ( config ) ;
@@ -348,10 +343,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
348343 this . dropContainer . removeItem ( this ) ;
349344 }
350345
351- const index = CdkDrag . _dragInstances . indexOf ( this ) ;
352- if ( index > - 1 ) {
353- CdkDrag . _dragInstances . splice ( index , 1 ) ;
354- }
346+ this . _dragDropRegistry . removeDirectiveNode ( this . element . nativeElement ) ;
355347
356348 // Unnecessary in most cases, but used to avoid extra change detections with `zone-paths-rxjs`.
357349 this . _ngZone . runOutsideAngular ( ( ) => {
@@ -487,12 +479,9 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
487479 // the item was projected into another item by something like `ngTemplateOutlet`.
488480 let parent = this . element . nativeElement . parentElement ;
489481 while ( parent ) {
490- if ( parent . classList . contains ( DRAG_HOST_CLASS ) ) {
491- ref . withParent (
492- CdkDrag . _dragInstances . find ( drag => {
493- return drag . element . nativeElement === parent ;
494- } ) ?. _dragRef || null ,
495- ) ;
482+ const parentDrag = this . _dragDropRegistry . getDragDirectiveForNode ( parent ) ;
483+ if ( parentDrag ) {
484+ ref . withParent ( parentDrag . _dragRef ) ;
496485 break ;
497486 }
498487 parent = parent . parentElement ;
0 commit comments