Skip to content

Commit c4045e7

Browse files
committed
perf(cdk-experimental/column-resize): add debounce to column header hover to prevent excessive handler rendering
Improve the user experience and performance of the `cdk-experimental` column resize feature by adding a `debounceTime` operator to the `headerCellHoveredDistinct` observable. Previously, the hover event triggered the handler immediately, which could result in unwanted handlers showing up when the user quickly moved their cursor over column headers. This change ensures that the handlers only appear if the user pauses (hovers for 300ms) over the column header, preventing handlers from rendering during fast cursor movements.
1 parent ba5cf3d commit c4045e7

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/cdk-experimental/column-resize/event-dispatcher.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {Injectable, NgZone, inject} from '@angular/core';
1010
import {combineLatest, MonoTypeOperatorFunction, Observable, Subject} from 'rxjs';
11-
import {distinctUntilChanged, map, share, skip, startWith} from 'rxjs/operators';
11+
import {distinctUntilChanged, map, share, skip, startWith, debounceTime} from 'rxjs/operators';
1212

1313
import {_closest} from '../popover-edit';
1414

@@ -33,7 +33,11 @@ export class HeaderRowEventDispatcher {
3333
readonly overlayHandleActiveForCell = new Subject<Element | null>();
3434

3535
/** Distinct and shared version of headerCellHovered. */
36-
readonly headerCellHoveredDistinct = this.headerCellHovered.pipe(distinctUntilChanged(), share());
36+
readonly headerCellHoveredDistinct = this.headerCellHovered.pipe(
37+
distinctUntilChanged(),
38+
debounceTime(200),
39+
share(),
40+
);
3741

3842
/**
3943
* Emits the header that is currently hovered or hosting an active resize event (with active

src/material-experimental/column-resize/column-resize.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
Injectable,
1010
ViewChild,
1111
} from '@angular/core';
12-
import {ComponentFixture, TestBed, fakeAsync, flush} from '@angular/core/testing';
12+
import {ComponentFixture, TestBed, fakeAsync, flush, tick} from '@angular/core/testing';
1313
import {MatTableModule} from '@angular/material/table';
1414
import {BehaviorSubject, Observable, ReplaySubject} from 'rxjs';
1515
import {dispatchKeyboardEvent} from '../../cdk/testing/private';
@@ -401,6 +401,7 @@ describe('Material Popover Edit', () => {
401401

402402
component.triggerHoverState();
403403
fixture.detectChanges();
404+
tick(200);
404405

405406
expect(
406407
component.getOverlayThumbElement(0).classList.contains('mat-column-resize-overlay-thumb'),
@@ -438,6 +439,7 @@ describe('Material Popover Edit', () => {
438439
component.completeResizeWithMouseInProgress(0);
439440
component.endHoverState();
440441
fixture.detectChanges();
442+
tick(200);
441443
flush();
442444

443445
expect(component.getOverlayThumbElement(0)).toBeUndefined();
@@ -451,6 +453,7 @@ describe('Material Popover Edit', () => {
451453

452454
component.triggerHoverState();
453455
fixture.detectChanges();
456+
tick(200);
454457
component.beginColumnResizeWithMouse(1);
455458

456459
const initialThumbPosition = component.getOverlayThumbPosition(1);
@@ -500,6 +503,7 @@ describe('Material Popover Edit', () => {
500503

501504
component.triggerHoverState();
502505
fixture.detectChanges();
506+
tick(200);
503507
component.beginColumnResizeWithMouse(1);
504508

505509
const initialThumbPosition = component.getOverlayThumbPosition(1);
@@ -535,6 +539,7 @@ describe('Material Popover Edit', () => {
535539

536540
component.triggerHoverState();
537541
fixture.detectChanges();
542+
tick(200);
538543
component.beginColumnResizeWithMouse(1, 2);
539544

540545
const initialPosition = component.getOverlayThumbPosition(1);
@@ -552,6 +557,7 @@ describe('Material Popover Edit', () => {
552557

553558
component.triggerHoverState();
554559
fixture.detectChanges();
560+
tick(200);
555561
component.beginColumnResizeWithMouse(1);
556562

557563
const initialThumbPosition = component.getOverlayThumbPosition(1);
@@ -589,6 +595,7 @@ describe('Material Popover Edit', () => {
589595

590596
component.triggerHoverState();
591597
fixture.detectChanges();
598+
tick(200);
592599

593600
expect(resize).toBe(null);
594601

@@ -610,6 +617,7 @@ describe('Material Popover Edit', () => {
610617

611618
component.triggerHoverState();
612619
fixture.detectChanges();
620+
tick(200);
613621
component.beginColumnResizeWithMouse(0);
614622

615623
component.updateResizeWithMouseInProgress(5);
@@ -674,6 +682,7 @@ describe('Material Popover Edit', () => {
674682

675683
component.triggerHoverState();
676684
fixture.detectChanges();
685+
tick(200);
677686

678687
component.resizeColumnWithMouse(1, 5);
679688
fixture.detectChanges();
@@ -699,6 +708,7 @@ describe('Material Popover Edit', () => {
699708

700709
component.triggerHoverState();
701710
fixture.detectChanges();
711+
tick(200);
702712

703713
component.resizeColumnWithMouse(1, 5);
704714
fixture.detectChanges();

0 commit comments

Comments
 (0)