Skip to content

Instantly share code, notes, and snippets.

@antongorodezkiy
Last active September 23, 2016 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antongorodezkiy/66b93d7d21291ddb633c9320e472fb13 to your computer and use it in GitHub Desktop.
Save antongorodezkiy/66b93d7d21291ddb633c9320e472fb13 to your computer and use it in GitHub Desktop.
vue-popper directive
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);
@antongorodezkiy
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment