antares/src/renderer/components/BaseContextMenu.vue

161 lines
3.4 KiB
Vue
Raw Normal View History

2020-05-22 19:32:55 +02:00
<template>
<div class="context" :class="{'bottom': isBottom}">
2020-10-04 17:21:21 +02:00
<a
class="context-overlay"
@click="close"
@contextmenu="close"
/>
2020-05-22 19:32:55 +02:00
<div
ref="contextContent"
2020-05-22 19:32:55 +02:00
class="context-container"
:style="position"
>
<slot />
</div>
</div>
</template>
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, Ref, ref } from 'vue';
const contextContent: Ref<HTMLDivElement> = ref(null);
const contextSize: Ref<DOMRect> = ref(null);
const isBottom: Ref<boolean> = ref(false);
const props = defineProps<{contextEvent: MouseEvent}>();
const emit = defineEmits(['close-context']);
const position = computed(() => {
let topCord = '0px';
let leftCord = '0px';
if (props.contextEvent) {
const { clientY, clientX } = props.contextEvent;
topCord = `${clientY + 2}px`;
leftCord = `${clientX + 5}px`;
if (contextSize.value) {
if (clientY + (contextSize.value.height < 200 ? 200 : contextSize.value.height) + 5 >= window.innerHeight) {
topCord = `${clientY + 3 - contextSize.value.height}px`;
isBottom.value = true;
}
if (clientX + contextSize.value.width + 5 >= window.innerWidth)
leftCord = `${clientX - contextSize.value.width}px`;
2020-05-22 19:32:55 +02:00
}
}
return {
top: topCord,
left: leftCord
};
});
const close = () => {
emit('close-context');
2020-05-22 19:32:55 +02:00
};
const onKey = (e: KeyboardEvent) => {
e.stopPropagation();
if (e.key === 'Escape')
close();
};
window.addEventListener('keydown', onKey);
onMounted(() => {
if (contextContent.value)
contextSize.value = contextContent.value.getBoundingClientRect();
});
onBeforeUnmount(() => {
window.removeEventListener('keydown', onKey);
});
2020-05-22 19:32:55 +02:00
</script>
<style lang="scss">
2021-01-06 11:57:49 +01:00
.context {
display: flex;
font-size: 16px;
z-index: 400;
justify-content: center;
align-items: center;
overflow: hidden;
position: fixed;
height: 100vh;
right: 0;
top: 0;
left: 0;
bottom: 0;
&:not(.bottom) .context-submenu {
top: -0.2rem;
}
&.bottom .context-submenu {
bottom: -0.2rem;
}
2021-01-06 11:57:49 +01:00
.context-container {
min-width: 100px;
z-index: 10;
padding: 0;
background: #1d1d1d;
border-radius: $border-radius;
border: 1px solid $bg-color-light-dark;
2020-07-31 18:16:28 +02:00
display: flex;
2021-01-06 11:57:49 +01:00
flex-direction: column;
position: absolute;
pointer-events: initial;
2020-07-31 18:16:28 +02:00
2021-01-06 11:57:49 +01:00
.context-element {
2020-05-22 19:32:55 +02:00
display: flex;
2021-01-06 11:57:49 +01:00
align-items: center;
margin: 0.2rem;
2021-06-29 23:31:18 +02:00
padding: 0.1rem 0.3rem;
border-radius: $border-radius;
2021-01-06 11:57:49 +01:00
cursor: pointer;
justify-content: space-between;
position: relative;
white-space: nowrap;
2020-05-22 19:32:55 +02:00
2021-01-06 11:57:49 +01:00
.context-submenu {
border-radius: $border-radius;
border: 1px solid $bg-color-light-dark;
2021-01-06 11:57:49 +01:00
opacity: 0;
visibility: hidden;
transition: opacity 0.2s;
position: absolute;
left: 100%;
min-width: 100px;
background: #1d1d1d;
2021-01-06 11:57:49 +01:00
}
2020-05-23 13:32:14 +02:00
2021-01-06 11:57:49 +01:00
&:hover {
.context-submenu {
display: block;
visibility: visible;
opacity: 1;
2020-07-31 18:16:28 +02:00
}
2020-05-22 19:32:55 +02:00
}
2020-07-31 18:16:28 +02:00
}
2021-01-06 11:57:49 +01:00
}
2020-05-22 19:32:55 +02:00
2021-01-06 11:57:49 +01:00
.context-overlay {
background: transparent;
bottom: 0;
cursor: default;
display: block;
left: 0;
position: absolute;
right: 0;
top: 0;
2020-07-31 18:16:28 +02:00
}
2021-01-06 11:57:49 +01:00
}
.disabled {
pointer-events: none;
filter: grayscale(100%);
opacity: 0.5;
}
2020-05-22 19:32:55 +02:00
</style>