@zag-js/dom-query
Version:
The dom helper library for zag.js machines
70 lines (68 loc) • 1.48 kB
JavaScript
import {
__publicField
} from "./chunk-QZ7TP4HQ.mjs";
// src/raf.ts
var AnimationFrame = class _AnimationFrame {
constructor() {
__publicField(this, "id", null);
__publicField(this, "fn_cleanup");
__publicField(this, "cleanup", () => {
this.cancel();
});
}
static create() {
return new _AnimationFrame();
}
request(fn) {
this.cancel();
this.id = globalThis.requestAnimationFrame(() => {
this.id = null;
this.fn_cleanup = fn?.();
});
}
cancel() {
if (this.id !== null) {
globalThis.cancelAnimationFrame(this.id);
this.id = null;
}
this.fn_cleanup?.();
this.fn_cleanup = void 0;
}
isActive() {
return this.id !== null;
}
};
function raf(fn) {
const frame = AnimationFrame.create();
frame.request(fn);
return frame.cleanup;
}
function nextTick(fn) {
const set = /* @__PURE__ */ new Set();
function raf2(fn2) {
const id = globalThis.requestAnimationFrame(fn2);
set.add(() => globalThis.cancelAnimationFrame(id));
}
raf2(() => raf2(fn));
return function cleanup() {
set.forEach((fn2) => fn2());
};
}
function queueBeforeEvent(el, type, cb) {
const cancelTimer = raf(() => {
el.removeEventListener(type, exec, true);
cb();
});
const exec = () => {
cancelTimer();
cb();
};
el.addEventListener(type, exec, { once: true, capture: true });
return cancelTimer;
}
export {
AnimationFrame,
nextTick,
queueBeforeEvent,
raf
};