11<template >
2- <div class =" context-menu context-menu-container" v-if =" visible" :style =" contextMenuPosition"
3- v-click-outside =" close" @click =" close" >
2+ <div class =" context-menu context-menu-container"
3+ :class =" openPosition"
4+ v-if =" visible"
5+ :style =" contextMenuPosition"
6+ v-click-outside =" close" @click =" close"
7+ ref =" contextMenu"
8+ >
49 <ul >
510 <context-menu-item
611 v-for =" (menuItem, index) in menuItems"
@@ -39,6 +44,7 @@ export default {
3944 top: 0 ,
4045 left: 0 ,
4146 },
47+ openPosition: ' context-menu-open-right' ,
4248 };
4349 },
4450
@@ -51,25 +57,37 @@ export default {
5157 * Accepts an Object with an `x, y` position or an instance of Event
5258 */
5359 open (position ) {
54- let x = 0 ;
55- let y = 0 ;
56-
57- if (typeof position !== ' undefined' && typeof position === ' object' ) {
58- if (position instanceof Event ) {
59- x = position .pageX ;
60- y = position .pageY ;
61- } else {
62- x = position .x ;
63- y = position .y ;
64- }
65- }
60+ this .visible = true ;
61+
62+ this .$nextTick (() => {
63+ let x = 0 ;
64+ let y = 0 ;
65+
66+ if (typeof position !== ' undefined' && typeof position === ' object' ) {
67+ if (position instanceof Event ) {
68+ const windowWidth = window .innerWidth ;
69+ const contextMenuWidth = this .$refs .contextMenu .getBoundingClientRect ().width ;
70+
71+ if (position .pageX >= (windowWidth - contextMenuWidth)) {
72+ this .openPosition = ' context-menu-open-left' ;
73+ x = windowWidth - contextMenuWidth - 10 ;
74+ } else {
75+ this .openPosition = ' context-menu-open-right' ;
76+ x = position .pageX ;
77+ }
6678
67- this .contextMenuPosition = {
68- left: ` ${ x} px` ,
69- top: ` ${ y} px` ,
70- };
79+ y = position .pageY ;
80+ } else {
81+ x = position .x ;
82+ y = position .y ;
83+ }
84+ }
7185
72- this .visible = true ;
86+ this .contextMenuPosition = {
87+ left: ` ${ x} px` ,
88+ top: ` ${ y} px` ,
89+ };
90+ });
7391 },
7492 },
7593
@@ -84,7 +102,6 @@ export default {
84102 </script >
85103
86104<style lang="scss">
87- // Context Menu Stuff, should probably move this somewhere else.
88105 $context-menu-border-radius : 4px ;
89106
90107 .context-menu-container {
@@ -151,5 +168,18 @@ export default {
151168 }
152169 }
153170 }
171+
172+ & .context-menu-open-left {
173+ ul {
174+ li {
175+ & :hover {
176+ > ul {
177+ left : auto ;
178+ right : calc (100% + 2px );
179+ }
180+ }
181+ }
182+ }
183+ }
154184 }
155185 </style >
0 commit comments