routie dev init since i didn't adhere to any proper guidance up until now
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
||||
// Utilities
|
||||
import { onBeforeUnmount, readonly, ref, watch } from 'vue';
|
||||
import { templateRef } from "../util/index.js";
|
||||
import { IN_BROWSER } from "../util/globals.js"; // Types
|
||||
export function useResizeObserver(callback, box = 'content') {
|
||||
const resizeRef = templateRef();
|
||||
const contentRect = ref();
|
||||
if (IN_BROWSER) {
|
||||
const observer = new ResizeObserver(entries => {
|
||||
callback?.(entries, observer);
|
||||
if (!entries.length) return;
|
||||
if (box === 'content') {
|
||||
contentRect.value = entries[0].contentRect;
|
||||
} else {
|
||||
contentRect.value = entries[0].target.getBoundingClientRect();
|
||||
}
|
||||
});
|
||||
onBeforeUnmount(() => {
|
||||
observer.disconnect();
|
||||
});
|
||||
watch(() => resizeRef.el, (newValue, oldValue) => {
|
||||
if (oldValue) {
|
||||
observer.unobserve(oldValue);
|
||||
contentRect.value = undefined;
|
||||
}
|
||||
if (newValue) observer.observe(newValue);
|
||||
}, {
|
||||
flush: 'post'
|
||||
});
|
||||
}
|
||||
return {
|
||||
resizeRef,
|
||||
contentRect: readonly(contentRect)
|
||||
};
|
||||
}
|
||||
//# sourceMappingURL=resizeObserver.js.map
|
||||
Reference in New Issue
Block a user