@alegendstale/holly-components
Version:
Reusable UI components created using lit
47 lines (46 loc) • 1.16 kB
JavaScript
class c {
/**
* All currently observed targets
* @type {WeakSet<Element>}
*/
#e = /* @__PURE__ */ new Set();
/**
* Documents to IntersectionObserver instances
* @type {WeakMap<Document, IntersectionObserver>}
*/
#t = /* @__PURE__ */ new WeakMap();
constructor(e) {
this.callback = e;
}
/**
* Begin observing the presence of an element.
* @param {Element} element - The element to observe.
*/
observe(e) {
if (this.#e.has(e))
return;
let t = e.ownerDocument, s = this.#t.get(t);
s || (s = new IntersectionObserver(
(r) => {
this.callback(r.map(({ target: o }) => ({ target: o })));
},
{ root: t.documentElement }
), this.#t.set(t, s)), this.#e.add(e), s.observe(e);
}
/**
* Stop observing the presence of an element.
* @param {Element} [element] - The element to stop observing. If not provided, all targets will be unobserved.
*/
unobserve(e) {
if (!e) {
for (const r of this.#e)
this.unobserve(r);
return;
}
let t = e.ownerDocument;
this.#t.get(t)?.unobserve(e), this.#e.delete(e);
}
}
export {
c as default
};