-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathContextMenu.vue
70 lines (60 loc) · 1.74 KB
/
ContextMenu.vue
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
<template>
<Teleport :to="`#${eleId}`">
<ContextSubMenuWrapperConstructor
ref="menuRef"
:options="options"
:show="show"
:container="container"
:isFullScreenContainer="!isNew"
@close="onClose"
>
<template v-for="(_, name) in slots" v-slot:[name]="data">
<slot :name="name" v-bind="data"/>
</template>
</ContextSubMenuWrapperConstructor>
</Teleport>
</template>
<script setup lang="ts">
import { Teleport, toRefs, ref, useSlots } from 'vue'
import type { PropType, VNode } from 'vue'
import type { ContextMenuInstance, MenuOptions } from './ContextMenuDefine'
import { genContainer } from "./ContextMenuUtils";
import ContextSubMenuWrapperConstructor from './ContextSubMenuWrapper.vue'
export type GlobalHasSlot = (name: string) => boolean;
export type GlobalRenderSlot = (name: string, params: Record<string, unknown>) => VNode;
/**
* Context menu component
*/
const emit = defineEmits([ 'update:show', 'close' ]);
const props = defineProps({
/**
* Menu options
*/
options: {
type: Object as PropType<MenuOptions>,
default: null
},
/**
* Show menu?
*/
show: {
type: Boolean,
default: false
},
})
const { options, show } = toRefs(props);
const { isNew, container, eleId } = genContainer(options.value);
const menuRef = ref<ContextMenuInstance | null>(null);
const slots = useSlots() as any;
function onClose(fromItem: undefined) {
emit('update:show', false);
emit('close');
options.value.onClose?.(fromItem);
}
defineExpose({
closeMenu: () => emit('update:show', false),
isClosed: () => !show.value,
getMenuRef: () => menuRef.value?.getMenuRef(),
getMenuDimensions: () => menuRef.value?.getMenuDimensions() ?? { width: 0, height: 0 },
});
</script>