11<template >
2- <div class =" mx-menu-ghost-host" >
2+ <div :id = " menuHostId " class =" mx-menu-ghost-host" >
33 <Transition
44 v-if =" options.menuTransitionProps"
55 appear
99 <ContextSubMenuConstructor
1010 v-if =" show"
1111 ref =" submenuInstance"
12- class =" mx-menu-host"
1312 :items =" options.items"
1413 :adjustPosition =" options.adjustPosition"
1514 :maxWidth =" options.maxWidth || MenuConstOptions.defaultMaxWidth"
2221 <ContextSubMenuConstructor
2322 v-else-if =" show"
2423 ref =" submenuInstance"
25- class =" mx-menu-host"
2624 :items =" options.items"
2725 :adjustPosition =" options.adjustPosition"
2826 :maxWidth =" options.maxWidth || MenuConstOptions.defaultMaxWidth"
@@ -40,6 +38,7 @@ import type { ContextMenuInstance, ContextSubMenuInstance, MenuItem, MenuOptions
4038import { MenuConstOptions } from ' ./ContextMenuDefine'
4139import { addOpenedContextMenu , removeOpenedContextMenu } from ' ./ContextMenuMutex' ;
4240import ContextSubMenuConstructor , { type SubMenuContext , type SubMenuParentContext } from ' ./ContextSubMenu.vue' ;
41+ import { genSubContainerId } from ' ./ContextMenuUtils' ;
4342
4443/**
4544 * Context menu component
@@ -131,7 +130,6 @@ function isClosed() {
131130 return closed ;
132131}
133132
134-
135133function installBodyEvents() {
136134 setTimeout (() => {
137135 document .addEventListener (" click" , onBodyClick , true );
@@ -155,7 +153,9 @@ function removeBodyEvents() {
155153
156154// For keyboard event, remember which submenu is active
157155const currentOpenedMenu = ref <SubMenuContext | null >();
156+ const menuHostId = genSubContainerId ();
158157provide (' globalSetCurrentSubMenu' , (menu : SubMenuContext | null ) => currentOpenedMenu .value = menu );
158+ provide (' globalGetMenuHostId' , menuHostId );
159159
160160function onMenuKeyDown(e : KeyboardEvent ) {
161161 let handled = true ;
@@ -202,28 +202,34 @@ function onMenuKeyDown(e: KeyboardEvent) {
202202 e .preventDefault ();
203203 }
204204}
205- function onBodyScroll() {
205+ function onBodyScroll(e : Event ) {
206206 // close when docunment scroll
207- if (options .value .closeWhenScroll !== false )
208- closeMenu ();
207+ if (options .value .closeWhenScroll !== false ) {
208+ checkTargetAndClose (e .target as HTMLElement , null );
209+ }
209210}
210211function onBodyClick(e : MouseEvent ) {
211212 checkTargetAndClose (e .target as HTMLElement , e );
212213}
213- function checkTargetAndClose(target : HTMLElement , e : MouseEvent ) {
214+ function checkTargetAndClose(target : HTMLElement , e : MouseEvent | null ) {
214215 // Loop target , Check whether the currently clicked element belongs to the current menu.
215216 // If yes, it will not be closed
216217 while (target ) {
217- if (target .classList && target .classList .contains (' mx-menu-host ' ))
218+ if (target .classList && target .classList .contains (' mx-context-menu ' ))
218219 return ;
219220 target = target .parentNode as HTMLElement ;
220221 }
221- if (options .value .clickCloseOnOutside !== false ) {
222- // Close menu
222+ if (e ) {
223+ if (options .value .clickCloseOnOutside !== false ) {
224+ // Close menu
225+ removeBodyEvents ();
226+ closeMenu ();
227+ } else {
228+ options .value .onClickOnOutside ?.(e );
229+ }
230+ } else {
223231 removeBodyEvents ();
224232 closeMenu ();
225- } else {
226- options .value .onClickOnOutside ?.(e );
227233 }
228234}
229235
@@ -244,11 +250,7 @@ provide('menuContext', {
244250 zIndex: options .value .zIndex || MenuConstOptions .defaultZindex ,
245251 container: container .value as unknown as HTMLElement ,
246252 adjustPadding: { x: 0 , y: 0 },
247- getParentAbsY : () => options .value .x ,
248- getParentAbsX : () => options .value .y ,
249253 getZoom : () => options .value .zoom || MenuConstOptions .defaultZoom ,
250- getParentX : () => 0 ,
251- getParentY : () => 0 ,
252254 getParentWidth : () => 0 ,
253255 getParentHeight : () => 0 ,
254256 getPositon : () => [options .value .x ,options .value .y ],
0 commit comments