@flexilla/utilities
Version:
Utilities package for flexilla library
75 lines (74 loc) • 1.79 kB
JavaScript
const m = ({
newElement: e,
existingElement: t
}) => {
if (!(e instanceof HTMLElement) || !(t instanceof HTMLElement))
throw new Error("Both parameters must be valid HTML elements.");
const n = t.parentElement;
if (n) n.insertBefore(e, t);
else throw new Error("Existing element must have a parent element.");
}, l = (e, t) => {
for (const [n, s] of Object.entries(t))
e.setAttribute(n, s);
}, d = ({
element: e,
callback: t,
type: n,
keysCheck: s
}) => {
const r = getComputedStyle(e), o = n === "transition" ? r.transition : r.animation;
if (o !== "none" && o !== "" && !s.includes(o)) {
const i = n === "transition" ? "transitionend" : "animationend", a = () => {
e.removeEventListener(i, a), t();
};
e.addEventListener(i, a, { once: !0 });
} else
t();
}, f = ({
element: e,
callback: t
}) => {
d({
element: e,
callback: t,
type: "transition",
keysCheck: ["all 0s ease 0s", "all"]
});
}, u = ({ element: e, callback: t }) => {
d({
element: e,
callback: t,
type: "animation",
keysCheck: ["none 0s ease 0s 1 normal none running"]
});
}, h = (e, t, n) => {
const s = new CustomEvent(t, { detail: n });
e.dispatchEvent(s);
}, v = ({
container: e,
attributeToWatch: t,
onChildAdded: n
}) => {
const s = new MutationObserver((r) => {
for (const o of r)
if (o.type === "childList" && Array.from(o.addedNodes).some(
(c) => c instanceof HTMLElement && c.hasAttribute(t)
)) {
n();
break;
}
});
return s.observe(e, {
childList: !0
}), () => {
s.disconnect();
};
};
export {
u as afterAnimation,
f as afterTransition,
m as appendBefore,
h as dispatchCustomEvent,
v as observeChildrenChanges,
l as setAttributes
};