zhangyong
2023-08-22 1353e87cb21a4032d585d7404bae9042f2ebcf08
1
{"version":3,"file":"use-content.mjs","sources":["../../../../../../../packages/components/popper/src/composables/use-content.ts"],"sourcesContent":["import { computed, inject, onMounted, ref, unref, watch } from 'vue'\nimport { isUndefined } from 'lodash-unified'\nimport { usePopper } from '@element-plus/hooks'\nimport { POPPER_INJECTION_KEY } from '../constants'\nimport { buildPopperOptions, unwrapMeasurableEl } from '../utils'\n\nimport type { Modifier } from '@popperjs/core'\nimport type { PartialOptions } from '@element-plus/hooks'\nimport type { PopperContentProps } from '../content'\n\nconst DEFAULT_ARROW_OFFSET = 0\n\nexport const usePopperContent = (props: PopperContentProps) => {\n  const { popperInstanceRef, contentRef, triggerRef, role } = inject(\n    POPPER_INJECTION_KEY,\n    undefined\n  )!\n\n  const arrowRef = ref<HTMLElement>()\n  const arrowOffset = ref<number>()\n\n  const eventListenerModifier = computed(() => {\n    return {\n      name: 'eventListeners',\n      enabled: !!props.visible,\n    } as Modifier<'eventListeners', any>\n  })\n\n  const arrowModifier = computed(() => {\n    const arrowEl = unref(arrowRef)\n    const offset = unref(arrowOffset) ?? DEFAULT_ARROW_OFFSET\n    // Seems like the `phase` and `fn` is required by Modifier type\n    // But on its documentation they didn't specify that.\n    // Refer to https://popper.js.org/docs/v2/modifiers/arrow/\n    return {\n      name: 'arrow',\n      enabled: !isUndefined(arrowEl),\n      options: {\n        element: arrowEl,\n        padding: offset,\n      },\n    } as any\n  })\n\n  const options = computed<PartialOptions>(() => {\n    return {\n      onFirstUpdate: () => {\n        update()\n      },\n      ...buildPopperOptions(props, [\n        unref(arrowModifier),\n        unref(eventListenerModifier),\n      ]),\n    }\n  })\n\n  const computedReference = computed(\n    () => unwrapMeasurableEl(props.referenceEl) || unref(triggerRef)\n  )\n\n  const { attributes, state, styles, update, forceUpdate, instanceRef } =\n    usePopper(computedReference, contentRef, options)\n\n  watch(instanceRef, (instance) => (popperInstanceRef.value = instance))\n\n  onMounted(() => {\n    watch(\n      () => unref(computedReference)?.getBoundingClientRect(),\n      () => {\n        update()\n      }\n    )\n  })\n\n  return {\n    attributes,\n    arrowRef,\n    contentRef,\n    instanceRef,\n    state,\n    styles,\n    role,\n\n    forceUpdate,\n    update,\n  }\n}\n\nexport type UsePopperContentReturn = ReturnType<typeof usePopperContent>\n"],"names":[],"mappings":";;;;;;;AAKA,MAAM,oBAAoB,GAAG,CAAC,CAAC;AACnB,MAAC,gBAAgB,GAAG,CAAC,KAAK,KAAK;AAC3C,EAAE,MAAM,EAAE,iBAAiB,EAAE,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC,oBAAoB,EAAE,KAAK,CAAC,CAAC,CAAC;AACnG,EAAE,MAAM,QAAQ,GAAG,GAAG,EAAE,CAAC;AACzB,EAAE,MAAM,WAAW,GAAG,GAAG,EAAE,CAAC;AAC5B,EAAE,MAAM,qBAAqB,GAAG,QAAQ,CAAC,MAAM;AAC/C,IAAI,OAAO;AACX,MAAM,IAAI,EAAE,gBAAgB;AAC5B,MAAM,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO;AAC9B,KAAK,CAAC;AACN,GAAG,CAAC,CAAC;AACL,EAAE,MAAM,aAAa,GAAG,QAAQ,CAAC,MAAM;AACvC,IAAI,IAAI,EAAE,CAAC;AACX,IAAI,MAAM,OAAO,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;AACpC,IAAI,MAAM,MAAM,GAAG,CAAC,EAAE,GAAG,KAAK,CAAC,WAAW,CAAC,KAAK,IAAI,GAAG,EAAE,GAAG,oBAAoB,CAAC;AACjF,IAAI,OAAO;AACX,MAAM,IAAI,EAAE,OAAO;AACnB,MAAM,OAAO,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC;AACpC,MAAM,OAAO,EAAE;AACf,QAAQ,OAAO,EAAE,OAAO;AACxB,QAAQ,OAAO,EAAE,MAAM;AACvB,OAAO;AACP,KAAK,CAAC;AACN,GAAG,CAAC,CAAC;AACL,EA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