@indielayer/ui
Version:
Indielayer UI Components with Tailwind CSS build for Vue 3
69 lines (68 loc) • 1.81 kB
JavaScript
import { ref as p, watch as d, getCurrentInstance as H, onBeforeUnmount as R } from "vue";
import { assert as I } from "../../utils/assert.js";
const b = "data-virtual-index";
function y({
defaultRowHeight: v,
key: o
}) {
const r = p(/* @__PURE__ */ new Map()), a = p(0);
o !== void 0 && typeof o == "object" && "value" in o && d(o, () => {
r.value = /* @__PURE__ */ new Map(), a.value++;
});
const w = () => {
a.value;
let e = 0;
return r.value.forEach((t) => {
e += t;
}), e === 0 ? v : e / r.value.size;
}, m = (e) => {
a.value;
const t = r.value.get(e);
return t !== void 0 ? t : v;
}, A = (e, t) => {
if (r.value.get(e) === t)
return;
const n = new Map(r.value);
n.set(e, t), r.value = n, a.value++;
}, E = (e) => {
if (e.length === 0)
return;
let t = !1;
const n = [];
if (e.forEach((s) => {
const { borderBoxSize: i, target: c } = s, g = c.getAttribute(b);
I(
g !== null,
`Invalid ${b} attribute value`
);
const h = parseInt(g), { blockSize: l } = i[0];
if (!l)
return;
r.value.get(h) !== l && (n.push({ index: h, height: l }), t = !0);
}), t) {
const s = new Map(r.value);
n.forEach(({ index: i, height: c }) => {
s.set(i, c);
}), r.value = s, a.value++;
}
}, u = new ResizeObserver(E);
function f() {
u.disconnect();
}
return H() && R(f), {
getAverageRowHeight: w,
getRowHeight: m,
setRowHeight: A,
observeRowElements: (e) => {
const t = Array.isArray(e) ? e : Array.from(e);
return t.forEach((n) => u.observe(n)), () => {
t.forEach((n) => u.unobserve(n));
};
},
cleanup: f
};
}
export {
b as DATA_ATTRIBUTE_LIST_INDEX,
y as useDynamicRowHeight
};