2
0
mirror of https://github.com/tenrok/vue-select.git synced 2026-05-23 03:54:04 +03:00
Files
vue-select/src/mixins/pointerScroll.js
T
Jeff Sagal 2eb39087fd feat: add autoscroll boolean prop (#1160)
* feat: add autoscroll boolean prop

Fixes #449

* refactor: update autoscroll implementation

Closes #1028
Closes #910

* refactor: only call maybeAdjustScroll in the watcher

* docs: upgrade vuepress
2020-04-12 15:44:52 -07:00

56 lines
1.3 KiB
JavaScript

export default {
props: {
autoscroll: {
type: Boolean,
default: true
}
},
watch: {
typeAheadPointer() {
if (this.autoscroll) {
this.maybeAdjustScroll();
}
},
},
methods: {
/**
* Adjust the scroll position of the dropdown list
* if the current pointer is outside of the
* overflow bounds.
* @returns {*}
*/
maybeAdjustScroll() {
const optionEl =
this.$refs.dropdownMenu?.children[this.typeAheadPointer] || false;
if (optionEl) {
const bounds = this.getDropdownViewport();
const { top, bottom, height } = optionEl.getBoundingClientRect();
if (top < bounds.top) {
return (this.$refs.dropdownMenu.scrollTop = optionEl.offsetTop);
} else if (bottom > bounds.bottom) {
return (this.$refs.dropdownMenu.scrollTop =
optionEl.offsetTop - (bounds.height - height));
}
}
},
/**
* The currently viewable portion of the dropdownMenu.
* @returns {{top: (string|*|number), bottom: *}}
*/
getDropdownViewport() {
return this.$refs.dropdownMenu
? this.$refs.dropdownMenu.getBoundingClientRect()
: {
height: 0,
top: 0,
bottom: 0
};
}
}
};