zs
2025-05-08 67ccbefb82b868fa1aa4bada181ed9dd2ddf92b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import type { SVGAttributes, TransitionProps, VNode } from "vue";
 
/**
 * Default config
 */
export const MenuConstOptions = {
  defaultDirection: 'br',
  defaultMinWidth: 100,
  defaultMaxWidth: 600,
  defaultZindex: 100,
  defaultAdjustPadding: {
    x: 0,
    y: 10.
  },
}
 
/**
 * ContextMenu Component ref interface, 
 * You can use `(this.$refs.myMenu as ContextMenuInstance)` or `const mymenu = ref<ContextMenuInstance>()`
 */
export interface ContextMenuInstance {
  /**
   * Close this menu.
   * @param fromItem The last clicked menu item, will pass to `MenuOptions.onClose` callback, if user does not click any item, can be `undefined`.
   */
  closeMenu(fromItem?: MenuItem|undefined): void;
  /**
   * Check if the menu is currently closed.
   */
  isClosed(): boolean;
}
 
export type MenuPopDirection = 'br'|'b'|'bl'|'tr'|'t'|'tl'|'l'|'r';
export type MenuChildren = MenuItem[];
 
 
export interface MenuOptions {
  /**
   * The items for this menu.
   */
  items    ?:    MenuItem[];
  /**
   * Menu display x position.
   */
  x: number;
  /**
   * Menu display y position.
   */
  y: number;
  /**
  * X-coordinate offset of submenu and parent menu.
  */
  xOffset    ?: number;
  /**
  * Y-coordinate offset of submenu and parent menu.
  */
  yOffset ?: number;
  /**
   * Set 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.
   * 
   * @default 'br'
   */
  direction ?: MenuPopDirection,
  /**
   * The z-index of this menu.
   */
  zIndex ?: number;
  /**
   * Custom menu class.
   */
  customClass    ?:    string;
  /**
   * Set whether users can use the mouse scroll wheel to scroll through long menus in the menu area.
   * 
   * @default false
   */
  mouseScroll ?: boolean;
  /**
   * Determine 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.
   * 
   * @default false
   */
  updownButtonSpaceholder?: boolean;
  /**
   * Theme for this menu. Default is 'default'
   * 
   * |theme|explain|example image|
   * |--|--|--|
   * |`default`|Default theme|![example-default-dark.jpg](https://raw.githubusercontent.com/imengyu/vue3-context-menu/main/screenshot/example-default.jpg)|
   * |`default dark`|Default theme with dark|![example-default-dark.jpg](https://raw.githubusercontent.com/imengyu/vue3-context-menu/main/screenshot/example-default-dark.jpg)|
   * |`flat`|Simple flat theme|![example-default-dark.jpg](https://raw.githubusercontent.com/imengyu/vue3-context-menu/main/screenshot/example-flat.jpg)|
   * |`flat dark`|Simple flat theme with dark|![example-default-dark.jpg](https://raw.githubusercontent.com/imengyu/vue3-context-menu/main/screenshot/example-flat-dark.jpg)|
   * |`win10`|Win10 like theme|![example-default-dark.jpg](https://raw.githubusercontent.com/imengyu/vue3-context-menu/main/screenshot/example-win10.jpg)|
   * |`win10 dark`|Win10 like theme with dark|![example-default-dark.jpg](https://raw.githubusercontent.com/imengyu/vue3-context-menu/main/screenshot/example-win10-dark.jpg)|
   * |`mac`|Mac like theme|![example-default-dark.jpg](https://raw.githubusercontent.com/imengyu/vue3-context-menu/main/screenshot/example-mac.jpg)|
   * |`mac dark`|Mac like theme with dark|![example-default-dark.jpg](https://raw.githubusercontent.com/imengyu/vue3-context-menu/main/screenshot/example-mac-dark.jpg)|
   * 
   * You can write new theme in your own css, 
   * customize your theme by overriding the default styles, for example:
   * ```scss
   * .mx-context-menu.my-theme-name {
      & {
        //Here can override css vars
        --mx-menu-backgroud: #ececec;
        --mx-menu-hover-backgroud: #0165e1;
      }
      
      //Customize the style of the menu here
      padding: 8px 0;
      box-shadow: 0px 5px 7px 1px var(--mx-menu-shadow-color);
      border: 1px solid var(--mx-menu-border-color);
 
      //Customize the style of the menu item
      .mx-context-menu-item {
        border-radius: 5px;
        margin: 0 6px;
        padding: 3px 6px;
      }
    }
   * ```
   */
  theme    ?: string;
  /**
   * If your element in menu item has this className, click it will ignore event. 
   */
  ignoreClickClassName?: string,
  /**
   * Set should close menu when the user click on other places.
   * 
   * @default true
   */
  clickCloseOnOutside ?: boolean;
  /**
   * If your element in menu item has this className, click it will ignore event and close hole menu. 
   */
  clickCloseClassName?: string,
  /**
   * Custom icon library font class name. (global)
   * 
   * Only for css font icon, If you use the svg icon, you do not need to use this.
   */
  iconFontClass ?:    string;
  /**
   * The Vue Transition props used when menu show or hide.
   * @default undefined
   */
  menuTransitionProps ?: TransitionProps;
  /**
   * Should a fixed-width icon area be reserved for menu items without icon. (global)
   * 
   * Default is true.
   * 
   * The width of icon area can be override with css var `--mx-menu-placeholder-width`.
   */
  preserveIconWidth ?: boolean;
  /**
   * Set whether the user can use keyboard keys to control the current menu.
   * 
   * Default: true
   * 
   * The control logic is consistent with the Windows right-click menu:
   * * Escape : Close current menu
   * * Enter : Click current menu item
   * * ArrowDown : Select the down menu item
   * * ArrowUp : Select the up menu item
   * * ArrowLeft : Back previous submenu
   * * ArrowRight : Open current menu item submenu
   * * Home : Select the first menu item
   * * End : Select the last menu item
   */
  keyboardControl ?: boolean;
  /**
   * Maximum width of main menu (in pixels)
   */
  maxWidth ?: number;
  /**
   * Minimum width of main menu (pixels)
   */
  minWidth ?: number;
  /**
   * Close when user scroll mouse ? Default is true.
   */
  closeWhenScroll ?: boolean;
  /**
   * Padding for submenu position adjust. Default is `{ x:0, y: 10 }`.
   */
  adjustPadding?: { x: number, y: number }|number,
  /**
   * By 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.
   * 
   * Default is true.
   */
  adjustPosition?: boolean,
  /**
   * Return the mounted node for MenuRoot.
   * 
   * Note: After you change the mount node, the menu display position may be incorrect. 
   * 
   * * The MenuOptions.x is the distance from the menu to the left edge of the container (container should `position: relative;`);
   * * The MenuOptions.y is the distance from the menu to the top edge of the container (container should `position: relative;`);;
   * 
   * So, you need to change the x and y values you passed in to ensure that the display position is correct.
   * 
   * You may need to use `ContextMenu.transformMenuPosition` to transform the menu display position:
   * 
   * ```
   * function onContextMenu(e: MouseEvent) {
      //MyContainerElement is the MenuRoot
      const scaledPosition = ContextMenu.transformMenuPosition(e.target as HTMLElement, e.offsetX, e.offsetY, MyContainerElement);
      menuData.x = scaledPosition.x;
      menuData.y = scaledPosition.y;
      //show menu
      ContextMenu.showContextMenu(menuData);
    }
   * ```
   */
  getContainer ?: HTMLElement | (() => HTMLElement);
 
