Skip to content

Commit

Permalink
fix(module:drawer): disable transition animation when placement change (
Browse files Browse the repository at this point in the history
#4609)

close #4224
  • Loading branch information
hsuanxyz authored Jan 8, 2020
1 parent a533980 commit e539096
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
2 changes: 2 additions & 0 deletions components/drawer/nz-drawer.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[class.ant-drawer-right]="nzPlacement === 'right'"
[class.ant-drawer-left]="nzPlacement === 'left'"
[style.transform]="offsetTransform"
[style.transition]="placementChanging ? 'none' : null"
[style.zIndex]="nzZIndex"
>
<div class="ant-drawer-mask" (click)="maskClick()" *ngIf="nzMask" [ngStyle]="nzMaskStyle"></div>
Expand All @@ -16,6 +17,7 @@
[style.width]="width"
[style.height]="height"
[style.transform]="transform"
[style.transition]="placementChanging ? 'none' : null"
>
<div class="ant-drawer-content">
<div class="ant-drawer-wrapper-body" [style.height]="isLeftOrRight ? '100%' : null">
Expand Down
22 changes: 21 additions & 1 deletion components/drawer/nz-drawer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export class NzDrawerComponent<T = any, R = any, D = any> extends NzDrawerRef<R>

destroy$ = new Subject<void>();
previouslyFocusedElement: HTMLElement;
placementChanging = false;
placementChangeTimeoutId = -1;
nzContentParams: D; // only service
overlayRef: OverlayRef | null;
portal: TemplatePortal;
Expand Down Expand Up @@ -190,26 +192,44 @@ export class NzDrawerComponent<T = any, R = any, D = any> extends NzDrawerRef<R>
}

ngOnChanges(changes: SimpleChanges): void {
if (changes.hasOwnProperty('nzVisible')) {
const { nzPlacement, nzVisible } = changes;
if (nzVisible) {
const value = changes.nzVisible.currentValue;
if (value) {
this.open();
} else {
this.close();
}
}
if (nzPlacement && !nzPlacement.isFirstChange()) {
this.triggerPlacementChangeCycleOnce();
}
}

ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
clearTimeout(this.placementChangeTimeoutId);
this.disposeOverlay();
}

private getAnimationDuration(): number {
return this.nzNoAnimation ? 0 : DRAWER_ANIMATE_DURATION;
}

// Disable the transition animation temporarily when the placement changing
private triggerPlacementChangeCycleOnce(): void {
if (!this.nzNoAnimation) {
this.placementChanging = true;
this.changeDetectorRef.markForCheck();
clearTimeout(this.placementChangeTimeoutId);
this.placementChangeTimeoutId = setTimeout(() => {
this.placementChanging = false;
this.changeDetectorRef.markForCheck();
}, this.getAnimationDuration());
}
}

close(result?: R): void {
this.isOpen = false;
this.updateOverlayStyle();
Expand Down
44 changes: 42 additions & 2 deletions components/drawer/nz-drawer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { async, ComponentFixture, fakeAsync, inject, TestBed, tick } from '@angu
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

import { dispatchKeyboardEvent } from 'ng-zorro-antd/core';
import { dispatchKeyboardEvent, NzNoAnimationModule } from 'ng-zorro-antd/core';

import { NzDrawerRef } from './nz-drawer-ref';
import { NzDrawerComponent } from './nz-drawer.component';
Expand All @@ -21,7 +21,7 @@ describe('NzDrawerComponent', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [NzDrawerModule, NoopAnimationsModule],
imports: [NzDrawerModule, NoopAnimationsModule, NzNoAnimationModule],
declarations: [NzTestDrawerComponent]
}).compileComponents();
}));
Expand Down Expand Up @@ -292,6 +292,44 @@ describe('NzDrawerComponent', () => {
fixture.detectChanges();
});

it('should disable the transition when the placement changing', fakeAsync(() => {
component.open();
tick(300);
fixture.detectChanges();
expect((overlayContainerElement.querySelector('.ant-drawer') as HTMLElement).style.transition).toBe('');
component.placement = 'top';
fixture.detectChanges();
expect((overlayContainerElement.querySelector('.ant-drawer') as HTMLElement).style.transition).toBe('none 0s ease 0s');
expect((overlayContainerElement.querySelector('.ant-drawer-content-wrapper') as HTMLElement).style.transition).toBe('none 0s ease 0s');
component.placement = 'right';
fixture.detectChanges();
component.close();
fixture.detectChanges();
tick(300);
fixture.detectChanges();
expect((overlayContainerElement.querySelector('.ant-drawer') as HTMLElement).style.transition).toBe('');
}));

it('should ignore set transition when `noAnimation` is `true` ', fakeAsync(() => {
component.noAnimation = true;
fixture.detectChanges();
component.open();
tick(300);
fixture.detectChanges();
expect((overlayContainerElement.querySelector('.ant-drawer') as HTMLElement).style.transition).toBe('');
component.placement = 'top';
fixture.detectChanges();
expect((overlayContainerElement.querySelector('.ant-drawer') as HTMLElement).style.transition).toBe('');
expect((overlayContainerElement.querySelector('.ant-drawer-content-wrapper') as HTMLElement).style.transition).toBe('');
fixture.detectChanges();
component.close();
component.placement = 'right';
component.noAnimation = false;
fixture.detectChanges();
tick(300);
fixture.detectChanges();
}));

it('should nzOffsetX work', () => {
component.open();
component.placement = 'left';
Expand Down Expand Up @@ -448,6 +486,7 @@ describe('NzDrawerService', () => {
[nzWidth]="width"
[nzHeight]="height"
[nzPlacement]="placement"
[nzNoAnimation]="noAnimation"
[nzTitle]="title"
[nzOffsetX]="offsetX"
[nzOffsetY]="offsetY"
Expand All @@ -469,6 +508,7 @@ class NzTestDrawerComponent {
width: string | number = '300px';
height: string | number = '300px';
placement = 'left';
noAnimation = false;
offsetX = 0;
offsetY = 0;
@ViewChild('customTitle', { static: false }) templateTitle: TemplateRef<void>;
Expand Down

0 comments on commit e539096

Please sign in to comment.