mirror of
https://github.com/imdap/ruoyi-plus-vben5.git
synced 2026-05-13 06:42:10 +08:00
feat: refactor context menu to capture native events (#7858)
* feat: refactor context menu to capture native events prevent context-menu to show in html input fields * fix: refactor context-menu.vue for improved structure * chore: fix format * chore: remove dead code * chore: fix lint * fix: update contenteditable selector in context menu proposed fixed by coderabitai
This commit is contained in:
@@ -9,7 +9,7 @@ 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';
|
||||||
|
|
||||||
@@ -35,6 +35,14 @@ const props = defineProps<
|
|||||||
|
|
||||||
const emits = defineEmits<ContextMenuRootEmits>();
|
const emits = defineEmits<ContextMenuRootEmits>();
|
||||||
|
|
||||||
|
const NATIVE_CONTEXT_SELECTORS = [
|
||||||
|
'input',
|
||||||
|
'textarea',
|
||||||
|
'select',
|
||||||
|
'[contenteditable]:not([contenteditable="false"])',
|
||||||
|
'.allow-native-context',
|
||||||
|
].join(', ');
|
||||||
|
|
||||||
const delegatedProps = computed(() => {
|
const delegatedProps = computed(() => {
|
||||||
const {
|
const {
|
||||||
class: _cls,
|
class: _cls,
|
||||||
@@ -59,12 +67,34 @@ function handleClick(menu: IContextMenuItem) {
|
|||||||
}
|
}
|
||||||
menu?.handler?.(props.handlerData);
|
menu?.handler?.(props.handlerData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const triggerRef = ref<HTMLElement | null>(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,
|
||||||
|
});
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<ContextMenu v-bind="forwarded">
|
<ContextMenu v-bind="forwarded">
|
||||||
<ContextMenuTrigger as-child>
|
<ContextMenuTrigger as-child>
|
||||||
<slot></slot>
|
<div ref="triggerRef">
|
||||||
|
<slot></slot>
|
||||||
|
</div>
|
||||||
</ContextMenuTrigger>
|
</ContextMenuTrigger>
|
||||||
<ContextMenuContent
|
<ContextMenuContent
|
||||||
:class="contentClass"
|
:class="contentClass"
|
||||||
|
|||||||
Reference in New Issue
Block a user