This repository has been archived by the owner on Feb 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(peekaboo): decorator for viewport scroll actions
- 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
1 parent
a949ba8
commit d419853
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |