@flexilla/alpine-offcanvas
Version:
AlpineJS plugin for adding offcanvas functionality to your AlpineJS components
378 lines (373 loc) • 14.7 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);
// builds/module.js
var module_exports = {};
__export(module_exports, {
default: () => module_default
});
module.exports = __toCommonJS(module_exports);
// ../../node_modules/@flexilla/offcanvas/dist/offcanvas.js
var g = Object.defineProperty;
var w = (t, e, n) => e in t ? g(t, e, { enumerable: true, configurable: true, writable: true, value: n }) : t[e] = n;
var o = (t, e, n) => w(t, typeof e != "symbol" ? e + "" : e, n);
var m = (t, e = document.body) => e.querySelector(t);
var h = (t, e = document.body) => Array.from(e.querySelectorAll(t));
var y = ({
newElement: t,
existingElement: e
}) => {
if (!(t instanceof HTMLElement) || !(e instanceof HTMLElement))
throw new Error("Both parameters must be valid HTML elements.");
const n = e.parentElement;
if (n)
n.insertBefore(t, e);
else
throw new Error("Existing element must have a parent element.");
};
var k = ({
element: t,
callback: e,
type: n,
keysCheck: s
}) => {
const a = getComputedStyle(t), i = a.transition;
if (i !== "none" && i !== "" && !s.includes(i)) {
const l = "transitionend", c = () => {
t.removeEventListener(l, c), e();
};
t.addEventListener(l, c, { once: true });
} else
e();
};
var O = ({
element: t,
callback: e
}) => {
k({
element: t,
callback: e,
type: "transition",
keysCheck: ["all 0s ease 0s", "all"]
});
};
var v = (t, e, n) => {
const s = new CustomEvent(e, { detail: n });
t.dispatchEvent(s);
};
function B(t) {
const e = () => {
document.querySelector(
"[data-fx-component]:not([data-component-initialized])"
) ? requestAnimationFrame(e) : t();
};
e();
}
function A(t, e, n = "move") {
if (!(t instanceof HTMLElement))
throw new Error("Source element must be an HTMLElement");
if (!(e instanceof HTMLElement))
throw new Error("Target element must be an HTMLElement");
if (!["move", "detachable"].includes(n))
throw new Error(`Invalid teleport mode: ${n}. Must be "move" or "detachable".`);
let s = document.createComment("teleporter-placeholder");
const a = t.parentNode;
return a ? a.insertBefore(s, t) : console.warn("Element has no parent; placeholder not inserted."), n === "move" ? (t.parentNode && e.appendChild(t), {
append() {
t.parentNode !== e && e.appendChild(t);
},
remove() {
s != null && s.parentNode && t.parentNode && s.parentNode.insertBefore(t, s);
},
restore() {
s != null && s.parentNode && t.parentNode !== a && s.parentNode.insertBefore(t, s);
}
}) : (t.parentNode && e.appendChild(t), {
append() {
e.contains(t) || e.appendChild(t);
},
remove() {
t.parentNode && t.remove();
},
restore() {
s != null && s.parentNode && !t.parentNode && s.parentNode.insertBefore(t, s);
}
});
}
var I = (t) => {
var e;
return (e = t.parentElement) == null ? void 0 : e.removeChild(t);
};
var C = (t) => {
t.setAttribute("data-state", "invisible"), O({
element: t,
callback() {
I(t);
}
});
};
var L = (t, e) => {
const n = t;
if (n === "" || !n)
return;
const s = document.createElement("div");
if (s.setAttribute("aria-hidden", "true"), s.setAttribute("data-state", "visible"), s.setAttribute("data-fx-offcanvas-overlay", ""), s.setAttribute("data-offcanvas-el", e), n === "")
return;
const a = n.split(" ");
return n !== "" && s.classList.add(...a), s;
};
var u = (t, e, n) => {
t.setAttribute("aria-hidden", n === "open" ? "false" : "true"), t.setAttribute("data-state", n), e || S(n);
};
var S = (t) => {
document.body.style.overflow = t === "open" ? "hidden" : "", document.body.style.overflowY = t === "open" ? "hidden" : "auto";
};
var T = (t, e) => {
if (t === e)
return;
t.setAttribute("aria-hidden", "true"), t.setAttribute("data-state", "close");
const n = m(`[data-fx-offcanvas-overlay][data-offcanvas-el=${t.getAttribute("id")}]`, t.parentElement);
n instanceof HTMLElement && C(n);
};
var x = (t) => {
const e = h("[data-fx-offcanvas][data-state=open]");
if (!(e.length <= 0))
for (const n of e)
T(n, t);
};
var d = class {
static initGlobalRegistry() {
window.$flexillaInstances || (window.$flexillaInstances = {});
}
static register(e, n, s) {
return this.initGlobalRegistry(), window.$flexillaInstances[e] || (window.$flexillaInstances[e] = []), this.getInstance(e, n) || (window.$flexillaInstances[e].push({ element: n, instance: s }), s);
}
static getInstance(e, n) {
var s, a;
return this.initGlobalRegistry(), (a = (s = window.$flexillaInstances[e]) == null ? void 0 : s.find(
(i) => i.element === n
)) == null ? void 0 : a.instance;
}
static removeInstance(e, n) {
this.initGlobalRegistry(), window.$flexillaInstances[e] && (window.$flexillaInstances[e] = window.$flexillaInstances[e].filter(
(s) => s.element !== n
));
}
static setup(e) {
e.setAttribute("data-fx-component", "fx");
}
static initialized(e) {
e.setAttribute("data-component-initialized", "initialized");
}
};
var f = class f2 {
/**
* Creates an instance of Offcanvas.
* @param offcanvas - The offcanvas element selector or HTMLElement
* @param options - Configuration options for the offcanvas
* @throws {Error} When the provided element is not a valid HTMLElement
*
* @example
* ```ts
* const offcanvas = new Offcanvas('#sidebar', {
* allowBodyScroll: true, // Allow scrolling when offcanvas is open
* staticBackdrop: false, // Close when clicking outside
* backdrop: 'dark', // Backdrop appearance
* onShow: () => console.log('Offcanvas shown'),
* onHide: () => console.log('Offcanvas hidden')
* });
* ```
*/
constructor(e, n = {}) {
o(this, "offCanvasElement");
o(this, "offCanvasTriggers");
o(this, "offCanvasCloseBtns");
o(this, "offCanvasId", "");
o(this, "dispatchEventToDocument");
o(this, "allowBodyScroll");
o(this, "staticBackdrop");
o(this, "backdrop");
o(this, "options");
o(this, "teleporter");
o(this, "moveElOnInit", () => {
B(() => this.teleporter.append());
});
o(this, "closeWhenClickOutSide", (e2) => {
const n2 = this.offCanvasElement.getAttribute("data-state") === "open", s2 = !this.offCanvasElement.contains(e2.target) && ![...this.offCanvasTriggers].includes(e2.target);
n2 && s2 && this.closeOffCanvas();
});
o(this, "closeOffCanvas", () => {
var i2, l2, c2, r2, p;
let e2 = false;
if (v(this.offCanvasElement, "offcanvas-before-hide", {
offcanvasId: this.offCanvasElement.id,
setExitAction: (b) => {
e2 = b;
}
}), ((c2 = (l2 = (i2 = this.options).beforeHide) == null ? void 0 : l2.call(i2)) == null ? void 0 : c2.cancelAction) || e2)
return;
const s2 = this.offCanvasElement.getAttribute("id"), a2 = m(`[data-fx-offcanvas-overlay][data-offcanvas-el=${s2}]`);
a2 instanceof HTMLElement && C(a2), this.offCanvasElement.blur(), u(
this.offCanvasElement,
this.allowBodyScroll,
"close"
), document.removeEventListener("keydown", this.closeWithEsc), !this.allowBodyScroll && !a2 && document.removeEventListener("click", this.closeWhenClickOutSide), (p = (r2 = this.options).onHide) == null || p.call(r2), v(this.offCanvasElement, "offcanvas-close", { offcanvasId: this.offCanvasElement.id });
});
o(this, "closeWithEsc", (e2) => {
e2.key === "Escape" && (e2.preventDefault(), this.closeOffCanvas());
});
o(this, "closeFromCloseBtn", (e2) => {
e2.target.blur(), this.closeOffCanvas();
});
o(this, "changeState", () => {
this.offCanvasElement.getAttribute("data-state") === "open" ? this.closeOffCanvas() : this.openOffCanvas();
});
o(this, "open", () => {
this.openOffCanvas();
});
o(this, "close", () => {
this.closeOffCanvas();
});
o(this, "setOptions", ({ allowBodyscroll: e2 }) => {
e2 !== void 0 && (this.allowBodyScroll = e2);
});
const s = typeof e == "string" ? m(e) : e;
if (!(s instanceof HTMLElement))
throw new Error("Invalid Offcanvas, the provided Element is not a valid HTMLElement");
this.offCanvasElement = s;
const a = d.getInstance("offcanvas", s);
if (a)
return a;
d.setup(this.offCanvasElement), this.options = n;
const { staticBackdrop: i, allowBodyScroll: l, backdrop: c } = this.options;
this.setupAttributes(), this.staticBackdrop = i || s.hasAttribute("data-static-backdrop") && s.dataset.staticBackdrop !== "false" || false, this.allowBodyScroll = l || s.hasAttribute("data-allow-body-scroll") && s.dataset.allowBodyScroll !== "false" || false;
const r = this.offCanvasElement.getAttribute("id");
if (!r || r === "")
throw new Error("\u274C id is required but missing on element:");
this.offCanvasId = r, this.offCanvasTriggers = this.findOffCanvasElements("[data-offcanvas-trigger]", false, r), this.offCanvasCloseBtns = this.findOffCanvasElements("[data-offcanvas-close]", true, r, this.offCanvasElement), this.backdrop = c || this.offCanvasElement.dataset.offcanvasBackdrop || "", this.dispatchEventToDocument = this.dispatchEventToDocument = this.options.dispatchEventToDocument || true, this.teleporter = A(this.offCanvasElement, document.body, "move"), this.setupOffcanvas(), this.moveElOnInit(), d.register("offcanvas", this.offCanvasElement, this), d.initialized(this.offCanvasElement);
}
findOffCanvasElements(e, n, s, a) {
return n ? h(`${e}`, a) : h(`${e}[data-target=${s}]`);
}
setupAttributes() {
this.offCanvasElement.hasAttribute("data-fx-offcanvas") || this.offCanvasElement.setAttribute("data-fx-offcanvas", "");
}
openOffCanvas() {
var s, a, i, l;
(a = (s = this.options).beforeShow) == null || a.call(s), x(this.offCanvasElement), u(
this.offCanvasElement,
this.allowBodyScroll,
"open"
);
const e = this.offCanvasElement.getAttribute("id"), n = L(
this.backdrop,
e
);
n instanceof HTMLElement && (y({ newElement: n, existingElement: this.offCanvasElement }), this.staticBackdrop || n.addEventListener("click", this.closeOffCanvas)), document.addEventListener("keydown", this.closeWithEsc), (l = (i = this.options).onShow) == null || l.call(i), v(this.offCanvasElement, "offcanvas-open", { offcanvasId: this.offCanvasElement.id });
}
initCloseBtns() {
for (const e of this.offCanvasCloseBtns)
e.addEventListener("click", this.closeFromCloseBtn);
}
initTriggers() {
for (const e of this.offCanvasTriggers)
e.addEventListener("click", this.changeState);
}
setupOffcanvas() {
this.initTriggers(), this.initCloseBtns(), this.dispatchEventToDocument && document.addEventListener(`sheet:${this.offCanvasId}:open`, this.open), this.dispatchEventToDocument && document.addEventListener(`sheet:${this.offCanvasId}:close`, this.close);
}
/**
* Cleans up the offcanvas instance by removing event listeners and references.
* Call this method when the offcanvas component is no longer needed to prevent memory leaks.
*
* @example
* ```ts
* const offcanvas = new Offcanvas('#sidebar');
* // ... use offcanvas ...
* offcanvas.cleanup();
* ```
*/
cleanup() {
for (const e of this.offCanvasTriggers)
e.removeEventListener("click", this.changeState);
for (const e of this.offCanvasCloseBtns)
e.removeEventListener("click", this.closeOffCanvas);
document.removeEventListener("keydown", this.closeWithEsc), this.allowBodyScroll || document.removeEventListener("click", this.closeWhenClickOutSide), this.dispatchEventToDocument && document.removeEventListener(`sheet:${this.offCanvasId}:open`, this.open), this.dispatchEventToDocument && document.removeEventListener(`sheet:${this.offCanvasId}:close`, this.close), d.removeInstance("offcanvas", this.offCanvasElement);
}
};
o(f, "autoInit", (e = "[data-fx-offcanvas]") => {
const n = h(e);
for (const s of n)
new f(s);
}), /**
* This is an alternative to using the constructor directly.
* @param offcanvas - The offcanvas element selector or HTMLElement
* @param options - Configuration options for the offcanvas
* @returns A new Offcanvas instance
*
* @example
* ```ts
* const offcanvas = Offcanvas.init('#sidebar', {
* allowBodyScroll: true,
* staticBackdrop: false
* });
* ```
*/
o(f, "init", (e, n = {}) => new f(e, n));
var E = f;
// src/index.js
function Offcanvas(Alpine) {
Alpine.directive("offcanvas", (el, {}, { cleanup }) => {
const offcanvasId = el.getAttribute("id");
if (!offcanvasId) {
console.error("\u274C id is required but missing on element:", el);
return;
}
const offcanvas_ = new E(el);
if (!Alpine.store("sheets")) {
Alpine.store("sheets", {});
}
Alpine.store("sheets")[offcanvasId] = offcanvas_;
const openHandler = () => offcanvas_.open();
const closeHandler = () => offcanvas_.close();
document.addEventListener(`sheet:${offcanvasId}:open`, openHandler);
document.addEventListener(`sheet:${offcanvasId}:close`, closeHandler);
cleanup(() => {
document.removeEventListener(`sheet:${offcanvasId}:open`, openHandler);
document.removeEventListener(
`sheet:${offcanvasId}:close`,
closeHandler
);
offcanvas_.cleanup();
delete Alpine.store("sheets")[offcanvasId];
});
});
Alpine.magic("offcanvas", (el) => (id) => {
if (!Alpine.store("sheets")) {
console.warn("\u26A0\uFE0F Alpine store for Offcanvas is not initialized.");
return null;
}
if (!Alpine.store("sheets")[id]) {
console.warn(`\u26A0\uFE0F No offcanvas instance found for ID: ${id}`);
return null;
}
return Alpine.store("sheets")[id];
});
}
var src_default = Offcanvas;
// builds/module.js
var module_default = src_default;