6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { Injectable , NgZone , OnDestroy , Inject } from '@angular/core' ;
9
+ import {
10
+ Injectable ,
11
+ NgZone ,
12
+ OnDestroy ,
13
+ Inject ,
14
+ inject ,
15
+ ApplicationRef ,
16
+ EnvironmentInjector ,
17
+ Component ,
18
+ ViewEncapsulation ,
19
+ ChangeDetectionStrategy ,
20
+ createComponent ,
21
+ } from '@angular/core' ;
10
22
import { DOCUMENT } from '@angular/common' ;
11
23
import { normalizePassiveListenerOptions } from '@angular/cdk/platform' ;
12
24
import { merge , Observable , Observer , Subject } from 'rxjs' ;
@@ -17,6 +29,23 @@ const activeCapturingEventOptions = normalizePassiveListenerOptions({
17
29
capture : true ,
18
30
} ) ;
19
31
32
+ /** Keeps track of the apps currently containing drag items. */
33
+ const activeApps = new Set < ApplicationRef > ( ) ;
34
+
35
+ /**
36
+ * Component used to load the drag&drop reset styles.
37
+ * @docs -private
38
+ */
39
+ @Component ( {
40
+ standalone : true ,
41
+ styleUrl : 'resets.css' ,
42
+ encapsulation : ViewEncapsulation . None ,
43
+ template : '' ,
44
+ changeDetection : ChangeDetectionStrategy . OnPush ,
45
+ host : { 'cdk-drag-resets-container' : '' } ,
46
+ } )
47
+ export class _ResetsLoader { }
48
+
20
49
/**
21
50
* Service that keeps track of all the drag item and drop container
22
51
* instances, and manages global event listeners on the `document`.
@@ -28,6 +57,8 @@ const activeCapturingEventOptions = normalizePassiveListenerOptions({
28
57
@Injectable ( { providedIn : 'root' } )
29
58
export class DragDropRegistry < I extends { isDragging ( ) : boolean } , C > implements OnDestroy {
30
59
private _document : Document ;
60
+ private _appRef = inject ( ApplicationRef ) ;
61
+ private _environmentInjector = inject ( EnvironmentInjector ) ;
31
62
32
63
/** Registered drop container instances. */
33
64
private _dropInstances = new Set < C > ( ) ;
@@ -136,6 +167,7 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
136
167
return ;
137
168
}
138
169
170
+ this . _loadResets ( ) ;
139
171
this . _activeDragInstances . push ( drag ) ;
140
172
141
173
if ( this . _activeDragInstances . length === 1 ) {
@@ -276,4 +308,23 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
276
308
277
309
this . _globalListeners . clear ( ) ;
278
310
}
311
+
312
+ // TODO(crisbeto): abstract this away into something reusable.
313
+ /** Loads the CSS resets needed for the module to work correctly. */
314
+ private _loadResets ( ) {
315
+ if ( ! activeApps . has ( this . _appRef ) ) {
316
+ activeApps . add ( this . _appRef ) ;
317
+
318
+ const componentRef = createComponent ( _ResetsLoader , {
319
+ environmentInjector : this . _environmentInjector ,
320
+ } ) ;
321
+
322
+ this . _appRef . onDestroy ( ( ) => {
323
+ activeApps . delete ( this . _appRef ) ;
324
+ if ( activeApps . size === 0 ) {
325
+ componentRef . destroy ( ) ;
326
+ }
327
+ } ) ;
328
+ }
329
+ }
279
330
}
0 commit comments