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:carousel): do not run change detection on non-handled keydown events #7097

Merged
merged 1 commit into from Dec 17, 2021
Merged
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
52 changes: 33 additions & 19 deletions components/carousel/carousel.component.ts
Expand Up @@ -17,6 +17,7 @@ import {
EventEmitter,
Inject,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Expand All @@ -29,7 +30,7 @@ import {
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { Subject } from 'rxjs';
import { fromEvent, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
Expand Down Expand Up @@ -67,7 +68,6 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'carousel';
#slickList
class="slick-list"
tabindex="-1"
(keydown)="onKeyDown($event)"
(mousedown)="pointerDown($event)"
(touchstart)="pointerDown($event)"
>
Expand Down Expand Up @@ -117,8 +117,8 @@ export class NzCarouselComponent implements AfterContentInit, AfterViewInit, OnD

@ContentChildren(NzCarouselContentDirective) carouselContents!: QueryList<NzCarouselContentDirective>;

@ViewChild('slickList', { static: false }) slickList?: ElementRef;
@ViewChild('slickTrack', { static: false }) slickTrack?: ElementRef;
@ViewChild('slickList', { static: true }) slickList!: ElementRef<HTMLElement>;
@ViewChild('slickTrack', { static: true }) slickTrack!: ElementRef<HTMLElement>;

@Input() nzDotRender?: TemplateRef<{ $implicit: number }>;
@Input() @WithConfig() nzEffect: NzCarouselEffects = 'scrollx';
Expand Down Expand Up @@ -172,6 +172,7 @@ export class NzCarouselComponent implements AfterContentInit, AfterViewInit, OnD
constructor(
elementRef: ElementRef,
public readonly nzConfigService: NzConfigService,
private readonly ngZone: NgZone,
private readonly renderer: Renderer2,
private readonly cdr: ChangeDetectorRef,
private readonly platform: Platform,
Expand All @@ -186,24 +187,47 @@ export class NzCarouselComponent implements AfterContentInit, AfterViewInit, OnD
this.el = elementRef.nativeElement;
}
ngOnInit(): void {
this.slickListEl = this.slickList!.nativeElement;
this.slickTrackEl = this.slickTrack!.nativeElement;

this.dir = this.directionality.value;

this.directionality.change?.pipe(takeUntil(this.destroy$)).subscribe((direction: Direction) => {
this.directionality.change.pipe(takeUntil(this.destroy$)).subscribe((direction: Direction) => {
this.dir = direction;
this.markContentActive(this.activeIndex);
this.cdr.detectChanges();
});

this.ngZone.runOutsideAngular(() => {
fromEvent<KeyboardEvent>(this.slickListEl, 'keydown')
.pipe(takeUntil(this.destroy$))
.subscribe(event => {
const { keyCode } = event;

if (keyCode !== LEFT_ARROW && keyCode !== RIGHT_ARROW) {
return;
}

event.preventDefault();

this.ngZone.run(() => {
if (keyCode === LEFT_ARROW) {
this.pre();
} else {
this.next();
}
this.cdr.markForCheck();
});
});
});
}

ngAfterContentInit(): void {
this.markContentActive(0);
}

ngAfterViewInit(): void {
this.slickListEl = this.slickList!.nativeElement;
this.slickTrackEl = this.slickTrack!.nativeElement;

this.carouselContents.changes.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.carouselContents.changes.subscribe(() => {
this.markContentActive(0);
this.layout();
});
Expand Down Expand Up @@ -259,16 +283,6 @@ export class NzCarouselComponent implements AfterContentInit, AfterViewInit, OnD
this.destroy$.complete();
}

onKeyDown(e: KeyboardEvent): void {
if (e.keyCode === LEFT_ARROW) {
e.preventDefault();
this.pre();
} else if (e.keyCode === RIGHT_ARROW) {
this.next();
e.preventDefault();
}
}

onLiClick = (index: number): void => {
if (this.dir === 'rtl') {
this.goTo(this.carouselContents.length - 1 - index);
Expand Down