vue-laze
Version:
Lazily render components in React
63 lines (60 loc) • 2 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
default: () => useLaze
});
module.exports = __toCommonJS(src_exports);
// src/useLaze.tsx
var import_vue = require("vue");
function useLaze(options) {
const visible = (0, import_vue.ref)(false);
const container = (0, import_vue.ref)(null);
(0, import_vue.watchEffect)((onCleanup) => {
visible.value = false;
let refresh = false;
if (options && options.refresh) {
refresh = typeof options.refresh === "boolean" ? options.refresh : options.refresh.value;
}
if (container.value) {
const current = container.value;
const observer = new IntersectionObserver((entries) => {
for (const entry of entries) {
if (refresh) {
visible.value = entry.isIntersecting;
} else if (entry.isIntersecting) {
visible.value = true;
observer.disconnect();
}
}
});
observer.observe(current);
onCleanup(() => {
observer.unobserve(current);
observer.disconnect();
});
}
});
return {
ref: container,
visible: (0, import_vue.readonly)(visible)
};
}
//# sourceMappingURL=index.cjs.map