Usage
Import
import '@imengyu/vue3-context-menu/lib/vue3-context-menu.css'
import ContextMenu from '@imengyu/vue3-context-menu'
createApp(App).use(ContextMenu)
Show menu
There are two ways to display menus:
The first is the function mode. You can use this.$contextmenu
or showContextMenu
global function displays a menu with menu data:
<template> <h1>Hello {{ msg }}</h1> </template> <script setup lang="ts"> import { ref } from 'vue'; const msg = ref<string>('world'); </script>
Note:
this.$contextmenu
can only be used in templates or optional api.
The second is the component mode. You can use the component and template to display the menu:
<template> <h1>Hello {{ msg }}</h1> </template> <script setup lang="ts"> import { ref } from 'vue'; const msg = ref<string>('world'); </script>
Dynamic change menu
You only need to declare the menu data as responsive data, so that you can dynamically modify the menu:
Function mode
const menuData = reactive({
items: [
{
label: 'Simple item',
onClick: () => alert('Click Simple item'),
},
]
});
//Use in function mode
ContextMenu.showContextMenu(menuData);
//You can change properties at any time after the menu is displayed:
menuData.items[0].label = 'My label CHANGED!'; //Change label
menuData.items[0].hidden = true; //Change hidden
Component mode
The dynamic control of component mode is simpler. You can use the v-if
directive to dynamically control the display, or you can directly bind menu parameters to variables. The changes are just like normal components.
The following example shows how to dynamically display/hide menus and dynamically modify menu item text in component mode:
<template>
<context-menu
v-model:show="show"
:options="optionsComponent"
>
<context-menu-item label="Test item dynamic show and hide" :clickClose="false" @click="showItem=!showItem" />
<context-menu-item v-if="showItem" label="Click the item above to show/hide me" />
<context-menu-sperator v-if="showItem" />
<context-menu-item :label="itemText" :clickClose="false" @click="changeLabelText" />
</context-menu>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import type { MenuOptions } from '@imengyu/vue3-context-menu';
export default defineComponent({
data() {
return {
show: false,
showItem: true,
itemText: 'Test item dynamic change label',
optionsComponent: {
x: 500,
y: 200
} as MenuOptions,
}
},
methods: {
onContextMenu(e : MouseEvent) {
e.preventDefault();
//Set the mouse position
this.optionsComponent.x = e.x;
this.optionsComponent.y = e.y;
//Show menu
this.show = true;
},
changeLabelText() {
this.itemText = (this.itemText == 'My label CHANGED!' ? 'Test item dynamic change label' : 'My label CHANGED!');
},
}
});
</script>
Menu bar
This library comes with a menu bar component, which is very similar to the function of the right-click menu and the menu bar. Therefore, it is integrated into the library, and you can also use this component to implement the main menu bar function of desktop programs.
<template> <h1>Hello {{ msg }}</h1> </template> <script setup lang="ts"> import { ref } from 'vue'; const msg = ref<string>('world'); </script>