fix: context menu outside window when near bottom or right edge

This commit is contained in:
Fabio 2020-10-14 19:32:36 +02:00
parent 27d114beef
commit d4ecaf65e5
1 changed files with 24 additions and 3 deletions

View File

@ -6,6 +6,7 @@
@contextmenu="close" @contextmenu="close"
/> />
<div <div
ref="contextContent"
class="context-container" class="context-container"
:style="position" :style="position"
> >
@ -20,17 +21,37 @@ export default {
props: { props: {
contextEvent: MouseEvent contextEvent: MouseEvent
}, },
data () {
return {
contextSize: null
};
},
computed: { computed: {
position () { position () {
return { // TODO: calc direction if near corners const { clientY, clientX } = this.contextEvent;
top: this.contextEvent.clientY + 5 + 'px', let topCord = `${clientY + 5}px`;
left: this.contextEvent.clientX + 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
}; };
} }
}, },
created () { created () {
window.addEventListener('keydown', this.onKey); window.addEventListener('keydown', this.onKey);
}, },
mounted () {
this.contextSize = this.$refs.contextContent.getBoundingClientRect();
},
beforeDestroy () { beforeDestroy () {
window.removeEventListener('keydown', this.onKey); window.removeEventListener('keydown', this.onKey);
}, },