Vue 2 Context Menu Component
This package, `vue-context-menu`, provides a simple context menu component specifically designed for Vue 2 applications. It allows developers to easily integrate custom right-click menus into their Vue 2 projects, attaching them to specific DOM elements. The latest stable version is 2.0.6, last published in July 2017, indicating that the project is effectively abandoned with no active maintenance or updates. Its release cadence was sporadic and has ceased. Key differentiators at the time were its straightforward API for attaching menus via `v-ref` and handling `contextmenu.prevent` events. However, it lacks features common in modern alternatives like built-in theming, nested menus, or programmatic API options, and is fundamentally incompatible with Vue 3. For new projects or those using Vue 3, alternative context menu libraries are highly recommended.
Common errors
-
Failed to resolve component: context-menu
cause The `contextMenu` component was imported but not registered in the Vue component's `components` option, or there's a typo in the component name.fixEnsure `import contextMenu from 'vue-context-menu'` is present and `components: { contextMenu }` is correctly defined in your Vue component's script section. -
TypeError: Cannot read properties of undefined (reading 'open')
cause The `$refs.ctxMenu` might not be available at the time `$refs.ctxMenu.open()` is called, typically because the component isn't mounted yet or the `ref` attribute on `context-menu` is misspelled or missing.fixVerify that `ref="ctxMenu"` is correctly assigned to the `<context-menu>` element and that the component is fully mounted before attempting to access it via `$refs`.
Warnings
- breaking This package is strictly for Vue 2.x applications. It is not compatible with Vue 3 and attempting to use it will result in critical errors due to significant API changes between Vue major versions.
- gotcha The package is effectively abandoned, with the last npm publish in July 2017. This means no new features, bug fixes, security updates, or compatibility with newer JavaScript ecosystems (e.g., Vite, modern tooling) should be expected.
- breaking Versions prior to 1.x (e.g., 0.0.13) were built for Vue 1.x. Using these older versions with Vue 2.x will cause breakage.
- gotcha The context menu relies on the `@contextmenu.prevent` event modifier. Forgetting this modifier will cause both the custom context menu and the browser's native context menu to appear simultaneously.
Install
-
npm install vue-context-menu -
yarn add vue-context-menu -
pnpm add vue-context-menu
Imports
- contextMenu
const contextMenu = require('vue-context-menu')import contextMenu from 'vue-context-menu'
- Component Registration
export default { components: { contextMenu } }
Quickstart
<template>
<div @contextmenu.prevent="$refs.ctxMenu.open">
Right-click me for a context menu!
</div>
<context-menu id="my-context-menu" ref="ctxMenu">
<li @click="doSomething('Option 1 clicked')">Option 1</li>
<li class="disabled">Option 2 (Disabled)</li>
<li @click="doSomething('Option 3 clicked')">Option 3</li>
</context-menu>
</template>
<script>
import contextMenu from 'vue-context-menu'
export default {
name: 'ContextMenuDemo',
components: { contextMenu },
methods: {
doSomething(message) {
alert(message)
// Optionally close the menu after action
this.$refs.ctxMenu.close()
}
}
}
</script>
<style>
/* Basic styling for the context menu for visibility */
#my-context-menu {
border: 1px solid #ccc;
background: white;
box-shadow: 2px 2px 5px rgba(0,0,0,0.2);
list-style: none;
padding: 5px 0;
margin: 0;
position: fixed;
z-index: 1000;
}
#my-context-menu li {
padding: 8px 15px;
cursor: pointer;
}
#my-context-menu li:hover:not(.disabled) {
background-color: #f0f0f0;
}
#my-context-menu li.disabled {
color: #aaa;
cursor: not-allowed;
}
</style>