@flexilla/alpine-modal 0.0.2 → 0.0.4
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 +250 -195
- package/dist/cdn.min.js +1 -1
- package/dist/module.cjs.js +250 -195
- package/dist/module.esm.js +250 -195
- package/package.json +3 -3
- package/src/index.js +28 -9
package/dist/module.esm.js
CHANGED
|
@@ -1,245 +1,300 @@
|
|
|
1
1
|
// ../../node_modules/@flexilla/modal/dist/modal.js
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return this.initGlobalRegistry(), window.$flexillaInstances[e] || (window.$flexillaInstances[e] = []), this.getInstance(e, n) || (window.$flexillaInstances[e].push({ element: n, instance: a }), a);
|
|
11
|
-
}
|
|
12
|
-
static getInstance(e, n) {
|
|
13
|
-
var a, d;
|
|
14
|
-
return this.initGlobalRegistry(), (d = (a = window.$flexillaInstances[e]) == null ? void 0 : a.find(
|
|
15
|
-
(s) => s.element === n
|
|
16
|
-
)) == null ? void 0 : d.instance;
|
|
17
|
-
}
|
|
18
|
-
static removeInstance(e, n) {
|
|
19
|
-
this.initGlobalRegistry(), window.$flexillaInstances[e] && (window.$flexillaInstances[e] = window.$flexillaInstances[e].filter(
|
|
20
|
-
(a) => a.element !== n
|
|
21
|
-
));
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
var r = (t, e = document.body) => e.querySelector(t);
|
|
25
|
-
var H = (t, e = document.body) => Array.from(e.querySelectorAll(t));
|
|
26
|
-
var J = ({
|
|
27
|
-
newElement: t,
|
|
28
|
-
existingElement: e
|
|
2
|
+
var p = Object.defineProperty;
|
|
3
|
+
var b = (o, t, e) => t in o ? p(o, t, { enumerable: true, configurable: true, writable: true, value: e }) : o[t] = e;
|
|
4
|
+
var s = (o, t, e) => b(o, typeof t != "symbol" ? t + "" : t, e);
|
|
5
|
+
var r = (o, t = document.body) => t.querySelector(o);
|
|
6
|
+
var c = (o, t = document.body) => Array.from(t.querySelectorAll(o));
|
|
7
|
+
var M = ({
|
|
8
|
+
newElement: o,
|
|
9
|
+
existingElement: t
|
|
29
10
|
}) => {
|
|
30
|
-
if (!(
|
|
11
|
+
if (!(o instanceof HTMLElement) || !(t instanceof HTMLElement))
|
|
31
12
|
throw new Error("Both parameters must be valid HTML elements.");
|
|
32
|
-
const
|
|
33
|
-
if (
|
|
34
|
-
|
|
13
|
+
const e = t.parentElement;
|
|
14
|
+
if (e)
|
|
15
|
+
e.insertBefore(o, t);
|
|
35
16
|
else
|
|
36
17
|
throw new Error("Existing element must have a parent element.");
|
|
37
18
|
};
|
|
38
|
-
var
|
|
39
|
-
element:
|
|
40
|
-
callback:
|
|
41
|
-
type:
|
|
42
|
-
keysCheck:
|
|
19
|
+
var g = ({
|
|
20
|
+
element: o,
|
|
21
|
+
callback: t,
|
|
22
|
+
type: e,
|
|
23
|
+
keysCheck: n
|
|
43
24
|
}) => {
|
|
44
|
-
const
|
|
45
|
-
if (
|
|
46
|
-
const
|
|
47
|
-
|
|
25
|
+
const a = getComputedStyle(o), i = a.animation;
|
|
26
|
+
if (i !== "none" && i !== "" && !n.includes(i)) {
|
|
27
|
+
const l = "animationend", d = () => {
|
|
28
|
+
o.removeEventListener(l, d), t();
|
|
48
29
|
};
|
|
49
|
-
|
|
30
|
+
o.addEventListener(l, d, { once: true });
|
|
50
31
|
} else
|
|
51
|
-
|
|
32
|
+
t();
|
|
52
33
|
};
|
|
53
|
-
var
|
|
54
|
-
|
|
55
|
-
element:
|
|
56
|
-
callback:
|
|
34
|
+
var y = ({ element: o, callback: t }) => {
|
|
35
|
+
g({
|
|
36
|
+
element: o,
|
|
37
|
+
callback: t,
|
|
57
38
|
type: "animation",
|
|
58
39
|
keysCheck: ["none 0s ease 0s 1 normal none running"]
|
|
59
40
|
});
|
|
60
41
|
};
|
|
61
|
-
var
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
};
|
|
65
|
-
var W = (t) => {
|
|
66
|
-
var e;
|
|
67
|
-
t instanceof HTMLElement && ((e = t.parentElement) == null || e.removeChild(t));
|
|
42
|
+
var f = (o, t, e) => {
|
|
43
|
+
const n = new CustomEvent(t, { detail: e });
|
|
44
|
+
o.dispatchEvent(n);
|
|
68
45
|
};
|
|
69
|
-
var
|
|
70
|
-
|
|
71
|
-
return n.setAttribute("aria-hidden", "true"), J({ newElement: n, existingElement: t }), n.classList.add(...e), n.setAttribute("data-modal-overlay", ""), n;
|
|
72
|
-
};
|
|
73
|
-
var k = (t, e, n) => {
|
|
74
|
-
if (!(e instanceof HTMLElement))
|
|
46
|
+
var E = (o, t, e) => {
|
|
47
|
+
if (!(t instanceof HTMLElement))
|
|
75
48
|
throw new Error("No modal-content found");
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
|
|
49
|
+
o.setAttribute("aria-hidden", e === "open" ? "false" : "true"), o.setAttribute("data-state", e), t.setAttribute("data-state", e);
|
|
50
|
+
const n = r("[data-modal-overlay]", o);
|
|
51
|
+
n instanceof HTMLElement && n.setAttribute("data-state", e);
|
|
79
52
|
};
|
|
80
|
-
var
|
|
81
|
-
if (!
|
|
82
|
-
|
|
53
|
+
var A = (o, t, e) => {
|
|
54
|
+
if (!o) {
|
|
55
|
+
t || (document.body.style.overflowY = "auto");
|
|
83
56
|
return;
|
|
84
57
|
}
|
|
85
|
-
|
|
58
|
+
c("[data-fx-modal][data-state=open]:not([data-allow-body-scroll=true]").filter((i) => i !== e).length === 0 && !t && (document.body.style.overflowY = "auto");
|
|
86
59
|
};
|
|
87
|
-
var
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const w = H("[data-fx-modal][data-state=open]");
|
|
106
|
-
for (const y of w)
|
|
107
|
-
if (y !== l) {
|
|
108
|
-
const M = r("[data-modal-overlay]", y);
|
|
109
|
-
M.setAttribute("data-state", "close");
|
|
110
|
-
const I = r("[data-modal-content]", y), E = M.getAttribute("data-overlay-nature") === "default";
|
|
111
|
-
k(y, I, "close"), E || W(M);
|
|
112
|
-
}
|
|
113
|
-
}, T = () => {
|
|
114
|
-
if (t.getAttribute("data-state") !== "open") {
|
|
115
|
-
if (X(t), i = S ? i : U({
|
|
116
|
-
modalContent: o,
|
|
117
|
-
overlayClassName: K
|
|
118
|
-
}), i == null || i.setAttribute("data-state", "open"), B(t, "modal-open", { modalId: t.id }), a || D !== "") {
|
|
119
|
-
const w = a ? a.enterAnimation : D;
|
|
120
|
-
w !== "" && o.style.setProperty("--un-modal-animation", w), k(t, o, "open"), N({
|
|
121
|
-
element: o,
|
|
122
|
-
callback: () => {
|
|
123
|
-
o.style.removeProperty("--un-modal-animation");
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
} else
|
|
127
|
-
k(t, o, "open");
|
|
128
|
-
p || (document.body.style.overflow = "hidden"), x || (document.addEventListener("keydown", R), x = true), t.focus(), s || i.addEventListener("click", A), u == null || u(), m == null || m({ isHidden: false });
|
|
129
|
-
}
|
|
130
|
-
}, A = () => {
|
|
131
|
-
var E;
|
|
132
|
-
let l = false;
|
|
133
|
-
B(t, "before-hide", {
|
|
134
|
-
modalId: t.id,
|
|
135
|
-
setExitAction: ($) => {
|
|
136
|
-
l = $;
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
const w = (E = b == null ? void 0 : b()) == null ? void 0 : E.cancelAction;
|
|
140
|
-
if (l || w)
|
|
141
|
-
return;
|
|
142
|
-
const y = () => {
|
|
143
|
-
k(t, o, "close"), V(C, p, t), S || W(i), B(t, "modal-close", { modalId: t.id });
|
|
144
|
-
}, M = () => {
|
|
145
|
-
x && (document.removeEventListener("keydown", R), x = false), t.blur(), h == null || h(), m == null || m({ isHidden: true });
|
|
146
|
-
}, I = (a == null ? void 0 : a.exitAnimation) && a.exitAnimation !== "" || P !== "";
|
|
147
|
-
if (i == null || i.setAttribute("data-state", "close"), o.setAttribute("data-state", "close"), I) {
|
|
148
|
-
const $ = a ? a.exitAnimation || "" : P;
|
|
149
|
-
o.style.setProperty("--un-modal-animation", $);
|
|
150
|
-
}
|
|
151
|
-
N({
|
|
152
|
-
element: o,
|
|
153
|
-
callback: () => {
|
|
154
|
-
I && o.style.removeProperty("--un-modal-animation"), y(), M();
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
}, G = (l) => {
|
|
158
|
-
l.preventDefault(), A();
|
|
159
|
-
};
|
|
160
|
-
return { autoInitModal: () => {
|
|
161
|
-
if (e instanceof HTMLElement && e.addEventListener("click", T), L.length > 0)
|
|
162
|
-
for (const l of L)
|
|
163
|
-
l.addEventListener("click", G);
|
|
164
|
-
}, showModal: T, hideModal: A, isHidden: () => t.dataset.state === "close", cleanup: () => {
|
|
165
|
-
if (e instanceof HTMLElement && e.removeEventListener("click", T), L.length > 0)
|
|
166
|
-
for (const l of L)
|
|
167
|
-
l.removeEventListener("click", G);
|
|
168
|
-
!s && i instanceof HTMLElement && i.removeEventListener("click", A), O.removeInstance("modal", t);
|
|
169
|
-
} };
|
|
60
|
+
var v = class {
|
|
61
|
+
static initGlobalRegistry() {
|
|
62
|
+
window.$flexillaInstances || (window.$flexillaInstances = {});
|
|
63
|
+
}
|
|
64
|
+
static register(t, e, n) {
|
|
65
|
+
return this.initGlobalRegistry(), window.$flexillaInstances[t] || (window.$flexillaInstances[t] = []), this.getInstance(t, e) || (window.$flexillaInstances[t].push({ element: e, instance: n }), n);
|
|
66
|
+
}
|
|
67
|
+
static getInstance(t, e) {
|
|
68
|
+
var n, a;
|
|
69
|
+
return this.initGlobalRegistry(), (a = (n = window.$flexillaInstances[t]) == null ? void 0 : n.find(
|
|
70
|
+
(i) => i.element === e
|
|
71
|
+
)) == null ? void 0 : a.instance;
|
|
72
|
+
}
|
|
73
|
+
static removeInstance(t, e) {
|
|
74
|
+
this.initGlobalRegistry(), window.$flexillaInstances[t] && (window.$flexillaInstances[t] = window.$flexillaInstances[t].filter(
|
|
75
|
+
(n) => n.element !== e
|
|
76
|
+
));
|
|
77
|
+
}
|
|
170
78
|
};
|
|
171
|
-
var
|
|
79
|
+
var C = (o) => {
|
|
80
|
+
var t;
|
|
81
|
+
o instanceof HTMLElement && ((t = o.parentElement) == null || t.removeChild(o));
|
|
82
|
+
};
|
|
83
|
+
var x = ({ modalContent: o, overlayClassName: t }) => {
|
|
84
|
+
const e = document.createElement("span");
|
|
85
|
+
return e.setAttribute("aria-hidden", "true"), M({ newElement: e, existingElement: o }), e.classList.add(...t), e.setAttribute("data-modal-overlay", ""), e;
|
|
86
|
+
};
|
|
87
|
+
var m = class m2 {
|
|
172
88
|
/**
|
|
173
89
|
* Creates a new Modal instance
|
|
174
90
|
* @param modal - The modal element or selector string to initialize
|
|
175
91
|
* @param options - Configuration options for the modal behavior
|
|
176
|
-
* @param
|
|
177
|
-
* @throws {Error} When the provided modal element is invalid or cannot be found
|
|
92
|
+
* @param triggerElements - Optional trigger elements or selectors that open the modal
|
|
178
93
|
*/
|
|
179
|
-
constructor(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
this
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
94
|
+
constructor(t, e = {}, n) {
|
|
95
|
+
s(this, "modalElement");
|
|
96
|
+
s(this, "modalId");
|
|
97
|
+
s(this, "modalContent");
|
|
98
|
+
s(this, "triggerButtons", []);
|
|
99
|
+
s(this, "overlayElement");
|
|
100
|
+
s(this, "dispatchEventToDocument");
|
|
101
|
+
s(this, "options");
|
|
102
|
+
s(this, "state");
|
|
103
|
+
s(this, "animationEnter");
|
|
104
|
+
s(this, "animationExit");
|
|
105
|
+
s(this, "animateContent");
|
|
106
|
+
s(this, "hasDefaultOverlay");
|
|
107
|
+
s(this, "enableStackedModals");
|
|
108
|
+
s(this, "preventCloseModal");
|
|
109
|
+
s(this, "isKeyDownEventRegistered");
|
|
110
|
+
s(this, "closeButtons");
|
|
111
|
+
s(this, "overlayClassName");
|
|
112
|
+
s(this, "allowBodyScroll");
|
|
113
|
+
s(this, "initAsOpen");
|
|
114
|
+
s(this, "closeAll", (t2) => {
|
|
115
|
+
if (this.enableStackedModals)
|
|
116
|
+
return;
|
|
117
|
+
const e2 = c("[data-fx-modal][data-state=open]");
|
|
118
|
+
for (const n2 of e2) {
|
|
119
|
+
const a2 = n2.dataset.modalId;
|
|
120
|
+
if (a2 !== t2.dataset.modalId) {
|
|
121
|
+
n2.blur(), r("[data-modal-overlay]", n2).setAttribute("data-state", "close");
|
|
122
|
+
const l2 = r("[data-modal-content]", n2);
|
|
123
|
+
E(n2, l2, "close"), document.dispatchEvent(new CustomEvent(`modal:${a2}:close`));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
s(this, "closeModalEsc", (t2) => {
|
|
128
|
+
t2.key === "Escape" && (t2.preventDefault(), this.preventCloseModal || this.hideModal());
|
|
129
|
+
});
|
|
130
|
+
s(this, "initModal", (t2, e2) => {
|
|
131
|
+
var h;
|
|
132
|
+
if (!(t2 instanceof HTMLDialogElement))
|
|
133
|
+
throw new Error("Modal Element must be a valid HTMLDialog Element");
|
|
134
|
+
const { allowBodyScroll: n2, animateContent: a2, preventCloseModal: i2, overlayClass: l2, enableStackedModals: d2 } = e2;
|
|
135
|
+
this.allowBodyScroll = t2.hasAttribute("data-allow-body-scroll") && t2.getAttribute("data-allow-body-scroll") !== "false" || n2 || false, this.preventCloseModal = t2.hasAttribute("data-prevent-close-modal") && t2.getAttribute("data-prevent-close-modal") !== "false" || i2 || false, this.enableStackedModals = t2.hasAttribute("data-enable-stacked") && t2.getAttribute("data-enable-stacked") !== "false" || d2 || false, this.overlayClassName = ((h = t2.dataset.modalOverlay) == null ? void 0 : h.split(" ")) || (l2 == null ? void 0 : l2.split(" ")) || "", this.isKeyDownEventRegistered = false, t2.setAttribute("data-allow-body-scroll", `${this.allowBodyScroll}`), this.closeButtons = c("[data-close-modal]", t2), this.hasDefaultOverlay = false, r("[data-modal-overlay]", t2) instanceof HTMLElement && (this.overlayElement = r("[data-modal-overlay]", t2), this.overlayElement.setAttribute("data-overlay-nature", "default"), this.hasDefaultOverlay = true), this.animateContent = a2, this.animationEnter = this.modalContent.dataset.enterAnimation || "", this.animationExit = this.modalContent.dataset.exitAnimation || "", this.overlayElement && this.overlayElement.setAttribute("data-state", "close"), this.addEvents();
|
|
136
|
+
});
|
|
137
|
+
s(this, "closeModalOnX", (t2) => {
|
|
138
|
+
t2.preventDefault(), this.hideModal();
|
|
139
|
+
});
|
|
140
|
+
s(this, "addEvents", () => {
|
|
141
|
+
for (const t2 of this.triggerButtons)
|
|
142
|
+
t2.addEventListener("click", this.showModal);
|
|
143
|
+
if (this.closeButtons.length > 0)
|
|
144
|
+
for (const t2 of this.closeButtons)
|
|
145
|
+
t2.addEventListener("click", this.closeModalOnX);
|
|
146
|
+
this.dispatchEventToDocument && document.addEventListener(`modal:${this.modalId}:open`, this.showModal), this.dispatchEventToDocument && document.addEventListener(`modal:${this.modalId}:close`, this.hideModal);
|
|
147
|
+
});
|
|
148
|
+
s(this, "showModal", () => {
|
|
149
|
+
var e2, n2, a2, i2, l2;
|
|
150
|
+
if (!(!this.initAsOpen && this.modalElement.getAttribute("data-state") === "open")) {
|
|
151
|
+
if (this.initAsOpen = false, this.closeAll(this.modalElement), this.overlayElement = this.hasDefaultOverlay ? this.overlayElement : x({
|
|
152
|
+
modalContent: this.modalContent,
|
|
153
|
+
overlayClassName: this.overlayClassName
|
|
154
|
+
}), (e2 = this.overlayElement) == null || e2.setAttribute("data-state", "open"), f(this.modalElement, "modal-open", { modalId: this.modalId }), this.animateContent || this.animationEnter !== "") {
|
|
155
|
+
const d2 = this.animateContent ? this.animateContent.enterAnimation : this.animationEnter;
|
|
156
|
+
d2 && d2 !== "" && this.modalContent.style.setProperty("--un-modal-animation", d2), E(this.modalElement, this.modalContent, "open"), y({
|
|
157
|
+
element: this.modalContent,
|
|
158
|
+
callback: () => {
|
|
159
|
+
this.modalContent.style.removeProperty("--un-modal-animation");
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
} else
|
|
163
|
+
E(this.modalElement, this.modalContent, "open");
|
|
164
|
+
this.allowBodyScroll || (document.body.style.overflow = "hidden"), this.isKeyDownEventRegistered || (document.addEventListener("keydown", this.closeModalEsc), this.isKeyDownEventRegistered = true), this.modalContent.focus(), this.preventCloseModal || this.overlayElement.addEventListener("click", this.hideModal), (a2 = (n2 = this.options).onShow) == null || a2.call(n2), (l2 = (i2 = this.options).onToggle) == null || l2.call(i2, { isHidden: false }), this.modalElement.showModal();
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
s(this, "closeModal", () => {
|
|
168
|
+
this.modalElement.setAttribute("aria-hidden", "true"), this.modalElement.setAttribute("data-state", "close"), this.modalElement.blur(), A(this.enableStackedModals || false, this.allowBodyScroll || false, this.modalElement), this.hasDefaultOverlay || C(this.overlayElement), f(this.modalElement, "modal-close", { modalId: this.modalElement.id });
|
|
169
|
+
});
|
|
170
|
+
s(this, "closeLastAction", () => {
|
|
171
|
+
var t2, e2, n2, a2;
|
|
172
|
+
this.isKeyDownEventRegistered && (document.removeEventListener("keydown", this.closeModalEsc), this.isKeyDownEventRegistered = false), this.modalElement.blur(), (e2 = (t2 = this.options).onHide) == null || e2.call(t2), (a2 = (n2 = this.options).onToggle) == null || a2.call(n2, { isHidden: true });
|
|
173
|
+
});
|
|
174
|
+
s(this, "hideModal", () => {
|
|
175
|
+
var a2, i2, l2, d2, h;
|
|
176
|
+
let t2 = false;
|
|
177
|
+
f(this.modalElement, "before-hide", {
|
|
178
|
+
modalId: this.modalId,
|
|
179
|
+
setExitAction: (u) => {
|
|
180
|
+
t2 = u;
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
const e2 = (l2 = (i2 = (a2 = this.options).beforeHide) == null ? void 0 : i2.call(a2)) == null ? void 0 : l2.cancelAction;
|
|
184
|
+
if (t2 || e2)
|
|
185
|
+
return;
|
|
186
|
+
const n2 = ((d2 = this.animateContent) == null ? void 0 : d2.exitAnimation) && this.animateContent.exitAnimation !== "" || this.animationExit && this.animationExit !== "";
|
|
187
|
+
if ((h = this.overlayElement) == null || h.setAttribute("data-state", "close"), this.modalContent.setAttribute("data-state", "close"), n2) {
|
|
188
|
+
const u = this.animationExit ? this.animationExit : this.animateContent && this.animateContent.exitAnimation || "";
|
|
189
|
+
this.modalContent.style.setProperty("--un-modal-animation", u);
|
|
190
|
+
}
|
|
191
|
+
y({
|
|
192
|
+
element: this.modalContent,
|
|
193
|
+
callback: () => {
|
|
194
|
+
n2 && this.modalContent.style.removeProperty("--un-modal-animation"), this.closeModal(), this.closeLastAction(), document.activeElement instanceof HTMLElement && document.activeElement.blur(), this.modalElement.close("modal-closed");
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
s(this, "cleanup", () => {
|
|
199
|
+
for (const t2 of this.triggerButtons)
|
|
200
|
+
t2.removeEventListener("click", this.showModal);
|
|
201
|
+
if (this.closeButtons.length > 0)
|
|
202
|
+
for (const t2 of this.closeButtons)
|
|
203
|
+
t2.removeEventListener("click", this.closeModalOnX);
|
|
204
|
+
!this.preventCloseModal && this.overlayElement instanceof HTMLElement && this.overlayElement.removeEventListener("click", this.hideModal), this.dispatchEventToDocument && document.removeEventListener(`modal:${this.modalId}:open`, this.showModal), this.dispatchEventToDocument && document.removeEventListener(`modal:${this.modalId}:close`, this.hideModal), v.removeInstance("modal", this.modalElement);
|
|
205
|
+
});
|
|
206
|
+
s(this, "setOptions", ({ state: t2, allowBodyscroll: e2 }) => {
|
|
207
|
+
t2 && (this.state = t2), e2 !== void 0 && (this.allowBodyScroll = e2), this.state === "open" ? this.showModal() : this.state === "close" && this.hideModal();
|
|
208
|
+
});
|
|
209
|
+
const a = typeof t == "string" ? r(t) : t;
|
|
210
|
+
if (!(a instanceof HTMLDialogElement))
|
|
211
|
+
throw new Error("Modal element not found or invalid. Please provide a valid HTMLDialogElement or selector.");
|
|
212
|
+
this.modalElement = a, this.options = e, this.state = (e == null ? void 0 : e.defaultState) || this.modalElement.dataset.state || "close";
|
|
213
|
+
const i = v.getInstance("modal", this.modalElement);
|
|
214
|
+
if (i)
|
|
215
|
+
return i;
|
|
194
216
|
this.modalElement.hasAttribute("data-fx-modal") || this.modalElement.setAttribute("data-fx-modal", "");
|
|
195
|
-
const
|
|
196
|
-
|
|
217
|
+
const l = r("[data-modal-content]", a);
|
|
218
|
+
if (!(l instanceof HTMLElement))
|
|
219
|
+
throw new Error("Modal content element not found or invalid. Please provide a valid HTMLElement or selector.");
|
|
220
|
+
this.modalContent = l;
|
|
221
|
+
const d = a.dataset.modalId;
|
|
222
|
+
this.modalId = `${d}`, this.initializeTriggers(n, d), this.dispatchEventToDocument = this.options.dispatchEventToDocument || true, this.initModal(this.modalElement, this.options), this.state === "open" ? (this.initAsOpen = true, this.showModal()) : (this.initAsOpen = false, this.modalElement.blur(), this.modalContent.setAttribute("data-state", "close"), this.modalElement.setAttribute("aria-hidden", "true"), this.modalElement.setAttribute("data-state", "close")), v.register("modal", this.modalElement, this);
|
|
223
|
+
}
|
|
224
|
+
initializeTriggers(t, e) {
|
|
225
|
+
if (!t && e) {
|
|
226
|
+
const a = c(`[data-modal-target='${e}'], [data-modal-trigger][data-modal-id='${e}']`);
|
|
227
|
+
this.triggerButtons = a;
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (!t)
|
|
231
|
+
return;
|
|
232
|
+
const n = Array.isArray(t) ? t : [t];
|
|
233
|
+
this.triggerButtons = n.map((a) => {
|
|
234
|
+
if (typeof a == "string") {
|
|
235
|
+
const i = r(a);
|
|
236
|
+
if (!(i instanceof HTMLElement))
|
|
237
|
+
throw new Error(`Trigger element not found: ${a}`);
|
|
238
|
+
return i;
|
|
239
|
+
}
|
|
240
|
+
if (!(a instanceof HTMLElement))
|
|
241
|
+
throw new Error("Invalid trigger element provided");
|
|
242
|
+
return a;
|
|
243
|
+
});
|
|
197
244
|
}
|
|
198
245
|
};
|
|
199
|
-
|
|
200
|
-
const
|
|
201
|
-
for (const
|
|
202
|
-
new
|
|
246
|
+
s(m, "autoInit", (t = "[data-fx-modal]") => {
|
|
247
|
+
const e = c(t);
|
|
248
|
+
for (const n of e)
|
|
249
|
+
new m(n);
|
|
203
250
|
}), /**
|
|
204
251
|
* Creates and initializes a new Modal instance
|
|
205
|
-
* @param modal - The modal element or selector string
|
|
206
|
-
* @param options - Configuration options for the modal
|
|
207
|
-
* @param triggerElement - Optional trigger element or selector
|
|
208
|
-
* @returns {Modal} A new Modal instance
|
|
209
|
-
*
|
|
210
|
-
* @example
|
|
211
|
-
* ```typescript
|
|
212
|
-
* const modal = Modal.init('#myModal', {
|
|
213
|
-
* defaultState: 'open',
|
|
214
|
-
* allowBodyScroll: true
|
|
215
|
-
* });
|
|
216
|
-
* ```
|
|
217
252
|
*/
|
|
218
|
-
|
|
219
|
-
var
|
|
253
|
+
s(m, "init", (t, e = {}, n) => new m(t, e, n));
|
|
254
|
+
var w = m;
|
|
220
255
|
|
|
221
256
|
// src/index.js
|
|
222
257
|
function Modal(Alpine) {
|
|
223
258
|
Alpine.directive("modal", (el, {}, { cleanup }) => {
|
|
224
259
|
const modalId = el.getAttribute("data-modal-id");
|
|
225
260
|
if (!modalId) {
|
|
226
|
-
console.error(
|
|
261
|
+
console.error(
|
|
262
|
+
"\u274C data-modal-id is required but missing on element:",
|
|
263
|
+
el
|
|
264
|
+
);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if (!(el instanceof HTMLDialogElement)) {
|
|
268
|
+
console.error(
|
|
269
|
+
"\u274C x-modal must be used only on an HTMLDialogElement:",
|
|
270
|
+
el
|
|
271
|
+
);
|
|
227
272
|
return;
|
|
228
273
|
}
|
|
229
|
-
const
|
|
274
|
+
const content = el.querySelector("[data-modal-content]");
|
|
275
|
+
if (!content) {
|
|
276
|
+
console.error(
|
|
277
|
+
"\u274C data-modal-content Element is required but missing in Modal Element:",
|
|
278
|
+
el
|
|
279
|
+
);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const modalInstance = new w(el, {
|
|
283
|
+
dispatchEventToDocument: false
|
|
284
|
+
});
|
|
230
285
|
if (!Alpine.store("modals")) {
|
|
231
286
|
Alpine.store("modals", {});
|
|
232
287
|
}
|
|
233
288
|
Alpine.store("modals")[modalId] = modalInstance;
|
|
234
|
-
const
|
|
235
|
-
const
|
|
236
|
-
document.addEventListener(`modal:${modalId}:open`,
|
|
237
|
-
document.addEventListener(`modal:${modalId}:close`,
|
|
289
|
+
const showModal = () => modalInstance.showModal();
|
|
290
|
+
const hideModal = () => modalInstance.hideModal();
|
|
291
|
+
document.addEventListener(`modal:${modalId}:open`, showModal);
|
|
292
|
+
document.addEventListener(`modal:${modalId}:close`, hideModal);
|
|
238
293
|
cleanup(() => {
|
|
239
|
-
document.removeEventListener(`modal:${modalId}:open`, openHandler);
|
|
240
|
-
document.removeEventListener(`modal:${modalId}:close`, closeHandler);
|
|
241
294
|
modalInstance.cleanup();
|
|
242
295
|
delete Alpine.store("modals")[modalId];
|
|
296
|
+
document.removeEventListener(`modal:${modalId}:open`, showModal);
|
|
297
|
+
document.removeEventListener(`modal:${modalId}:close`, hideModal);
|
|
243
298
|
});
|
|
244
299
|
});
|
|
245
300
|
Alpine.magic("modal", () => (id) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flexilla/alpine-modal",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "AlpineJS plugin for creating Modal, Dialog, alert dialog components",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"Dissmiss"
|
|
65
65
|
],
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@flexilla/modal": "^2.0
|
|
67
|
+
"@flexilla/modal": "^2.2.0"
|
|
68
68
|
},
|
|
69
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "584636c3f3b85dfa71666f5354221dcfc385f9e6"
|
|
70
70
|
}
|
package/src/index.js
CHANGED
|
@@ -5,28 +5,47 @@ function Modal(Alpine) {
|
|
|
5
5
|
const modalId = el.getAttribute("data-modal-id");
|
|
6
6
|
|
|
7
7
|
if (!modalId) {
|
|
8
|
-
console.error(
|
|
8
|
+
console.error(
|
|
9
|
+
"❌ data-modal-id is required but missing on element:",
|
|
10
|
+
el
|
|
11
|
+
);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if(!(el instanceof HTMLDialogElement)) {
|
|
15
|
+
console.error(
|
|
16
|
+
"❌ x-modal must be used only on an HTMLDialogElement:",
|
|
17
|
+
el
|
|
18
|
+
);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const content = el.querySelector("[data-modal-content]");
|
|
22
|
+
if (!content) {
|
|
23
|
+
console.error(
|
|
24
|
+
"❌ data-modal-content Element is required but missing in Modal Element:",
|
|
25
|
+
el
|
|
26
|
+
);
|
|
9
27
|
return;
|
|
10
28
|
}
|
|
11
29
|
|
|
12
|
-
const modalInstance = new FlexillaModal(el
|
|
30
|
+
const modalInstance = new FlexillaModal(el, {
|
|
31
|
+
dispatchEventToDocument: false,
|
|
32
|
+
});
|
|
13
33
|
if (!Alpine.store("modals")) {
|
|
14
34
|
Alpine.store("modals", {});
|
|
15
35
|
}
|
|
16
36
|
|
|
17
37
|
Alpine.store("modals")[modalId] = modalInstance;
|
|
18
38
|
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
document.addEventListener(`modal:${modalId}:
|
|
23
|
-
document.addEventListener(`modal:${modalId}:close`, closeHandler);
|
|
39
|
+
const showModal = () => modalInstance.showModal();
|
|
40
|
+
const hideModal = () => modalInstance.hideModal();
|
|
41
|
+
document.addEventListener(`modal:${modalId}:open`, showModal);
|
|
42
|
+
document.addEventListener(`modal:${modalId}:close`, hideModal);
|
|
24
43
|
|
|
25
44
|
cleanup(() => {
|
|
26
|
-
document.removeEventListener(`modal:${modalId}:open`, openHandler);
|
|
27
|
-
document.removeEventListener(`modal:${modalId}:close`, closeHandler);
|
|
28
45
|
modalInstance.cleanup();
|
|
29
46
|
delete Alpine.store("modals")[modalId];
|
|
47
|
+
document.removeEventListener(`modal:${modalId}:open`, showModal);
|
|
48
|
+
document.removeEventListener(`modal:${modalId}:close`, hideModal);
|
|
30
49
|
});
|
|
31
50
|
});
|
|
32
51
|
|