You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
6.7 KiB
1 line
6.7 KiB
1 month ago
|
{"version":3,"file":"index.mjs","sources":["../../../../../packages/hooks/use-popper/index.ts"],"sourcesContent":["import { computed, onBeforeUnmount, ref, shallowRef, unref, watch } from 'vue'\nimport { createPopper } from '@popperjs/core'\nimport { fromPairs } from 'lodash-unified'\n\nimport type { Ref } from 'vue'\nimport type {\n Instance,\n Modifier,\n Options,\n State,\n VirtualElement,\n} from '@popperjs/core'\n\ntype ElementType = HTMLElement | undefined\ntype ReferenceElement = ElementType | VirtualElement\nexport type PartialOptions = Partial<Options>\n\nexport const usePopper = (\n referenceElementRef: Ref<ReferenceElement>,\n popperElementRef: Ref<ElementType>,\n opts: Ref<PartialOptions> | PartialOptions = {} as PartialOptions\n) => {\n const stateUpdater = {\n name: 'updateState',\n enabled: true,\n phase: 'write',\n fn: ({ state }) => {\n const derivedState = deriveState(state)\n\n Object.assign(states.value, derivedState)\n },\n requires: ['computeStyles'],\n } as Modifier<'updateState', any>\n\n const options = computed<Options>(() => {\n const { onFirstUpdate, placement, strategy, modifiers } = unref(opts)\n\n return {\n onFirstUpdate,\n placement: placement || 'bottom',\n strategy: strategy || 'absolute',\n modifiers: [\n ...(modifiers || []),\n stateUpdater,\n { name: 'applyStyles', enabled: false },\n ],\n }\n })\n\n const instanceRef = shallowRef<Instance | undefined>()\n const states = ref<Pick<State, 'styles' | 'attributes'>>({\n styles: {\n popper: {\n position: unref(options).strategy,\n left: '0',\n top: '0',\n },\n arrow: {\n position: 'absolute',\n },\n },\n attributes: {},\n })\n\n const destroy = () => {\n if (!instanceRef.value) return\n\n instanceRef.value.destroy()\n instanceRef.value = undefined\n }\n\n watch(\n options,\n (newOptions) => {\n const instance = unref(instanceRef)\n if (instance) {\n instance.setOptions(newOptions)\n }\n },\n {\n deep: true,\n }\n )\n\n watch(\n [referenceElementRef, popperElementRef],\n ([referenceElement, popperElement]) => {\n destroy()\n if (!referenceElement || !popperElement) return\n\n instanceRef.value = createPopper(\n referenceElement,\n popperElement,\n unref(options)\n )\n }\n )\n\n onBeforeUnmount(() => {\n destroy()\n })\n\n return {\n state: computed(() => ({ ...(unref(instanceRef)?.state || {}) })),\n styles: computed(() => unref(states).styles),\n attributes: computed(() => unref(states).attributes),\n update: () => unref(instanceRef)?.update(),\n forceUpdate: () => unref(instanceRef)?.forceUpdate(),\n // Preventing end users from modifying the instance.\n instanceRef: computed(() => unref(instanceRef)),\n }\n}\n\nfunction deriveState(state: State) {\n const elements = Object.keys(state.elements) as unknown as Array<\n keyof State['elements']\n >\n\n const styles = fromPairs(\n elements.map(\n (element) =>\n [element, state.styles[element] || {}] as [\n string,\n State['styles'][keyof State['styles']]\n ]\n )\n )\n\n const attributes = fromPairs(\n elements.map(\n (element) =>\n [element, state.attributes[element]] as [\n string,\n State['attributes'][keyof State['attributes']]\n ]\n )\n )\n\n return {\n styles,\n attributes,\n }\n}\n\nexport type UsePopperReturn = ReturnType<typeof usePopper>\n"],"names":[],"mappings":";;;;AAGY,MAAC,SAAS,GAAG,CAAC,mBAAmB,EAAE,gBAAgB,EAAE,IAAI,GAAG,EAAE,KAAK;AAC/E,EAAE,MAAM,YAAY,GAAG;AACvB,IAAI,IAAI,EAAE,aAAa;AACvB,IAAI,OAAO,EAAE,IAAI;AACjB,IAAI,KAAK,EAAE,OAAO;AAClB,IAAI,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,KAAK;AACvB,MAAM,MAAM,YAAY,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;AAC9C,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;AAChD,KAAK;AACL,IAAI,QAAQ,EAAE,CAAC,eAAe,CAAC;AAC/B,GAAG,CAAC;AACJ,EAAE,MAAM,OAAO,GAAG,QAAQ,CAAC,MAAM
|