vuestic-ui
Version:
Vue 3 UI Framework
1 lines • 2.12 kB
Source Map (JSON)
{"version":3,"file":"useElementRect.mjs","sources":["../../../../src/composables/useElementRect.ts"],"sourcesContent":["import { Ref, onBeforeUnmount, onMounted, ref } from 'vue'\n\nexport const useElementRect = (element: Ref<HTMLElement | null>) => {\n const rect = ref({ top: 0, left: 0, width: 0, height: 0, bottom: 0, right: 0 }) satisfies Ref<{\n top: number\n left: number\n width: number\n height: number\n bottom: number\n right: number\n }>\n\n let resizeObserver: ResizeObserver | undefined\n let mutationObserver: MutationObserver | undefined\n\n const updateRect = () => {\n if (element.value) {\n rect.value = element.value.getBoundingClientRect()\n }\n }\n\n onMounted(() => {\n resizeObserver = new ResizeObserver(updateRect)\n mutationObserver = new MutationObserver(updateRect)\n\n element.value && resizeObserver.observe(element.value)\n element.value && mutationObserver.observe(element.value, { attributes: true, childList: true, subtree: true })\n\n window.addEventListener('resize', updateRect)\n window.addEventListener('scroll', updateRect)\n\n updateRect()\n })\n\n onBeforeUnmount(() => {\n resizeObserver?.disconnect()\n mutationObserver?.disconnect()\n\n window.removeEventListener('resize', updateRect)\n window.removeEventListener('scroll', updateRect)\n\n resizeObserver = undefined\n mutationObserver = undefined\n })\n\n return rect\n}\n"],"names":[],"mappings":";AAEa,MAAA,iBAAiB,CAAC,YAAqC;AAClE,QAAM,OAAO,IAAI,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,GAAG,OAAO,GAAG;AAS1E,MAAA;AACA,MAAA;AAEJ,QAAM,aAAa,MAAM;AACvB,QAAI,QAAQ,OAAO;AACZ,WAAA,QAAQ,QAAQ,MAAM,sBAAsB;AAAA,IACnD;AAAA,EAAA;AAGF,YAAU,MAAM;AACG,qBAAA,IAAI,eAAe,UAAU;AAC3B,uBAAA,IAAI,iBAAiB,UAAU;AAElD,YAAQ,SAAS,eAAe,QAAQ,QAAQ,KAAK;AACrD,YAAQ,SAAS,iBAAiB,QAAQ,QAAQ,OAAO,EAAE,YAAY,MAAM,WAAW,MAAM,SAAS,KAAM,CAAA;AAEtG,WAAA,iBAAiB,UAAU,UAAU;AACrC,WAAA,iBAAiB,UAAU,UAAU;AAEjC;EAAA,CACZ;AAED,kBAAgB,MAAM;AACpB,qDAAgB;AAChB,yDAAkB;AAEX,WAAA,oBAAoB,UAAU,UAAU;AACxC,WAAA,oBAAoB,UAAU,UAAU;AAE9B,qBAAA;AACE,uBAAA;AAAA,EAAA,CACpB;AAEM,SAAA;AACT;"}