44f226951a6f9370af81539faa7b1e5df3930921f09e36388dea88a607209ed74843dc683f01c1e52832a25176b7f3cb739722b075c1240e175de09e1e6eb4 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { inject, ref, computed, unref, watch, onMounted } from 'vue';
  2. import { isUndefined } from 'lodash-unified';
  3. import { POPPER_INJECTION_KEY } from '../constants.mjs';
  4. import { buildPopperOptions, unwrapMeasurableEl } from '../utils.mjs';
  5. import { usePopper } from '../../../../hooks/use-popper/index.mjs';
  6. const DEFAULT_ARROW_OFFSET = 0;
  7. const usePopperContent = (props) => {
  8. const { popperInstanceRef, contentRef, triggerRef, role } = inject(POPPER_INJECTION_KEY, void 0);
  9. const arrowRef = ref();
  10. const arrowOffset = computed(() => props.arrowOffset);
  11. const eventListenerModifier = computed(() => {
  12. return {
  13. name: "eventListeners",
  14. enabled: !!props.visible
  15. };
  16. });
  17. const arrowModifier = computed(() => {
  18. var _a;
  19. const arrowEl = unref(arrowRef);
  20. const offset = (_a = unref(arrowOffset)) != null ? _a : DEFAULT_ARROW_OFFSET;
  21. return {
  22. name: "arrow",
  23. enabled: !isUndefined(arrowEl),
  24. options: {
  25. element: arrowEl,
  26. padding: offset
  27. }
  28. };
  29. });
  30. const options = computed(() => {
  31. return {
  32. onFirstUpdate: () => {
  33. update();
  34. },
  35. ...buildPopperOptions(props, [
  36. unref(arrowModifier),
  37. unref(eventListenerModifier)
  38. ])
  39. };
  40. });
  41. const computedReference = computed(() => unwrapMeasurableEl(props.referenceEl) || unref(triggerRef));
  42. const { attributes, state, styles, update, forceUpdate, instanceRef } = usePopper(computedReference, contentRef, options);
  43. watch(instanceRef, (instance) => popperInstanceRef.value = instance, {
  44. flush: "sync"
  45. });
  46. onMounted(() => {
  47. watch(() => {
  48. var _a, _b;
  49. return (_b = (_a = unref(computedReference)) == null ? void 0 : _a.getBoundingClientRect) == null ? void 0 : _b.call(_a);
  50. }, () => {
  51. update();
  52. });
  53. });
  54. return {
  55. attributes,
  56. arrowRef,
  57. contentRef,
  58. instanceRef,
  59. state,
  60. styles,
  61. role,
  62. forceUpdate,
  63. update
  64. };
  65. };
  66. export { usePopperContent };
  67. //# sourceMappingURL=use-content.mjs.map