1.0.3 • Published 4 years ago

@duoa/vue-context-menu v1.0.3

Weekly downloads
9
License
MIT
Repository
github
Last release
4 years ago

Vue Context Menu

Context Menu (Right Click Menu) base on Vue2.x

Take a glance

Online Demo

Installation

npm i -S @duoa/vue-context-menu

Usage

Register the component

import Vue from 'vue'
import VueContextMenu from '@duoa/vue-context-menu'
// Because this components has its styles, you must also import the css file.
import '@duoa/vue-context-menu/dist/vue-context-menu.css'

// Register
Vue.use(VueContextMenu)

// You can also pass the options
Vue.use(VueImgPreview, {
  directiveName = 'ContextMenu', // Customizable directive name
  prototypeName = '$contextMenu', // Customizable Vue.prototype method name
  zIndex: 9999 // Context Menu z-index
})

You may now use the v-context-menu directive in your markup.

<template>
  <button v-context-menu="options">Right Click This Button</button>
</template>
<script>
export default {
  computed: {
    options () {
      return {
        width: 200,
        actions: [
          {
            label: 'Copy',
            subLabel: 'Ctrl + C',
            disabled: false,
            onClick: () => {
              console.log('copy')
            }
          },
          {
            type: 'sep' // seperator with a horizontal line
          },
          ...
        ]
      }
    }
  }
}
</script>

That's all you need to do!

If you want to manually open an context menu, you can you this.$contextMenu method.

export default {
  methods: {
    handleClick (e) {
      e.preventDefault()
      this.$contextMenu({
        // You must calculate the menu position by yourself.
        left: e.pageX,
        top: e.pageY,
        action: [
          ...
        ]
      })
    }
  }
}

Configuration

NameTypeDefault ValueRequiredDescription
actionsarray[]--menu list
autoClosebooleanfalse--auto close context menu when mouse leaves.
maskbooleantrue--whether a mask is displayed
customClassstring----custom class names for context menu
zIndexnumber9999--menu z-index
widthnumber200--menu width
topnumber0--menu top
leftnumber0--menu left
onBeforefunction----triggers before the context menu opens
onCancelfunction----triggers after the context menu is canceled

action items configuration

NameTypeDefault ValueRequiredDescription
labelstring''--menu item label
subLabelstring''--menu item sub label
iconstring''--icon class name
disabledbooleanfalse--menu item disabled
onClickfunction----click handler