Skip to content

Commit 31e3088

Browse files
committed
fix(cdk/drag-drop): optionally inject parent drag in preview and placeholder (#28750)
Fixes a regression from #28633 where using `cdkDragPlaceholder` or `cdkDragPreview` without a `cdkDrag` would throw. Technically this is a no-op, but it appears that folks started depending on the old behavior so these changes bring it back. Fixes #28744. (cherry picked from commit 9343131)
1 parent 85b9424 commit 31e3088

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ export const CDK_DRAG_PLACEHOLDER = new InjectionToken<CdkDragPlaceholder>('CdkD
2626
providers: [{provide: CDK_DRAG_PLACEHOLDER, useExisting: CdkDragPlaceholder}],
2727
})
2828
export class CdkDragPlaceholder<T = any> implements OnDestroy {
29-
private _drag = inject(CDK_DRAG_PARENT);
29+
private _drag = inject(CDK_DRAG_PARENT, {optional: true});
3030

3131
/** Context data to be added to the placeholder template instance. */
3232
@Input() data: T;
3333

3434
constructor(public templateRef: TemplateRef<T>) {
35-
this._drag._setPlaceholderTemplate(this);
35+
this._drag?._setPlaceholderTemplate(this);
3636
}
3737

3838
ngOnDestroy(): void {
39-
this._drag._resetPlaceholderTemplate(this);
39+
this._drag?._resetPlaceholderTemplate(this);
4040
}
4141
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const CDK_DRAG_PREVIEW = new InjectionToken<CdkDragPreview>('CdkDragPrevi
3434
providers: [{provide: CDK_DRAG_PREVIEW, useExisting: CdkDragPreview}],
3535
})
3636
export class CdkDragPreview<T = any> implements OnDestroy {
37-
private _drag = inject(CDK_DRAG_PARENT);
37+
private _drag = inject(CDK_DRAG_PARENT, {optional: true});
3838

3939
/** Context data to be added to the preview template instance. */
4040
@Input() data: T;
@@ -43,10 +43,10 @@ export class CdkDragPreview<T = any> implements OnDestroy {
4343
@Input({transform: booleanAttribute}) matchSize: boolean = false;
4444

4545
constructor(public templateRef: TemplateRef<T>) {
46-
this._drag._setPreviewTemplate(this);
46+
this._drag?._setPreviewTemplate(this);
4747
}
4848

4949
ngOnDestroy(): void {
50-
this._drag._resetPreviewTemplate(this);
50+
this._drag?._resetPreviewTemplate(this);
5151
}
5252
}

0 commit comments

Comments
 (0)