@abydahana/aksara-ui 0.1.0
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/LICENSE +21 -0
- package/README.md +163 -0
- package/dist/aksara.css +33499 -0
- package/dist/aksara.d.ts +284 -0
- package/dist/aksara.esm.js +1604 -0
- package/dist/aksara.js +1608 -0
- package/dist/aksara.min.css +1 -0
- package/dist/aksara.min.js +1 -0
- package/dist/components/buttons/button-group.d.ts +10 -0
- package/dist/components/buttons/button-group.js +12 -0
- package/dist/components/buttons/button.d.ts +18 -0
- package/dist/components/buttons/button.js +28 -0
- package/dist/components/buttons/close-button.d.ts +9 -0
- package/dist/components/buttons/close-button.js +13 -0
- package/dist/components/buttons/index.d.ts +3 -0
- package/dist/components/buttons/index.js +3 -0
- package/dist/components/data/accordion.d.ts +17 -0
- package/dist/components/data/accordion.js +37 -0
- package/dist/components/data/avatar.d.ts +26 -0
- package/dist/components/data/avatar.js +64 -0
- package/dist/components/data/card.d.ts +39 -0
- package/dist/components/data/card.js +34 -0
- package/dist/components/data/carousel.d.ts +15 -0
- package/dist/components/data/carousel.js +47 -0
- package/dist/components/data/index.d.ts +7 -0
- package/dist/components/data/index.js +7 -0
- package/dist/components/data/list-group.d.ts +22 -0
- package/dist/components/data/list-group.js +30 -0
- package/dist/components/data/media-grid.d.ts +19 -0
- package/dist/components/data/media-grid.js +47 -0
- package/dist/components/data/table.d.ts +35 -0
- package/dist/components/data/table.js +30 -0
- package/dist/components/feedback/alert.d.ts +10 -0
- package/dist/components/feedback/alert.js +19 -0
- package/dist/components/feedback/badge.d.ts +13 -0
- package/dist/components/feedback/badge.js +10 -0
- package/dist/components/feedback/index.d.ts +6 -0
- package/dist/components/feedback/index.js +6 -0
- package/dist/components/feedback/placeholder.d.ts +9 -0
- package/dist/components/feedback/placeholder.js +11 -0
- package/dist/components/feedback/progress.d.ts +14 -0
- package/dist/components/feedback/progress.js +22 -0
- package/dist/components/feedback/spinner.d.ts +9 -0
- package/dist/components/feedback/spinner.js +12 -0
- package/dist/components/feedback/toast.d.ts +22 -0
- package/dist/components/feedback/toast.js +36 -0
- package/dist/components/form/checkbox.d.ts +17 -0
- package/dist/components/form/checkbox.js +29 -0
- package/dist/components/form/floating-label.d.ts +9 -0
- package/dist/components/form/floating-label.js +14 -0
- package/dist/components/form/form-control.d.ts +30 -0
- package/dist/components/form/form-control.js +28 -0
- package/dist/components/form/index.d.ts +11 -0
- package/dist/components/form/index.js +11 -0
- package/dist/components/form/input-group.d.ts +16 -0
- package/dist/components/form/input-group.js +14 -0
- package/dist/components/form/input.d.ts +18 -0
- package/dist/components/form/input.js +19 -0
- package/dist/components/form/layout.d.ts +20 -0
- package/dist/components/form/layout.js +15 -0
- package/dist/components/form/radio.d.ts +32 -0
- package/dist/components/form/radio.js +49 -0
- package/dist/components/form/range.d.ts +13 -0
- package/dist/components/form/range.js +18 -0
- package/dist/components/form/select.d.ts +25 -0
- package/dist/components/form/select.js +37 -0
- package/dist/components/form/switch.d.ts +16 -0
- package/dist/components/form/switch.js +30 -0
- package/dist/components/form/textarea.d.ts +17 -0
- package/dist/components/form/textarea.js +19 -0
- package/dist/components/index.d.ts +7 -0
- package/dist/components/index.js +7 -0
- package/dist/components/navigation/breadcrumb.d.ts +13 -0
- package/dist/components/navigation/breadcrumb.js +25 -0
- package/dist/components/navigation/index.d.ts +5 -0
- package/dist/components/navigation/index.js +5 -0
- package/dist/components/navigation/navbar.d.ts +20 -0
- package/dist/components/navigation/navbar.js +22 -0
- package/dist/components/navigation/pagination.d.ts +15 -0
- package/dist/components/navigation/pagination.js +31 -0
- package/dist/components/navigation/scrollspy.d.ts +23 -0
- package/dist/components/navigation/scrollspy.js +43 -0
- package/dist/components/navigation/tabs.d.ts +15 -0
- package/dist/components/navigation/tabs.js +56 -0
- package/dist/components/overlay/collapse.d.ts +20 -0
- package/dist/components/overlay/collapse.js +27 -0
- package/dist/components/overlay/dropdown.d.ts +33 -0
- package/dist/components/overlay/dropdown.js +41 -0
- package/dist/components/overlay/index.d.ts +7 -0
- package/dist/components/overlay/index.js +7 -0
- package/dist/components/overlay/media-modal.d.ts +15 -0
- package/dist/components/overlay/media-modal.js +83 -0
- package/dist/components/overlay/modal.d.ts +32 -0
- package/dist/components/overlay/modal.js +47 -0
- package/dist/components/overlay/offcanvas.d.ts +20 -0
- package/dist/components/overlay/offcanvas.js +33 -0
- package/dist/components/overlay/popover.d.ts +19 -0
- package/dist/components/overlay/popover.js +24 -0
- package/dist/components/overlay/tooltip.d.ts +17 -0
- package/dist/components/overlay/tooltip.js +21 -0
- package/dist/components/utils.d.ts +3 -0
- package/dist/components/utils.js +25 -0
- package/package.json +111 -0
|
@@ -0,0 +1,1604 @@
|
|
|
1
|
+
/*! Aksara UI v0.1.0 | Static CSS, no JIT, vanilla JS */
|
|
2
|
+
const registry = new Map();
|
|
3
|
+
const instances = new Set();
|
|
4
|
+
const focusableSelector = [
|
|
5
|
+
"a[href]",
|
|
6
|
+
"area[href]",
|
|
7
|
+
"button:not([disabled])",
|
|
8
|
+
"input:not([disabled])",
|
|
9
|
+
"select:not([disabled])",
|
|
10
|
+
"textarea:not([disabled])",
|
|
11
|
+
"iframe",
|
|
12
|
+
"object",
|
|
13
|
+
"embed",
|
|
14
|
+
"[contenteditable]",
|
|
15
|
+
"[tabindex]:not([tabindex='-1'])"
|
|
16
|
+
].join(",");
|
|
17
|
+
function toElements(target) {
|
|
18
|
+
if (!target)
|
|
19
|
+
return [];
|
|
20
|
+
if (typeof target === "string")
|
|
21
|
+
return Array.from(document.querySelectorAll(target));
|
|
22
|
+
if (target instanceof HTMLElement)
|
|
23
|
+
return [target];
|
|
24
|
+
if (target instanceof Element)
|
|
25
|
+
return [target];
|
|
26
|
+
if (target instanceof NodeList || Array.isArray(target)) {
|
|
27
|
+
return Array.from(target).filter((item) => item instanceof HTMLElement);
|
|
28
|
+
}
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
function resolveArgs(target, options, defaultSelector) {
|
|
32
|
+
if (target &&
|
|
33
|
+
typeof target === "object" &&
|
|
34
|
+
!(target instanceof Element) &&
|
|
35
|
+
!(target instanceof NodeList) &&
|
|
36
|
+
!Array.isArray(target)) {
|
|
37
|
+
const opts = target;
|
|
38
|
+
const resolvedTarget = opts.target || opts.selector || defaultSelector;
|
|
39
|
+
return { target: resolvedTarget, options: opts };
|
|
40
|
+
}
|
|
41
|
+
return { target: target || defaultSelector, options: (options || {}) };
|
|
42
|
+
}
|
|
43
|
+
function dataName(name) {
|
|
44
|
+
return name.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
|
|
45
|
+
}
|
|
46
|
+
function getData(element, component, name) {
|
|
47
|
+
return element?.getAttribute(`data-${component}-${dataName(name)}`) ?? null;
|
|
48
|
+
}
|
|
49
|
+
function getOption(options, element, component, name, fallback) {
|
|
50
|
+
if (options[name] !== undefined)
|
|
51
|
+
return options[name];
|
|
52
|
+
const value = getData(element, component, name);
|
|
53
|
+
return value !== null ? value : fallback;
|
|
54
|
+
}
|
|
55
|
+
function getBooleanOption(options, element, component, name, fallback) {
|
|
56
|
+
const value = getOption(options, element, component, name, fallback);
|
|
57
|
+
if (value === "true" || value === true)
|
|
58
|
+
return true;
|
|
59
|
+
if (value === "false" || value === false)
|
|
60
|
+
return false;
|
|
61
|
+
return Boolean(value);
|
|
62
|
+
}
|
|
63
|
+
function getNumberOption(options, element, component, name, fallback) {
|
|
64
|
+
const value = getOption(options, element, component, name, fallback);
|
|
65
|
+
const number = Number(value);
|
|
66
|
+
return Number.isNaN(number) ? fallback : number;
|
|
67
|
+
}
|
|
68
|
+
function clamp(value, min, max) {
|
|
69
|
+
return Math.min(Math.max(value, min), max);
|
|
70
|
+
}
|
|
71
|
+
function emit(element, name, detail = {}) {
|
|
72
|
+
element.dispatchEvent(new CustomEvent(`aksara:${name}`, { bubbles: true, detail }));
|
|
73
|
+
}
|
|
74
|
+
function getInstance(element, Type, options = {}) {
|
|
75
|
+
const key = Type.componentName;
|
|
76
|
+
let map = registry.get(key);
|
|
77
|
+
if (!map) {
|
|
78
|
+
map = new WeakMap();
|
|
79
|
+
registry.set(key, map);
|
|
80
|
+
}
|
|
81
|
+
if (!map.has(element)) {
|
|
82
|
+
const instance = new Type(element, options);
|
|
83
|
+
map.set(element, instance);
|
|
84
|
+
instances.add(instance);
|
|
85
|
+
}
|
|
86
|
+
else if (options && Object.keys(options).length) {
|
|
87
|
+
const instance = map.get(element);
|
|
88
|
+
if (instance && typeof instance.updateOptions === "function") {
|
|
89
|
+
instance.updateOptions(options);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return map.get(element);
|
|
93
|
+
}
|
|
94
|
+
function createInstanceList(instanceArray) {
|
|
95
|
+
return new Proxy(instanceArray, {
|
|
96
|
+
get(target, prop) {
|
|
97
|
+
if (prop in target) {
|
|
98
|
+
const val = target[prop];
|
|
99
|
+
return typeof val === "function" ? val.bind(target) : val;
|
|
100
|
+
}
|
|
101
|
+
return (...args) => {
|
|
102
|
+
target.forEach((inst) => {
|
|
103
|
+
const fn = inst[prop];
|
|
104
|
+
if (typeof fn === "function") {
|
|
105
|
+
fn(...args);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return createInstanceList(target);
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
function createAll(target, Type, options = {}) {
|
|
114
|
+
const items = toElements(target);
|
|
115
|
+
const insts = items.map((element) => getInstance(element, Type, options));
|
|
116
|
+
if (insts.length === 1)
|
|
117
|
+
return insts[0];
|
|
118
|
+
return createInstanceList(insts);
|
|
119
|
+
}
|
|
120
|
+
export class Component {
|
|
121
|
+
element;
|
|
122
|
+
options;
|
|
123
|
+
cleanups;
|
|
124
|
+
constructor(element, options = {}) {
|
|
125
|
+
this.element = element;
|
|
126
|
+
this.options = options;
|
|
127
|
+
this.cleanups = [];
|
|
128
|
+
}
|
|
129
|
+
updateOptions(options = {}) {
|
|
130
|
+
this.options = { ...this.options, ...options };
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
on(target, type, handler, options) {
|
|
134
|
+
target.addEventListener(type, handler, options);
|
|
135
|
+
this.cleanups.push(() => target.removeEventListener(type, handler, options));
|
|
136
|
+
}
|
|
137
|
+
destroy() {
|
|
138
|
+
this.cleanups.splice(0).forEach((cleanup) => cleanup());
|
|
139
|
+
instances.delete(this);
|
|
140
|
+
const ctor = this.constructor;
|
|
141
|
+
emit(this.element, `${ctor.componentName || "component"}:destroy`, { instance: this });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
export class Modal extends Component {
|
|
145
|
+
static componentName = "modal";
|
|
146
|
+
isOpen;
|
|
147
|
+
previousFocus = null;
|
|
148
|
+
dialog;
|
|
149
|
+
dragState = null;
|
|
150
|
+
backdrop;
|
|
151
|
+
keyboard;
|
|
152
|
+
focus;
|
|
153
|
+
scrollLock;
|
|
154
|
+
draggable;
|
|
155
|
+
constructor(element, options = {}) {
|
|
156
|
+
super(element, options);
|
|
157
|
+
this.isOpen = element.classList.contains("modal-open");
|
|
158
|
+
this.dialog = element.querySelector(".modal-dialog");
|
|
159
|
+
this.updateOptions(options);
|
|
160
|
+
this.element.setAttribute("role", this.element.getAttribute("role") || "dialog");
|
|
161
|
+
this.element.setAttribute("aria-modal", "true");
|
|
162
|
+
this.element.setAttribute("aria-hidden", this.isOpen ? "false" : "true");
|
|
163
|
+
this.on(this.element, "click", (event) => {
|
|
164
|
+
if (event.target === this.element) {
|
|
165
|
+
if (this.backdrop === "static") {
|
|
166
|
+
const staticClass = `${this.constructor.componentName}-static`;
|
|
167
|
+
this.element.classList.add(staticClass);
|
|
168
|
+
setTimeout(() => this.element.classList.remove(staticClass), 150);
|
|
169
|
+
}
|
|
170
|
+
else if (this.backdrop) {
|
|
171
|
+
this.hide();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
this.on(document, "keydown", (event) => this.onKeydown(event));
|
|
176
|
+
this.bindDrag();
|
|
177
|
+
}
|
|
178
|
+
updateOptions(options = {}) {
|
|
179
|
+
super.updateOptions(options);
|
|
180
|
+
const component = this.constructor.componentName;
|
|
181
|
+
const backdrop = getOption(this.options, this.element, component, "backdrop", true);
|
|
182
|
+
this.backdrop = backdrop === "static" ? "static" : !(backdrop === false || backdrop === "false");
|
|
183
|
+
this.keyboard = getBooleanOption(this.options, this.element, component, "keyboard", true);
|
|
184
|
+
this.focus = getBooleanOption(this.options, this.element, component, "focus", true);
|
|
185
|
+
this.scrollLock = getBooleanOption(this.options, this.element, component, "scrollLock", true);
|
|
186
|
+
this.draggable = getBooleanOption(this.options, this.element, component, "draggable", true);
|
|
187
|
+
return this;
|
|
188
|
+
}
|
|
189
|
+
bindDrag() {
|
|
190
|
+
if (!this.dialog)
|
|
191
|
+
return;
|
|
192
|
+
const header = this.dialog.querySelector(".modal-header");
|
|
193
|
+
if (!header)
|
|
194
|
+
return;
|
|
195
|
+
this.on(header, "pointerdown", (event) => this.startDrag(event));
|
|
196
|
+
}
|
|
197
|
+
startDrag(event) {
|
|
198
|
+
if (!this.isOpen || !this.draggable || !this.dialog || event.button !== 0)
|
|
199
|
+
return;
|
|
200
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
201
|
+
if (target?.closest("button,a,input,select,textarea,label,[data-modal-close],[data-no-drag]"))
|
|
202
|
+
return;
|
|
203
|
+
const originX = Number.parseFloat(this.dialog.style.getPropertyValue("--aksara-modal-x")) || 0;
|
|
204
|
+
const originY = Number.parseFloat(this.dialog.style.getPropertyValue("--aksara-modal-y")) || 0;
|
|
205
|
+
const rect = this.dialog.getBoundingClientRect();
|
|
206
|
+
const baseLeft = rect.left - originX;
|
|
207
|
+
const baseTop = rect.top - originY;
|
|
208
|
+
const padding = 8;
|
|
209
|
+
const move = (moveEvent) => this.drag(moveEvent);
|
|
210
|
+
const end = () => {
|
|
211
|
+
document.removeEventListener("pointermove", move);
|
|
212
|
+
document.removeEventListener("pointerup", end);
|
|
213
|
+
document.removeEventListener("pointercancel", end);
|
|
214
|
+
this.dialog?.classList.remove("is-dragging");
|
|
215
|
+
this.dragState = null;
|
|
216
|
+
};
|
|
217
|
+
this.dragState = {
|
|
218
|
+
originX,
|
|
219
|
+
originY,
|
|
220
|
+
pointerX: event.clientX,
|
|
221
|
+
pointerY: event.clientY,
|
|
222
|
+
minX: padding - baseLeft,
|
|
223
|
+
maxX: window.innerWidth - padding - rect.width - baseLeft,
|
|
224
|
+
minY: padding - baseTop,
|
|
225
|
+
maxY: window.innerHeight - padding - rect.height - baseTop
|
|
226
|
+
};
|
|
227
|
+
this.dialog.classList.add("is-dragging");
|
|
228
|
+
document.addEventListener("pointermove", move);
|
|
229
|
+
document.addEventListener("pointerup", end);
|
|
230
|
+
document.addEventListener("pointercancel", end);
|
|
231
|
+
event.preventDefault();
|
|
232
|
+
}
|
|
233
|
+
drag(event) {
|
|
234
|
+
if (!this.dialog || !this.dragState)
|
|
235
|
+
return;
|
|
236
|
+
const nextX = clamp(this.dragState.originX + event.clientX - this.dragState.pointerX, this.dragState.minX, this.dragState.maxX);
|
|
237
|
+
const nextY = clamp(this.dragState.originY + event.clientY - this.dragState.pointerY, this.dragState.minY, this.dragState.maxY);
|
|
238
|
+
this.dialog.style.setProperty("--aksara-modal-x", `${nextX}px`);
|
|
239
|
+
this.dialog.style.setProperty("--aksara-modal-y", `${nextY}px`);
|
|
240
|
+
}
|
|
241
|
+
resetPosition() {
|
|
242
|
+
if (!this.dialog)
|
|
243
|
+
return;
|
|
244
|
+
this.dialog.style.removeProperty("--aksara-modal-x");
|
|
245
|
+
this.dialog.style.removeProperty("--aksara-modal-y");
|
|
246
|
+
this.dialog.classList.remove("is-dragging");
|
|
247
|
+
}
|
|
248
|
+
show() {
|
|
249
|
+
if (this.isOpen)
|
|
250
|
+
return this;
|
|
251
|
+
this.previousFocus = document.activeElement;
|
|
252
|
+
this.isOpen = true;
|
|
253
|
+
this.element.classList.add("modal-open");
|
|
254
|
+
this.element.setAttribute("aria-hidden", "false");
|
|
255
|
+
if (this.scrollLock)
|
|
256
|
+
document.documentElement.style.overflow = "hidden";
|
|
257
|
+
if (this.focus)
|
|
258
|
+
this.focusFirst();
|
|
259
|
+
emit(this.element, "modal:show", { instance: this });
|
|
260
|
+
return this;
|
|
261
|
+
}
|
|
262
|
+
hide() {
|
|
263
|
+
if (!this.isOpen)
|
|
264
|
+
return this;
|
|
265
|
+
this.isOpen = false;
|
|
266
|
+
this.element.classList.remove("modal-open");
|
|
267
|
+
this.element.setAttribute("aria-hidden", "true");
|
|
268
|
+
this.resetPosition();
|
|
269
|
+
if (this.scrollLock)
|
|
270
|
+
document.documentElement.style.overflow = "";
|
|
271
|
+
if (this.previousFocus && typeof this.previousFocus.focus === "function")
|
|
272
|
+
this.previousFocus.focus();
|
|
273
|
+
emit(this.element, "modal:hide", { instance: this });
|
|
274
|
+
return this;
|
|
275
|
+
}
|
|
276
|
+
toggle() {
|
|
277
|
+
return this.isOpen ? this.hide() : this.show();
|
|
278
|
+
}
|
|
279
|
+
focusFirst() {
|
|
280
|
+
const focusable = Array.from(this.element.querySelectorAll(focusableSelector));
|
|
281
|
+
const first = focusable[0] || this.element;
|
|
282
|
+
if (!this.element.hasAttribute("tabindex"))
|
|
283
|
+
this.element.setAttribute("tabindex", "-1");
|
|
284
|
+
first.focus({ preventScroll: true });
|
|
285
|
+
}
|
|
286
|
+
onKeydown(event) {
|
|
287
|
+
if (!this.isOpen)
|
|
288
|
+
return;
|
|
289
|
+
if (event.key === "Escape") {
|
|
290
|
+
if (this.keyboard) {
|
|
291
|
+
event.preventDefault();
|
|
292
|
+
this.hide();
|
|
293
|
+
}
|
|
294
|
+
else if (this.backdrop === "static") {
|
|
295
|
+
const staticClass = `${this.constructor.componentName}-static`;
|
|
296
|
+
this.element.classList.add(staticClass);
|
|
297
|
+
setTimeout(() => this.element.classList.remove(staticClass), 150);
|
|
298
|
+
}
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if (event.key !== "Tab")
|
|
302
|
+
return;
|
|
303
|
+
const focusable = Array.from(this.element.querySelectorAll(focusableSelector));
|
|
304
|
+
if (!focusable.length) {
|
|
305
|
+
event.preventDefault();
|
|
306
|
+
this.element.focus();
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const first = focusable[0];
|
|
310
|
+
const last = focusable[focusable.length - 1];
|
|
311
|
+
if (event.shiftKey && document.activeElement === first) {
|
|
312
|
+
event.preventDefault();
|
|
313
|
+
last.focus();
|
|
314
|
+
}
|
|
315
|
+
else if (!event.shiftKey && document.activeElement === last) {
|
|
316
|
+
event.preventDefault();
|
|
317
|
+
first.focus();
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
export class Dropdown extends Component {
|
|
322
|
+
static componentName = "dropdown";
|
|
323
|
+
trigger;
|
|
324
|
+
menu;
|
|
325
|
+
isOpen;
|
|
326
|
+
originalParent;
|
|
327
|
+
originalSibling;
|
|
328
|
+
scrollHandler;
|
|
329
|
+
placement;
|
|
330
|
+
container;
|
|
331
|
+
offset;
|
|
332
|
+
keyboard;
|
|
333
|
+
closeOnSelect;
|
|
334
|
+
closeOnOutside;
|
|
335
|
+
constructor(element, options = {}) {
|
|
336
|
+
super(element, options);
|
|
337
|
+
let trigger;
|
|
338
|
+
let menu = null;
|
|
339
|
+
if (element.matches("[data-dropdown]")) {
|
|
340
|
+
trigger = element;
|
|
341
|
+
}
|
|
342
|
+
else if (element.classList.contains("dropdown")) {
|
|
343
|
+
trigger =
|
|
344
|
+
element.querySelector("[data-dropdown]") || element.querySelector("button, a");
|
|
345
|
+
menu = element.querySelector(".dropdown-menu");
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
trigger = element;
|
|
349
|
+
}
|
|
350
|
+
this.trigger = trigger;
|
|
351
|
+
if (!menu) {
|
|
352
|
+
const menuSelector = options.menu ||
|
|
353
|
+
options.target ||
|
|
354
|
+
(trigger && (trigger.getAttribute("data-dropdown") || trigger.getAttribute("data-target")));
|
|
355
|
+
if (menuSelector && typeof menuSelector === "string") {
|
|
356
|
+
menu = document.querySelector(menuSelector);
|
|
357
|
+
}
|
|
358
|
+
else if (menuSelector instanceof HTMLElement) {
|
|
359
|
+
menu = menuSelector;
|
|
360
|
+
}
|
|
361
|
+
if (!menu && trigger) {
|
|
362
|
+
if (trigger.nextElementSibling && trigger.nextElementSibling.classList.contains("dropdown-menu")) {
|
|
363
|
+
menu = trigger.nextElementSibling;
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
menu = trigger.parentElement?.querySelector(".dropdown-menu") || null;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
this.menu = menu;
|
|
371
|
+
this.isOpen = false;
|
|
372
|
+
this.originalParent = null;
|
|
373
|
+
this.originalSibling = null;
|
|
374
|
+
this.scrollHandler = null;
|
|
375
|
+
this.updateOptions(options);
|
|
376
|
+
if (this.trigger?.matches(".btn-sm, .input-sm, .form-control-sm, .form-select-sm") ||
|
|
377
|
+
this.element.classList.contains("dropdown-sm")) {
|
|
378
|
+
this.menu?.classList.add("dropdown-menu-sm");
|
|
379
|
+
}
|
|
380
|
+
if (this.trigger) {
|
|
381
|
+
this.trigger.setAttribute("aria-haspopup", "true");
|
|
382
|
+
this.trigger.setAttribute("aria-expanded", "false");
|
|
383
|
+
this.on(this.trigger, "click", (event) => {
|
|
384
|
+
const customEvent = event;
|
|
385
|
+
if (customEvent.__aksaraDropdownHandled)
|
|
386
|
+
return;
|
|
387
|
+
customEvent.__aksaraDropdownHandled = true;
|
|
388
|
+
event.preventDefault();
|
|
389
|
+
this.toggle(event);
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
this.on(document, "click", (event) => {
|
|
393
|
+
if (!this.closeOnOutside)
|
|
394
|
+
return;
|
|
395
|
+
const target = event.target;
|
|
396
|
+
if (!this.element.contains(target) && !this.menu?.contains(target)) {
|
|
397
|
+
this.hide();
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
this.on(document, "keydown", (event) => {
|
|
401
|
+
if (this.keyboard && event.key === "Escape")
|
|
402
|
+
this.hide();
|
|
403
|
+
});
|
|
404
|
+
if (this.menu) {
|
|
405
|
+
this.on(this.menu, "click", (event) => {
|
|
406
|
+
const target = event.target;
|
|
407
|
+
const item = target.closest(".dropdown-item");
|
|
408
|
+
if (this.closeOnSelect &&
|
|
409
|
+
item &&
|
|
410
|
+
!item.disabled &&
|
|
411
|
+
!item.classList.contains("disabled")) {
|
|
412
|
+
this.hide();
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
updateOptions(options = {}) {
|
|
418
|
+
super.updateOptions(options);
|
|
419
|
+
this.placement = getOption(this.options, this.trigger, "dropdown", "placement", getData(this.element, "dropdown", "placement") || "bottom");
|
|
420
|
+
this.container = getOption(this.options, this.trigger, "dropdown", "container", getData(this.element, "dropdown", "container") || "body");
|
|
421
|
+
this.offset = getNumberOption(this.options, this.trigger, "dropdown", "offset", getNumberOption({}, this.element, "dropdown", "offset", 4));
|
|
422
|
+
this.keyboard = getBooleanOption(this.options, this.trigger, "dropdown", "keyboard", getBooleanOption({}, this.element, "dropdown", "keyboard", true));
|
|
423
|
+
this.closeOnSelect = getBooleanOption(this.options, this.trigger, "dropdown", "closeOnSelect", getBooleanOption({}, this.element, "dropdown", "closeOnSelect", true));
|
|
424
|
+
this.closeOnOutside = getBooleanOption(this.options, this.trigger, "dropdown", "closeOnOutside", getBooleanOption({}, this.element, "dropdown", "closeOnOutside", true));
|
|
425
|
+
return this;
|
|
426
|
+
}
|
|
427
|
+
position() {
|
|
428
|
+
if (!this.menu || !this.isOpen || !this.trigger)
|
|
429
|
+
return;
|
|
430
|
+
this.menu.style.display = "block";
|
|
431
|
+
this.menu.style.position = "absolute";
|
|
432
|
+
this.menu.style.zIndex = "1000";
|
|
433
|
+
const triggerRect = this.trigger.getBoundingClientRect();
|
|
434
|
+
const menuRect = this.menu.getBoundingClientRect();
|
|
435
|
+
const gap = this.offset;
|
|
436
|
+
let top;
|
|
437
|
+
let left;
|
|
438
|
+
let placement = this.placement;
|
|
439
|
+
if (placement === "auto") {
|
|
440
|
+
const spaceBelow = window.innerHeight - triggerRect.bottom;
|
|
441
|
+
const spaceAbove = triggerRect.top;
|
|
442
|
+
placement = spaceBelow >= menuRect.height || spaceBelow >= spaceAbove ? "bottom" : "top";
|
|
443
|
+
}
|
|
444
|
+
this.menu.classList.remove("dropdown-top", "dropdown-bottom", "dropdown-start", "dropdown-end");
|
|
445
|
+
this.menu.classList.add(`dropdown-${placement}`);
|
|
446
|
+
const isRtl = document.documentElement.getAttribute("dir") === "rtl" || document.body?.getAttribute("dir") === "rtl";
|
|
447
|
+
if (placement === "top") {
|
|
448
|
+
top = triggerRect.top - menuRect.height - gap;
|
|
449
|
+
left = isRtl ? triggerRect.right - menuRect.width : triggerRect.left;
|
|
450
|
+
}
|
|
451
|
+
else if (placement === "start") {
|
|
452
|
+
top = triggerRect.top;
|
|
453
|
+
left = isRtl ? triggerRect.right + gap : triggerRect.left - menuRect.width - gap;
|
|
454
|
+
}
|
|
455
|
+
else if (placement === "end") {
|
|
456
|
+
top = triggerRect.top;
|
|
457
|
+
left = isRtl ? triggerRect.left - menuRect.width - gap : triggerRect.right + gap;
|
|
458
|
+
}
|
|
459
|
+
else {
|
|
460
|
+
top = triggerRect.bottom + gap;
|
|
461
|
+
left = isRtl ? triggerRect.right - menuRect.width : triggerRect.left;
|
|
462
|
+
}
|
|
463
|
+
const viewportWidth = window.innerWidth;
|
|
464
|
+
left = Math.max(8, Math.min(left, viewportWidth - menuRect.width - 8));
|
|
465
|
+
this.menu.style.top = `${top + window.scrollY}px`;
|
|
466
|
+
this.menu.style.left = `${left + window.scrollX}px`;
|
|
467
|
+
this.menu.style.bottom = "auto";
|
|
468
|
+
this.menu.style.right = "auto";
|
|
469
|
+
}
|
|
470
|
+
show() {
|
|
471
|
+
if (!this.menu || this.isOpen)
|
|
472
|
+
return this;
|
|
473
|
+
this.isOpen = true;
|
|
474
|
+
this.originalParent = this.menu.parentNode;
|
|
475
|
+
this.originalSibling = this.menu.nextSibling;
|
|
476
|
+
let containerEl = document.body;
|
|
477
|
+
if (typeof this.container === "string" && this.container !== "body") {
|
|
478
|
+
containerEl = document.querySelector(this.container) || document.body;
|
|
479
|
+
}
|
|
480
|
+
else if (this.container instanceof HTMLElement) {
|
|
481
|
+
containerEl = this.container;
|
|
482
|
+
}
|
|
483
|
+
containerEl.appendChild(this.menu);
|
|
484
|
+
this.menu.classList.add("dropdown-open");
|
|
485
|
+
this.trigger?.setAttribute("aria-expanded", "true");
|
|
486
|
+
this.position();
|
|
487
|
+
this.scrollHandler = () => this.position();
|
|
488
|
+
window.addEventListener("scroll", this.scrollHandler, { passive: true });
|
|
489
|
+
window.addEventListener("resize", this.scrollHandler);
|
|
490
|
+
emit(this.element, "dropdown:show", { instance: this });
|
|
491
|
+
return this;
|
|
492
|
+
}
|
|
493
|
+
hide() {
|
|
494
|
+
if (!this.menu || !this.isOpen)
|
|
495
|
+
return this;
|
|
496
|
+
this.isOpen = false;
|
|
497
|
+
if (this.scrollHandler) {
|
|
498
|
+
window.removeEventListener("scroll", this.scrollHandler);
|
|
499
|
+
window.removeEventListener("resize", this.scrollHandler);
|
|
500
|
+
this.scrollHandler = null;
|
|
501
|
+
}
|
|
502
|
+
this.menu.classList.remove("dropdown-open", "dropdown-top", "dropdown-bottom", "dropdown-start", "dropdown-end");
|
|
503
|
+
this.trigger?.setAttribute("aria-expanded", "false");
|
|
504
|
+
if (this.originalParent) {
|
|
505
|
+
this.originalParent.insertBefore(this.menu, this.originalSibling);
|
|
506
|
+
}
|
|
507
|
+
this.menu.style.display = "";
|
|
508
|
+
this.menu.style.position = "";
|
|
509
|
+
this.menu.style.top = "";
|
|
510
|
+
this.menu.style.left = "";
|
|
511
|
+
this.menu.style.zIndex = "";
|
|
512
|
+
emit(this.element, "dropdown:hide", { instance: this });
|
|
513
|
+
return this;
|
|
514
|
+
}
|
|
515
|
+
toggle(event) {
|
|
516
|
+
const clickEvent = (event || window.event);
|
|
517
|
+
if (clickEvent && clickEvent.type === "click") {
|
|
518
|
+
if (clickEvent.__aksaraDropdownHandled && !event) {
|
|
519
|
+
return this;
|
|
520
|
+
}
|
|
521
|
+
clickEvent.__aksaraDropdownHandled = true;
|
|
522
|
+
}
|
|
523
|
+
return this.isOpen ? this.hide() : this.show();
|
|
524
|
+
}
|
|
525
|
+
destroy() {
|
|
526
|
+
this.hide();
|
|
527
|
+
super.destroy();
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
export class FloatingText extends Component {
|
|
531
|
+
static dataAttribute = "data-floating";
|
|
532
|
+
static role = "tooltip";
|
|
533
|
+
static componentName = "floating";
|
|
534
|
+
tip;
|
|
535
|
+
showTimeout;
|
|
536
|
+
hideTimeout;
|
|
537
|
+
container;
|
|
538
|
+
html;
|
|
539
|
+
content;
|
|
540
|
+
placement;
|
|
541
|
+
trigger;
|
|
542
|
+
offset;
|
|
543
|
+
showDelay;
|
|
544
|
+
hideDelay;
|
|
545
|
+
keyboard;
|
|
546
|
+
constructor(element, options = {}) {
|
|
547
|
+
super(element, options);
|
|
548
|
+
this.tip = null;
|
|
549
|
+
this.showTimeout = null;
|
|
550
|
+
this.hideTimeout = null;
|
|
551
|
+
this.updateOptions(options);
|
|
552
|
+
this.bindTriggers();
|
|
553
|
+
this.on(document, "keydown", (event) => {
|
|
554
|
+
if (this.keyboard && event.key === "Escape")
|
|
555
|
+
this.hide();
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
updateOptions(options = {}) {
|
|
559
|
+
super.updateOptions(options);
|
|
560
|
+
const ctor = this.constructor;
|
|
561
|
+
const component = ctor.componentName;
|
|
562
|
+
const rawContent = (this.options.content ??
|
|
563
|
+
this.options.text ??
|
|
564
|
+
getData(this.element, component, "content") ??
|
|
565
|
+
this.element.getAttribute(ctor.dataAttribute) ??
|
|
566
|
+
"");
|
|
567
|
+
this.container = getOption(this.options, this.element, component, "container", this.element.getAttribute("data-container") || "body");
|
|
568
|
+
this.html = getBooleanOption(this.options, this.element, component, "html", this.element.getAttribute("data-html") === "true");
|
|
569
|
+
this.content = rawContent;
|
|
570
|
+
this.placement = getOption(this.options, this.element, component, "placement", this.element.getAttribute("data-placement") || "top");
|
|
571
|
+
this.trigger = getOption(this.options, this.element, component, "trigger", "hover focus");
|
|
572
|
+
this.offset = getNumberOption(this.options, this.element, component, "offset", 8);
|
|
573
|
+
this.showDelay = getNumberOption(this.options, this.element, component, "showDelay", 0);
|
|
574
|
+
this.hideDelay = getNumberOption(this.options, this.element, component, "hideDelay", 0);
|
|
575
|
+
this.keyboard = getBooleanOption(this.options, this.element, component, "keyboard", true);
|
|
576
|
+
if (this.tip) {
|
|
577
|
+
if (this.html) {
|
|
578
|
+
this.tip.innerHTML = this.content;
|
|
579
|
+
}
|
|
580
|
+
else {
|
|
581
|
+
this.tip.textContent = this.content;
|
|
582
|
+
}
|
|
583
|
+
this.position();
|
|
584
|
+
}
|
|
585
|
+
return this;
|
|
586
|
+
}
|
|
587
|
+
bindTriggers() {
|
|
588
|
+
const triggers = String(this.trigger).split(/\s+/).filter(Boolean);
|
|
589
|
+
if (triggers.includes("hover")) {
|
|
590
|
+
this.on(this.element, "mouseenter", () => this.scheduleShow());
|
|
591
|
+
this.on(this.element, "mouseleave", () => this.scheduleHide());
|
|
592
|
+
}
|
|
593
|
+
if (triggers.includes("focus")) {
|
|
594
|
+
this.on(this.element, "focus", () => this.scheduleShow());
|
|
595
|
+
this.on(this.element, "blur", () => this.scheduleHide());
|
|
596
|
+
}
|
|
597
|
+
if (triggers.includes("click")) {
|
|
598
|
+
this.on(this.element, "click", (event) => {
|
|
599
|
+
event.preventDefault();
|
|
600
|
+
this.toggle();
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
scheduleShow() {
|
|
605
|
+
if (this.hideTimeout)
|
|
606
|
+
clearTimeout(this.hideTimeout);
|
|
607
|
+
if (this.showTimeout)
|
|
608
|
+
clearTimeout(this.showTimeout);
|
|
609
|
+
this.showTimeout = setTimeout(() => this.show(), this.showDelay);
|
|
610
|
+
}
|
|
611
|
+
scheduleHide() {
|
|
612
|
+
if (this.showTimeout)
|
|
613
|
+
clearTimeout(this.showTimeout);
|
|
614
|
+
if (this.hideTimeout)
|
|
615
|
+
clearTimeout(this.hideTimeout);
|
|
616
|
+
this.hideTimeout = setTimeout(() => this.hide(), this.hideDelay);
|
|
617
|
+
}
|
|
618
|
+
show() {
|
|
619
|
+
if (this.tip || !this.content)
|
|
620
|
+
return this;
|
|
621
|
+
const ctor = this.constructor;
|
|
622
|
+
this.tip = document.createElement("div");
|
|
623
|
+
this.tip.className = ctor.componentName;
|
|
624
|
+
if (this.html) {
|
|
625
|
+
this.tip.innerHTML = this.content;
|
|
626
|
+
}
|
|
627
|
+
else {
|
|
628
|
+
this.tip.textContent = this.content;
|
|
629
|
+
}
|
|
630
|
+
this.tip.setAttribute("role", ctor.role);
|
|
631
|
+
let containerEl = document.body;
|
|
632
|
+
if (typeof this.container === "string" && this.container !== "body") {
|
|
633
|
+
containerEl = document.querySelector(this.container) || document.body;
|
|
634
|
+
}
|
|
635
|
+
else if (this.container instanceof HTMLElement) {
|
|
636
|
+
containerEl = this.container;
|
|
637
|
+
}
|
|
638
|
+
containerEl.append(this.tip);
|
|
639
|
+
this.position();
|
|
640
|
+
emit(this.element, `${ctor.componentName}:show`, { instance: this });
|
|
641
|
+
return this;
|
|
642
|
+
}
|
|
643
|
+
toggle() {
|
|
644
|
+
return this.tip ? this.hide() : this.show();
|
|
645
|
+
}
|
|
646
|
+
hide() {
|
|
647
|
+
if (!this.tip)
|
|
648
|
+
return this;
|
|
649
|
+
const ctor = this.constructor;
|
|
650
|
+
this.tip.remove();
|
|
651
|
+
this.tip = null;
|
|
652
|
+
emit(this.element, `${ctor.componentName}:hide`, { instance: this });
|
|
653
|
+
return this;
|
|
654
|
+
}
|
|
655
|
+
position() {
|
|
656
|
+
if (!this.tip)
|
|
657
|
+
return;
|
|
658
|
+
const rect = this.element.getBoundingClientRect();
|
|
659
|
+
const tipRect = this.tip.getBoundingClientRect();
|
|
660
|
+
const gap = this.offset;
|
|
661
|
+
const isRtl = document.documentElement.getAttribute("dir") === "rtl" || document.body?.getAttribute("dir") === "rtl";
|
|
662
|
+
let placement = this.placement;
|
|
663
|
+
if (placement === "auto") {
|
|
664
|
+
const spaceBelow = window.innerHeight - rect.bottom;
|
|
665
|
+
const spaceAbove = rect.top;
|
|
666
|
+
placement = spaceAbove >= tipRect.height || spaceAbove >= spaceBelow ? "top" : "bottom";
|
|
667
|
+
}
|
|
668
|
+
let physicalPlacement = placement;
|
|
669
|
+
if (placement === "start") {
|
|
670
|
+
physicalPlacement = isRtl ? "right" : "left";
|
|
671
|
+
}
|
|
672
|
+
else if (placement === "end") {
|
|
673
|
+
physicalPlacement = isRtl ? "left" : "right";
|
|
674
|
+
}
|
|
675
|
+
const ctor = this.constructor;
|
|
676
|
+
this.tip.className = `${ctor.componentName} ${ctor.componentName}-${placement}`;
|
|
677
|
+
const topMap = {
|
|
678
|
+
top: rect.top - tipRect.height - gap,
|
|
679
|
+
bottom: rect.bottom + gap,
|
|
680
|
+
left: rect.top + rect.height / 2 - tipRect.height / 2,
|
|
681
|
+
right: rect.top + rect.height / 2 - tipRect.height / 2
|
|
682
|
+
};
|
|
683
|
+
const top = topMap[physicalPlacement] ?? rect.top - tipRect.height - gap;
|
|
684
|
+
const leftMap = {
|
|
685
|
+
top: rect.left + rect.width / 2 - tipRect.width / 2,
|
|
686
|
+
bottom: rect.left + rect.width / 2 - tipRect.width / 2,
|
|
687
|
+
left: rect.left - tipRect.width - gap,
|
|
688
|
+
right: rect.right + gap
|
|
689
|
+
};
|
|
690
|
+
const left = leftMap[physicalPlacement] ?? rect.left;
|
|
691
|
+
this.tip.style.top = `${Math.max(4, top + window.scrollY)}px`;
|
|
692
|
+
this.tip.style.left = `${Math.max(4, left + window.scrollX)}px`;
|
|
693
|
+
}
|
|
694
|
+
destroy() {
|
|
695
|
+
if (this.showTimeout)
|
|
696
|
+
clearTimeout(this.showTimeout);
|
|
697
|
+
if (this.hideTimeout)
|
|
698
|
+
clearTimeout(this.hideTimeout);
|
|
699
|
+
this.hide();
|
|
700
|
+
super.destroy();
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
export class Tooltip extends FloatingText {
|
|
704
|
+
static componentName = "tooltip";
|
|
705
|
+
static dataAttribute = "data-tooltip";
|
|
706
|
+
static role = "tooltip";
|
|
707
|
+
}
|
|
708
|
+
export class Popover extends FloatingText {
|
|
709
|
+
static componentName = "popover";
|
|
710
|
+
static dataAttribute = "data-popover";
|
|
711
|
+
static role = "dialog";
|
|
712
|
+
}
|
|
713
|
+
function collapsePanel(panel, duration = 250) {
|
|
714
|
+
const animatedPanel = panel;
|
|
715
|
+
if (animatedPanel._aksaraCollapseTimer) {
|
|
716
|
+
clearTimeout(animatedPanel._aksaraCollapseTimer);
|
|
717
|
+
delete animatedPanel._aksaraCollapseTimer;
|
|
718
|
+
}
|
|
719
|
+
if (animatedPanel._aksaraCollapseEnd) {
|
|
720
|
+
panel.removeEventListener("transitionend", animatedPanel._aksaraCollapseEnd);
|
|
721
|
+
delete animatedPanel._aksaraCollapseEnd;
|
|
722
|
+
}
|
|
723
|
+
if (duration <= 0 ||
|
|
724
|
+
(typeof window !== "undefined" && window.matchMedia?.("(prefers-reduced-motion: reduce)").matches)) {
|
|
725
|
+
panel.hidden = true;
|
|
726
|
+
panel.style.removeProperty("height");
|
|
727
|
+
panel.style.removeProperty("overflow");
|
|
728
|
+
panel.style.removeProperty("padding-top");
|
|
729
|
+
panel.style.removeProperty("padding-bottom");
|
|
730
|
+
panel.style.removeProperty("opacity");
|
|
731
|
+
panel.style.removeProperty("transition");
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
const computed = window.getComputedStyle(panel);
|
|
735
|
+
const currentHeight = panel.getBoundingClientRect().height;
|
|
736
|
+
const currentPaddingTop = computed.paddingTop;
|
|
737
|
+
const currentPaddingBottom = computed.paddingBottom;
|
|
738
|
+
panel.style.overflow = "hidden";
|
|
739
|
+
panel.style.height = `${currentHeight}px`;
|
|
740
|
+
panel.style.paddingTop = currentPaddingTop;
|
|
741
|
+
panel.style.paddingBottom = currentPaddingBottom;
|
|
742
|
+
panel.style.opacity = "1";
|
|
743
|
+
panel.style.transition = `height ${duration}ms cubic-bezier(0.4, 0, 0.2, 1), padding-top ${duration}ms cubic-bezier(0.4, 0, 0.2, 1), padding-bottom ${duration}ms cubic-bezier(0.4, 0, 0.2, 1), opacity ${Math.round(duration * 0.7)}ms ease`;
|
|
744
|
+
void panel.offsetHeight;
|
|
745
|
+
panel.style.height = "0px";
|
|
746
|
+
panel.style.paddingTop = "0px";
|
|
747
|
+
panel.style.paddingBottom = "0px";
|
|
748
|
+
panel.style.opacity = "0";
|
|
749
|
+
const cleanup = () => {
|
|
750
|
+
panel.hidden = true;
|
|
751
|
+
panel.style.removeProperty("height");
|
|
752
|
+
panel.style.removeProperty("overflow");
|
|
753
|
+
panel.style.removeProperty("padding-top");
|
|
754
|
+
panel.style.removeProperty("padding-bottom");
|
|
755
|
+
panel.style.removeProperty("opacity");
|
|
756
|
+
panel.style.removeProperty("transition");
|
|
757
|
+
if (animatedPanel._aksaraCollapseEnd) {
|
|
758
|
+
panel.removeEventListener("transitionend", animatedPanel._aksaraCollapseEnd);
|
|
759
|
+
delete animatedPanel._aksaraCollapseEnd;
|
|
760
|
+
}
|
|
761
|
+
delete animatedPanel._aksaraCollapseTimer;
|
|
762
|
+
};
|
|
763
|
+
const onEnd = (e) => {
|
|
764
|
+
if (e.target === panel && e.propertyName === "height") {
|
|
765
|
+
cleanup();
|
|
766
|
+
}
|
|
767
|
+
};
|
|
768
|
+
animatedPanel._aksaraCollapseEnd = onEnd;
|
|
769
|
+
panel.addEventListener("transitionend", onEnd);
|
|
770
|
+
animatedPanel._aksaraCollapseTimer = setTimeout(cleanup, duration + 60);
|
|
771
|
+
}
|
|
772
|
+
function expandPanel(panel, duration = 250) {
|
|
773
|
+
const animatedPanel = panel;
|
|
774
|
+
if (animatedPanel._aksaraCollapseTimer) {
|
|
775
|
+
clearTimeout(animatedPanel._aksaraCollapseTimer);
|
|
776
|
+
delete animatedPanel._aksaraCollapseTimer;
|
|
777
|
+
}
|
|
778
|
+
if (animatedPanel._aksaraCollapseEnd) {
|
|
779
|
+
panel.removeEventListener("transitionend", animatedPanel._aksaraCollapseEnd);
|
|
780
|
+
delete animatedPanel._aksaraCollapseEnd;
|
|
781
|
+
}
|
|
782
|
+
if (duration <= 0 ||
|
|
783
|
+
(typeof window !== "undefined" && window.matchMedia?.("(prefers-reduced-motion: reduce)").matches)) {
|
|
784
|
+
panel.hidden = false;
|
|
785
|
+
panel.style.removeProperty("height");
|
|
786
|
+
panel.style.removeProperty("overflow");
|
|
787
|
+
panel.style.removeProperty("padding-top");
|
|
788
|
+
panel.style.removeProperty("padding-bottom");
|
|
789
|
+
panel.style.removeProperty("opacity");
|
|
790
|
+
panel.style.removeProperty("transition");
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
panel.hidden = false;
|
|
794
|
+
panel.style.removeProperty("height");
|
|
795
|
+
panel.style.removeProperty("padding-top");
|
|
796
|
+
panel.style.removeProperty("padding-bottom");
|
|
797
|
+
panel.style.removeProperty("opacity");
|
|
798
|
+
panel.style.removeProperty("transition");
|
|
799
|
+
const computed = window.getComputedStyle(panel);
|
|
800
|
+
const targetPaddingTop = computed.paddingTop;
|
|
801
|
+
const targetPaddingBottom = computed.paddingBottom;
|
|
802
|
+
const targetHeight = panel.getBoundingClientRect().height;
|
|
803
|
+
panel.style.overflow = "hidden";
|
|
804
|
+
panel.style.height = "0px";
|
|
805
|
+
panel.style.paddingTop = "0px";
|
|
806
|
+
panel.style.paddingBottom = "0px";
|
|
807
|
+
panel.style.opacity = "0";
|
|
808
|
+
void panel.offsetHeight;
|
|
809
|
+
panel.style.transition = `height ${duration}ms cubic-bezier(0.4, 0, 0.2, 1), padding-top ${duration}ms cubic-bezier(0.4, 0, 0.2, 1), padding-bottom ${duration}ms cubic-bezier(0.4, 0, 0.2, 1), opacity ${duration}ms ease`;
|
|
810
|
+
panel.style.height = `${targetHeight}px`;
|
|
811
|
+
panel.style.paddingTop = targetPaddingTop;
|
|
812
|
+
panel.style.paddingBottom = targetPaddingBottom;
|
|
813
|
+
panel.style.opacity = "1";
|
|
814
|
+
const cleanup = () => {
|
|
815
|
+
panel.style.removeProperty("height");
|
|
816
|
+
panel.style.removeProperty("overflow");
|
|
817
|
+
panel.style.removeProperty("padding-top");
|
|
818
|
+
panel.style.removeProperty("padding-bottom");
|
|
819
|
+
panel.style.removeProperty("opacity");
|
|
820
|
+
panel.style.removeProperty("transition");
|
|
821
|
+
if (animatedPanel._aksaraCollapseEnd) {
|
|
822
|
+
panel.removeEventListener("transitionend", animatedPanel._aksaraCollapseEnd);
|
|
823
|
+
delete animatedPanel._aksaraCollapseEnd;
|
|
824
|
+
}
|
|
825
|
+
delete animatedPanel._aksaraCollapseTimer;
|
|
826
|
+
};
|
|
827
|
+
const onEnd = (e) => {
|
|
828
|
+
if (e.target === panel && e.propertyName === "height") {
|
|
829
|
+
cleanup();
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
animatedPanel._aksaraCollapseEnd = onEnd;
|
|
833
|
+
panel.addEventListener("transitionend", onEnd);
|
|
834
|
+
animatedPanel._aksaraCollapseTimer = setTimeout(cleanup, duration + 60);
|
|
835
|
+
}
|
|
836
|
+
export class Accordion extends Component {
|
|
837
|
+
static componentName = "accordion";
|
|
838
|
+
parent;
|
|
839
|
+
collapsible;
|
|
840
|
+
buttonSelector;
|
|
841
|
+
duration;
|
|
842
|
+
constructor(element, options = {}) {
|
|
843
|
+
super(element, options);
|
|
844
|
+
this.updateOptions(options);
|
|
845
|
+
this.on(element, "click", (event) => {
|
|
846
|
+
const target = event.target;
|
|
847
|
+
const button = target.closest(this.buttonSelector);
|
|
848
|
+
if (!button || !element.contains(button))
|
|
849
|
+
return;
|
|
850
|
+
const panelSelector = button.getAttribute("data-accordion");
|
|
851
|
+
if (!panelSelector)
|
|
852
|
+
return;
|
|
853
|
+
const panel = document.querySelector(panelSelector);
|
|
854
|
+
if (!panel)
|
|
855
|
+
return;
|
|
856
|
+
const expanded = button.getAttribute("aria-expanded") === "true";
|
|
857
|
+
if (expanded && !this.collapsible)
|
|
858
|
+
return;
|
|
859
|
+
const parentScope = this.getParentScope();
|
|
860
|
+
if (!expanded && parentScope) {
|
|
861
|
+
parentScope.querySelectorAll(this.buttonSelector).forEach((otherButton) => {
|
|
862
|
+
if (otherButton === button)
|
|
863
|
+
return;
|
|
864
|
+
otherButton.setAttribute("aria-expanded", "false");
|
|
865
|
+
const otherPanelSelector = otherButton.getAttribute("data-accordion");
|
|
866
|
+
if (otherPanelSelector) {
|
|
867
|
+
const otherPanel = document.querySelector(otherPanelSelector);
|
|
868
|
+
if (otherPanel && (!otherPanel.hidden || otherPanel.offsetHeight > 0)) {
|
|
869
|
+
collapsePanel(otherPanel, this.duration);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
button.setAttribute("aria-expanded", String(!expanded));
|
|
875
|
+
if (expanded) {
|
|
876
|
+
collapsePanel(panel, this.duration);
|
|
877
|
+
}
|
|
878
|
+
else {
|
|
879
|
+
expandPanel(panel, this.duration);
|
|
880
|
+
}
|
|
881
|
+
emit(element, "accordion:toggle", { button, panel, expanded: !expanded });
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
updateOptions(options = {}) {
|
|
885
|
+
super.updateOptions(options);
|
|
886
|
+
const parentVal = getOption(this.options, this.element, "accordion", "parent", true);
|
|
887
|
+
if (parentVal === "true")
|
|
888
|
+
this.parent = true;
|
|
889
|
+
else if (parentVal === "false")
|
|
890
|
+
this.parent = false;
|
|
891
|
+
else
|
|
892
|
+
this.parent = parentVal;
|
|
893
|
+
this.collapsible = getBooleanOption(this.options, this.element, "accordion", "collapsible", true);
|
|
894
|
+
this.buttonSelector = getOption(this.options, this.element, "accordion", "buttonSelector", "[data-accordion]");
|
|
895
|
+
this.duration = getNumberOption(this.options, this.element, "accordion", "duration", 250);
|
|
896
|
+
return this;
|
|
897
|
+
}
|
|
898
|
+
getParentScope() {
|
|
899
|
+
if (this.parent === false)
|
|
900
|
+
return null;
|
|
901
|
+
if (this.parent === true)
|
|
902
|
+
return this.element;
|
|
903
|
+
if (this.parent instanceof Element)
|
|
904
|
+
return this.parent;
|
|
905
|
+
if (typeof this.parent === "string")
|
|
906
|
+
return document.querySelector(this.parent) || this.element;
|
|
907
|
+
return this.element;
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
export class Tabs extends Component {
|
|
911
|
+
static componentName = "tabs";
|
|
912
|
+
tabSelector;
|
|
913
|
+
panelSelector;
|
|
914
|
+
active;
|
|
915
|
+
activeIndex;
|
|
916
|
+
keyboard;
|
|
917
|
+
loop;
|
|
918
|
+
orientation;
|
|
919
|
+
constructor(element, options = {}) {
|
|
920
|
+
super(element, options);
|
|
921
|
+
this.updateOptions(options);
|
|
922
|
+
this.on(element, "click", (event) => {
|
|
923
|
+
const target = event.target;
|
|
924
|
+
const tab = target.closest(this.tabSelector);
|
|
925
|
+
if (!tab || !element.contains(tab))
|
|
926
|
+
return;
|
|
927
|
+
event.preventDefault();
|
|
928
|
+
this.show(tab);
|
|
929
|
+
});
|
|
930
|
+
this.on(element, "keydown", (event) => {
|
|
931
|
+
if (!this.keyboard)
|
|
932
|
+
return;
|
|
933
|
+
const keyEvent = event;
|
|
934
|
+
const keys = this.orientation === "vertical" ? ["ArrowUp", "ArrowDown"] : ["ArrowLeft", "ArrowRight"];
|
|
935
|
+
if (!keys.includes(keyEvent.key))
|
|
936
|
+
return;
|
|
937
|
+
const tabs = Array.from(element.querySelectorAll(this.tabSelector));
|
|
938
|
+
const index = tabs.indexOf(document.activeElement);
|
|
939
|
+
if (index < 0)
|
|
940
|
+
return;
|
|
941
|
+
event.preventDefault();
|
|
942
|
+
const isNext = keyEvent.key === "ArrowRight" || keyEvent.key === "ArrowDown";
|
|
943
|
+
const fallback = this.loop ? (isNext ? tabs[0] : tabs[tabs.length - 1]) : tabs[index];
|
|
944
|
+
const next = isNext ? tabs[index + 1] || fallback : tabs[index - 1] || fallback;
|
|
945
|
+
if (next) {
|
|
946
|
+
next.focus();
|
|
947
|
+
this.show(next);
|
|
948
|
+
}
|
|
949
|
+
});
|
|
950
|
+
this.activateInitial();
|
|
951
|
+
}
|
|
952
|
+
updateOptions(options = {}) {
|
|
953
|
+
const shouldActivate = (options.active !== undefined || options.activeIndex !== undefined) && this.cleanups.length > 0;
|
|
954
|
+
super.updateOptions(options);
|
|
955
|
+
this.tabSelector = getOption(this.options, this.element, "tabs", "tabSelector", "[data-tabs]");
|
|
956
|
+
this.panelSelector = getOption(this.options, this.element, "tabs", "panelSelector", null);
|
|
957
|
+
this.active = getOption(this.options, this.element, "tabs", "active", null);
|
|
958
|
+
this.activeIndex = getNumberOption(this.options, this.element, "tabs", "activeIndex", -1);
|
|
959
|
+
this.keyboard = getBooleanOption(this.options, this.element, "tabs", "keyboard", true);
|
|
960
|
+
this.loop = getBooleanOption(this.options, this.element, "tabs", "loop", true);
|
|
961
|
+
this.orientation = getOption(this.options, this.element, "tabs", "orientation", this.element.getAttribute("aria-orientation") || "horizontal");
|
|
962
|
+
this.element.setAttribute("role", this.element.getAttribute("role") || "tablist");
|
|
963
|
+
this.element.setAttribute("aria-orientation", this.orientation);
|
|
964
|
+
if (shouldActivate)
|
|
965
|
+
this.activateInitial();
|
|
966
|
+
return this;
|
|
967
|
+
}
|
|
968
|
+
activateInitial() {
|
|
969
|
+
const tabs = Array.from(this.element.querySelectorAll(this.tabSelector));
|
|
970
|
+
if (!tabs.length)
|
|
971
|
+
return;
|
|
972
|
+
let initial = null;
|
|
973
|
+
if (this.active instanceof HTMLElement) {
|
|
974
|
+
initial = this.active;
|
|
975
|
+
}
|
|
976
|
+
else if (typeof this.active === "string") {
|
|
977
|
+
initial =
|
|
978
|
+
this.element.querySelector(this.active) || document.querySelector(this.active);
|
|
979
|
+
}
|
|
980
|
+
if (!initial && this.activeIndex >= 0) {
|
|
981
|
+
initial = tabs[this.activeIndex] || null;
|
|
982
|
+
}
|
|
983
|
+
if (!initial) {
|
|
984
|
+
initial = tabs.find((tab) => tab.getAttribute("aria-selected") === "true") || tabs[0];
|
|
985
|
+
}
|
|
986
|
+
if (initial)
|
|
987
|
+
this.show(initial);
|
|
988
|
+
}
|
|
989
|
+
show(tab) {
|
|
990
|
+
const selector = tab.getAttribute("data-tabs");
|
|
991
|
+
const panel = selector ? document.querySelector(selector) : null;
|
|
992
|
+
if (!panel)
|
|
993
|
+
return this;
|
|
994
|
+
this.element.querySelectorAll(this.tabSelector).forEach((item) => {
|
|
995
|
+
const selected = item === tab;
|
|
996
|
+
item.setAttribute("role", item.getAttribute("role") || "tab");
|
|
997
|
+
item.setAttribute("aria-selected", String(selected));
|
|
998
|
+
item.tabIndex = selected ? 0 : -1;
|
|
999
|
+
const targetSelector = item.getAttribute("data-tabs");
|
|
1000
|
+
const target = targetSelector ? document.querySelector(targetSelector) : null;
|
|
1001
|
+
if (target)
|
|
1002
|
+
target.hidden = !selected;
|
|
1003
|
+
});
|
|
1004
|
+
if (this.panelSelector) {
|
|
1005
|
+
document.querySelectorAll(this.panelSelector).forEach((item) => {
|
|
1006
|
+
if (item !== panel)
|
|
1007
|
+
item.hidden = true;
|
|
1008
|
+
});
|
|
1009
|
+
}
|
|
1010
|
+
panel.setAttribute("role", panel.getAttribute("role") || "tabpanel");
|
|
1011
|
+
emit(this.element, "tabs:show", { tab, panel, instance: this });
|
|
1012
|
+
return this;
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
export class Toast extends Component {
|
|
1016
|
+
static componentName = "toast";
|
|
1017
|
+
timeout = null;
|
|
1018
|
+
delay;
|
|
1019
|
+
autohide;
|
|
1020
|
+
constructor(element, options = {}) {
|
|
1021
|
+
super(element, options);
|
|
1022
|
+
this.updateOptions(options);
|
|
1023
|
+
element.setAttribute("role", element.getAttribute("role") || "status");
|
|
1024
|
+
element.setAttribute("aria-live", element.getAttribute("aria-live") || "polite");
|
|
1025
|
+
}
|
|
1026
|
+
updateOptions(options = {}) {
|
|
1027
|
+
super.updateOptions(options);
|
|
1028
|
+
this.delay = getNumberOption(this.options, this.element, "toast", "delay", 5000);
|
|
1029
|
+
this.autohide = getBooleanOption(this.options, this.element, "toast", "autohide", true);
|
|
1030
|
+
return this;
|
|
1031
|
+
}
|
|
1032
|
+
show() {
|
|
1033
|
+
this.element.classList.add("toast-show");
|
|
1034
|
+
if (this.timeout)
|
|
1035
|
+
clearTimeout(this.timeout);
|
|
1036
|
+
if (this.autohide && this.delay > 0)
|
|
1037
|
+
this.timeout = setTimeout(() => this.hide(), this.delay);
|
|
1038
|
+
emit(this.element, "toast:show", { instance: this });
|
|
1039
|
+
return this;
|
|
1040
|
+
}
|
|
1041
|
+
hide() {
|
|
1042
|
+
this.element.classList.remove("toast-show");
|
|
1043
|
+
if (this.timeout)
|
|
1044
|
+
clearTimeout(this.timeout);
|
|
1045
|
+
emit(this.element, "toast:hide", { instance: this });
|
|
1046
|
+
return this;
|
|
1047
|
+
}
|
|
1048
|
+
destroy() {
|
|
1049
|
+
if (this.timeout)
|
|
1050
|
+
clearTimeout(this.timeout);
|
|
1051
|
+
super.destroy();
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
export class Carousel extends Component {
|
|
1055
|
+
static componentName = "carousel";
|
|
1056
|
+
timeout = null;
|
|
1057
|
+
items;
|
|
1058
|
+
index;
|
|
1059
|
+
interval;
|
|
1060
|
+
autoplay;
|
|
1061
|
+
pauseOnHover;
|
|
1062
|
+
keyboard;
|
|
1063
|
+
loop;
|
|
1064
|
+
constructor(element, options = {}) {
|
|
1065
|
+
super(element, options);
|
|
1066
|
+
this.items = Array.from(element.querySelectorAll("[data-carousel-item]"));
|
|
1067
|
+
this.index = this.resolveInitialIndex();
|
|
1068
|
+
this.updateOptions(options);
|
|
1069
|
+
this.show(this.index);
|
|
1070
|
+
this.on(element, "click", (event) => {
|
|
1071
|
+
const target = event.target;
|
|
1072
|
+
const action = target.closest("[data-carousel]");
|
|
1073
|
+
if (!action)
|
|
1074
|
+
return;
|
|
1075
|
+
const value = action.getAttribute("data-carousel");
|
|
1076
|
+
if (value === "next")
|
|
1077
|
+
this.next();
|
|
1078
|
+
if (value === "prev")
|
|
1079
|
+
this.prev();
|
|
1080
|
+
if (value !== "next" && value !== "prev" && value !== "")
|
|
1081
|
+
this.show(Number(value));
|
|
1082
|
+
});
|
|
1083
|
+
this.on(element, "keydown", (event) => {
|
|
1084
|
+
const keyEvent = event;
|
|
1085
|
+
if (!this.keyboard || !["ArrowLeft", "ArrowRight"].includes(keyEvent.key))
|
|
1086
|
+
return;
|
|
1087
|
+
event.preventDefault();
|
|
1088
|
+
if (keyEvent.key === "ArrowRight")
|
|
1089
|
+
this.next();
|
|
1090
|
+
if (keyEvent.key === "ArrowLeft")
|
|
1091
|
+
this.prev();
|
|
1092
|
+
});
|
|
1093
|
+
if (this.pauseOnHover) {
|
|
1094
|
+
this.on(element, "mouseenter", () => this.pause());
|
|
1095
|
+
this.on(element, "mouseleave", () => this.play());
|
|
1096
|
+
}
|
|
1097
|
+
if (this.autoplay)
|
|
1098
|
+
this.play();
|
|
1099
|
+
}
|
|
1100
|
+
updateOptions(options = {}) {
|
|
1101
|
+
super.updateOptions(options);
|
|
1102
|
+
this.interval = getNumberOption(this.options, this.element, "carousel", "interval", 5000);
|
|
1103
|
+
this.autoplay = getBooleanOption(this.options, this.element, "carousel", "autoplay", false);
|
|
1104
|
+
this.pauseOnHover = getBooleanOption(this.options, this.element, "carousel", "pauseOnHover", true);
|
|
1105
|
+
this.keyboard = getBooleanOption(this.options, this.element, "carousel", "keyboard", true);
|
|
1106
|
+
this.loop = getBooleanOption(this.options, this.element, "carousel", "loop", true);
|
|
1107
|
+
const activeIndex = getNumberOption(this.options, this.element, "carousel", "activeIndex", -1);
|
|
1108
|
+
if (activeIndex >= 0)
|
|
1109
|
+
this.show(activeIndex);
|
|
1110
|
+
if (this.autoplay)
|
|
1111
|
+
this.play();
|
|
1112
|
+
if (!this.autoplay)
|
|
1113
|
+
this.pause();
|
|
1114
|
+
return this;
|
|
1115
|
+
}
|
|
1116
|
+
resolveInitialIndex() {
|
|
1117
|
+
const configured = getNumberOption(this.options, this.element, "carousel", "activeIndex", -1);
|
|
1118
|
+
if (configured >= 0)
|
|
1119
|
+
return configured;
|
|
1120
|
+
return Math.max(0, this.items.findIndex((item) => !item.hidden));
|
|
1121
|
+
}
|
|
1122
|
+
show(index) {
|
|
1123
|
+
if (!this.items.length)
|
|
1124
|
+
return this;
|
|
1125
|
+
if (!this.loop && (index < 0 || index >= this.items.length))
|
|
1126
|
+
return this;
|
|
1127
|
+
this.index = (index + this.items.length) % this.items.length;
|
|
1128
|
+
this.items.forEach((item, itemIndex) => {
|
|
1129
|
+
item.hidden = itemIndex !== this.index;
|
|
1130
|
+
});
|
|
1131
|
+
emit(this.element, "carousel:show", { index: this.index, instance: this });
|
|
1132
|
+
return this;
|
|
1133
|
+
}
|
|
1134
|
+
next() {
|
|
1135
|
+
return this.show(this.index + 1);
|
|
1136
|
+
}
|
|
1137
|
+
prev() {
|
|
1138
|
+
return this.show(this.index - 1);
|
|
1139
|
+
}
|
|
1140
|
+
play() {
|
|
1141
|
+
if (this.timeout)
|
|
1142
|
+
clearInterval(this.timeout);
|
|
1143
|
+
if (this.autoplay && this.interval > 0) {
|
|
1144
|
+
this.timeout = setInterval(() => this.next(), this.interval);
|
|
1145
|
+
}
|
|
1146
|
+
return this;
|
|
1147
|
+
}
|
|
1148
|
+
pause() {
|
|
1149
|
+
if (this.timeout)
|
|
1150
|
+
clearInterval(this.timeout);
|
|
1151
|
+
this.timeout = null;
|
|
1152
|
+
return this;
|
|
1153
|
+
}
|
|
1154
|
+
destroy() {
|
|
1155
|
+
this.pause();
|
|
1156
|
+
super.destroy();
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
export class Offcanvas extends Modal {
|
|
1160
|
+
static componentName = "offcanvas";
|
|
1161
|
+
show() {
|
|
1162
|
+
if (this.isOpen)
|
|
1163
|
+
return this;
|
|
1164
|
+
this.previousFocus = document.activeElement;
|
|
1165
|
+
this.isOpen = true;
|
|
1166
|
+
this.element.classList.add("offcanvas-open");
|
|
1167
|
+
this.element.setAttribute("aria-hidden", "false");
|
|
1168
|
+
if (this.scrollLock)
|
|
1169
|
+
document.documentElement.style.overflow = "hidden";
|
|
1170
|
+
if (this.focus)
|
|
1171
|
+
this.focusFirst();
|
|
1172
|
+
emit(this.element, "offcanvas:show", { instance: this });
|
|
1173
|
+
return this;
|
|
1174
|
+
}
|
|
1175
|
+
hide() {
|
|
1176
|
+
if (!this.isOpen)
|
|
1177
|
+
return this;
|
|
1178
|
+
this.isOpen = false;
|
|
1179
|
+
this.element.classList.remove("offcanvas-open");
|
|
1180
|
+
this.element.setAttribute("aria-hidden", "true");
|
|
1181
|
+
if (this.scrollLock)
|
|
1182
|
+
document.documentElement.style.overflow = "";
|
|
1183
|
+
if (this.previousFocus && typeof this.previousFocus.focus === "function")
|
|
1184
|
+
this.previousFocus.focus();
|
|
1185
|
+
emit(this.element, "offcanvas:hide", { instance: this });
|
|
1186
|
+
return this;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
function readTriggerOptions(trigger, component, names) {
|
|
1190
|
+
return names.reduce((options, name) => {
|
|
1191
|
+
const value = getData(trigger, component, name);
|
|
1192
|
+
if (value !== null)
|
|
1193
|
+
options[name] = value;
|
|
1194
|
+
return options;
|
|
1195
|
+
}, {});
|
|
1196
|
+
}
|
|
1197
|
+
function dismissTarget(target) {
|
|
1198
|
+
if (!target)
|
|
1199
|
+
return;
|
|
1200
|
+
if (target.classList.contains("modal")) {
|
|
1201
|
+
getInstance(target, Modal).hide();
|
|
1202
|
+
}
|
|
1203
|
+
else if (target.classList.contains("offcanvas")) {
|
|
1204
|
+
getInstance(target, Offcanvas).hide();
|
|
1205
|
+
}
|
|
1206
|
+
else if (target.classList.contains("toast")) {
|
|
1207
|
+
getInstance(target, Toast).hide();
|
|
1208
|
+
}
|
|
1209
|
+
else if (target.classList.contains("dropdown") || target.matches("[data-dropdown]")) {
|
|
1210
|
+
getInstance(target, Dropdown).hide();
|
|
1211
|
+
}
|
|
1212
|
+
else {
|
|
1213
|
+
target.remove();
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
function resolveDismissTargets(trigger) {
|
|
1217
|
+
const selector = trigger.getAttribute("data-dismiss");
|
|
1218
|
+
if (selector) {
|
|
1219
|
+
try {
|
|
1220
|
+
return Array.from(document.querySelectorAll(selector));
|
|
1221
|
+
}
|
|
1222
|
+
catch {
|
|
1223
|
+
return [];
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
const localTarget = trigger.closest(".alert,.toast,.modal,.offcanvas,.dropdown,.popover,.tooltip");
|
|
1227
|
+
return localTarget ? [localTarget] : [];
|
|
1228
|
+
}
|
|
1229
|
+
function bindDataApi() {
|
|
1230
|
+
document.querySelectorAll("[data-dismiss]").forEach((trigger) => {
|
|
1231
|
+
const customTrigger = trigger;
|
|
1232
|
+
if (customTrigger.__aksaraDismissTrigger)
|
|
1233
|
+
return;
|
|
1234
|
+
customTrigger.__aksaraDismissTrigger = true;
|
|
1235
|
+
trigger.addEventListener("click", (event) => {
|
|
1236
|
+
event.preventDefault();
|
|
1237
|
+
resolveDismissTargets(trigger).forEach((target) => dismissTarget(target));
|
|
1238
|
+
});
|
|
1239
|
+
});
|
|
1240
|
+
document.querySelectorAll("[data-modal-close]").forEach((trigger) => {
|
|
1241
|
+
const customTrigger = trigger;
|
|
1242
|
+
if (customTrigger.__aksaraModalCloseTrigger)
|
|
1243
|
+
return;
|
|
1244
|
+
customTrigger.__aksaraModalCloseTrigger = true;
|
|
1245
|
+
trigger.addEventListener("click", (event) => {
|
|
1246
|
+
event.preventDefault();
|
|
1247
|
+
const targetSelector = trigger.getAttribute("data-modal-close");
|
|
1248
|
+
const modal = targetSelector
|
|
1249
|
+
? document.querySelector(targetSelector)
|
|
1250
|
+
: trigger.closest(".modal");
|
|
1251
|
+
if (modal)
|
|
1252
|
+
getInstance(modal, Modal).hide();
|
|
1253
|
+
});
|
|
1254
|
+
});
|
|
1255
|
+
document.querySelectorAll("[data-modal]").forEach((trigger) => {
|
|
1256
|
+
const customTrigger = trigger;
|
|
1257
|
+
if (customTrigger.__aksaraModalTrigger)
|
|
1258
|
+
return;
|
|
1259
|
+
customTrigger.__aksaraModalTrigger = true;
|
|
1260
|
+
trigger.addEventListener("click", (event) => {
|
|
1261
|
+
event.preventDefault();
|
|
1262
|
+
const targetSelector = trigger.getAttribute("data-modal");
|
|
1263
|
+
if (targetSelector) {
|
|
1264
|
+
const target = document.querySelector(targetSelector);
|
|
1265
|
+
if (target) {
|
|
1266
|
+
const options = readTriggerOptions(trigger, "modal", [
|
|
1267
|
+
"backdrop",
|
|
1268
|
+
"keyboard",
|
|
1269
|
+
"focus",
|
|
1270
|
+
"scrollLock",
|
|
1271
|
+
"draggable"
|
|
1272
|
+
]);
|
|
1273
|
+
getInstance(target, Modal, options).show();
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
});
|
|
1277
|
+
});
|
|
1278
|
+
document.querySelectorAll("[data-offcanvas-close]").forEach((trigger) => {
|
|
1279
|
+
const customTrigger = trigger;
|
|
1280
|
+
if (customTrigger.__aksaraOffcanvasCloseTrigger)
|
|
1281
|
+
return;
|
|
1282
|
+
customTrigger.__aksaraOffcanvasCloseTrigger = true;
|
|
1283
|
+
trigger.addEventListener("click", (event) => {
|
|
1284
|
+
event.preventDefault();
|
|
1285
|
+
const targetSelector = trigger.getAttribute("data-offcanvas-close");
|
|
1286
|
+
const panel = targetSelector
|
|
1287
|
+
? document.querySelector(targetSelector)
|
|
1288
|
+
: trigger.closest(".offcanvas");
|
|
1289
|
+
if (panel)
|
|
1290
|
+
getInstance(panel, Offcanvas).hide();
|
|
1291
|
+
});
|
|
1292
|
+
});
|
|
1293
|
+
document.querySelectorAll("[data-offcanvas]").forEach((trigger) => {
|
|
1294
|
+
const customTrigger = trigger;
|
|
1295
|
+
if (customTrigger.__aksaraOffcanvasTrigger)
|
|
1296
|
+
return;
|
|
1297
|
+
customTrigger.__aksaraOffcanvasTrigger = true;
|
|
1298
|
+
trigger.addEventListener("click", (event) => {
|
|
1299
|
+
event.preventDefault();
|
|
1300
|
+
const targetSelector = trigger.getAttribute("data-offcanvas");
|
|
1301
|
+
if (targetSelector) {
|
|
1302
|
+
const target = document.querySelector(targetSelector);
|
|
1303
|
+
if (target) {
|
|
1304
|
+
const options = readTriggerOptions(trigger, "offcanvas", ["backdrop", "keyboard", "focus", "scrollLock"]);
|
|
1305
|
+
getInstance(target, Offcanvas, options).show();
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
});
|
|
1309
|
+
});
|
|
1310
|
+
document.querySelectorAll("[data-toast]").forEach((trigger) => {
|
|
1311
|
+
const customTrigger = trigger;
|
|
1312
|
+
if (customTrigger.__aksaraToastTrigger)
|
|
1313
|
+
return;
|
|
1314
|
+
customTrigger.__aksaraToastTrigger = true;
|
|
1315
|
+
trigger.addEventListener("click", (event) => {
|
|
1316
|
+
event.preventDefault();
|
|
1317
|
+
const targetSelector = trigger.getAttribute("data-toast");
|
|
1318
|
+
if (targetSelector) {
|
|
1319
|
+
const target = document.querySelector(targetSelector);
|
|
1320
|
+
if (target) {
|
|
1321
|
+
const options = readTriggerOptions(trigger, "toast", ["delay", "autohide"]);
|
|
1322
|
+
getInstance(target, Toast, options).show();
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
});
|
|
1326
|
+
});
|
|
1327
|
+
}
|
|
1328
|
+
const injectedArbitraryRules = new Set();
|
|
1329
|
+
let arbitrarySheet = null;
|
|
1330
|
+
let arbitraryRuleIndex = 0;
|
|
1331
|
+
function getArbitrarySheet() {
|
|
1332
|
+
if (typeof document === "undefined")
|
|
1333
|
+
return null;
|
|
1334
|
+
if (!arbitrarySheet) {
|
|
1335
|
+
if ("adoptedStyleSheets" in document && typeof CSSStyleSheet !== "undefined") {
|
|
1336
|
+
arbitrarySheet = new CSSStyleSheet();
|
|
1337
|
+
document.adoptedStyleSheets = [...document.adoptedStyleSheets, arbitrarySheet];
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
return arbitrarySheet;
|
|
1341
|
+
}
|
|
1342
|
+
function escapeArbitraryClass(className) {
|
|
1343
|
+
return Array.from(className)
|
|
1344
|
+
.map((char, index) => {
|
|
1345
|
+
if (/^[a-zA-Z_-]$/.test(char))
|
|
1346
|
+
return char;
|
|
1347
|
+
if (/^[0-9]$/.test(char)) {
|
|
1348
|
+
return index === 0 ? `\\${char.charCodeAt(0).toString(16).padStart(6, "0")}` : char;
|
|
1349
|
+
}
|
|
1350
|
+
return `\\${char}`;
|
|
1351
|
+
})
|
|
1352
|
+
.join("");
|
|
1353
|
+
}
|
|
1354
|
+
const runtimeArbitraryPropertyMap = {
|
|
1355
|
+
m: (v) => `margin:${v}`,
|
|
1356
|
+
mt: (v) => `margin-block-start:${v}`,
|
|
1357
|
+
mb: (v) => `margin-block-end:${v}`,
|
|
1358
|
+
ms: (v) => `margin-inline-start:${v}`,
|
|
1359
|
+
me: (v) => `margin-inline-end:${v}`,
|
|
1360
|
+
mx: (v) => `margin-inline:${v}`,
|
|
1361
|
+
my: (v) => `margin-block:${v}`,
|
|
1362
|
+
p: (v) => `padding:${v}`,
|
|
1363
|
+
pt: (v) => `padding-block-start:${v}`,
|
|
1364
|
+
pb: (v) => `padding-block-end:${v}`,
|
|
1365
|
+
ps: (v) => `padding-inline-start:${v}`,
|
|
1366
|
+
pe: (v) => `padding-inline-end:${v}`,
|
|
1367
|
+
px: (v) => `padding-inline:${v}`,
|
|
1368
|
+
py: (v) => `padding-block:${v}`,
|
|
1369
|
+
gap: (v) => `gap:${v}`,
|
|
1370
|
+
"row-gap": (v) => `row-gap:${v}`,
|
|
1371
|
+
"col-gap": (v) => `column-gap:${v}`,
|
|
1372
|
+
g: (v) => `--aksara-gutter-x:${v};--aksara-gutter-y:${v}`,
|
|
1373
|
+
gx: (v) => `--aksara-gutter-x:${v}`,
|
|
1374
|
+
gy: (v) => `--aksara-gutter-y:${v}`,
|
|
1375
|
+
w: (v) => `width:${v}`,
|
|
1376
|
+
h: (v) => `height:${v}`,
|
|
1377
|
+
"min-w": (v) => `min-width:${v}`,
|
|
1378
|
+
"max-w": (v) => `max-width:${v}`,
|
|
1379
|
+
"min-h": (v) => `min-height:${v}`,
|
|
1380
|
+
"max-h": (v) => `max-height:${v}`,
|
|
1381
|
+
top: (v) => `inset-block-start:${v}`,
|
|
1382
|
+
bottom: (v) => `inset-block-end:${v}`,
|
|
1383
|
+
start: (v) => `inset-inline-start:${v}`,
|
|
1384
|
+
end: (v) => `inset-inline-end:${v}`,
|
|
1385
|
+
inset: (v) => `inset:${v}`,
|
|
1386
|
+
"inset-x": (v) => `inset-inline:${v}`,
|
|
1387
|
+
"inset-y": (v) => `inset-block:${v}`,
|
|
1388
|
+
"translate-x": (v) => `transform:translateX(${v})`,
|
|
1389
|
+
"translate-y": (v) => `transform:translateY(${v})`,
|
|
1390
|
+
rotate: (v) => `transform:rotate(${v})`,
|
|
1391
|
+
scale: (v) => `transform:scale(${v})`,
|
|
1392
|
+
rounded: (v) => `border-radius:${v}`,
|
|
1393
|
+
border: (v) => `border-width:${v}`,
|
|
1394
|
+
opacity: (v) => `opacity:${v}`,
|
|
1395
|
+
z: (v) => `z-index:${v}`,
|
|
1396
|
+
leading: (v) => `line-height:${v}`,
|
|
1397
|
+
tracking: (v) => `letter-spacing:${v}`,
|
|
1398
|
+
text: (v) => (/^(#[0-9a-fA-F]+|rgb|hsl)/.test(v) ? `color:${v}` : `font-size:${v}`),
|
|
1399
|
+
bg: (v) => `background-color:${v}`,
|
|
1400
|
+
overscroll: (v) => `overscroll-behavior:${v}`,
|
|
1401
|
+
"overscroll-x": (v) => `overscroll-behavior-x:${v}`,
|
|
1402
|
+
"overscroll-y": (v) => `overscroll-behavior-y:${v}`
|
|
1403
|
+
};
|
|
1404
|
+
const runtimeBreakpoints = {
|
|
1405
|
+
sm: "36rem",
|
|
1406
|
+
md: "48rem",
|
|
1407
|
+
lg: "62rem",
|
|
1408
|
+
xl: "75rem",
|
|
1409
|
+
"2xl": "87.5rem",
|
|
1410
|
+
"3xl": "100rem"
|
|
1411
|
+
};
|
|
1412
|
+
const runtimeStateSelectors = {
|
|
1413
|
+
hover: ":hover",
|
|
1414
|
+
focus: ":focus",
|
|
1415
|
+
active: ":active",
|
|
1416
|
+
disabled: ":disabled",
|
|
1417
|
+
checked: ":checked",
|
|
1418
|
+
selected: '[aria-selected="true"]',
|
|
1419
|
+
visited: ":visited",
|
|
1420
|
+
first: ":first-child",
|
|
1421
|
+
last: ":last-child",
|
|
1422
|
+
odd: ":nth-child(odd)",
|
|
1423
|
+
even: ":nth-child(even)"
|
|
1424
|
+
};
|
|
1425
|
+
function injectArbitraryClass(className) {
|
|
1426
|
+
if (injectedArbitraryRules.has(className))
|
|
1427
|
+
return;
|
|
1428
|
+
injectedArbitraryRules.add(className);
|
|
1429
|
+
const parts = className.split(":");
|
|
1430
|
+
const utility = parts.pop();
|
|
1431
|
+
if (!utility)
|
|
1432
|
+
return;
|
|
1433
|
+
const match = utility.match(/^(-)?([a-zA-Z0-9_-]+)-\[(.+)\]$/);
|
|
1434
|
+
if (!match)
|
|
1435
|
+
return;
|
|
1436
|
+
const isNegative = Boolean(match[1]);
|
|
1437
|
+
const prop = match[2];
|
|
1438
|
+
let val = match[3];
|
|
1439
|
+
const resolver = runtimeArbitraryPropertyMap[prop];
|
|
1440
|
+
if (!resolver)
|
|
1441
|
+
return;
|
|
1442
|
+
if (isNegative && !val.startsWith("-")) {
|
|
1443
|
+
val = `-${val}`;
|
|
1444
|
+
}
|
|
1445
|
+
const declarations = resolver(val);
|
|
1446
|
+
const escaped = escapeArbitraryClass(className);
|
|
1447
|
+
let selector = `.${escaped}`;
|
|
1448
|
+
let stateSelector = "";
|
|
1449
|
+
let themeVariant = "";
|
|
1450
|
+
let breakpointVariant = "";
|
|
1451
|
+
for (const v of parts) {
|
|
1452
|
+
if (v === "dark" || v === "light")
|
|
1453
|
+
themeVariant = v;
|
|
1454
|
+
else if (runtimeBreakpoints[v])
|
|
1455
|
+
breakpointVariant = v;
|
|
1456
|
+
else if (runtimeStateSelectors[v])
|
|
1457
|
+
stateSelector += runtimeStateSelectors[v];
|
|
1458
|
+
}
|
|
1459
|
+
selector += stateSelector;
|
|
1460
|
+
let rule = `${selector}{${declarations}}`;
|
|
1461
|
+
if (themeVariant) {
|
|
1462
|
+
rule =
|
|
1463
|
+
themeVariant === "dark"
|
|
1464
|
+
? `[data-theme="dark"] ${selector},.dark ${selector}{${declarations}}`
|
|
1465
|
+
: `[data-theme="light"] ${selector},.light ${selector}{${declarations}}`;
|
|
1466
|
+
}
|
|
1467
|
+
if (breakpointVariant) {
|
|
1468
|
+
rule = `@media (min-width:${runtimeBreakpoints[breakpointVariant]}){${rule}}`;
|
|
1469
|
+
}
|
|
1470
|
+
const sheet = getArbitrarySheet();
|
|
1471
|
+
if (sheet) {
|
|
1472
|
+
try {
|
|
1473
|
+
sheet.insertRule(rule, arbitraryRuleIndex++);
|
|
1474
|
+
}
|
|
1475
|
+
catch {
|
|
1476
|
+
// Ignore if browser rejects rule syntax
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
function processElementClasses(el) {
|
|
1481
|
+
const classAttr = el.getAttribute("class");
|
|
1482
|
+
if (!classAttr || !classAttr.includes("-["))
|
|
1483
|
+
return;
|
|
1484
|
+
const tokens = classAttr.split(/\s+/);
|
|
1485
|
+
for (const token of tokens) {
|
|
1486
|
+
if (token.includes("-[") && token.endsWith("]")) {
|
|
1487
|
+
injectArbitraryClass(token);
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
function scanArbitraryDom(root = document) {
|
|
1492
|
+
if (typeof document === "undefined")
|
|
1493
|
+
return;
|
|
1494
|
+
if (root instanceof Element) {
|
|
1495
|
+
processElementClasses(root);
|
|
1496
|
+
}
|
|
1497
|
+
root.querySelectorAll('[class*="-["]').forEach(processElementClasses);
|
|
1498
|
+
}
|
|
1499
|
+
let mutationObserverStarted = false;
|
|
1500
|
+
function setupArbitraryObserver() {
|
|
1501
|
+
if (mutationObserverStarted || typeof MutationObserver === "undefined" || typeof document === "undefined")
|
|
1502
|
+
return;
|
|
1503
|
+
mutationObserverStarted = true;
|
|
1504
|
+
const observer = new MutationObserver((mutations) => {
|
|
1505
|
+
for (const m of mutations) {
|
|
1506
|
+
if (m.type === "attributes" && m.target instanceof Element) {
|
|
1507
|
+
processElementClasses(m.target);
|
|
1508
|
+
}
|
|
1509
|
+
else if (m.type === "childList") {
|
|
1510
|
+
m.addedNodes.forEach((node) => {
|
|
1511
|
+
if (node instanceof Element) {
|
|
1512
|
+
scanArbitraryDom(node);
|
|
1513
|
+
}
|
|
1514
|
+
});
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
});
|
|
1518
|
+
observer.observe(document.documentElement, {
|
|
1519
|
+
subtree: true,
|
|
1520
|
+
childList: true,
|
|
1521
|
+
attributes: true,
|
|
1522
|
+
attributeFilter: ["class"]
|
|
1523
|
+
});
|
|
1524
|
+
}
|
|
1525
|
+
export const Aksara = {
|
|
1526
|
+
Modal,
|
|
1527
|
+
Tooltip,
|
|
1528
|
+
Popover,
|
|
1529
|
+
Accordion,
|
|
1530
|
+
Dropdown,
|
|
1531
|
+
Tabs,
|
|
1532
|
+
Toast,
|
|
1533
|
+
Carousel,
|
|
1534
|
+
Offcanvas,
|
|
1535
|
+
modal: (target, options) => {
|
|
1536
|
+
const args = resolveArgs(target, options, "[data-modal], .modal");
|
|
1537
|
+
return createAll(args.target, Modal, args.options);
|
|
1538
|
+
},
|
|
1539
|
+
tooltip: (target, options) => {
|
|
1540
|
+
const args = resolveArgs(target, options, "[data-tooltip]");
|
|
1541
|
+
return createAll(args.target, Tooltip, args.options);
|
|
1542
|
+
},
|
|
1543
|
+
popover: (target, options) => {
|
|
1544
|
+
const args = resolveArgs(target, options, "[data-popover]");
|
|
1545
|
+
return createAll(args.target, Popover, args.options);
|
|
1546
|
+
},
|
|
1547
|
+
accordion: (target, options) => {
|
|
1548
|
+
const args = resolveArgs(target, options, ".accordion");
|
|
1549
|
+
return createAll(args.target, Accordion, args.options);
|
|
1550
|
+
},
|
|
1551
|
+
dropdown: (target, options) => {
|
|
1552
|
+
const args = resolveArgs(target, options, "[data-dropdown], .dropdown");
|
|
1553
|
+
return createAll(args.target, Dropdown, args.options);
|
|
1554
|
+
},
|
|
1555
|
+
tabs: (target, options) => {
|
|
1556
|
+
const args = resolveArgs(target, options, ".tabs");
|
|
1557
|
+
return createAll(args.target, Tabs, args.options);
|
|
1558
|
+
},
|
|
1559
|
+
toast: (target, options) => {
|
|
1560
|
+
const args = resolveArgs(target, options, ".toast");
|
|
1561
|
+
return createAll(args.target, Toast, args.options);
|
|
1562
|
+
},
|
|
1563
|
+
carousel: (target, options) => {
|
|
1564
|
+
const args = resolveArgs(target, options, ".carousel");
|
|
1565
|
+
return createAll(args.target, Carousel, args.options);
|
|
1566
|
+
},
|
|
1567
|
+
offcanvas: (target, options) => {
|
|
1568
|
+
const args = resolveArgs(target, options, ".offcanvas");
|
|
1569
|
+
return createAll(args.target, Offcanvas, args.options);
|
|
1570
|
+
},
|
|
1571
|
+
init(root = document) {
|
|
1572
|
+
scanArbitraryDom(root);
|
|
1573
|
+
setupArbitraryObserver();
|
|
1574
|
+
createAll(root.querySelectorAll("[data-tooltip]"), Tooltip);
|
|
1575
|
+
createAll(root.querySelectorAll("[data-popover]"), Popover);
|
|
1576
|
+
const dropdowns = new Set();
|
|
1577
|
+
root.querySelectorAll(".dropdown").forEach((el) => dropdowns.add(el));
|
|
1578
|
+
root.querySelectorAll("[data-dropdown]").forEach((el) => {
|
|
1579
|
+
if (!el.closest(".dropdown")) {
|
|
1580
|
+
dropdowns.add(el);
|
|
1581
|
+
}
|
|
1582
|
+
});
|
|
1583
|
+
createAll(Array.from(dropdowns), Dropdown);
|
|
1584
|
+
createAll(root.querySelectorAll(".accordion"), Accordion);
|
|
1585
|
+
createAll(root.querySelectorAll(".tabs"), Tabs);
|
|
1586
|
+
createAll(root.querySelectorAll(".toast"), Toast);
|
|
1587
|
+
createAll(root.querySelectorAll(".carousel"), Carousel);
|
|
1588
|
+
createAll(root.querySelectorAll(".modal"), Modal);
|
|
1589
|
+
createAll(root.querySelectorAll(".offcanvas"), Offcanvas);
|
|
1590
|
+
bindDataApi();
|
|
1591
|
+
emit(document.documentElement, "init", { root });
|
|
1592
|
+
return this;
|
|
1593
|
+
},
|
|
1594
|
+
destroy() {
|
|
1595
|
+
Array.from(instances).forEach((instance) => instance.destroy());
|
|
1596
|
+
registry.clear();
|
|
1597
|
+
emit(document.documentElement, "destroy");
|
|
1598
|
+
return this;
|
|
1599
|
+
}
|
|
1600
|
+
};
|
|
1601
|
+
if (typeof document !== "undefined") {
|
|
1602
|
+
document.addEventListener("DOMContentLoaded", () => Aksara.init());
|
|
1603
|
+
}
|
|
1604
|
+
export default Aksara;
|