Skip to content

Commit d72e16b

Browse files
committed
perf: only recalculate the bounding rectangle once after scrolling
1 parent 7fcae02 commit d72e16b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/droppable.directive.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,35 @@ export class DroppableDirective implements OnInit, OnDestroy {
8383
this.element.nativeElement,
8484
this.dragActiveClass
8585
);
86-
let droppableRectangle = this.element.nativeElement.getBoundingClientRect();
86+
const droppableRectangle: {
87+
cache?: ClientRect;
88+
updateCache: boolean;
89+
} = {
90+
updateCache: true
91+
};
8792

8893
const deregisterScrollListener = this.renderer.listen(
8994
this.scrollContainer
9095
? this.scrollContainer.elementRef.nativeElement
9196
: 'window',
9297
'scroll',
9398
() => {
94-
droppableRectangle = this.element.nativeElement.getBoundingClientRect();
99+
droppableRectangle.updateCache = true;
95100
}
96101
);
97102

98103
let currentDragDropData: any;
99104
const overlaps$ = drag$.pipe(
100105
map(({ clientX, clientY, dropData }) => {
101106
currentDragDropData = dropData;
107+
if (droppableRectangle.updateCache) {
108+
droppableRectangle.cache = this.element.nativeElement.getBoundingClientRect();
109+
droppableRectangle.updateCache = false;
110+
}
102111
return isCoordinateWithinRectangle(
103112
clientX,
104113
clientY,
105-
droppableRectangle
114+
droppableRectangle.cache as ClientRect
106115
);
107116
})
108117
);

0 commit comments

Comments
 (0)