Skip to content

Commit

Permalink
fix(module:drawer): provide custom scroll strategy (#2095)
Browse files Browse the repository at this point in the history
close #2070
  • Loading branch information
hsuanxyz authored and Jason committed Sep 7, 2018
1 parent e438cff commit b993068
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
19 changes: 19 additions & 0 deletions components/core/overlay/scroll/nz-block-scroll-strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ScrollStrategy } from '@angular/cdk/overlay';
import { Renderer2 } from '@angular/core';

export class NzBlockScrollStrategy implements ScrollStrategy {

constructor(private document: Document, private renderer: Renderer2) {
}

attach(): void {}

enable(): void {
this.renderer.setStyle(document.body, 'overflow', 'hidden');
}

disable(): void {
this.renderer.removeStyle(document.body, 'overflow');
}

}
19 changes: 19 additions & 0 deletions components/core/overlay/scroll/nz-scroll-strategy-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable, Renderer2, RendererFactory2 } from '@angular/core';
import { NzBlockScrollStrategy } from './nz-block-scroll-strategy';

@Injectable({providedIn: 'root'})
export class NzScrollStrategyOptions {
private document: Document;
private renderer: Renderer2;
constructor(
rendererFactory: RendererFactory2,
// tslint:disable-next-line:no-any
@Inject(DOCUMENT) document: any
) {
this.document = document;
this.renderer = rendererFactory.createRenderer(null, null);
}

block = () => new NzBlockScrollStrategy(this.document, this.renderer);
}
9 changes: 8 additions & 1 deletion components/drawer/nz-drawer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {

import { CdkOverlayOrigin, Overlay, OverlayConfig, OverlayRef } from '@angular/cdk/overlay';
import { TemplatePortal } from '@angular/cdk/portal';
import { NzScrollStrategyOptions } from '../core/overlay/scroll/nz-scroll-strategy-options';

import { InputBoolean } from '../core/util/convert';

Expand Down Expand Up @@ -77,6 +78,7 @@ export class NzDrawerComponent implements OnInit, OnDestroy {
private renderer: Renderer2,
private overlay: Overlay,
private elementRef: ElementRef,
private nzScrollStrategyOptions: NzScrollStrategyOptions,
private viewContainerRef: ViewContainerRef) {

}
Expand Down Expand Up @@ -109,13 +111,18 @@ export class NzDrawerComponent implements OnInit, OnDestroy {

getOverlayConfig(): OverlayConfig {
return new OverlayConfig({
scrollStrategy: this.overlay.scrollStrategies.block()
scrollStrategy: this.nzScrollStrategyOptions.block()
});
}

updateOverlayStyle(): void {
if (this.overlayRef && this.overlayRef.overlayElement) {
this.renderer.setStyle(this.overlayRef.overlayElement, 'pointer-events', this.isOpen ? 'auto' : 'none');
if (this.isOpen) {
this.overlayRef.getConfig().scrollStrategy.enable();
} else {
this.overlayRef.getConfig().scrollStrategy.disable();
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions components/drawer/nz-drawer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ describe('NzDrawerComponent', () => {
expect(component.drawerComponent.nzVisible).toBe(false);
});

it('should set body overflow', () => {
component.open();
fixture.detectChanges();
expect(document.body.style.overflow).toBe('hidden');
component.close();
fixture.detectChanges();
expect(document.body.style.overflow).toBe('');
});

it('should hied close button', () => {
component.closable = false;
component.open();
Expand Down

0 comments on commit b993068

Please sign in to comment.