  /**
   * This 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.
   */
  onClose ?: ((lastClickItem: MenuItem|undefined) => void) | undefined;
  /**
   * Event for MenuBar component
   */
  onKeyFocusMoveLeft ?:  (() => void) | undefined;
  /**
   * Event for MenuBar component
   */
  onKeyFocusMoveRight ?:  (() => void) | undefined;
}
export interface MenuItem {
  /**
   * The label of this menu item.
   * 
   * Can be a callback. Use `h` to render custom content.
   * 
   * ```js
   * { 
   *   label: h('div', {
   *     style: {
   *       fontSize: '20px',
   *       color: '#f98',
   *     }
   *   }, "Item with custom render"),
   * },
   * ```
   */
  label ?: string|VNode|((label: string) => VNode),
  /**
   * The icon for this menu item.
   */
  icon ?: string|VNode|((icon: string) => VNode),
  /**
   * Custom icon library font class name.
   * 
   * Only for css font icon, If you use the svg icon, you do not need to use this.
   */
  iconFontClass ?: string,
  /**
   * Should a fixed-width icon area be reserved for menu items without icon. (this item)
   * 
   * Default is inherit from `MenuOptions.preserveIconWidth` .
   * 
   * The width of icon area can be override with css var `--mx-menu-placeholder-width`.
   */
  preserveIconWidth ?: boolean;
  /**
   * Display icons use svg symbol (`<use xlink:href="#icon-symbol-name">`) , only valid when icon attribute is empty.
   */
  svgIcon ?: string,
  /**
   * The user-defined attribute of the svg tag, which is valid when using `svgIcon`.
   */
  svgProps ?: SVGAttributes,
  /**
   * Disable menu item?
   */
  disabled ?: boolean,
  /**
   * Hide menu item?
   */
  hidden ?: boolean,
  /**
   * Is this menu item checked?
   * 
   * The check mark are displayed on the left side of the icon, so it is not recommended to display the icon at the same time.
   */
  checked ?: boolean,
  /**
   * Shortcut key text display on the right.
   * 
   * The shortcut keys here are only for display. You need to handle the key events by yourself.
   */
  shortcut ?: string|string,
  /**
   * Set 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.
   * 
   */
  direction ?: MenuPopDirection,
  /**
   * By 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.
   * 
   * Default is inherit from `MenuOptions.adjustPosition`.
   */
  adjustSubMenuPosition ?: boolean,
  /**
   * When there are subitems in this item, is it allowed to trigger its own click event? Default is false
   */
  clickableWhenHasChildren ?: boolean,
  /**
   * Should close menu when Click this menu item ?
   */
  clickClose ?: boolean,
  /**
   * Is this menu item separated from the menu item?
   * 
   * * `true` or `'down'`: Separator is show below menu.
   * * `'up'`: Separator is show above menu.
   * * `'self'`: Mark this item is a Separator.
   * * `false`: No Separator.
   */
  divided ?: boolean|'up'|'down'|'self',
  /**
   * Custom css class for submenu
   */
  customClass ?: string,
  /**
   * Submenu maximum width (in pixels).
   */
  maxWidth ?: number|string,
  /**
   * Submenu minimum width (in pixels).
   */
  minWidth ?: number|string,
  /**
   * Menu item click event handler.
   * 
   * @param e The current event of `click` or `keydown` (when user use keyboard press this menu)
   */
  onClick ?: (e?: MouseEvent|KeyboardEvent) => void,
  /**
   * This event emit when submenu of this item is closing.
   */
  onSubMenuClose ?: (() => void) | undefined;
  /**
   * This event emit when submenu of this item is showing.
   */
  onSubMenuOpen ?: (() => void) | undefined;
  /**
   * A custom render callback that allows you to customize the rendering
   *  of the current item.
   */
  customRender ?: VNode|((item: MenuItem) => VNode),
  /**
   * Child menu items (Valid in function mode).
   */
  children ?: MenuChildren,
}
 
export interface ContextMenuPositionData {
  x: number;
  y: number;
}
 
export declare interface MenuItemRenderData extends Omit<MenuItem, 'children'|'customRender'|'onClick'> {
  /**
   * Global theme
   */
  theme: 'light'|'dark',
  /**
   * This value indicates whether the current menu submenu is open
   */
  isOpen: boolean,
  /**
   * This value indicates whether the current menu has submenus
   */
  hasChildren: boolean,
  /**
   * Click event callback of custom element, which is used for menu internal event
   */
  onClick: (e: MouseEvent) => void,
  /**
   * MouseEnter event callback of custom element, which is used for menu internal event
   */
  onMouseEnter: (e: MouseEvent) => void,
}