{"version":3,"file":"useCursorAnchor.mjs","sources":["../../../../../../src/components/va-dropdown/hooks/useCursorAnchor.ts"],"sourcesContent":["import { computed, ComputedRef, reactive, Ref } from 'vue'\nimport { useEvent } from '../../../composables'\nimport { CursorAnchor } from '../types'\n\n/**\n * Returns floating-ui compatible virtual element (https://floating-ui.com/docs/virtual-elements)\n * Floating UI can't automatically update position of the element that is not in the DOM, so we need to update it manually\n * We save mouse position relatively to the anchor element and recalculate it once anchor position changes (after scroll/resize/...)\n * @param anchorRef anchor element ref\n * @param noUpdate flag that indicates that we should not update the position of the dropdown\n */\nexport const useCursorAnchor = (anchorRef: Ref<HTMLElement | undefined>, enabled: Ref<boolean>): ComputedRef<CursorAnchor> => {\n  const position = reactive({ x: 0, y: 0 })\n\n  useEvent(['mousemove', 'mousedown', 'mouseup'], (e: MouseEvent) => {\n    if (!enabled.value) { return }\n\n    const { x, y } = anchorRef.value?.getBoundingClientRect() ?? { x: 0, y: 0 }\n\n    position.x = e.clientX - x\n    position.y = e.clientY - y\n  }, anchorRef)\n\n  return computed(() => {\n    return {\n      getBoundingClientRect () {\n        // anchor position possibly changed, we need to update the position of the floating element\n        const { x, y } = anchorRef.value?.getBoundingClientRect() ?? { x: 0, y: 0 }\n        const resX = position.x + x\n        const resY = position.y + y\n        return {\n          width: 0,\n          height: 0,\n          x: resX,\n          y: resY,\n          top: resY,\n          right: resX,\n          bottom: resY,\n          left: resX,\n        }\n      },\n      contextElement: anchorRef.value,\n    }\n  })\n}\n"],"names":[],"mappings":";;AAWa,MAAA,kBAAkB,CAAC,WAAyC,YAAqD;AAC5H,QAAM,WAAW,SAAS,EAAE,GAAG,GAAG,GAAG,GAAG;AAExC,WAAS,CAAC,aAAa,aAAa,SAAS,GAAG,CAAC,MAAkB;;AAC7D,QAAA,CAAC,QAAQ,OAAO;AAAE;AAAA,IAAO;AAE7B,UAAM,EAAE,GAAG,EAAE,MAAI,eAAU,UAAV,mBAAiB,4BAA2B,EAAE,GAAG,GAAG,GAAG,EAAE;AAEjE,aAAA,IAAI,EAAE,UAAU;AAChB,aAAA,IAAI,EAAE,UAAU;AAAA,KACxB,SAAS;AAEZ,SAAO,SAAS,MAAM;AACb,WAAA;AAAA,MACL,wBAAyB;;AAEvB,cAAM,EAAE,GAAG,EAAE,MAAI,eAAU,UAAV,mBAAiB,4BAA2B,EAAE,GAAG,GAAG,GAAG,EAAE;AACpE,cAAA,OAAO,SAAS,IAAI;AACpB,cAAA,OAAO,SAAS,IAAI;AACnB,eAAA;AAAA,UACL,OAAO;AAAA,UACP,QAAQ;AAAA,UACR,GAAG;AAAA,UACH,GAAG;AAAA,UACH,KAAK;AAAA,UACL,OAAO;AAAA,UACP,QAAQ;AAAA,UACR,MAAM;AAAA,QAAA;AAAA,MAEV;AAAA,MACA,gBAAgB,UAAU;AAAA,IAAA;AAAA,EAC5B,CACD;AACH;"}