Skip to content

Commit

Permalink
add hide on scroll attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
MrFrankel committed Jan 11, 2018
1 parent 1a08d49 commit e5fe199
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ SystemJS
| popperShowOnStart | number | 0 |
| popperTrigger | Trigger(string) | hover |
| popperPositionFixed | boolean | false |
| popperCloseOnClickOutside| boolean | true |
| popperHideOnClickOutside | boolean | true |
| popperHideOnScroll | boolean | false |
| popperForceDetection | boolean | false |
| popperTrigger | Trigger(string) | hover |
| popperModifiers | popperModifier | undefined|
Expand Down
2 changes: 1 addition & 1 deletion example/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<section id="two" class="wrapper alt style2">
<section id="two" class="wrapper alt style2" style="height: 100vh; overflow: auto">
<section class="spotlight">
<div class="example">
<div #popper1
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-popper",
"version": "1.8.8",
"version": "1.9.0",
"description": "ngx-popper is an angular wrapper for popper.js",
"directories": {
"test": "test"
Expand Down
47 changes: 41 additions & 6 deletions src/popper-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class PopperController implements OnInit, OnChanges {
private scheduledHideTimeout: any;
private subscriptions: any[] = [];
private globalClick: any;
private globalScroll: any;

constructor(private viewContainerRef: ViewContainerRef,
private changeDetectorRef: ChangeDetectorRef,
Expand Down Expand Up @@ -67,6 +68,12 @@ export class PopperController implements OnInit, OnChanges {
@Input('popperCloseOnClickOutside')
closeOnClickOutside: boolean = true;

@Input('popperHideOnClickOutside')
hideOnClickOutside: boolean;

@Input('popperHideOnScroll')
hideOnScroll: boolean = false;

@Input('popperPositionFixed')
positionFixed: boolean;

Expand Down Expand Up @@ -114,8 +121,15 @@ export class PopperController implements OnInit, OnChanges {
this.scheduledShow();
}

hideOnClickOutside($event: MouseEvent): void {
if (this.disabled || !this.closeOnClickOutside) {
hideOnClickOutsideHandler($event: MouseEvent): void {
if (this.disabled || !this.hideOnClickOutside) {
return;
}
this.scheduledHide($event, this.hideTimeout);
}

hideOnScrollHandler($event: MouseEvent): void {
if (this.disabled || !this.hideOnScroll) {
return;
}
this.scheduledHide($event, this.hideTimeout);
Expand Down Expand Up @@ -144,6 +158,10 @@ export class PopperController implements OnInit, OnChanges {
}

ngOnInit() {
//Support legacy prop
this.hideOnClickOutside = typeof this.hideOnClickOutside === 'undefined' ?
this.closeOnClickOutside : this.hideOnClickOutside;

if (typeof this.content === 'string') {
const text = this.content;
this.content = this.constructContent();
Expand All @@ -170,7 +188,7 @@ export class PopperController implements OnInit, OnChanges {
ngOnDestroy() {
this.subscriptions.forEach(sub => sub.unsubscribe && sub.unsubscribe());
this.subscriptions.length = 0;
this.clearGlobalClick();
this.clearEventListeners();
}

toggle() {
Expand All @@ -194,7 +212,8 @@ export class PopperController implements OnInit, OnChanges {
if (this.timeoutAfterShow > 0) {
this.scheduledHide(null, this.timeoutAfterShow);
}
this.globalClick = this.renderer.listen('document', 'click', this.hideOnClickOutside.bind(this))
this.globalClick = this.renderer.listen('document', 'click', this.hideOnClickOutsideHandler.bind(this));
this.globalScroll = this.renderer.listen(this.getScrollParent(this.getRefElement()), 'scroll', this.hideOnScrollHandler.bind(this));
}

hide() {
Expand All @@ -211,7 +230,7 @@ export class PopperController implements OnInit, OnChanges {
(this.content as PopperContent).hide();
}
this.popperOnHidden.emit(this);
this.clearGlobalClick();
this.clearEventListeners();
}

scheduledShow(delay: number = this.showDelay) {
Expand Down Expand Up @@ -251,8 +270,9 @@ export class PopperController implements OnInit, OnChanges {
this.showTrigger = this.showTrigger || PopperController.baseOptions.trigger;
}

private clearGlobalClick() {
private clearEventListeners() {
this.globalClick && typeof this.globalClick === 'function' && this.globalClick();
this.globalScroll && typeof this.globalScroll === 'function' && this.globalScroll();
}

private overrideShowTimeout() {
Expand Down Expand Up @@ -288,4 +308,19 @@ export class PopperController implements OnInit, OnChanges {
this.subscriptions.push(popperRef.onHidden.subscribe(this.hide.bind(this)));
}

private getScrollParent(node) {
const isElement = node instanceof HTMLElement;
const overflowY = isElement && window.getComputedStyle(node).overflowY;
const isScrollable = overflowY !== 'visible' && overflowY !== 'hidden';

if (!node) {
return null;
} else if (isScrollable && node.scrollHeight >= node.clientHeight) {
return node;
}

return this.getScrollParent(node.parentNode) || document;
}


}

0 comments on commit e5fe199

Please sign in to comment.