Skip to content
This repository has been archived by the owner on Feb 2, 2019. It is now read-only.

Commit

Permalink
feat(backdrop): add hideScroll input to hide parent scroll while shown
Browse files Browse the repository at this point in the history
  • Loading branch information
justindujardin committed Jan 24, 2016
1 parent 684b7dc commit 2e6240d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ng2-material/components/backdrop/backdrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Component} from "angular2/core";
import {Input} from "angular2/core";
import {Output} from "angular2/core";
import {EventEmitter} from "angular2/core";
import {DOM} from "angular2/src/platform/dom/dom_adapter";

/**
* An overlay for content on the page.
Expand All @@ -29,6 +30,12 @@ export class MdBackdrop {
@Input()
clickClose: boolean = false;

/**
* When true, disable the parent container scroll while the backdrop is active.
*/
@Input()
hideScroll: boolean = true;

/**
* Emits when the backdrop begins to hide.
*/
Expand Down Expand Up @@ -80,6 +87,7 @@ export class MdBackdrop {

private _visible: boolean = false;
private _transitioning: boolean = false;
private _previousOverflow: string = null;

onClick() {
if (this.clickClose && !this._transitioning && this.visible) {
Expand All @@ -99,6 +107,16 @@ export class MdBackdrop {
this._transitioning = true;
this.onShowing.emit(this);
let action = this.transitionAddClass ? Animate.enter : Animate.leave;

if (this.hideScroll && this.element && !this._previousOverflow) {
let parent = DOM.parentElement(this.element.nativeElement);
let style = DOM.getStyle(parent, 'overflow');
if (style !== 'hidden') {
this._previousOverflow = DOM.getStyle(parent, 'overflow');
DOM.setStyle(parent, 'overflow', 'hidden');
}
}

return action(this.element.nativeElement, this.transitionClass).then(() => {
this._transitioning = false;
this.onShown.emit(this);
Expand All @@ -117,6 +135,13 @@ export class MdBackdrop {
this._transitioning = true;
this.onHiding.emit(this);
let action = this.transitionAddClass ? Animate.leave : Animate.enter;

if (this.hideScroll && this.element && this._previousOverflow !== null) {
let parent = DOM.parentElement(this.element.nativeElement);
DOM.setStyle(parent, 'overflow', this._previousOverflow);
this._previousOverflow = null;
}

return action(this.element.nativeElement, this.transitionClass).then(() => {
this._transitioning = false;
this.onHidden.emit(this);
Expand Down

0 comments on commit 2e6240d

Please sign in to comment.