Skip to content

Commit

Permalink
fix(cdk/drag-drop): optionally inject parent drag in preview and plac…
Browse files Browse the repository at this point in the history
…eholder (#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)
  • Loading branch information
crisbeto committed Mar 20, 2024
1 parent 85b9424 commit 31e3088
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/cdk/drag-drop/directives/drag-placeholder.ts
Expand Up @@ -26,16 +26,16 @@ export const CDK_DRAG_PLACEHOLDER = new InjectionToken<CdkDragPlaceholder>('CdkD
providers: [{provide: CDK_DRAG_PLACEHOLDER, useExisting: CdkDragPlaceholder}],
})
export class CdkDragPlaceholder<T = any> implements OnDestroy {
private _drag = inject(CDK_DRAG_PARENT);
private _drag = inject(CDK_DRAG_PARENT, {optional: true});

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

constructor(public templateRef: TemplateRef<T>) {
this._drag._setPlaceholderTemplate(this);
this._drag?._setPlaceholderTemplate(this);
}

ngOnDestroy(): void {
this._drag._resetPlaceholderTemplate(this);
this._drag?._resetPlaceholderTemplate(this);
}
}
6 changes: 3 additions & 3 deletions src/cdk/drag-drop/directives/drag-preview.ts
Expand Up @@ -34,7 +34,7 @@ export const CDK_DRAG_PREVIEW = new InjectionToken<CdkDragPreview>('CdkDragPrevi
providers: [{provide: CDK_DRAG_PREVIEW, useExisting: CdkDragPreview}],
})
export class CdkDragPreview<T = any> implements OnDestroy {
private _drag = inject(CDK_DRAG_PARENT);
private _drag = inject(CDK_DRAG_PARENT, {optional: true});

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

constructor(public templateRef: TemplateRef<T>) {
this._drag._setPreviewTemplate(this);
this._drag?._setPreviewTemplate(this);
}

ngOnDestroy(): void {
this._drag._resetPreviewTemplate(this);
this._drag?._resetPreviewTemplate(this);
}
}

0 comments on commit 31e3088

Please sign in to comment.