Skip to content

Instantly share code, notes, and snippets.

@nithindavid
Created March 21, 2018 04:21
Show Gist options
  • Save nithindavid/236e4c026f2de2a005a125eb9a54fb4e to your computer and use it in GitHub Desktop.
Save nithindavid/236e4c026f2de2a005a125eb9a54fb4e to your computer and use it in GitHub Desktop.
Smooth Scroll bar wrapper vue by @dwightjack
import Scrollbar from 'smooth-scrollbar';
import VueTypes from 'vue-types';
export default {
props: {
options: VueTypes.object,
tagName: String,
active: VueTypes.bool
},
render(h) {
if (this.tagName) return h(this.tagName, this.$slots.default);
return this.$slots.default[0];
},
watch: {
active(active) {
this.$nextTick(active ? this.attach : this.destroy);
}
},
mounted() {
if (this.active) {
this.$nextTick(this.attach);
}
},
beforeDestroy() {
this.destroy();
},
methods: {
attach() {
if (!this.scrollbar) {
const el = this.$el || this.$slots.default[0].elm;
this.scrollbar = Scrollbar.init(el, this.options);
this.scrollbar.addListener((status) => this.$emit('scroll', status));
} else {
this.scrollbar.update();
}
},
destroy() {
if (this.scrollbar) {
const el = this.$el || this.$slots.default[0].elm;
el.removeAttribute('data-scrollbar');
this.scrollbar.destroy();
this.scrollbar = null;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment