@flexilla/alpine-offcanvas 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cdn.js CHANGED
@@ -12,8 +12,10 @@
12
12
  if (!(s instanceof HTMLElement) || !(t instanceof HTMLElement))
13
13
  throw new Error("Both parameters must be valid HTML elements.");
14
14
  const e = t.parentElement;
15
- if (e) e.insertBefore(s, t);
16
- else throw new Error("Existing element must have a parent element.");
15
+ if (e)
16
+ e.insertBefore(s, t);
17
+ else
18
+ throw new Error("Existing element must have a parent element.");
17
19
  };
18
20
  var k = ({
19
21
  element: s,
@@ -59,9 +61,11 @@
59
61
  };
60
62
  var B = (s, t) => {
61
63
  const e = s;
62
- if (e === "" || !e) return;
64
+ if (e === "" || !e)
65
+ return;
63
66
  const n = document.createElement("div");
64
- if (n.setAttribute("aria-hidden", "true"), n.setAttribute("data-state", "visible"), n.setAttribute("data-fx-offcanvas-overlay", ""), n.setAttribute("data-offcanvas-el", t), e === "") return;
67
+ if (n.setAttribute("aria-hidden", "true"), n.setAttribute("data-state", "visible"), n.setAttribute("data-fx-offcanvas-overlay", ""), n.setAttribute("data-offcanvas-el", t), e === "")
68
+ return;
65
69
  const a = e.split(" ");
66
70
  return e !== "" && n.classList.add(...a), n;
67
71
  };
@@ -72,7 +76,8 @@
72
76
  document.body.style.overflow = s === "open" ? "hidden" : "", document.body.style.overflowY = s === "open" ? "hidden" : "auto";
73
77
  };
74
78
  var x = (s, t) => {
75
- if (s === t) return;
79
+ if (s === t)
80
+ return;
76
81
  s.setAttribute("aria-hidden", "true"), s.setAttribute("data-state", "close");
77
82
  const e = m(`[data-fx-offcanvas-overlay][data-offcanvas-el=${s.getAttribute("id")}]`, s.parentElement);
78
83
  e instanceof HTMLElement && p(e);
@@ -80,7 +85,8 @@
80
85
  var I = (s) => {
81
86
  const t = d("[data-fx-offcanvas][data-state=open]");
82
87
  if (!(t.length <= 0))
83
- for (const e of t) x(e, s);
88
+ for (const e of t)
89
+ x(e, s);
84
90
  };
85
91
  var h = class {
86
92
  static initGlobalRegistry() {
@@ -139,7 +145,8 @@
139
145
  setExitAction: (g) => {
140
146
  t2 = g;
141
147
  }
142
- }), ((f2 = (l2 = (i2 = this.options).beforeHide) == null ? void 0 : l2.call(i2)) == null ? void 0 : f2.cancelAction) || t2) return;
148
+ }), ((f2 = (l2 = (i2 = this.options).beforeHide) == null ? void 0 : l2.call(i2)) == null ? void 0 : f2.cancelAction) || t2)
149
+ return;
143
150
  const n2 = this.offCanvasElement.getAttribute("id"), a2 = m(`[data-fx-offcanvas-overlay][data-offcanvas-el=${n2}]`);
144
151
  a2 instanceof HTMLElement && p(a2), C(
145
152
  this.offCanvasElement,
@@ -154,7 +161,8 @@
154
161
  this.offCanvasElement.getAttribute("data-state") === "open" ? this.closeOffCanvas() : this.openOffCanvas();
155
162
  });
156
163
  const n = typeof t == "string" ? m(t) : t;
157
- if (!(n instanceof HTMLElement)) throw new Error("Invalid Offcanvas, the provided Element is not a valid HTMLElement");
164
+ if (!(n instanceof HTMLElement))
165
+ throw new Error("Invalid Offcanvas, the provided Element is not a valid HTMLElement");
158
166
  const a = h.getInstance("offcanvas", n);
159
167
  if (a)
160
168
  return a;
@@ -184,10 +192,12 @@
184
192
  e instanceof HTMLElement && (y({ newElement: e, existingElement: this.offCanvasElement }), this.staticBackdrop || e.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 });
185
193
  }
186
194
  initCloseBtns() {
187
- for (const t of this.offCanvasCloseBtns) t.addEventListener("click", this.closeOffCanvas);
195
+ for (const t of this.offCanvasCloseBtns)
196
+ t.addEventListener("click", this.closeOffCanvas);
188
197
  }
189
198
  initTriggers() {
190
- for (const t of this.offCanvasTriggers) t.addEventListener("click", this.changeState);
199
+ for (const t of this.offCanvasTriggers)
200
+ t.addEventListener("click", this.changeState);
191
201
  }
192
202
  setupOffcanvas() {
193
203
  this.initTriggers(), this.initCloseBtns();
@@ -241,7 +251,8 @@
241
251
  };
242
252
  o(c, "autoInit", (t = "[data-fx-offcanvas]") => {
243
253
  const e = d(t);
244
- for (const n of e) new c(n);
254
+ for (const n of e)
255
+ new c(n);
245
256
  }), /**
246
257
  * Creates a new instance of Offcanvas with the given element and options.
247
258
  * This is an alternative to using the constructor directly.
@@ -264,11 +275,41 @@
264
275
  // src/index.js
265
276
  function Offcanvas(Alpine) {
266
277
  Alpine.directive("offcanvas", (el, {}, { cleanup }) => {
278
+ const offcanvasId = el.getAttribute("id");
279
+ if (!modalId) {
280
+ console.error("\u274C id is required but missing on element:", el);
281
+ return;
282
+ }
267
283
  const offcanvas_ = new E(el);
284
+ if (!Alpine.store("sheets")) {
285
+ Alpine.store("sheets", {});
286
+ }
287
+ Alpine.store("sheets")[offcanvasId] = modal_;
288
+ const openHandler = () => offcanvas_.open();
289
+ const closeHandler = () => offcanvas_.close();
290
+ document.addEventListener(`sheet:${offcanvasId}:open`, openHandler);
291
+ document.addEventListener(`sheet:${offcanvasId}:close`, closeHandler);
268
292
  cleanup(() => {
293
+ document.removeEventListener(`sheet:${modalId}:open`, openHandler);
294
+ document.removeEventListener(
295
+ `sheet:${offcanvasId}:close`,
296
+ closeHandler
297
+ );
269
298
  offcanvas_.cleanup();
299
+ delete Alpine.store("sheets")[modalId];
270
300
  });
271
301
  });
302
+ Alpine.magic("offcanvas", (el) => (id) => {
303
+ if (!Alpine.store("sheets")) {
304
+ console.warn("\u26A0\uFE0F Alpine store for Offcanvas is not initialized.");
305
+ return null;
306
+ }
307
+ if (!Alpine.store("sheets")[id]) {
308
+ console.warn(`\u26A0\uFE0F No offcanvas instance found for ID: ${id}`);
309
+ return null;
310
+ }
311
+ return Alpine.store("sheets")[id];
312
+ });
272
313
  }
273
314
  var src_default = Offcanvas;
274
315
 
package/dist/cdn.min.js CHANGED
@@ -1 +1 @@
1
- (()=>{var L=Object.defineProperty,I=(e,t,n)=>t in e?L(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,i=(e,t,n)=>I(e,typeof t!="symbol"?t+"":t,n),C=(e,t=document.body)=>t.querySelector(e),m=(e,t=document.body)=>Array.from(t.querySelectorAll(e)),S=({newElement:e,existingElement:t})=>{if(!(e instanceof HTMLElement)||!(t instanceof HTMLElement))throw new Error("Both parameters must be valid HTML elements.");let n=t.parentElement;if(n)n.insertBefore(e,t);else throw new Error("Existing element must have a parent element.")},T=({element:e,callback:t,type:n,keysCheck:a})=>{let s=getComputedStyle(e),o=s.transition;if(o!=="none"&&o!==""&&!a.includes(o)){let l="transitionend",d=()=>{e.removeEventListener(l,d),t()};e.addEventListener(l,d,{once:!0})}else t()},$=({element:e,callback:t})=>{T({element:e,callback:t,type:"transition",keysCheck:["all 0s ease 0s","all"]})},E=(e,t,n)=>{let a=new CustomEvent(t,{detail:n});e.dispatchEvent(a)},H=e=>{var t;return(t=e.parentElement)==null?void 0:t.removeChild(e)},k=e=>{e.setAttribute("data-state","invisible"),$({element:e,callback(){H(e)}})},M=(e,t)=>{let n=e;if(n===""||!n)return;let a=document.createElement("div");if(a.setAttribute("aria-hidden","true"),a.setAttribute("data-state","visible"),a.setAttribute("data-fx-offcanvas-overlay",""),a.setAttribute("data-offcanvas-el",t),n==="")return;let s=n.split(" ");return n!==""&&a.classList.add(...s),a},y=(e,t,n)=>{e.setAttribute("aria-hidden",n==="open"?"false":"true"),e.setAttribute("data-state",n),t||W(n)},W=e=>{document.body.style.overflow=e==="open"?"hidden":"",document.body.style.overflowY=e==="open"?"hidden":"auto"},G=(e,t)=>{if(e===t)return;e.setAttribute("aria-hidden","true"),e.setAttribute("data-state","close");let n=C(`[data-fx-offcanvas-overlay][data-offcanvas-el=${e.getAttribute("id")}]`,e.parentElement);n instanceof HTMLElement&&k(n)},R=e=>{let t=m("[data-fx-offcanvas][data-state=open]");if(!(t.length<=0))for(let n of t)G(n,e)},r=class{static initGlobalRegistry(){window.$flexillaInstances||(window.$flexillaInstances={})}static register(t,n,a){return this.initGlobalRegistry(),window.$flexillaInstances[t]||(window.$flexillaInstances[t]=[]),this.getInstance(t,n)||(window.$flexillaInstances[t].push({element:n,instance:a}),a)}static getInstance(t,n){var a,s;return this.initGlobalRegistry(),(s=(a=window.$flexillaInstances[t])==null?void 0:a.find(o=>o.element===n))==null?void 0:s.instance}static removeInstance(t,n){this.initGlobalRegistry(),window.$flexillaInstances[t]&&(window.$flexillaInstances[t]=window.$flexillaInstances[t].filter(a=>a.element!==n))}},c=class{constructor(t,n={}){i(this,"offCanvasElement"),i(this,"offCanvasTriggers"),i(this,"offCanvasCloseBtns"),i(this,"allowBodyScroll"),i(this,"staticBackdrop"),i(this,"backdrop"),i(this,"options"),i(this,"closeWhenClickOutSide",f=>{let v=this.offCanvasElement.getAttribute("data-state")==="open",h=!this.offCanvasElement.contains(f.target)&&![...this.offCanvasTriggers].includes(f.target);v&&h&&this.closeOffCanvas()}),i(this,"closeOffCanvas",()=>{var f,v,h,b,w;let g=!1;if(E(this.offCanvasElement,"offcanvas-before-hide",{offcanvasId:this.offCanvasElement.id,setExitAction:B=>{g=B}}),((h=(v=(f=this.options).beforeHide)==null?void 0:v.call(f))==null?void 0:h.cancelAction)||g)return;let x=this.offCanvasElement.getAttribute("id"),u=C(`[data-fx-offcanvas-overlay][data-offcanvas-el=${x}]`);u instanceof HTMLElement&&k(u),y(this.offCanvasElement,this.allowBodyScroll,"close"),document.removeEventListener("keydown",this.closeWithEsc),!this.allowBodyScroll&&!u&&document.removeEventListener("click",this.closeWhenClickOutSide),(w=(b=this.options).onHide)==null||w.call(b),E(this.offCanvasElement,"offcanvas-close",{offcanvasId:this.offCanvasElement.id})}),i(this,"closeWithEsc",f=>{f.preventDefault(),f.key==="Escape"&&this.closeOffCanvas()}),i(this,"changeState",()=>{this.offCanvasElement.getAttribute("data-state")==="open"?this.closeOffCanvas():this.openOffCanvas()});let a=typeof t=="string"?C(t):t;if(!(a instanceof HTMLElement))throw new Error("Invalid Offcanvas, the provided Element is not a valid HTMLElement");let s=r.getInstance("offcanvas",a);if(s)return s;this.options=n;let{staticBackdrop:o,allowBodyScroll:l,backdrop:d}=this.options;this.offCanvasElement=a,this.setupAttributes(),this.staticBackdrop=o||a.hasAttribute("data-static-backdrop")&&a.dataset.staticBackdrop!=="false"||!1,this.allowBodyScroll=l||a.hasAttribute("data-allow-body-scroll")&&a.dataset.allowBodyScroll!=="false"||!1;let p=this.offCanvasElement.getAttribute("id");this.offCanvasTriggers=this.findOffCanvasElements("[data-offcanvas-trigger]",!1,p),this.offCanvasCloseBtns=this.findOffCanvasElements("[data-offcanvas-close]",!0,p,this.offCanvasElement),this.backdrop=d||this.offCanvasElement.dataset.offcanvasBackdrop||"",this.setupOffcanvas(),r.register("offcanvas",this.offCanvasElement,this)}findOffCanvasElements(t,n,a,s){return n?m(`${t}`,s):m(`${t}[data-target=${a}]`)}setupAttributes(){this.offCanvasElement.hasAttribute("data-fx-offcanvas")||this.offCanvasElement.setAttribute("data-fx-offcanvas","")}openOffCanvas(){var t,n,a,s;(n=(t=this.options).beforeShow)==null||n.call(t),R(this.offCanvasElement),y(this.offCanvasElement,this.allowBodyScroll,"open");let o=this.offCanvasElement.getAttribute("id"),l=M(this.backdrop,o);l instanceof HTMLElement&&(S({newElement:l,existingElement:this.offCanvasElement}),this.staticBackdrop||l.addEventListener("click",this.closeOffCanvas)),document.addEventListener("keydown",this.closeWithEsc),(s=(a=this.options).onShow)==null||s.call(a),E(this.offCanvasElement,"offcanvas-open",{offcanvasId:this.offCanvasElement.id})}initCloseBtns(){for(let t of this.offCanvasCloseBtns)t.addEventListener("click",this.closeOffCanvas)}initTriggers(){for(let t of this.offCanvasTriggers)t.addEventListener("click",this.changeState)}setupOffcanvas(){this.initTriggers(),this.initCloseBtns()}open(){this.openOffCanvas()}close(){this.closeOffCanvas()}cleanup(){for(let t of this.offCanvasTriggers)t.removeEventListener("click",this.changeState);for(let t of this.offCanvasCloseBtns)t.removeEventListener("click",this.closeOffCanvas);document.removeEventListener("keydown",this.closeWithEsc),this.allowBodyScroll||document.removeEventListener("click",this.closeWhenClickOutSide),r.removeInstance("offcanvas",this.offCanvasElement)}};i(c,"autoInit",(e="[data-fx-offcanvas]")=>{let t=m(e);for(let n of t)new c(n)}),i(c,"init",(e,t={})=>new c(e,t));var A=c;function q(e){e.directive("offcanvas",(t,{},{cleanup:n})=>{let a=new A(t);n(()=>{a.cleanup()})})}var O=q;document.addEventListener("alpine:init",()=>{O(window.Alpine)});})();
1
+ (()=>{var B=Object.defineProperty,A=(t,e,n)=>e in t?B(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,o=(t,e,n)=>A(t,typeof e!="symbol"?e+"":e,n),C=(t,e=document.body)=>e.querySelector(t),m=(t,e=document.body)=>Array.from(e.querySelectorAll(t)),S=({newElement:t,existingElement:e})=>{if(!(t instanceof HTMLElement)||!(e instanceof HTMLElement))throw new Error("Both parameters must be valid HTML elements.");let n=e.parentElement;if(n)n.insertBefore(t,e);else throw new Error("Existing element must have a parent element.")},$=({element:t,callback:e,type:n,keysCheck:s})=>{let a=getComputedStyle(t),i=a.transition;if(i!=="none"&&i!==""&&!s.includes(i)){let l="transitionend",d=()=>{t.removeEventListener(l,d),e()};t.addEventListener(l,d,{once:!0})}else e()},T=({element:t,callback:e})=>{$({element:t,callback:e,type:"transition",keysCheck:["all 0s ease 0s","all"]})},E=(t,e,n)=>{let s=new CustomEvent(e,{detail:n});t.dispatchEvent(s)},H=t=>{var e;return(e=t.parentElement)==null?void 0:e.removeChild(t)},k=t=>{t.setAttribute("data-state","invisible"),T({element:t,callback(){H(t)}})},M=(t,e)=>{let n=t;if(n===""||!n)return;let 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;let a=n.split(" ");return n!==""&&s.classList.add(...a),s},y=(t,e,n)=>{t.setAttribute("aria-hidden",n==="open"?"false":"true"),t.setAttribute("data-state",n),e||W(n)},W=t=>{document.body.style.overflow=t==="open"?"hidden":"",document.body.style.overflowY=t==="open"?"hidden":"auto"},G=(t,e)=>{if(t===e)return;t.setAttribute("aria-hidden","true"),t.setAttribute("data-state","close");let n=C(`[data-fx-offcanvas-overlay][data-offcanvas-el=${t.getAttribute("id")}]`,t.parentElement);n instanceof HTMLElement&&k(n)},R=t=>{let e=m("[data-fx-offcanvas][data-state=open]");if(!(e.length<=0))for(let n of e)G(n,t)},c=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))}},r=class{constructor(e,n={}){o(this,"offCanvasElement"),o(this,"offCanvasTriggers"),o(this,"offCanvasCloseBtns"),o(this,"allowBodyScroll"),o(this,"staticBackdrop"),o(this,"backdrop"),o(this,"options"),o(this,"closeWhenClickOutSide",f=>{let v=this.offCanvasElement.getAttribute("data-state")==="open",h=!this.offCanvasElement.contains(f.target)&&![...this.offCanvasTriggers].includes(f.target);v&&h&&this.closeOffCanvas()}),o(this,"closeOffCanvas",()=>{var f,v,h,b,w;let g=!1;if(E(this.offCanvasElement,"offcanvas-before-hide",{offcanvasId:this.offCanvasElement.id,setExitAction:x=>{g=x}}),((h=(v=(f=this.options).beforeHide)==null?void 0:v.call(f))==null?void 0:h.cancelAction)||g)return;let I=this.offCanvasElement.getAttribute("id"),u=C(`[data-fx-offcanvas-overlay][data-offcanvas-el=${I}]`);u instanceof HTMLElement&&k(u),y(this.offCanvasElement,this.allowBodyScroll,"close"),document.removeEventListener("keydown",this.closeWithEsc),!this.allowBodyScroll&&!u&&document.removeEventListener("click",this.closeWhenClickOutSide),(w=(b=this.options).onHide)==null||w.call(b),E(this.offCanvasElement,"offcanvas-close",{offcanvasId:this.offCanvasElement.id})}),o(this,"closeWithEsc",f=>{f.preventDefault(),f.key==="Escape"&&this.closeOffCanvas()}),o(this,"changeState",()=>{this.offCanvasElement.getAttribute("data-state")==="open"?this.closeOffCanvas():this.openOffCanvas()});let s=typeof e=="string"?C(e):e;if(!(s instanceof HTMLElement))throw new Error("Invalid Offcanvas, the provided Element is not a valid HTMLElement");let a=c.getInstance("offcanvas",s);if(a)return a;this.options=n;let{staticBackdrop:i,allowBodyScroll:l,backdrop:d}=this.options;this.offCanvasElement=s,this.setupAttributes(),this.staticBackdrop=i||s.hasAttribute("data-static-backdrop")&&s.dataset.staticBackdrop!=="false"||!1,this.allowBodyScroll=l||s.hasAttribute("data-allow-body-scroll")&&s.dataset.allowBodyScroll!=="false"||!1;let p=this.offCanvasElement.getAttribute("id");this.offCanvasTriggers=this.findOffCanvasElements("[data-offcanvas-trigger]",!1,p),this.offCanvasCloseBtns=this.findOffCanvasElements("[data-offcanvas-close]",!0,p,this.offCanvasElement),this.backdrop=d||this.offCanvasElement.dataset.offcanvasBackdrop||"",this.setupOffcanvas(),c.register("offcanvas",this.offCanvasElement,this)}findOffCanvasElements(e,n,s,a){return n?m(`${e}`,a):m(`${e}[data-target=${s}]`)}setupAttributes(){this.offCanvasElement.hasAttribute("data-fx-offcanvas")||this.offCanvasElement.setAttribute("data-fx-offcanvas","")}openOffCanvas(){var e,n,s,a;(n=(e=this.options).beforeShow)==null||n.call(e),R(this.offCanvasElement),y(this.offCanvasElement,this.allowBodyScroll,"open");let i=this.offCanvasElement.getAttribute("id"),l=M(this.backdrop,i);l instanceof HTMLElement&&(S({newElement:l,existingElement:this.offCanvasElement}),this.staticBackdrop||l.addEventListener("click",this.closeOffCanvas)),document.addEventListener("keydown",this.closeWithEsc),(a=(s=this.options).onShow)==null||a.call(s),E(this.offCanvasElement,"offcanvas-open",{offcanvasId:this.offCanvasElement.id})}initCloseBtns(){for(let e of this.offCanvasCloseBtns)e.addEventListener("click",this.closeOffCanvas)}initTriggers(){for(let e of this.offCanvasTriggers)e.addEventListener("click",this.changeState)}setupOffcanvas(){this.initTriggers(),this.initCloseBtns()}open(){this.openOffCanvas()}close(){this.closeOffCanvas()}cleanup(){for(let e of this.offCanvasTriggers)e.removeEventListener("click",this.changeState);for(let e of this.offCanvasCloseBtns)e.removeEventListener("click",this.closeOffCanvas);document.removeEventListener("keydown",this.closeWithEsc),this.allowBodyScroll||document.removeEventListener("click",this.closeWhenClickOutSide),c.removeInstance("offcanvas",this.offCanvasElement)}};o(r,"autoInit",(t="[data-fx-offcanvas]")=>{let e=m(t);for(let n of e)new r(n)}),o(r,"init",(t,e={})=>new r(t,e));var O=r;function q(t){t.directive("offcanvas",(e,{},{cleanup:n})=>{let s=e.getAttribute("id");if(!modalId){console.error("\u274C id is required but missing on element:",e);return}let a=new O(e);t.store("sheets")||t.store("sheets",{}),t.store("sheets")[s]=modal_;let i=()=>a.open(),l=()=>a.close();document.addEventListener(`sheet:${s}:open`,i),document.addEventListener(`sheet:${s}:close`,l),n(()=>{document.removeEventListener(`sheet:${modalId}:open`,i),document.removeEventListener(`sheet:${s}:close`,l),a.cleanup(),delete t.store("sheets")[modalId]})}),t.magic("offcanvas",e=>n=>t.store("sheets")?t.store("sheets")[n]?t.store("sheets")[n]:(console.warn(`\u26A0\uFE0F No offcanvas instance found for ID: ${n}`),null):(console.warn("\u26A0\uFE0F Alpine store for Offcanvas is not initialized."),null))}var L=q;document.addEventListener("alpine:init",()=>{L(window.Alpine)});})();
@@ -36,8 +36,10 @@ var y = ({
36
36
  if (!(s instanceof HTMLElement) || !(t instanceof HTMLElement))
37
37
  throw new Error("Both parameters must be valid HTML elements.");
38
38
  const e = t.parentElement;
39
- if (e) e.insertBefore(s, t);
40
- else throw new Error("Existing element must have a parent element.");
39
+ if (e)
40
+ e.insertBefore(s, t);
41
+ else
42
+ throw new Error("Existing element must have a parent element.");
41
43
  };
42
44
  var k = ({
43
45
  element: s,
@@ -83,9 +85,11 @@ var p = (s) => {
83
85
  };
84
86
  var B = (s, t) => {
85
87
  const e = s;
86
- if (e === "" || !e) return;
88
+ if (e === "" || !e)
89
+ return;
87
90
  const n = document.createElement("div");
88
- if (n.setAttribute("aria-hidden", "true"), n.setAttribute("data-state", "visible"), n.setAttribute("data-fx-offcanvas-overlay", ""), n.setAttribute("data-offcanvas-el", t), e === "") return;
91
+ if (n.setAttribute("aria-hidden", "true"), n.setAttribute("data-state", "visible"), n.setAttribute("data-fx-offcanvas-overlay", ""), n.setAttribute("data-offcanvas-el", t), e === "")
92
+ return;
89
93
  const a = e.split(" ");
90
94
  return e !== "" && n.classList.add(...a), n;
91
95
  };
@@ -96,7 +100,8 @@ var S = (s) => {
96
100
  document.body.style.overflow = s === "open" ? "hidden" : "", document.body.style.overflowY = s === "open" ? "hidden" : "auto";
97
101
  };
98
102
  var x = (s, t) => {
99
- if (s === t) return;
103
+ if (s === t)
104
+ return;
100
105
  s.setAttribute("aria-hidden", "true"), s.setAttribute("data-state", "close");
101
106
  const e = m(`[data-fx-offcanvas-overlay][data-offcanvas-el=${s.getAttribute("id")}]`, s.parentElement);
102
107
  e instanceof HTMLElement && p(e);
@@ -104,7 +109,8 @@ var x = (s, t) => {
104
109
  var I = (s) => {
105
110
  const t = d("[data-fx-offcanvas][data-state=open]");
106
111
  if (!(t.length <= 0))
107
- for (const e of t) x(e, s);
112
+ for (const e of t)
113
+ x(e, s);
108
114
  };
109
115
  var h = class {
110
116
  static initGlobalRegistry() {
@@ -163,7 +169,8 @@ var c = class c2 {
163
169
  setExitAction: (g) => {
164
170
  t2 = g;
165
171
  }
166
- }), ((f2 = (l2 = (i2 = this.options).beforeHide) == null ? void 0 : l2.call(i2)) == null ? void 0 : f2.cancelAction) || t2) return;
172
+ }), ((f2 = (l2 = (i2 = this.options).beforeHide) == null ? void 0 : l2.call(i2)) == null ? void 0 : f2.cancelAction) || t2)
173
+ return;
167
174
  const n2 = this.offCanvasElement.getAttribute("id"), a2 = m(`[data-fx-offcanvas-overlay][data-offcanvas-el=${n2}]`);
168
175
  a2 instanceof HTMLElement && p(a2), C(
169
176
  this.offCanvasElement,
@@ -178,7 +185,8 @@ var c = class c2 {
178
185
  this.offCanvasElement.getAttribute("data-state") === "open" ? this.closeOffCanvas() : this.openOffCanvas();
179
186
  });
180
187
  const n = typeof t == "string" ? m(t) : t;
181
- if (!(n instanceof HTMLElement)) throw new Error("Invalid Offcanvas, the provided Element is not a valid HTMLElement");
188
+ if (!(n instanceof HTMLElement))
189
+ throw new Error("Invalid Offcanvas, the provided Element is not a valid HTMLElement");
182
190
  const a = h.getInstance("offcanvas", n);
183
191
  if (a)
184
192
  return a;
@@ -208,10 +216,12 @@ var c = class c2 {
208
216
  e instanceof HTMLElement && (y({ newElement: e, existingElement: this.offCanvasElement }), this.staticBackdrop || e.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 });
209
217
  }
210
218
  initCloseBtns() {
211
- for (const t of this.offCanvasCloseBtns) t.addEventListener("click", this.closeOffCanvas);
219
+ for (const t of this.offCanvasCloseBtns)
220
+ t.addEventListener("click", this.closeOffCanvas);
212
221
  }
213
222
  initTriggers() {
214
- for (const t of this.offCanvasTriggers) t.addEventListener("click", this.changeState);
223
+ for (const t of this.offCanvasTriggers)
224
+ t.addEventListener("click", this.changeState);
215
225
  }
216
226
  setupOffcanvas() {
217
227
  this.initTriggers(), this.initCloseBtns();
@@ -265,7 +275,8 @@ var c = class c2 {
265
275
  };
266
276
  o(c, "autoInit", (t = "[data-fx-offcanvas]") => {
267
277
  const e = d(t);
268
- for (const n of e) new c(n);
278
+ for (const n of e)
279
+ new c(n);
269
280
  }), /**
270
281
  * Creates a new instance of Offcanvas with the given element and options.
271
282
  * This is an alternative to using the constructor directly.
@@ -288,11 +299,41 @@ var E = c;
288
299
  // src/index.js
289
300
  function Offcanvas(Alpine) {
290
301
  Alpine.directive("offcanvas", (el, {}, { cleanup }) => {
302
+ const offcanvasId = el.getAttribute("id");
303
+ if (!modalId) {
304
+ console.error("\u274C id is required but missing on element:", el);
305
+ return;
306
+ }
291
307
  const offcanvas_ = new E(el);
308
+ if (!Alpine.store("sheets")) {
309
+ Alpine.store("sheets", {});
310
+ }
311
+ Alpine.store("sheets")[offcanvasId] = modal_;
312
+ const openHandler = () => offcanvas_.open();
313
+ const closeHandler = () => offcanvas_.close();
314
+ document.addEventListener(`sheet:${offcanvasId}:open`, openHandler);
315
+ document.addEventListener(`sheet:${offcanvasId}:close`, closeHandler);
292
316
  cleanup(() => {
317
+ document.removeEventListener(`sheet:${modalId}:open`, openHandler);
318
+ document.removeEventListener(
319
+ `sheet:${offcanvasId}:close`,
320
+ closeHandler
321
+ );
293
322
  offcanvas_.cleanup();
323
+ delete Alpine.store("sheets")[modalId];
294
324
  });
295
325
  });
326
+ Alpine.magic("offcanvas", (el) => (id) => {
327
+ if (!Alpine.store("sheets")) {
328
+ console.warn("\u26A0\uFE0F Alpine store for Offcanvas is not initialized.");
329
+ return null;
330
+ }
331
+ if (!Alpine.store("sheets")[id]) {
332
+ console.warn(`\u26A0\uFE0F No offcanvas instance found for ID: ${id}`);
333
+ return null;
334
+ }
335
+ return Alpine.store("sheets")[id];
336
+ });
296
337
  }
297
338
  var src_default = Offcanvas;
298
339
 
@@ -11,8 +11,10 @@ var y = ({
11
11
  if (!(s instanceof HTMLElement) || !(t instanceof HTMLElement))
12
12
  throw new Error("Both parameters must be valid HTML elements.");
13
13
  const e = t.parentElement;
14
- if (e) e.insertBefore(s, t);
15
- else throw new Error("Existing element must have a parent element.");
14
+ if (e)
15
+ e.insertBefore(s, t);
16
+ else
17
+ throw new Error("Existing element must have a parent element.");
16
18
  };
17
19
  var k = ({
18
20
  element: s,
@@ -58,9 +60,11 @@ var p = (s) => {
58
60
  };
59
61
  var B = (s, t) => {
60
62
  const e = s;
61
- if (e === "" || !e) return;
63
+ if (e === "" || !e)
64
+ return;
62
65
  const n = document.createElement("div");
63
- if (n.setAttribute("aria-hidden", "true"), n.setAttribute("data-state", "visible"), n.setAttribute("data-fx-offcanvas-overlay", ""), n.setAttribute("data-offcanvas-el", t), e === "") return;
66
+ if (n.setAttribute("aria-hidden", "true"), n.setAttribute("data-state", "visible"), n.setAttribute("data-fx-offcanvas-overlay", ""), n.setAttribute("data-offcanvas-el", t), e === "")
67
+ return;
64
68
  const a = e.split(" ");
65
69
  return e !== "" && n.classList.add(...a), n;
66
70
  };
@@ -71,7 +75,8 @@ var S = (s) => {
71
75
  document.body.style.overflow = s === "open" ? "hidden" : "", document.body.style.overflowY = s === "open" ? "hidden" : "auto";
72
76
  };
73
77
  var x = (s, t) => {
74
- if (s === t) return;
78
+ if (s === t)
79
+ return;
75
80
  s.setAttribute("aria-hidden", "true"), s.setAttribute("data-state", "close");
76
81
  const e = m(`[data-fx-offcanvas-overlay][data-offcanvas-el=${s.getAttribute("id")}]`, s.parentElement);
77
82
  e instanceof HTMLElement && p(e);
@@ -79,7 +84,8 @@ var x = (s, t) => {
79
84
  var I = (s) => {
80
85
  const t = d("[data-fx-offcanvas][data-state=open]");
81
86
  if (!(t.length <= 0))
82
- for (const e of t) x(e, s);
87
+ for (const e of t)
88
+ x(e, s);
83
89
  };
84
90
  var h = class {
85
91
  static initGlobalRegistry() {
@@ -138,7 +144,8 @@ var c = class c2 {
138
144
  setExitAction: (g) => {
139
145
  t2 = g;
140
146
  }
141
- }), ((f2 = (l2 = (i2 = this.options).beforeHide) == null ? void 0 : l2.call(i2)) == null ? void 0 : f2.cancelAction) || t2) return;
147
+ }), ((f2 = (l2 = (i2 = this.options).beforeHide) == null ? void 0 : l2.call(i2)) == null ? void 0 : f2.cancelAction) || t2)
148
+ return;
142
149
  const n2 = this.offCanvasElement.getAttribute("id"), a2 = m(`[data-fx-offcanvas-overlay][data-offcanvas-el=${n2}]`);
143
150
  a2 instanceof HTMLElement && p(a2), C(
144
151
  this.offCanvasElement,
@@ -153,7 +160,8 @@ var c = class c2 {
153
160
  this.offCanvasElement.getAttribute("data-state") === "open" ? this.closeOffCanvas() : this.openOffCanvas();
154
161
  });
155
162
  const n = typeof t == "string" ? m(t) : t;
156
- if (!(n instanceof HTMLElement)) throw new Error("Invalid Offcanvas, the provided Element is not a valid HTMLElement");
163
+ if (!(n instanceof HTMLElement))
164
+ throw new Error("Invalid Offcanvas, the provided Element is not a valid HTMLElement");
157
165
  const a = h.getInstance("offcanvas", n);
158
166
  if (a)
159
167
  return a;
@@ -183,10 +191,12 @@ var c = class c2 {
183
191
  e instanceof HTMLElement && (y({ newElement: e, existingElement: this.offCanvasElement }), this.staticBackdrop || e.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 });
184
192
  }
185
193
  initCloseBtns() {
186
- for (const t of this.offCanvasCloseBtns) t.addEventListener("click", this.closeOffCanvas);
194
+ for (const t of this.offCanvasCloseBtns)
195
+ t.addEventListener("click", this.closeOffCanvas);
187
196
  }
188
197
  initTriggers() {
189
- for (const t of this.offCanvasTriggers) t.addEventListener("click", this.changeState);
198
+ for (const t of this.offCanvasTriggers)
199
+ t.addEventListener("click", this.changeState);
190
200
  }
191
201
  setupOffcanvas() {
192
202
  this.initTriggers(), this.initCloseBtns();
@@ -240,7 +250,8 @@ var c = class c2 {
240
250
  };
241
251
  o(c, "autoInit", (t = "[data-fx-offcanvas]") => {
242
252
  const e = d(t);
243
- for (const n of e) new c(n);
253
+ for (const n of e)
254
+ new c(n);
244
255
  }), /**
245
256
  * Creates a new instance of Offcanvas with the given element and options.
246
257
  * This is an alternative to using the constructor directly.
@@ -263,11 +274,41 @@ var E = c;
263
274
  // src/index.js
264
275
  function Offcanvas(Alpine) {
265
276
  Alpine.directive("offcanvas", (el, {}, { cleanup }) => {
277
+ const offcanvasId = el.getAttribute("id");
278
+ if (!modalId) {
279
+ console.error("\u274C id is required but missing on element:", el);
280
+ return;
281
+ }
266
282
  const offcanvas_ = new E(el);
283
+ if (!Alpine.store("sheets")) {
284
+ Alpine.store("sheets", {});
285
+ }
286
+ Alpine.store("sheets")[offcanvasId] = modal_;
287
+ const openHandler = () => offcanvas_.open();
288
+ const closeHandler = () => offcanvas_.close();
289
+ document.addEventListener(`sheet:${offcanvasId}:open`, openHandler);
290
+ document.addEventListener(`sheet:${offcanvasId}:close`, closeHandler);
267
291
  cleanup(() => {
292
+ document.removeEventListener(`sheet:${modalId}:open`, openHandler);
293
+ document.removeEventListener(
294
+ `sheet:${offcanvasId}:close`,
295
+ closeHandler
296
+ );
268
297
  offcanvas_.cleanup();
298
+ delete Alpine.store("sheets")[modalId];
269
299
  });
270
300
  });
301
+ Alpine.magic("offcanvas", (el) => (id) => {
302
+ if (!Alpine.store("sheets")) {
303
+ console.warn("\u26A0\uFE0F Alpine store for Offcanvas is not initialized.");
304
+ return null;
305
+ }
306
+ if (!Alpine.store("sheets")[id]) {
307
+ console.warn(`\u26A0\uFE0F No offcanvas instance found for ID: ${id}`);
308
+ return null;
309
+ }
310
+ return Alpine.store("sheets")[id];
311
+ });
271
312
  }
272
313
  var src_default = Offcanvas;
273
314
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flexilla/alpine-offcanvas",
3
- "version": "0.0.0",
3
+ "version": "0.0.1",
4
4
  "description": "AlpineJS plugin for adding offcanvas functionality to your AlpineJS components",
5
5
  "type": "module",
6
6
  "private": false,
@@ -60,5 +60,5 @@
60
60
  "dependencies": {
61
61
  "@flexilla/offcanvas": "^2.0.10"
62
62
  },
63
- "gitHead": "3c7ea3f74d67ac5d9ab2817d747ad7127eb542b5"
63
+ "gitHead": "2de9f4208852d8b159dfd7a84c4e8d03f9d53a86"
64
64
  }
package/src/index.js CHANGED
@@ -2,11 +2,45 @@ import { OffCanvas as FlexillaOffcanvas } from "@flexilla/offcanvas";
2
2
 
3
3
  function Offcanvas(Alpine) {
4
4
  Alpine.directive("offcanvas", (el, {}, { cleanup }) => {
5
- const offcanvas_ = new FlexillaOffcanvas(el)
5
+ const offcanvasId = el.getAttribute("id");
6
+ if (!modalId) {
7
+ console.error("❌ id is required but missing on element:", el);
8
+ return;
9
+ }
10
+ const offcanvas_ = new FlexillaOffcanvas(el);
11
+
12
+ if (!Alpine.store("sheets")) {
13
+ Alpine.store("sheets", {});
14
+ }
15
+
16
+ Alpine.store("sheets")[offcanvasId] = modal_;
17
+ const openHandler = () => offcanvas_.open();
18
+ const closeHandler = () => offcanvas_.close();
19
+
20
+ document.addEventListener(`sheet:${offcanvasId}:open`, openHandler);
21
+ document.addEventListener(`sheet:${offcanvasId}:close`, closeHandler);
6
22
  cleanup(() => {
7
- offcanvas_.cleanup()
23
+ document.removeEventListener(`sheet:${modalId}:open`, openHandler);
24
+ document.removeEventListener(
25
+ `sheet:${offcanvasId}:close`,
26
+ closeHandler
27
+ );
28
+ offcanvas_.cleanup();
29
+ delete Alpine.store("sheets")[modalId];
8
30
  });
9
31
  });
32
+
33
+ Alpine.magic("offcanvas", (el) => (id) => {
34
+ if (!Alpine.store("sheets")) {
35
+ console.warn("⚠️ Alpine store for Offcanvas is not initialized.");
36
+ return null;
37
+ }
38
+ if (!Alpine.store("sheets")[id]) {
39
+ console.warn(`⚠️ No offcanvas instance found for ID: ${id}`);
40
+ return null;
41
+ }
42
+ return Alpine.store("sheets")[id];
43
+ });
10
44
  }
11
45
 
12
- export default Offcanvas;
46
+ export default Offcanvas;