Skip to content

Commit

Permalink
Add option to close popover when it is constrained/cutoff from the page
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Elwood authored and Scott Elwood committed Oct 19, 2017
1 parent 24bb9a3 commit 2839711
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
18 changes: 16 additions & 2 deletions dist/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ var Popoverjs = function () {
}, {
key: 'positionerOptions',
get: function get() {
return Object.assign({}, this.options);
return Object.assign({
hide: this.forceHide.bind(this)
}, this.options);
}
}]);

Expand Down Expand Up @@ -1205,6 +1207,7 @@ var defaults = {
scrollPositioning: true,
scrollParentConstraint: true,
applyClassesToAttachment: false,
closeOnCutoff: false,
constraints: [{
popover: 'top right',
attachment: 'bottom right'
Expand Down Expand Up @@ -1478,7 +1481,7 @@ var Positioner = function () {

this.scrollParent = this.getScrollParent();
if (this.scrollParent) {
this.scrollParent.addEventListener('scroll', this.onResize.bind(this));
this.scrollParent.addEventListener('scroll', this.onScroll.bind(this));
}
}
}, {
Expand All @@ -1502,11 +1505,20 @@ var Positioner = function () {
key: 'onResize',
value: function onResize() {
this.position();
this.attemptAutoClose();
}
}, {
key: 'onScroll',
value: function onScroll() {
this.position();
this.attemptAutoClose();
}
}, {
key: 'attemptAutoClose',
value: function attemptAutoClose() {
if (this.isCompletelyConstrained && this.options.closeOnCutoff) {
this.options.hide();
}
}
}, {
key: 'disable',
Expand Down Expand Up @@ -1536,6 +1548,8 @@ var Positioner = function () {
if (activeConstraint) {
this.applyConstraint(activeConstraint);
}

this.isCompletelyConstrained = !activeConstraint;
}
}, {
key: 'getActiveConstraint',
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ class Popoverjs {
}

get positionerOptions() {
return Object.assign({}, this.options);
return Object.assign({
hide: this.forceHide.bind(this),
}, this.options);
}

setUpPositioner() {
Expand Down
13 changes: 12 additions & 1 deletion src/positioner.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const defaults = {
scrollPositioning: true,
scrollParentConstraint: true,
applyClassesToAttachment: false,
closeOnCutoff: false,
constraints: [{
popover: 'top right',
attachment: 'bottom right',
Expand Down Expand Up @@ -258,7 +259,7 @@ class Positioner {

this.scrollParent = this.getScrollParent();
if (this.scrollParent) {
this.scrollParent.addEventListener('scroll', this.onResize.bind(this));
this.scrollParent.addEventListener('scroll', this.onScroll.bind(this));
}
}

Expand All @@ -279,10 +280,18 @@ class Positioner {

onResize() {
this.position();
this.attemptAutoClose();
}

onScroll() {
this.position();
this.attemptAutoClose();
}

attemptAutoClose() {
if (this.isCompletelyConstrained && this.options.closeOnCutoff) {
this.options.hide();
}
}

disable() {
Expand Down Expand Up @@ -310,6 +319,8 @@ class Positioner {
if (activeConstraint) {
this.applyConstraint(activeConstraint);
}

this.isCompletelyConstrained = !activeConstraint;
}

getActiveConstraint() {
Expand Down

0 comments on commit 2839711

Please sign in to comment.