Skip to content

Commit

Permalink
fix(ContextMenu): flash of closing and opening when interact with tri…
Browse files Browse the repository at this point in the history
…gger
  • Loading branch information
zernonia committed Nov 30, 2024
1 parent 8f40689 commit 635b90e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/ContextMenu/ContextMenuContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ const hasInteractedOutside = ref(false)
"
@interact-outside="
(event) => {
const originalEvent = event.detail.originalEvent as PointerEvent
// Prevent closing when right click (button=2) with the trigger element
if (originalEvent.button === 2 && event.target === rootContext.triggerElement.value) {
event.preventDefault()
}
if (!event.defaultPrevented && !rootContext.modal.value)
hasInteractedOutside = true;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/ContextMenu/ContextMenuRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type ContextMenuRootContext = {
onOpenChange: (open: boolean) => void
modal: Ref<boolean>
dir: Ref<Direction>
triggerElement: Ref<HTMLElement | undefined>
}
export interface ContextMenuRootProps extends Omit<MenuProps, 'open'> {}
Expand All @@ -35,6 +36,7 @@ useForwardExpose()
const dir = useDirection(propDir)
const open = ref(false)
const triggerElement = ref<HTMLElement>()
provideContextMenuRootContext({
open,
Expand All @@ -43,6 +45,7 @@ provideContextMenuRootContext({
},
dir,
modal,
triggerElement,
})
watch(open, (value) => {
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/ContextMenu/ContextMenuTrigger.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface ContextMenuTriggerProps extends PrimitiveProps {
</script>

<script setup lang="ts">
import { computed, nextTick, ref, toRefs } from 'vue'
import { computed, nextTick, onMounted, ref, toRefs } from 'vue'
import { injectContextMenuRootContext } from './ContextMenuRoot.vue'
import { isTouchOrPen } from './utils'
import { useForwardExpose } from '@/shared'
Expand All @@ -30,7 +30,7 @@ const props = withDefaults(defineProps<ContextMenuTriggerProps>(), {
})
const { disabled } = toRefs(props)
const { forwardRef } = useForwardExpose()
const { forwardRef, currentElement } = useForwardExpose()
const rootContext = injectContextMenuRootContext()
const point = ref<Point>({ x: 0, y: 0 })
const virtualEl = computed(() => ({
Expand Down Expand Up @@ -86,6 +86,12 @@ async function handlePointerEvent(event: PointerEvent) {
clearLongPress()
}
}
onMounted(() => {
if (currentElement.value) {
rootContext.triggerElement.value = currentElement.value
}
})
</script>

<template>
Expand All @@ -102,6 +108,7 @@ async function handlePointerEvent(event: PointerEvent) {
:data-disabled="disabled ? '' : undefined"
:style="{
WebkitTouchCallout: 'none',
pointerEvents: 'auto',
}"
v-bind="$attrs"
@contextmenu="handleContextMenu"
Expand Down

0 comments on commit 635b90e

Please # to comment.