vuestic-ui
Version:
Vue 3 UI Framework
39 lines (38 loc) • 1.35 kB
JavaScript
import { ref, computed, watch, onBeforeUnmount, unref } from "vue";
import { u as unwrapEl } from "../utils/unwrapEl.mjs";
const useIntersectionObserver = (cb, options = ref({}), target = ref([]), enabled = true) => {
const observer = ref();
const disconnectObserver = () => {
var _a;
(_a = observer.value) == null ? void 0 : _a.disconnect();
};
const observeTarget = (target2) => {
var _a;
const disclosedTarget = unwrapEl(unref(target2));
disclosedTarget && ((_a = observer.value) == null ? void 0 : _a.observe(disclosedTarget));
};
const observeAll = (targets) => {
targets.forEach(observeTarget);
};
const initObserver = () => {
observer.value = new IntersectionObserver(cb, options.value);
};
const isIntersectionDisabled = computed(() => !enabled || !(typeof window !== "undefined" && "IntersectionObserver" in window));
watch([target, options], ([newTarget]) => {
if (isIntersectionDisabled.value) {
return;
}
disconnectObserver();
if (!newTarget) {
return;
}
initObserver();
Array.isArray(newTarget) ? observeAll(newTarget) : observeTarget(newTarget);
}, { immediate: true });
onBeforeUnmount(disconnectObserver);
return { isIntersectionDisabled };
};
export {
useIntersectionObserver as u
};
//# sourceMappingURL=useIntersectionObserver.mjs.map