Skip to content

Commit

Permalink
Merge pull request #92 from HubSpot/before-close
Browse files Browse the repository at this point in the history
Add before close hook
  • Loading branch information
geekjuice committed Jun 24, 2015
2 parents 5a5e245 + 1d5b57f commit 814d2a1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "tether-drop",
"version": "1.1.2",
"version": "1.2.0",
"homepage": "https://github.com/HubSpot/drop",
"authors": [
"Adam Schwartz <adam.flynn.schwartz@gmail.com>",
Expand Down
13 changes: 11 additions & 2 deletions dist/js/drop.js
@@ -1,4 +1,4 @@
/*! tether-drop 1.1.2 */
/*! tether-drop 1.2.0 */

(function(root, factory) {
if (typeof define === 'function' && define.amd) {
Expand Down Expand Up @@ -116,6 +116,7 @@ function createContext() {
defaults: {
position: 'bottom left',
openOn: 'click',
beforeClose: null,
constrainToScrollParent: true,
constrainToWindow: true,
classes: '',
Expand Down Expand Up @@ -429,6 +430,14 @@ function createContext() {
return;
}

var _options = this.options;
var remove = _options.remove;
var beforeClose = _options.beforeClose;

if (typeof beforeClose === 'function' && beforeClose() === false) {
return;
}

removeClass(this.drop, '' + drop.classPrefix + '-open');
removeClass(this.drop, '' + drop.classPrefix + '-after-open');

Expand All @@ -442,7 +451,7 @@ function createContext() {

drop.updateBodyClasses();

if (this.options.remove) {
if (remove) {
this.remove();
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/js/drop.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions docs/intro.md
Expand Up @@ -164,6 +164,11 @@ true
false
```

#### `beforeClose`

Function that is run before closing the drop. If the function returns `false`, the closing of the drop will be prevented. Useful if you only want to programatically close the drop.


#### `tetherOptions`

Additional options can be passed to customize Drop even further. These will get passed to the underlying Tether instance used to position the drop. See the the [Tether documentation](http://tether.io) for more information. Set to `false` to disable Tether.
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "tether-drop",
"version": "1.1.2",
"version": "1.2.0",
"description": "Client-side library for creating dropdowns",
"authors": [
"Zack Bloom <zackbloom@gmail.com>",
Expand Down
8 changes: 7 additions & 1 deletion src/js/drop.js
Expand Up @@ -76,6 +76,7 @@ function createContext(options={}) {
defaults: {
position: 'bottom left',
openOn: 'click',
beforeClose: null,
constrainToScrollParent: true,
constrainToWindow: true,
classes: '',
Expand Down Expand Up @@ -368,6 +369,11 @@ function createContext(options={}) {
return;
}

const {remove, beforeClose} = this.options;
if (typeof beforeClose === 'function' && beforeClose() === false) {
return;
}

removeClass(this.drop, `${ drop.classPrefix }-open`);
removeClass(this.drop, `${ drop.classPrefix }-after-open`);

Expand All @@ -381,7 +387,7 @@ function createContext(options={}) {

drop.updateBodyClasses();

if (this.options.remove) {
if (remove) {
this.remove();
}
}
Expand Down

0 comments on commit 814d2a1

Please sign in to comment.