fix: context submenu outside view when near the edge, fixes #506

This commit is contained in:
Fabio Di Stasio 2022-12-22 11:27:13 +01:00
parent 55c1604e7f
commit c08946e932
1 changed files with 104 additions and 77 deletions

View File

@ -1,5 +1,5 @@
<template>
<div class="context" :class="{'bottom': isBottom}">
<div class="context" :class="[{ 'bottom': isBottom }, { 'right': isRight }]">
<a
class="context-overlay"
@click="close"
@ -19,9 +19,10 @@
import { computed, onBeforeUnmount, onMounted, Ref, ref } from 'vue';
const contextContent: Ref<HTMLDivElement> = ref(null);
const contextSize: Ref<DOMRect> = ref(null);
const contextSize: Ref<{height: number; width: number; subHeight?: number; subWidth?: number}> = ref(null);
const isBottom: Ref<boolean> = ref(false);
const props = defineProps<{contextEvent: MouseEvent}>();
const isRight: Ref<boolean> = ref(false);
const props = defineProps<{ contextEvent: MouseEvent }>();
const emit = defineEmits(['close-context']);
const position = computed(() => {
@ -39,8 +40,16 @@ const position = computed(() => {
isBottom.value = true;
}
if (clientX + contextSize.value.width + 5 >= window.innerWidth)
if (clientY + contextSize.value.subHeight + contextSize.value.height >= window.innerHeight)
isBottom.value = true;
if (clientX + contextSize.value.width + 5 >= window.innerWidth) {
leftCord = `${clientX - contextSize.value.width}px`;
isRight.value = true;
}
if (clientX + contextSize.value.subWidth + contextSize.value.width >= window.innerWidth)
isRight.value = true;
}
}
@ -62,8 +71,20 @@ const onKey = (e: KeyboardEvent) => {
window.addEventListener('keydown', onKey);
onMounted(() => {
if (contextContent.value)
if (contextContent.value) {
contextSize.value = contextContent.value.getBoundingClientRect();
const submenus = contextContent.value.querySelectorAll<HTMLDivElement>('.context-submenu');
for (const submenu of submenus) {
const submenuSize = submenu.getBoundingClientRect();
if (!contextSize.value.subHeight || submenuSize.height > contextSize.value.subHeight)
contextSize.value.subHeight = submenuSize.height;
if (!contextSize.value.subWidth || submenuSize.width > contextSize.value.subWidth)
contextSize.value.subWidth = submenuSize.width;
}
}
});
onBeforeUnmount(() => {
@ -94,6 +115,14 @@ onBeforeUnmount(() => {
bottom: -0.2rem;
}
&:not(.right) .context-submenu {
left: 100%;
}
&.right .context-submenu {
right: 100%;
}
.context-container {
min-width: 100px;
z-index: 10;
@ -124,7 +153,6 @@ onBeforeUnmount(() => {
visibility: hidden;
transition: opacity 0.2s;
position: absolute;
left: 100%;
min-width: 100px;
background: #1d1d1d;
}
@ -156,5 +184,4 @@ onBeforeUnmount(() => {
filter: grayscale(100%);
opacity: 0.5;
}
</style>