antares/src/renderer/components/BaseContextMenu.vue

149 lines
3.0 KiB
Vue
Raw Normal View History

2020-05-22 19:32:55 +02:00
<template>
<div class="context">
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>
export default {
name: 'BaseContextMenu',
props: {
contextEvent: MouseEvent
},
data () {
return {
contextSize: null
};
},
2020-05-22 19:32:55 +02:00
computed: {
position () {
const { clientY, clientX } = this.contextEvent;
let topCord = `${clientY + 5}px`;
let leftCord = `${clientX + 5}px`;
if (this.contextSize) {
if (clientY + this.contextSize.height + 5 >= window.innerHeight)
topCord = `${clientY - this.contextSize.height}px`;
if (clientX + this.contextSize.width + 5 >= window.innerWidth)
leftCord = `${clientX - this.contextSize.width}px`;
}
return {
top: topCord,
left: leftCord
2020-05-22 19:32:55 +02:00
};
}
},
2020-10-04 17:21:21 +02:00
created () {
window.addEventListener('keydown', this.onKey);
},
mounted () {
this.contextSize = this.$refs.contextContent.getBoundingClientRect();
},
2020-10-04 17:21:21 +02:00
beforeDestroy () {
window.removeEventListener('keydown', this.onKey);
},
2020-05-22 19:32:55 +02:00
methods: {
close () {
2020-08-12 18:11:48 +02:00
this.$emit('close-context');
2020-10-04 17:21:21 +02:00
},
onKey (e) {
e.stopPropagation();
if (e.key === 'Escape')
this.close();
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;
.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;
padding: 0.1rem;
border-radius: $border-radius;
2021-01-06 11:57:49 +01:00
cursor: pointer;
justify-content: space-between;
position: relative;
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%;
top: 0;
min-width: 100px;
}
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>