Skip to content

Commit da65ef2

Browse files
committed
refactor(cdk/drag-drop): use common service for loading structural styles
Switches the drag&drop module to use the common service to load the preview styles.
1 parent 5e82075 commit da65ef2

File tree

2 files changed

+6
-30
lines changed

2 files changed

+6
-30
lines changed

src/cdk/drag-drop/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ ng_module(
2424
"//src/cdk/bidi",
2525
"//src/cdk/coercion",
2626
"//src/cdk/platform",
27+
"//src/cdk/private",
2728
"//src/cdk/scrolling",
2829
"@npm//@angular/common",
2930
"@npm//@angular/core",

src/cdk/drag-drop/drag-drop-registry.ts

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,21 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {normalizePassiveListenerOptions} from '@angular/cdk/platform';
10-
import {DOCUMENT} from '@angular/common';
119
import {
12-
ApplicationRef,
1310
ChangeDetectionStrategy,
1411
Component,
15-
EnvironmentInjector,
1612
Inject,
1713
Injectable,
1814
NgZone,
1915
OnDestroy,
2016
ViewEncapsulation,
2117
WritableSignal,
22-
createComponent,
2318
inject,
2419
signal,
2520
} from '@angular/core';
21+
import {DOCUMENT} from '@angular/common';
22+
import {normalizePassiveListenerOptions} from '@angular/cdk/platform';
23+
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
2624
import {Observable, Observer, Subject, merge} from 'rxjs';
2725
import type {DropListRef} from './drop-list-ref';
2826
import type {DragRef} from './drag-ref';
@@ -33,9 +31,6 @@ const activeCapturingEventOptions = normalizePassiveListenerOptions({
3331
capture: true,
3432
});
3533

36-
/** Keeps track of the apps currently containing drag items. */
37-
const activeApps = new Set<ApplicationRef>();
38-
3934
/**
4035
* Component used to load the drag&drop reset styles.
4136
* @docs-private
@@ -59,8 +54,7 @@ export class _ResetsLoader {}
5954
@Injectable({providedIn: 'root'})
6055
export class DragDropRegistry<_ = unknown, __ = unknown> implements OnDestroy {
6156
private _document: Document;
62-
private _appRef = inject(ApplicationRef);
63-
private _environmentInjector = inject(EnvironmentInjector);
57+
private _styleLoader = inject(_CdkPrivateStyleLoader);
6458

6559
/** Registered drop container instances. */
6660
private _dropInstances = new Set<DropListRef>();
@@ -169,7 +163,7 @@ export class DragDropRegistry<_ = unknown, __ = unknown> implements OnDestroy {
169163
return;
170164
}
171165

172-
this._loadResets();
166+
this._styleLoader.load(_ResetsLoader);
173167
this._activeDragInstances.update(instances => [...instances, drag]);
174168

175169
if (this._activeDragInstances().length === 1) {
@@ -313,23 +307,4 @@ export class DragDropRegistry<_ = unknown, __ = unknown> implements OnDestroy {
313307

314308
this._globalListeners.clear();
315309
}
316-
317-
// TODO(crisbeto): abstract this away into something reusable.
318-
/** Loads the CSS resets needed for the module to work correctly. */
319-
private _loadResets() {
320-
if (!activeApps.has(this._appRef)) {
321-
activeApps.add(this._appRef);
322-
323-
const componentRef = createComponent(_ResetsLoader, {
324-
environmentInjector: this._environmentInjector,
325-
});
326-
327-
this._appRef.onDestroy(() => {
328-
activeApps.delete(this._appRef);
329-
if (activeApps.size === 0) {
330-
componentRef.destroy();
331-
}
332-
});
333-
}
334-
}
335310
}

0 commit comments

Comments
 (0)