Skip to content
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

perf(module:dropdown): do not run change detection if the dropdown has been clicked inside #7135

Merged
merged 1 commit into from Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion components/dropdown/context-menu.service.spec.ts
@@ -1,5 +1,5 @@
import { OverlayContainer, ScrollDispatcher } from '@angular/cdk/overlay';
import { Component, Provider, Type, ViewChild } from '@angular/core';
import { ApplicationRef, Component, Provider, Type, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, inject, TestBed, tick } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { Subject } from 'rxjs';
Expand Down Expand Up @@ -119,6 +119,22 @@ describe('context-menu', () => {
expect(overlayContainerElement.textContent).toBe('');
}).not.toThrowError();
}));
it('should not run change detection if the overlay is clicked inside', async () => {
const fixture = createComponent(NzTestDropdownContextMenuComponent, [], []);
fixture.detectChanges();
const fakeEvent = createMouseEvent('contextmenu', 300, 300);
const component = fixture.componentInstance;
component.nzContextMenuService.create(fakeEvent, component.nzDropdownMenuComponent);
fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
const appRef = TestBed.inject(ApplicationRef);
spyOn(appRef, 'tick');
overlayContainerElement.querySelector('ul')!.click();
expect(appRef.tick).toHaveBeenCalledTimes(0);
document.body.click();
expect(appRef.tick).toHaveBeenCalledTimes(1);
});
});

@Component({
Expand Down
33 changes: 20 additions & 13 deletions components/dropdown/context-menu.service.ts
Expand Up @@ -5,8 +5,8 @@

import { ConnectionPositionPair, Overlay, OverlayRef } from '@angular/cdk/overlay';
import { TemplatePortal } from '@angular/cdk/portal';
import { Injectable } from '@angular/core';
import { fromEvent, merge, Subscription } from 'rxjs';
import { Injectable, NgZone } from '@angular/core';
import { fromEvent, Subscription } from 'rxjs';
import { filter, take } from 'rxjs/operators';

import { NzContextMenuServiceModule } from './context-menu.service.module';
Expand All @@ -26,7 +26,7 @@ export class NzContextMenuService {
private overlayRef: OverlayRef | null = null;
private closeSubscription = Subscription.EMPTY;

constructor(private overlay: Overlay) {}
constructor(private ngZone: NgZone, private overlay: Overlay) {}

create($event: MouseEvent | { x: number; y: number }, nzDropdownMenuComponent: NzDropdownMenuComponent): void {
this.close(true);
Expand All @@ -44,17 +44,24 @@ export class NzContextMenuService {
disposeOnNavigation: true,
scrollStrategy: this.overlay.scrollStrategies.close()
});
this.closeSubscription = merge(
nzDropdownMenuComponent.descendantMenuItemClick$,
fromEvent<MouseEvent>(document, 'click').pipe(
filter(event => !!this.overlayRef && !this.overlayRef.overlayElement.contains(event.target as HTMLElement)),
/** handle firefox contextmenu event **/
filter(event => event.button !== 2),
take(1)

this.closeSubscription = new Subscription();

this.closeSubscription.add(nzDropdownMenuComponent.descendantMenuItemClick$.subscribe(() => this.close()));

this.closeSubscription.add(
this.ngZone.runOutsideAngular(() =>
fromEvent<MouseEvent>(document, 'click')
.pipe(
filter(event => !!this.overlayRef && !this.overlayRef.overlayElement.contains(event.target as HTMLElement)),
/** handle firefox contextmenu event **/
filter(event => event.button !== 2),
take(1)
)
.subscribe(() => this.ngZone.run(() => this.close()))
)
).subscribe(() => {
this.close();
});
);

this.overlayRef.attach(
new TemplatePortal(nzDropdownMenuComponent.templateRef, nzDropdownMenuComponent.viewContainerRef)
);
Expand Down