Skip to content

Commit a3cdb16

Browse files
authored
fix(cdk/drag-drop): defer loading reset styles (#29056)
Currently we load the reset styles for the preview up-front which seems to break some Jest tests since Jest's CSS parser does't understand layers. These changes switch to loading the styles only once dragging has started. Fixes #29053.
1 parent ad7a14b commit a3cdb16

File tree

3 files changed

+54
-55
lines changed

3 files changed

+54
-55
lines changed

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

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';
1022
import {DOCUMENT} from '@angular/common';
1123
import {normalizePassiveListenerOptions} from '@angular/cdk/platform';
1224
import {merge, Observable, Observer, Subject} from 'rxjs';
@@ -17,6 +29,23 @@ const activeCapturingEventOptions = normalizePassiveListenerOptions({
1729
capture: true,
1830
});
1931

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+
2049
/**
2150
* Service that keeps track of all the drag item and drop container
2251
* instances, and manages global event listeners on the `document`.
@@ -28,6 +57,8 @@ const activeCapturingEventOptions = normalizePassiveListenerOptions({
2857
@Injectable({providedIn: 'root'})
2958
export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements OnDestroy {
3059
private _document: Document;
60+
private _appRef = inject(ApplicationRef);
61+
private _environmentInjector = inject(EnvironmentInjector);
3162

3263
/** Registered drop container instances. */
3364
private _dropInstances = new Set<C>();
@@ -136,6 +167,7 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
136167
return;
137168
}
138169

170+
this._loadResets();
139171
this._activeDragInstances.push(drag);
140172

141173
if (this._activeDragInstances.length === 1) {
@@ -276,4 +308,23 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
276308

277309
this._globalListeners.clear();
278310
}
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+
}
279330
}

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

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {
10-
Injectable,
11-
Inject,
12-
NgZone,
13-
ElementRef,
14-
Component,
15-
ViewEncapsulation,
16-
ChangeDetectionStrategy,
17-
ApplicationRef,
18-
inject,
19-
createComponent,
20-
EnvironmentInjector,
21-
} from '@angular/core';
9+
import {Injectable, Inject, NgZone, ElementRef} from '@angular/core';
2210
import {DOCUMENT} from '@angular/common';
2311
import {ViewportRuler} from '@angular/cdk/scrolling';
2412
import {DragRef, DragRefConfig} from './drag-ref';
@@ -31,31 +19,11 @@ const DEFAULT_CONFIG = {
3119
pointerDirectionChangeThreshold: 5,
3220
};
3321

34-
/** Keeps track of the apps currently containing badges. */
35-
const activeApps = new Set<ApplicationRef>();
36-
37-
/**
38-
* Component used to load the drag&drop reset styles.
39-
* @docs-private
40-
*/
41-
@Component({
42-
standalone: true,
43-
styleUrl: 'resets.css',
44-
encapsulation: ViewEncapsulation.None,
45-
template: '',
46-
changeDetection: ChangeDetectionStrategy.OnPush,
47-
host: {'cdk-drag-resets-container': ''},
48-
})
49-
export class _ResetsLoader {}
50-
5122
/**
5223
* Service that allows for drag-and-drop functionality to be attached to DOM elements.
5324
*/
5425
@Injectable({providedIn: 'root'})
5526
export class DragDrop {
56-
private _appRef = inject(ApplicationRef);
57-
private _environmentInjector = inject(EnvironmentInjector);
58-
5927
constructor(
6028
@Inject(DOCUMENT) private _document: any,
6129
private _ngZone: NgZone,
@@ -72,7 +40,6 @@ export class DragDrop {
7240
element: ElementRef<HTMLElement> | HTMLElement,
7341
config: DragRefConfig = DEFAULT_CONFIG,
7442
): DragRef<T> {
75-
this._loadResets();
7643
return new DragRef<T>(
7744
element,
7845
config,
@@ -96,23 +63,4 @@ export class DragDrop {
9663
this._viewportRuler,
9764
);
9865
}
99-
100-
// TODO(crisbeto): abstract this away into something reusable.
101-
/** Loads the CSS resets needed for the module to work correctly. */
102-
private _loadResets() {
103-
if (!activeApps.has(this._appRef)) {
104-
activeApps.add(this._appRef);
105-
106-
const componentRef = createComponent(_ResetsLoader, {
107-
environmentInjector: this._environmentInjector,
108-
});
109-
110-
this._appRef.onDestroy(() => {
111-
activeApps.delete(this._appRef);
112-
if (activeApps.size === 0) {
113-
componentRef.destroy();
114-
}
115-
});
116-
}
117-
}
11866
}

src/cdk/drag-drop/public-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export {CDK_DRAG_PARENT} from './drag-parent';
1414
export * from './drag-events';
1515
export * from './drag-utils';
1616
export * from './drag-drop-module';
17-
export * from './drag-drop-registry';
17+
export {DragDropRegistry} from './drag-drop-registry';
1818

1919
export {CdkDropList} from './directives/drop-list';
2020
export * from './directives/config';

0 commit comments

Comments
 (0)