Last active
September 23, 2016 13:10
-
-
Save antongorodezkiy/66b93d7d21291ddb633c9320e472fb13 to your computer and use it in GitHub Desktop.
vue-popper directive
This file contains hidden or 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
const Popper = require('popper.js'); | |
(function (Vue) { | |
let VuePopperDirective = { | |
params: ['placement', 'content', 'show'], | |
popper: null, | |
initPopper() { | |
this.popper = new Popper( | |
$(this.el), | |
{ | |
content: this.params.content || '', | |
contentType: 'html' | |
}, | |
{ | |
placement: this.params.placement || 'bottom', | |
removeOnDestroy: true | |
} | |
); | |
}, | |
destroyPopper() { | |
if (this.popper) { | |
this.popper.destroy(); | |
this.popper = null; | |
} | |
}, | |
bind() { | |
Vue.nextTick(() => { | |
if (this.params.show) { | |
this.initPopper(); | |
} | |
}); | |
}, | |
paramWatchers: { | |
show(val, oldVal) { | |
if (!!this.params.show) { | |
this.destroyPopper(); | |
Vue.nextTick(() => { | |
this.initPopper(); | |
}); | |
} | |
else if (this.popper) { | |
this.destroyPopper(); | |
} | |
} | |
}, | |
unbind() { | |
this.destroyPopper(); | |
} | |
}; | |
/* | |
* Install Vue Directive if Vue is available | |
*/ | |
if (typeof Vue !== "undefined") { | |
Vue.directive('popper', VuePopperDirective); | |
} | |
})(window.Vue); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Moved to https://github.com/antongorodezkiy/vue-popper-directive