Skip to content

Function mode

ContextMenu

ts
import ContextMenu from '@imengyu/vue3-context-menu'

Global Function

ContextMenu.showContextMenu(options: MenuOptions, customSlots?: Record<string, Slot>)

Show context menu.

ParamDescription
optionsThe options of menu.
customSlotsThese slots to allow you to customize the style of the current menu, the names of these slots are the same as those in the component mode. For customization methods, please refer in here.

Return

TypeDescription
ContextMenuInstanceThe instance of the current menu, can calling closeMenu to close this menu, calling isClosed to check if the menu is currently closed.

ContextMenu.closeContextMenu()

Manually close the currently open context menu.

this.$contextmenu

Same as ContextMenu.showContextMenu but this function is registered to vue global property.

ContextMenu.transformMenuPosition

If your body element is in a scaled state (e.g. transform: scale(0.5)), this may lead to the wrong position of the menu display. You can use this function to convert the correct menu display position:

ts
import ContextMenu from '@imengyu/vue3-context-menu'

function onContextMenu(e: MouseEvent) {
  const scaledPosition = ContextMenu.transformMenuPosition(e.target as HTMLElement, e.offsetX, e.offsetY);
  //Full code of menuData is in `/examples/views/InScaledBody.vue`
  menuData.x = scaledPosition.x;
  menuData.y = scaledPosition.y;
  //show menu
  ContextMenu.showContextMenu(menuData);
}
PropertyDescriptionTypeOptional valueDefault
items
The items for this menu.
MenuItem[]
xMenu display x position.number0
yMenu display y position.number0
xOffsetX-coordinate offset of submenu and parent menu.number0
yOffsetY-coordinate offset of submenu and parent menu.number0
iconFontClassCustom icon library font class name. (global). Only for css font icon, If you use the svg icon, you do not need to use this.stringiconfont
zIndexThe z-index of this menunumber2
iconFontClassCustom icon library font class name.stringiconfont
preserveIconWidthShould a fixed-width icon area be reserved for menu items without icon.boolean-true
keyboardControlSet whether the user can use keyboard keys to control the current menu.booleantrue
closeWhenScrollSet whether to close the menu when the user scrolls the mouse.boolean-true
mouseScrollSet whether users can use the mouse scroll wheel to scroll through long menus in the menu area.booleanfalse
themeThe theme of this menu.string'default' 'dark' 'flat' 'win10' 'mac'default
minWidthSubmenu minimum width (in pixels).number or string100
maxWidthSubmenu maximum width (in pixels).number or string600
adjustPaddingPadding for submenu position adjust.{ x: number, y: number } or number{ x:0, y: 10 }
adjustPositionBy default, the menu will automatically adjust its position to prevent it overflow the container. If you allow menu overflow containers, you can set this to false.booleantrue
directionSet the mian menu pop-up direction relative to coordinates. Default is 'br', if adjustPosition is true then the menu will determine the pop-up direction based on its distance from the screen edge.'br' or 'b' or 'bl' or 'tr' or 't' or 'tl' or 'l' or 'r''br'
menuTransitionPropsThe Vue Transition props used when menu show or hide.TransitionProps
ignoreClickClassNameIf your element in menu item has this className, click it will ignore event.string
clickCloseOnOutsideSet should close menu when the user click on other places.booleantrue
clickCloseClassNameIf your element in menu item has this className, click it will ignore event and close hole menu.string
updownButtonSpaceholderDetermine whether the up/down buttons in the menu item require space holder. The purpose of this variable is because some menu themes add blank padding above and below the menu, which are just enough to place up/down buttons. If there is no blank padding in your custom menu theme, you can set this field to provide blank space for up/down buttons to prevent obscuring menu items.booleanfalse
getContainerReturn the mounted node for MenuRoot. GuideHTMLElement or (() => HTMLElement)
onCloseThis event emit when this menu is closing. (Usually used in function mode). Param lastClickItem The last clicked menu item, if user does not click any item, it is undefined. This param only valid in function mode.`((lastClickItem: MenuItemundefined) => void)`
PropertyDescriptionTypeOptional valueDefault
label
The label of menu.
string or VNode or ((label: string) => VNode)
iconThe icon for menu item.string or VNode or ((icon: string) => VNode)
iconFontClassCustom icon library font class name.stringiconfont
preserveIconWidthShould a fixed-width icon area be reserved for menu items without icon.boolean-true
svgIconDisplay icons use svg symbol (<use xlink:href="...">) , only valid when icon attribute is empty.string
svgPropsThe user-defined attribute of the svg tag, which is valid when using svgIcon.SVGAttributes
disabledDisable menu item?booleanfalse
hiddenHide menu item?booleanfalse
checkedIs this menu item checked?booleanfalse
shortcutShortcut key text display on the right. The shortcut keys here are only for display. You need to handle the key events by yourself.string''
adjustSubMenuPositionBy default, the submenu will automatically adjust its position to prevent it overflow the container. If you allow menu overflow containers, you can set this to false.booleaninherit from MenuOptions.adjustPosition
clickableWhenHasChildrenWhen there are subitems in this item, is it allowed to trigger its own click event?booleanfalse
clickCloseShould close menu when Click this menu item ?booleantrue
dividedShould show Separator?
  • true or 'down': Separator is show below menu.
  • 'up': Separator is show above menu.
  • 'self': Mark this item is a Separator.
  • false: No Separator.
boolean or 'up' or 'down' or 'self'false
customClassCustom submenu class.string
minWidthSubmenu minimum width (in pixels).number or string100
maxWidthSubmenu maximum width (in pixels).number or string600
directionSet the submenu pop-up direction relative to coordinates. Default is inherted from MenuOptions.direction, if adjustSubMenuPosition is true then the submenu will determine the pop-up direction based on its distance from the screen edge.'br' or 'b' or 'bl' or 'tr' or 't' or 'tl' or 'l' or 'r'inherit from MenuOptions.direction
onClickMenu item click event handler.Function()
onSubMenuCloseThis event emit when submenu of this item is closing.(() => void)
onSubMenuOpenThis event emit when submenu of this item is showing.(() => void)
customRenderA custom render callback that allows you to customize the rendering of the current item.VNode or ((item: MenuItemRenderData) => VNode)
childrenSubmenu items.MenuItem[]

Released under the MIT License.