Skip to content

Commit c73769b

Browse files
committed
Add isClosed instance function to check if the menu is currently closed. (#77)
1 parent f45a7a0 commit c73769b

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

library/ContextMenu.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export default defineComponent({
3939
4040
ctx.expose({
4141
closeMenu: () => ctx.emit('update:show', false),
42+
isClosed: () => !show.value,
4243
});
4344
4445
return () => {

library/ContextMenuDefine.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export interface ContextMenuInstance {
2424
* @param fromItem The last clicked menu item, will pass to `MenuOptions.onClose` callback, if user does not click any item, can be `undefined`.
2525
*/
2626
closeMenu(fromItem?: MenuItem|undefined): void;
27+
/**
28+
* Check if the menu is currently closed.
29+
*/
30+
isClosed(): boolean;
2731
}
2832

2933
export type MenuPopDirection = 'br'|'b'|'bl'|'tr'|'t'|'tl'|'l'|'r';

library/ContextMenuInstance.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,74 @@ export default {
5454
},
5555
/**
5656
* Show a ContextMenu in page, same as `this.$contextmenu`
57+
*
58+
* For example:
59+
*
60+
* ```ts
61+
* onContextMenu(e : MouseEvent) {
62+
* //prevent the browser's default menu
63+
* e.preventDefault();
64+
* //show your menu
65+
* ContextMenu.showContextMenu({
66+
* x: e.x,
67+
* y: e.y,
68+
* items: [
69+
* {
70+
* label: "A menu item",
71+
* onClick: () => {
72+
* alert("You click a menu item");
73+
* }
74+
* },
75+
* {
76+
* label: "A submenu",
77+
* children: [
78+
* { label: "Item1" },
79+
* { label: "Item2" },
80+
* { label: "Item3" },
81+
* ]
82+
* },
83+
* ]
84+
* });
85+
* }
86+
* ```
87+
*
88+
* You can pass customSlots to custom rendering this menu.
89+
*
90+
* For example, custom rendering #itemRender and #separatorRender:
91+
* ```ts
92+
* ContextMenu.showContextMenu({
93+
* ...
94+
* } as MenuOptions, {
95+
* //Use slot in function mode
96+
* itemRender: ({ disabled, label, icon, showRightArrow, onClick, onMouseEnter }) => [ h('div', {
97+
* class: 'my-menu-item'+(disabled?' disabled':''),
98+
* onMouseenter: onMouseEnter,
99+
* onClick: onClick,
100+
* }, [
101+
* icon ? h('img', { src: icon }) : h('div', { class: 'icon-place-holder' }),
102+
* h('span', label),
103+
* showRightArrow ? h('span', { class: 'right-arraw' }, '>>') : h('div'),
104+
* ]) ],
105+
* separatorRender: () => [ h('div', { class: 'my-menu-sperator' }) ]
106+
* })
107+
* ```
108+
*
57109
* @param options The options of ContextMenu
58110
* @param customSlots You can provide some custom slots to customize the rendering style of the menu. These slots are the same as the slots of component ContextMenu.
59111
* @returns Menu instance
60112
*/
61113
showContextMenu(options : MenuOptions, customSlots?: Record<string, Slot>) : ContextMenuInstance {
62114
return $contextmenu(options, customSlots);
63115
},
64-
//Close the currently open menu
116+
/**
117+
* Get if there is a menu open now.
118+
*/
119+
isAnyContextMenuOpen() {
120+
121+
},
122+
/**
123+
* Close the currently open menu
124+
*/
65125
closeContextMenu,
66126
//Tools
67127
transformMenuPosition,

library/ContextSubMenuWrapper.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ export default defineComponent({
6767
}
6868
});
6969
70-
const instance = { closeMenu };
70+
const instance = {
71+
closeMenu,
72+
isClosed,
73+
};
74+
let closed = false;
7175
7276
function openMenu() {
7377
installBodyEvents();
@@ -77,12 +81,17 @@ export default defineComponent({
7781
ctx.emit("update:show", false);
7882
ctx.emit("close", fromItem);
7983
84+
closed = true;
8085
removeOpenedContextMenu(instance);
8186
}
87+
function isClosed() {
88+
return closed;
89+
}
8290
8391
//Expose instance function
8492
ctx.expose({
8593
closeMenu: closeMenu,
94+
isClosed: isClosed,
8695
});
8796
8897
function installBodyEvents() {

0 commit comments

Comments
 (0)