Skip to content

fix(cdk/drag-drop): error if preview dimensions are accessed too early #24498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export class DragRef<T = any> {
/** Whether the native dragging interactions have been enabled on the root element. */
private _nativeInteractionsEnabled = true;

/** Cached dimensions of the preview element. */
/** Cached dimensions of the preview element. Should be read via `_getPreviewRect`. */
private _previewRect?: ClientRect;

/** Cached dimensions of the boundary element. */
Expand Down Expand Up @@ -686,15 +686,6 @@ export class DragRef<T = any> {
return;
}

// We only need the preview dimensions if we have a boundary element.
if (this._boundaryElement) {
// Cache the preview element rect if we haven't cached it already or if
// we cached it too early before the element dimensions were computed.
if (!this._previewRect || (!this._previewRect.width && !this._previewRect.height)) {
this._previewRect = (this._preview || this._rootElement).getBoundingClientRect();
}
}

// We prevent the default action down here so that we know that dragging has started. This is
// important for touch devices where doing this too early can unnecessarily block scrolling,
// if there's a dragging delay.
Expand Down Expand Up @@ -1246,11 +1237,11 @@ export class DragRef<T = any> {
if (this._boundaryRect) {
const {x: pickupX, y: pickupY} = this._pickupPositionInElement;
const boundaryRect = this._boundaryRect;
const previewRect = this._previewRect!;
const {width: previewWidth, height: previewHeight} = this._getPreviewRect();
const minY = boundaryRect.top + pickupY;
const maxY = boundaryRect.bottom - (previewRect.height - pickupY);
const maxY = boundaryRect.bottom - (previewHeight - pickupY);
const minX = boundaryRect.left + pickupX;
const maxX = boundaryRect.right - (previewRect.width - pickupX);
const maxX = boundaryRect.right - (previewWidth - pickupX);

x = clamp(x, minX, maxX);
y = clamp(y, minY, maxY);
Expand Down Expand Up @@ -1518,6 +1509,17 @@ export class DragRef<T = any> {

return coerceElement(previewContainer);
}

/** Lazily resolves and returns the dimensions of the preview. */
private _getPreviewRect(): ClientRect {
// Cache the preview element rect if we haven't cached it already or if
// we cached it too early before the element dimensions were computed.
if (!this._previewRect || (!this._previewRect.width && !this._previewRect.height)) {
this._previewRect = (this._preview || this._rootElement).getBoundingClientRect();
}

return this._previewRect;
}
}

/**
Expand Down