Skip to content

Commit 25b2fe2

Browse files
author
Johnathan Barrett
committed
Quick and dirty check to see where to position the menu and expanded menus when right clicking
1 parent 57447fc commit 25b2fe2

File tree

3 files changed

+96
-48
lines changed

3 files changed

+96
-48
lines changed

example/App.vue

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
Right Click Me!
77
</div>
88

9-
<div class="console" v-html="consoleMessage.length ? consoleMessage.join('<br>') : '...'"></div>
9+
<div class="console"
10+
v-html="consoleMessage.length ? consoleMessage.join('<br>') : '...'">
11+
</div>
1012

1113
</div>
1214
</template>
@@ -18,34 +20,48 @@ export default {
1820
name: 'app',
1921
data() {
2022
return {
21-
menuItems: [
22-
{
23-
label: 'First Menu Item',
24-
},
25-
{
26-
label: 'Disabled Menu Item',
27-
disabled: true,
23+
menuItems: [],
24+
consoleMessage: [],
25+
};
26+
},
27+
28+
mounted() {
29+
this.menuItems = [
30+
{
31+
label: 'First Menu Item',
32+
handler: () => {
33+
this.consoleMessage.unshift('First Menu Item Clicked');
2834
},
29-
{
30-
label: 'I have children',
31-
children: [
32-
{
33-
label: 'Child Item 1',
35+
},
36+
{
37+
label: 'Disabled Menu Item',
38+
disabled: true,
39+
},
40+
{
41+
label: 'I have children',
42+
children: [
43+
{
44+
label: 'Child Item 1',
45+
handler: () => {
46+
this.consoleMessage.unshift('Child Item 1 Clicked');
3447
},
35-
{
36-
label: 'I also have children',
37-
children: [
38-
{
39-
label: 'Child Item 2',
48+
},
49+
{
50+
label: 'I also have children',
51+
children: [
52+
{
53+
label: 'Child Item 2',
54+
handler: () => {
55+
this.consoleMessage.unshift('Child Item 2 Clicked');
4056
},
41-
],
42-
},
43-
],
44-
},
45-
],
46-
consoleMessage: [],
47-
};
57+
},
58+
],
59+
},
60+
],
61+
},
62+
];
4863
},
64+
4965
components: {
5066
ContextMenu,
5167
},
@@ -55,6 +71,8 @@ export default {
5571
<style lang="scss">
5672
body, html {
5773
height: 100%;
74+
margin: 0;
75+
padding: 0;
5876
}
5977
6078
#app {
@@ -72,8 +90,8 @@ export default {
7290
}
7391
7492
.context-menu-trigger {
75-
width: 50%;
76-
margin: 2em auto;
93+
width: 100%;
94+
/*margin: 2em auto;*/
7795
padding: 2em;
7896
background: #c0c0c0;
7997
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"url": "https://github.com/Johnathan/vue-context-menu-popup/issues",
2020
"email": "johnathan.barrett@gmail.com"
2121
},
22-
"version": "1.0.2",
22+
"version": "1.0.4",
2323
"private": false,
2424
"scripts": {
2525
"serve": "vue-cli-service serve ./example/main.js --open",

src/components/ContextMenu.vue

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
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

Comments
 (0)