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> <template>
<div class="context" :class="{'bottom': isBottom}"> <div class="context" :class="[{ 'bottom': isBottom }, { 'right': isRight }]">
<a <a
class="context-overlay" class="context-overlay"
@click="close" @click="close"
@ -19,9 +19,10 @@
import { computed, onBeforeUnmount, onMounted, Ref, ref } from 'vue'; import { computed, onBeforeUnmount, onMounted, Ref, ref } from 'vue';
const contextContent: Ref<HTMLDivElement> = ref(null); 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 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 emit = defineEmits(['close-context']);
const position = computed(() => { const position = computed(() => {
@ -39,8 +40,16 @@ const position = computed(() => {
isBottom.value = true; 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`; 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); window.addEventListener('keydown', onKey);
onMounted(() => { onMounted(() => {
if (contextContent.value) if (contextContent.value) {
contextSize.value = contextContent.value.getBoundingClientRect(); 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(() => { onBeforeUnmount(() => {
@ -73,88 +94,94 @@ onBeforeUnmount(() => {
<style lang="scss"> <style lang="scss">
.context { .context {
display: flex; display: flex;
font-size: 16px; font-size: 16px;
z-index: 400; z-index: 400;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
overflow: hidden; overflow: hidden;
position: fixed; position: fixed;
height: 100vh; height: 100vh;
right: 0; right: 0;
top: 0; top: 0;
left: 0; left: 0;
bottom: 0; bottom: 0;
&:not(.bottom) .context-submenu { &:not(.bottom) .context-submenu {
top: -0.2rem; top: -0.2rem;
} }
&.bottom .context-submenu { &.bottom .context-submenu {
bottom: -0.2rem; bottom: -0.2rem;
} }
.context-container { &:not(.right) .context-submenu {
min-width: 100px; left: 100%;
z-index: 10; }
padding: 0;
background: #1d1d1d;
border-radius: $border-radius;
border: 1px solid $bg-color-light-dark;
display: flex;
flex-direction: column;
position: absolute;
pointer-events: initial;
.context-element { &.right .context-submenu {
display: flex; right: 100%;
align-items: center; }
margin: 0.2rem;
padding: 0.1rem 0.3rem; .context-container {
min-width: 100px;
z-index: 10;
padding: 0;
background: #1d1d1d;
border-radius: $border-radius; border-radius: $border-radius;
cursor: pointer; border: 1px solid $bg-color-light-dark;
justify-content: space-between; display: flex;
position: relative; flex-direction: column;
white-space: nowrap; position: absolute;
pointer-events: initial;
.context-submenu { .context-element {
border-radius: $border-radius; display: flex;
border: 1px solid $bg-color-light-dark; align-items: center;
opacity: 0; margin: 0.2rem;
visibility: hidden; padding: 0.1rem 0.3rem;
transition: opacity 0.2s; border-radius: $border-radius;
position: absolute; cursor: pointer;
left: 100%; justify-content: space-between;
min-width: 100px; position: relative;
background: #1d1d1d; white-space: nowrap;
.context-submenu {
border-radius: $border-radius;
border: 1px solid $bg-color-light-dark;
opacity: 0;
visibility: hidden;
transition: opacity 0.2s;
position: absolute;
min-width: 100px;
background: #1d1d1d;
}
&:hover {
.context-submenu {
display: block;
visibility: visible;
opacity: 1;
}
}
} }
}
&:hover { .context-overlay {
.context-submenu { background: transparent;
display: block; bottom: 0;
visibility: visible; cursor: default;
opacity: 1; display: block;
} left: 0;
} position: absolute;
} right: 0;
} top: 0;
}
.context-overlay {
background: transparent;
bottom: 0;
cursor: default;
display: block;
left: 0;
position: absolute;
right: 0;
top: 0;
}
} }
.disabled { .disabled {
pointer-events: none; pointer-events: none;
filter: grayscale(100%); filter: grayscale(100%);
opacity: 0.5; opacity: 0.5;
} }
</style> </style>