From 80552cb994db024c3b14fd75bd9047ae7d5704ad Mon Sep 17 00:00:00 2001 From: mew <88119911+doraemonxxx@users.noreply.github.com> Date: Mon, 27 Apr 2026 09:55:03 +0800 Subject: [PATCH] fix: refactor context-menu.vue for improved structure --- .../components/context-menu/context-menu.vue | 110 +++++++++++------- 1 file changed, 65 insertions(+), 45 deletions(-) diff --git a/packages/@core/ui-kit/shadcn-ui/src/components/context-menu/context-menu.vue b/packages/@core/ui-kit/shadcn-ui/src/components/context-menu/context-menu.vue index be234e4ab..975458119 100644 --- a/packages/@core/ui-kit/shadcn-ui/src/components/context-menu/context-menu.vue +++ b/packages/@core/ui-kit/shadcn-ui/src/components/context-menu/context-menu.vue @@ -3,15 +3,15 @@ import type { ContextMenuContentProps, ContextMenuRootEmits, ContextMenuRootProps, -} from 'reka-ui'; +} from "reka-ui"; -import type { ClassType } from '@vben-core/typings'; +import type { ClassType } from "@vben-core/typings"; -import type { IContextMenuItem } from './interface'; +import type { IContextMenuItem } from "./interface"; -import { computed } from 'vue'; +import { computed, onMounted, onUnmounted, ref } from "vue"; -import { useForwardPropsEmits } from 'reka-ui'; +import { useForwardPropsEmits } from "reka-ui"; import { ContextMenu, @@ -20,7 +20,7 @@ import { ContextMenuSeparator, ContextMenuShortcut, ContextMenuTrigger, -} from '../../ui/context-menu'; +} from "../../ui/context-menu"; const props = defineProps< ContextMenuRootProps & { @@ -36,12 +36,12 @@ const props = defineProps< const emits = defineEmits(); const NATIVE_CONTEXT_SELECTORS = [ - 'input', - 'textarea', - 'select', + "input", + "textarea", + "select", '[contenteditable="true"]', - '.allow-native-context', -].join(', '); + ".allow-native-context", +].join(", "); const delegatedProps = computed(() => { const { @@ -75,42 +75,62 @@ function handleContextMenuCapture(e: MouseEvent) { e.stopPropagation(); } } + +const triggerRef = ref(null); + +function onContextMenuCapture(e: MouseEvent) { + if ((e.target as HTMLElement).closest(NATIVE_CONTEXT_SELECTORS)) { + e.stopPropagation(); + } +} + +onMounted(() => { + triggerRef.value?.addEventListener("contextmenu", onContextMenuCapture, { + capture: true, + }); +}); + +onUnmounted(() => { + triggerRef.value?.removeEventListener("contextmenu", onContextMenuCapture, { + capture: true, + }); +});