index.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { computed, unref, shallowRef, ref, watch, onBeforeUnmount } from 'vue';
  2. import { createPopper } from '@popperjs/core';
  3. import { fromPairs } from 'lodash-unified';
  4. const usePopper = (referenceElementRef, popperElementRef, opts = {}) => {
  5. const stateUpdater = {
  6. name: "updateState",
  7. enabled: true,
  8. phase: "write",
  9. fn: ({ state }) => {
  10. const derivedState = deriveState(state);
  11. Object.assign(states.value, derivedState);
  12. },
  13. requires: ["computeStyles"]
  14. };
  15. const options = computed(() => {
  16. const { onFirstUpdate, placement, strategy, modifiers } = unref(opts);
  17. return {
  18. onFirstUpdate,
  19. placement: placement || "bottom",
  20. strategy: strategy || "absolute",
  21. modifiers: [
  22. ...modifiers || [],
  23. stateUpdater,
  24. { name: "applyStyles", enabled: false }
  25. ]
  26. };
  27. });
  28. const instanceRef = shallowRef();
  29. const states = ref({
  30. styles: {
  31. popper: {
  32. position: unref(options).strategy,
  33. left: "0",
  34. top: "0"
  35. },
  36. arrow: {
  37. position: "absolute"
  38. }
  39. },
  40. attributes: {}
  41. });
  42. const destroy = () => {
  43. if (!instanceRef.value)
  44. return;
  45. instanceRef.value.destroy();
  46. instanceRef.value = void 0;
  47. };
  48. watch(
  49. options,
  50. (newOptions) => {
  51. const instance = unref(instanceRef);
  52. if (instance) {
  53. instance.setOptions(newOptions);
  54. }
  55. },
  56. {
  57. deep: true
  58. }
  59. );
  60. watch(
  61. [referenceElementRef, popperElementRef],
  62. ([referenceElement, popperElement]) => {
  63. destroy();
  64. if (!referenceElement || !popperElement)
  65. return;
  66. instanceRef.value = createPopper(
  67. referenceElement,
  68. popperElement,
  69. unref(options)
  70. );
  71. }
  72. );
  73. onBeforeUnmount(() => {
  74. destroy();
  75. });
  76. return {
  77. state: computed(() => {
  78. var _a;
  79. return { ...((_a = unref(instanceRef)) == null ? void 0 : _a.state) || {} };
  80. }),
  81. styles: computed(() => unref(states).styles),
  82. attributes: computed(() => unref(states).attributes),
  83. update: () => {
  84. var _a;
  85. return (_a = unref(instanceRef)) == null ? void 0 : _a.update();
  86. },
  87. forceUpdate: () => {
  88. var _a;
  89. return (_a = unref(instanceRef)) == null ? void 0 : _a.forceUpdate();
  90. },
  91. instanceRef: computed(() => unref(instanceRef))
  92. };
  93. };
  94. function deriveState(state) {
  95. const elements = Object.keys(state.elements);
  96. const styles = fromPairs(
  97. elements.map(
  98. (element) => [element, state.styles[element] || {}]
  99. )
  100. );
  101. const attributes = fromPairs(
  102. elements.map(
  103. (element) => [element, state.attributes[element]]
  104. )
  105. );
  106. return {
  107. styles,
  108. attributes
  109. };
  110. }
  111. export { usePopper };
  112. //# sourceMappingURL=index.mjs.map