Fix dropdown opening direction [SCI-9428]

This commit is contained in:
Martin Artnik 2023-10-03 16:03:11 +02:00
parent 64449af994
commit 1acf67db5a
2 changed files with 22 additions and 14 deletions

View file

@ -0,0 +1,14 @@
export function isInViewPort(el) {
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <=
(window.innerHeight ||
document.documentElement.clientHeight) /*or $(window).height() */ &&
rect.right <=
(window.innerWidth ||
document.documentElement.clientWidth) /*or $(window).width() */
);
}

View file

@ -63,6 +63,8 @@
<script> <script>
import { isInViewPort } from './isInViewPort.js';
export default { export default {
name: 'DropdownMenu', name: 'DropdownMenu',
props: { props: {
@ -82,18 +84,19 @@ export default {
watch: { watch: {
showMenu() { showMenu() {
if (this.showMenu) { if (this.showMenu) {
this.openUp = false;
this.$nextTick(() => { this.$nextTick(() => {
this.$refs.flyout.style.marginBottom = `${this.$refs.openBtn.offsetHeight}px`; this.$refs.flyout.style.marginBottom = `${this.$refs.openBtn.offsetHeight}px`;
this.verticalPositionFlyout(); this.updateOpenDirectoin();
}) })
} }
} }
}, },
mounted() { mounted() {
document.addEventListener('scroll', this.verticalPositionFlyout); document.addEventListener('scroll', this.updateOpenDirectoin);
}, },
unmounted() { unmounted() {
document.removeEventListener('scroll', this.verticalPositionFlyout); document.removeEventListener('scroll', this.updateOpenDirectoin);
}, },
methods: { methods: {
closeMenu() { closeMenu() {
@ -110,20 +113,11 @@ export default {
this.closeMenu(); this.closeMenu();
}, },
verticalPositionFlyout() { updateOpenDirectoin() {
if (!this.showMenu) return; if (!this.showMenu) return;
const btn = this.$refs.openBtn; this.openUp = !isInViewPort(this.$refs.flyout);
const screenHeight = window.innerHeight;
const btnBottom = btn.getBoundingClientRect().bottom;
if (screenHeight / 2 < btnBottom) {
this.openUp = true;
} else {
this.openUp = false;
} }
} }
}
} }
</script> </script>