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

Commit

Permalink
feat(peekaboo): decorator for viewport scroll actions
Browse files Browse the repository at this point in the history
 - mimic the component used on the google design page.
 - apply an action when the peekaboo breakpoint is triggered. support 'show' and 'hide' actions.
 - TODO: mapping of viewport sizes (e.g. 'sm','lg','md') to peekaboo breakpoints. Use to support different breakpoints on smaller viewports.
 - TODO: tests
  • Loading branch information
justindujardin committed Dec 28, 2015
1 parent a949ba8 commit d419853
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ng2-material/components/peekaboo/peekaboo.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[md-peekaboo] {
&[action=show] {
display: none;
&.md-peekaboo-active {
display: inherit;
}
}
&[action=hide] {
display: inherit;
&.md-peekaboo-active {
display: none;
}
}
}
67 changes: 67 additions & 0 deletions ng2-material/components/peekaboo/peekaboo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {Directive} from "angular2/core";
import {Media, MediaListener} from '../../core/util/media';
import {DOM} from "angular2/src/platform/dom/dom_adapter";
import {OnDestroy} from "angular2/core";
import {debounce} from "../../core/util/util";
import {Input} from "angular2/core";
import {CONST} from "angular2/src/facade/lang";
import {isPresent} from "angular2/src/facade/lang";
import {Attribute} from "angular2/core";


/** Different peekaboo actions to apply when active */
@CONST()
export class PeekabooAction {
@CONST() static SHOW = 'show';
@CONST() static HIDE = 'hide';
}

/**
* @name mdPeekaboo
*
* @description
* The `[md-peekaboo]` directive is an attribute that toggles the visibility of elements based
* on the current viewport size and scrollTop.
*
*/
@Directive({
selector: '[md-peekaboo]',
inputs: ['breaks'],
host: {
'[class.md-peekaboo-active]': 'active',
'[attr.action]': 'action'
}
})
export class MdPeekaboo implements OnDestroy {
//TODO(jdd): This is just testing hacks, use mapping size->scrollTop
@Input() breaks: number = 100;

@Input() action: string;

private _active: boolean;
get active(): boolean {
return this._active;
}

get scrollTop(): number {
return window.pageYOffset || document.documentElement.scrollTop;
}

constructor(@Attribute('action') action: string) {
this.action = isPresent(action) ? action : PeekabooAction.SHOW;
this._active = this._evaluate(this.scrollTop);
window.addEventListener('scroll', this._windowScroll);
}

ngOnDestroy(): any {
window.removeEventListener('scroll', this._windowScroll);
}

private _evaluate(scrollTop: number): boolean {
return scrollTop >= this.breaks;
}

private _windowScroll = (ev: Event) => {
this._active = this._evaluate(this.scrollTop);
};
}

0 comments on commit d419853

Please sign in to comment.