{"version":3,"file":"ContextMenuTrigger.cjs","sources":["../../src/ContextMenu/ContextMenuTrigger.vue"],"sourcesContent":["<script lang=\"ts\">\nimport type { Point } from '@/Menu/utils'\nimport type { PrimitiveProps } from '@/Primitive'\n\nexport interface ContextMenuTriggerProps extends PrimitiveProps {\n  /**\n   * When `true`, the context menu would not open when right-clicking.\n   *\n   * Note that this will also restore the native context menu.\n   */\n  disabled?: boolean\n}\n</script>\n\n<script setup lang=\"ts\">\nimport { computed, nextTick, onMounted, ref, toRefs } from 'vue'\nimport { injectContextMenuRootContext } from './ContextMenuRoot.vue'\nimport { isTouchOrPen } from './utils'\nimport { useForwardExpose } from '@/shared'\nimport { Primitive } from '@/Primitive'\nimport { MenuAnchor } from '@/Menu'\n\ndefineOptions({\n  inheritAttrs: false,\n})\n\nconst props = withDefaults(defineProps<ContextMenuTriggerProps>(), {\n  as: 'span',\n  disabled: false,\n})\nconst { disabled } = toRefs(props)\n\nconst { forwardRef, currentElement } = useForwardExpose()\nconst rootContext = injectContextMenuRootContext()\nconst point = ref<Point>({ x: 0, y: 0 })\nconst virtualEl = computed(() => ({\n  getBoundingClientRect: () =>\n    ({\n      width: 0,\n      height: 0,\n      left: point.value.x,\n      right: point.value.x,\n      top: point.value.y,\n      bottom: point.value.y,\n      ...point.value,\n    } as DOMRect),\n}))\n\nconst longPressTimer = ref(0)\nfunction clearLongPress() {\n  window.clearTimeout(longPressTimer.value)\n}\n\nfunction handleOpen(event: MouseEvent | PointerEvent) {\n  point.value = { x: event.clientX, y: event.clientY }\n  rootContext.onOpenChange(true)\n}\n\nasync function handleContextMenu(event: PointerEvent) {\n  if (!disabled.value) {\n    await nextTick()\n    if (!event.defaultPrevented) {\n      clearLongPress()\n      handleOpen(event)\n      event.preventDefault()\n    }\n  }\n}\n\nasync function handlePointerDown(event: PointerEvent) {\n  if (!disabled.value) {\n    await nextTick()\n\n    if (isTouchOrPen(event) && !event.defaultPrevented) {\n      // clear the long press here in case there's multiple touch points\n      clearLongPress()\n      longPressTimer.value = window.setTimeout(() => handleOpen(event), 700)\n    }\n  }\n}\n\nasync function handlePointerEvent(event: PointerEvent) {\n  if (!disabled.value) {\n    await nextTick()\n    if (isTouchOrPen(event) && !event.defaultPrevented)\n      clearLongPress()\n  }\n}\n\nonMounted(() => {\n  if (currentElement.value) {\n    rootContext.triggerElement.value = currentElement.value\n  }\n})\n</script>\n\n<template>\n  <MenuAnchor\n    as=\"template\"\n    :reference=\"virtualEl\"\n  />\n\n  <Primitive\n    :ref=\"forwardRef\"\n    :as=\"as\"\n    :as-child=\"asChild\"\n    :data-state=\"rootContext.open.value ? 'open' : 'closed'\"\n    :data-disabled=\"disabled ? '' : undefined\"\n    :style=\"{\n      WebkitTouchCallout: 'none',\n      pointerEvents: 'auto',\n    }\"\n    v-bind=\"$attrs\"\n    @contextmenu=\"handleContextMenu\"\n    @pointerdown=\"handlePointerDown\"\n    @pointermove=\"handlePointerEvent\"\n    @pointercancel=\"handlePointerEvent\"\n    @pointerup=\"handlePointerEvent\"\n  >\n    <slot />\n  </Primitive>\n</template>\n"],"names":["toRefs","useForwardExpose","injectContextMenuRootContext","ref","computed","nextTick","isTouchOrPen","onMounted"],"mappings":";;;;;;;;;;;;;;;;;;;;AA0BA,IAAA,MAAM,KAAQ,GAAA,OAAA;AAId,IAAA,MAAM,EAAE,QAAA,EAAa,GAAAA,UAAA,CAAO,KAAK,CAAA;AAEjC,IAAA,MAAM,EAAE,UAAA,EAAY,cAAe,EAAA,GAAIC,wCAAiB,EAAA;AACxD,IAAA,MAAM,cAAcC,wDAA6B,EAAA;AACjD,IAAA,MAAM,QAAQC,OAAW,CAAA,EAAE,GAAG,CAAG,EAAA,CAAA,EAAG,GAAG,CAAA;AACvC,IAAM,MAAA,SAAA,GAAYC,aAAS,OAAO;AAAA,MAChC,uBAAuB,OACpB;AAAA,QACC,KAAO,EAAA,CAAA;AAAA,QACP,MAAQ,EAAA,CAAA;AAAA,QACR,IAAA,EAAM,MAAM,KAAM,CAAA,C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