@lanrenbang/basecoat-ultra 0.1.7 → 0.2.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.
@@ -1,93 +1,83 @@
1
- const initOTP = (root = document) => {
2
- const containers = root.querySelectorAll(".input-otp");
3
- containers.forEach((container) => {
4
- if (container.hasAttribute("data-initialized")) return;
5
- const inputs = Array.from(container.querySelectorAll("input"));
6
- inputs.forEach((input, index) => {
7
- input.type = "text";
8
- input.inputMode = "text";
9
- input.setAttribute("maxlength", "1");
10
- input.setAttribute("autocomplete", "one-time-code");
11
- input.addEventListener("focus", () => {
12
- input.select();
13
- });
14
- input.addEventListener("input", (e) => {
15
- const val = e.target.value;
16
- if (val === "") {
17
- return;
1
+ const initOTP = (container) => {
2
+ const inputs = Array.from(container.querySelectorAll("input"));
3
+ const getOTPValue = () => inputs.map((input) => input.value).join("");
4
+ const dispatchChange = () => {
5
+ container.dispatchEvent(new CustomEvent("change", {
6
+ bubbles: true,
7
+ detail: { value: getOTPValue() }
8
+ }));
9
+ };
10
+ inputs.forEach((input, index) => {
11
+ input.type = "text";
12
+ input.inputMode = "text";
13
+ input.setAttribute("maxlength", "1");
14
+ input.setAttribute("autocomplete", "one-time-code");
15
+ input.addEventListener("focus", () => {
16
+ input.select();
17
+ });
18
+ input.addEventListener("input", (e) => {
19
+ const val = e.target.value;
20
+ if (val === "") {
21
+ dispatchChange();
22
+ return;
23
+ }
24
+ if (val.length > 1) {
25
+ input.value = val.slice(-1);
26
+ }
27
+ dispatchChange();
28
+ if (input.value.length === 1 && index < inputs.length - 1) {
29
+ inputs[index + 1].focus();
30
+ }
31
+ });
32
+ input.addEventListener("keydown", (e) => {
33
+ if (e.key === "Backspace") {
34
+ e.preventDefault();
35
+ if (input.value !== "") {
36
+ input.value = "";
37
+ dispatchChange();
38
+ } else if (index > 0) {
39
+ inputs[index - 1].focus();
18
40
  }
19
- if (val.length > 1) {
20
- input.value = val.slice(-1);
41
+ } else if (e.key === "ArrowLeft") {
42
+ if (index > 0) {
43
+ e.preventDefault();
44
+ inputs[index - 1].focus();
21
45
  }
22
- if (input.value.length === 1 && index < inputs.length - 1) {
46
+ } else if (e.key === "ArrowRight") {
47
+ if (index < inputs.length - 1) {
48
+ e.preventDefault();
23
49
  inputs[index + 1].focus();
24
50
  }
25
- });
26
- input.addEventListener("keydown", (e) => {
27
- if (e.key === "Backspace") {
28
- e.preventDefault();
29
- if (input.value !== "") {
30
- input.value = "";
31
- } else if (index > 0) {
32
- inputs[index - 1].focus();
33
- }
34
- } else if (e.key === "ArrowLeft") {
35
- if (index > 0) {
36
- e.preventDefault();
37
- inputs[index - 1].focus();
38
- }
39
- } else if (e.key === "ArrowRight") {
40
- if (index < inputs.length - 1) {
41
- e.preventDefault();
42
- inputs[index + 1].focus();
43
- }
44
- } else if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) {
45
- if (input.value.length >= 1 && input.selectionStart === input.selectionEnd) {
46
- input.value = "";
47
- }
51
+ } else if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) {
52
+ if (input.value.length >= 1 && input.selectionStart === input.selectionEnd) {
53
+ input.value = "";
48
54
  }
49
- });
50
- input.addEventListener("paste", (e) => {
51
- e.preventDefault();
52
- const pasteData = (e.clipboardData || window.clipboardData).getData("text");
53
- const chars = pasteData.split("");
54
- let currentIndex = index;
55
- chars.forEach((char) => {
56
- if (currentIndex < inputs.length) {
57
- inputs[currentIndex].value = char;
58
- currentIndex++;
59
- }
60
- });
55
+ }
56
+ });
57
+ input.addEventListener("paste", (e) => {
58
+ e.preventDefault();
59
+ const pasteData = (e.clipboardData || window.clipboardData).getData("text");
60
+ const chars = pasteData.split("");
61
+ let currentIndex = index;
62
+ chars.forEach((char) => {
61
63
  if (currentIndex < inputs.length) {
62
- inputs[currentIndex].focus();
63
- } else {
64
- inputs[inputs.length - 1].focus();
64
+ inputs[currentIndex].value = char;
65
+ currentIndex++;
65
66
  }
66
67
  });
67
- });
68
- container.setAttribute("data-initialized", "true");
69
- });
70
- };
71
- if (typeof window !== "undefined") {
72
- const observer = new MutationObserver((mutations) => {
73
- mutations.forEach((mutation) => {
74
- if (mutation.type === "childList") {
75
- mutation.addedNodes.forEach((node) => {
76
- if (node.nodeType === 1) {
77
- if (node.classList?.contains("input-otp")) {
78
- initOTP(node.parentNode);
79
- } else if (node.querySelector?.(".input-otp")) {
80
- initOTP(node);
81
- }
82
- }
83
- });
68
+ dispatchChange();
69
+ if (currentIndex < inputs.length) {
70
+ inputs[currentIndex].focus();
71
+ } else {
72
+ inputs[inputs.length - 1].focus();
84
73
  }
85
74
  });
86
75
  });
87
- document.addEventListener("DOMContentLoaded", () => {
88
- initOTP();
89
- observer.observe(document.body, { childList: true, subtree: true });
90
- });
76
+ container.dataset.inputOtpInitialized = true;
77
+ container.dispatchEvent(new CustomEvent("basecoat:initialized"));
78
+ };
79
+ if (window.basecoat) {
80
+ window.basecoat.register("input-otp", ".input-otp:not([data-input-otp-initialized])", initOTP);
91
81
  }
92
82
  export {
93
83
  initOTP
@@ -1,38 +1,33 @@
1
- const f = (c = document) => {
2
- c.querySelectorAll(".input-otp").forEach((s) => {
3
- if (s.hasAttribute("data-initialized")) return;
4
- const e = Array.from(s.querySelectorAll("input"));
5
- e.forEach((t, r) => {
6
- t.type = "text", t.inputMode = "text", t.setAttribute("maxlength", "1"), t.setAttribute("autocomplete", "one-time-code"), t.addEventListener("focus", () => {
7
- t.select();
8
- }), t.addEventListener("input", (a) => {
9
- const o = a.target.value;
10
- o !== "" && (o.length > 1 && (t.value = o.slice(-1)), t.value.length === 1 && r < e.length - 1 && e[r + 1].focus());
11
- }), t.addEventListener("keydown", (a) => {
12
- a.key === "Backspace" ? (a.preventDefault(), t.value !== "" ? t.value = "" : r > 0 && e[r - 1].focus()) : a.key === "ArrowLeft" ? r > 0 && (a.preventDefault(), e[r - 1].focus()) : a.key === "ArrowRight" ? r < e.length - 1 && (a.preventDefault(), e[r + 1].focus()) : a.key.length === 1 && !a.ctrlKey && !a.metaKey && !a.altKey && t.value.length >= 1 && t.selectionStart === t.selectionEnd && (t.value = "");
13
- }), t.addEventListener("paste", (a) => {
14
- a.preventDefault();
15
- const n = (a.clipboardData || window.clipboardData).getData("text").split("");
16
- let l = r;
17
- n.forEach((u) => {
18
- l < e.length && (e[l].value = u, l++);
19
- }), l < e.length ? e[l].focus() : e[e.length - 1].focus();
20
- });
21
- }), s.setAttribute("data-initialized", "true");
22
- });
23
- };
24
- if (typeof window < "u") {
25
- const c = new MutationObserver((i) => {
26
- i.forEach((s) => {
27
- s.type === "childList" && s.addedNodes.forEach((e) => {
28
- e.nodeType === 1 && (e.classList?.contains("input-otp") ? f(e.parentNode) : e.querySelector?.(".input-otp") && f(e));
29
- });
1
+ const u = (o) => {
2
+ const a = Array.from(o.querySelectorAll("input")), i = () => a.map((e) => e.value).join(""), c = () => {
3
+ o.dispatchEvent(new CustomEvent("change", {
4
+ bubbles: !0,
5
+ detail: { value: i() }
6
+ }));
7
+ };
8
+ a.forEach((e, l) => {
9
+ e.type = "text", e.inputMode = "text", e.setAttribute("maxlength", "1"), e.setAttribute("autocomplete", "one-time-code"), e.addEventListener("focus", () => {
10
+ e.select();
11
+ }), e.addEventListener("input", (t) => {
12
+ const r = t.target.value;
13
+ if (r === "") {
14
+ c();
15
+ return;
16
+ }
17
+ r.length > 1 && (e.value = r.slice(-1)), c(), e.value.length === 1 && l < a.length - 1 && a[l + 1].focus();
18
+ }), e.addEventListener("keydown", (t) => {
19
+ t.key === "Backspace" ? (t.preventDefault(), e.value !== "" ? (e.value = "", c()) : l > 0 && a[l - 1].focus()) : t.key === "ArrowLeft" ? l > 0 && (t.preventDefault(), a[l - 1].focus()) : t.key === "ArrowRight" ? l < a.length - 1 && (t.preventDefault(), a[l + 1].focus()) : t.key.length === 1 && !t.ctrlKey && !t.metaKey && !t.altKey && e.value.length >= 1 && e.selectionStart === e.selectionEnd && (e.value = "");
20
+ }), e.addEventListener("paste", (t) => {
21
+ t.preventDefault();
22
+ const n = (t.clipboardData || window.clipboardData).getData("text").split("");
23
+ let s = l;
24
+ n.forEach((f) => {
25
+ s < a.length && (a[s].value = f, s++);
26
+ }), c(), s < a.length ? a[s].focus() : a[a.length - 1].focus();
30
27
  });
31
- });
32
- document.addEventListener("DOMContentLoaded", () => {
33
- f(), c.observe(document.body, { childList: !0, subtree: !0 });
34
- });
35
- }
28
+ }), o.dataset.inputOtpInitialized = !0, o.dispatchEvent(new CustomEvent("basecoat:initialized"));
29
+ };
30
+ window.basecoat && window.basecoat.register("input-otp", ".input-otp:not([data-input-otp-initialized])", u);
36
31
  export {
37
- f as initOTP
32
+ u as initOTP
38
33
  };
@@ -0,0 +1,107 @@
1
+ (() => {
2
+ const initPagination = (container) => {
3
+ const prevBtn = container.querySelector("[data-pagination-prev]");
4
+ const nextBtn = container.querySelector("[data-pagination-next]");
5
+ const pageButtons = container.querySelectorAll("[data-pagination-page]");
6
+ if (!prevBtn || !nextBtn || pageButtons.length === 0) {
7
+ console.error("Pagination: Missing required elements (prev, next, or page buttons)", container);
8
+ return;
9
+ }
10
+ let totalPages = parseInt(container.dataset.totalPages, 10) || pageButtons.length;
11
+ let currentPage = 1;
12
+ const initialCurrent = container.querySelector('[aria-current="page"]') || container.querySelector("[data-pagination-page].btn-outline");
13
+ if (initialCurrent) {
14
+ currentPage = parseInt(initialCurrent.dataset.paginationPage, 10) || 1;
15
+ }
16
+ const updateUI = () => {
17
+ if (currentPage <= 1) {
18
+ prevBtn.setAttribute("disabled", "");
19
+ prevBtn.setAttribute("aria-disabled", "true");
20
+ } else {
21
+ prevBtn.removeAttribute("disabled");
22
+ prevBtn.removeAttribute("aria-disabled");
23
+ }
24
+ if (currentPage >= totalPages) {
25
+ nextBtn.setAttribute("disabled", "");
26
+ nextBtn.setAttribute("aria-disabled", "true");
27
+ } else {
28
+ nextBtn.removeAttribute("disabled");
29
+ nextBtn.removeAttribute("aria-disabled");
30
+ }
31
+ pageButtons.forEach((btn) => {
32
+ const page = parseInt(btn.dataset.paginationPage, 10);
33
+ if (page === currentPage) {
34
+ btn.setAttribute("aria-current", "page");
35
+ btn.classList.remove("btn-ghost");
36
+ btn.classList.add("btn-outline");
37
+ } else {
38
+ btn.removeAttribute("aria-current");
39
+ btn.classList.remove("btn-outline");
40
+ btn.classList.add("btn-ghost");
41
+ }
42
+ });
43
+ };
44
+ const goToPage = (page, triggerEvent = true) => {
45
+ if (page < 1 || page > totalPages || page === currentPage) return;
46
+ const oldPage = currentPage;
47
+ currentPage = page;
48
+ updateUI();
49
+ if (triggerEvent) {
50
+ container.dispatchEvent(new CustomEvent("change", {
51
+ bubbles: true,
52
+ detail: {
53
+ page: currentPage,
54
+ previousPage: oldPage,
55
+ totalPages
56
+ }
57
+ }));
58
+ }
59
+ };
60
+ prevBtn.addEventListener("click", () => {
61
+ goToPage(currentPage - 1);
62
+ });
63
+ nextBtn.addEventListener("click", () => {
64
+ goToPage(currentPage + 1);
65
+ });
66
+ pageButtons.forEach((btn) => {
67
+ btn.addEventListener("click", () => {
68
+ const page = parseInt(btn.dataset.paginationPage, 10);
69
+ if (!isNaN(page)) {
70
+ goToPage(page);
71
+ }
72
+ });
73
+ });
74
+ container.addEventListener("keydown", (e) => {
75
+ if (e.key === "ArrowLeft" || e.key === "ArrowUp") {
76
+ e.preventDefault();
77
+ goToPage(currentPage - 1);
78
+ } else if (e.key === "ArrowRight" || e.key === "ArrowDown") {
79
+ e.preventDefault();
80
+ goToPage(currentPage + 1);
81
+ } else if (e.key === "Home") {
82
+ e.preventDefault();
83
+ goToPage(1);
84
+ } else if (e.key === "End") {
85
+ e.preventDefault();
86
+ goToPage(totalPages);
87
+ }
88
+ });
89
+ updateUI();
90
+ container.goToPage = (page) => goToPage(page);
91
+ container.getCurrentPage = () => currentPage;
92
+ container.getTotalPages = () => totalPages;
93
+ container.setTotalPages = (total) => {
94
+ totalPages = total;
95
+ if (currentPage > totalPages) {
96
+ goToPage(totalPages);
97
+ } else {
98
+ updateUI();
99
+ }
100
+ };
101
+ container.dataset.paginationInitialized = true;
102
+ container.dispatchEvent(new CustomEvent("basecoat:initialized"));
103
+ };
104
+ if (window.basecoat) {
105
+ window.basecoat.register("pagination", "nav.pagination:not([data-pagination-initialized])", initPagination);
106
+ }
107
+ })();
@@ -0,0 +1,43 @@
1
+ (() => {
2
+ const u = (t) => {
3
+ const r = t.querySelector("[data-pagination-prev]"), n = t.querySelector("[data-pagination-next]"), l = t.querySelectorAll("[data-pagination-page]");
4
+ if (!r || !n || l.length === 0) {
5
+ console.error("Pagination: Missing required elements (prev, next, or page buttons)", t);
6
+ return;
7
+ }
8
+ let s = parseInt(t.dataset.totalPages, 10) || l.length, a = 1;
9
+ const g = t.querySelector('[aria-current="page"]') || t.querySelector("[data-pagination-page].btn-outline");
10
+ g && (a = parseInt(g.dataset.paginationPage, 10) || 1);
11
+ const d = () => {
12
+ a <= 1 ? (r.setAttribute("disabled", ""), r.setAttribute("aria-disabled", "true")) : (r.removeAttribute("disabled"), r.removeAttribute("aria-disabled")), a >= s ? (n.setAttribute("disabled", ""), n.setAttribute("aria-disabled", "true")) : (n.removeAttribute("disabled"), n.removeAttribute("aria-disabled")), l.forEach((e) => {
13
+ parseInt(e.dataset.paginationPage, 10) === a ? (e.setAttribute("aria-current", "page"), e.classList.remove("btn-ghost"), e.classList.add("btn-outline")) : (e.removeAttribute("aria-current"), e.classList.remove("btn-outline"), e.classList.add("btn-ghost"));
14
+ });
15
+ }, i = (e, o = !0) => {
16
+ if (e < 1 || e > s || e === a) return;
17
+ const p = a;
18
+ a = e, d(), o && t.dispatchEvent(new CustomEvent("change", {
19
+ bubbles: !0,
20
+ detail: {
21
+ page: a,
22
+ previousPage: p,
23
+ totalPages: s
24
+ }
25
+ }));
26
+ };
27
+ r.addEventListener("click", () => {
28
+ i(a - 1);
29
+ }), n.addEventListener("click", () => {
30
+ i(a + 1);
31
+ }), l.forEach((e) => {
32
+ e.addEventListener("click", () => {
33
+ const o = parseInt(e.dataset.paginationPage, 10);
34
+ isNaN(o) || i(o);
35
+ });
36
+ }), t.addEventListener("keydown", (e) => {
37
+ e.key === "ArrowLeft" || e.key === "ArrowUp" ? (e.preventDefault(), i(a - 1)) : e.key === "ArrowRight" || e.key === "ArrowDown" ? (e.preventDefault(), i(a + 1)) : e.key === "Home" ? (e.preventDefault(), i(1)) : e.key === "End" && (e.preventDefault(), i(s));
38
+ }), d(), t.goToPage = (e) => i(e), t.getCurrentPage = () => a, t.getTotalPages = () => s, t.setTotalPages = (e) => {
39
+ s = e, a > s ? i(s) : d();
40
+ }, t.dataset.paginationInitialized = !0, t.dispatchEvent(new CustomEvent("basecoat:initialized"));
41
+ };
42
+ window.basecoat && window.basecoat.register("pagination", "nav.pagination:not([data-pagination-initialized])", u);
43
+ })();
@@ -1,6 +1,6 @@
1
1
  var global = typeof window !== "undefined" ? window : null;
2
2
  var ssr = global === null;
3
- var document$1 = !ssr ? global.document : void 0;
3
+ var document = !ssr ? global.document : void 0;
4
4
  var addEventListener = "addEventListener";
5
5
  var removeEventListener = "removeEventListener";
6
6
  var getBoundingClientRect = "getBoundingClientRect";
@@ -12,7 +12,7 @@ var NOOP = function() {
12
12
  return false;
13
13
  };
14
14
  var calc = ssr ? "calc" : ["", "-webkit-", "-moz-", "-o-"].filter(function(prefix) {
15
- var el = document$1.createElement("div");
15
+ var el = document.createElement("div");
16
16
  el.style.cssText = "width:" + prefix + "calc(9px)";
17
17
  return !!el.style.length;
18
18
  }).shift() + "calc";
@@ -21,7 +21,7 @@ var isString = function(v) {
21
21
  };
22
22
  var elementOrSelector = function(el) {
23
23
  if (isString(el)) {
24
- var ele = document$1.querySelector(el);
24
+ var ele = document.querySelector(el);
25
25
  if (!ele) {
26
26
  throw new Error("Selector " + el + " did not match a DOM element");
27
27
  }
@@ -55,7 +55,7 @@ var getGutterSize = function(gutterSize, isFirst, isLast, gutterAlign) {
55
55
  return gutterSize;
56
56
  };
57
57
  var defaultGutterFn = function(i, gutterDirection) {
58
- var gut = document$1.createElement("div");
58
+ var gut = document.createElement("div");
59
59
  gut.className = "gutter gutter-" + gutterDirection;
60
60
  return gut;
61
61
  };
@@ -294,7 +294,7 @@ var Split = function(idsOption, options) {
294
294
  b.style.pointerEvents = "";
295
295
  self.gutter.style.cursor = "";
296
296
  self.parent.style.cursor = "";
297
- document$1.body.style.cursor = "";
297
+ document.body.style.cursor = "";
298
298
  }
299
299
  function startDragging(e) {
300
300
  if ("button" in e && e.button !== 0) {
@@ -329,7 +329,7 @@ var Split = function(idsOption, options) {
329
329
  b.style.pointerEvents = "none";
330
330
  self.gutter.style.cursor = cursor;
331
331
  self.parent.style.cursor = cursor;
332
- document$1.body.style.cursor = cursor;
332
+ document.body.style.cursor = cursor;
333
333
  calculateSizes.call(self);
334
334
  self.dragOffset = getMousePosition(e) - self.end;
335
335
  }
@@ -471,29 +471,33 @@ var Split = function(idsOption, options) {
471
471
  pairs
472
472
  };
473
473
  };
474
- const initResizable = (root = document) => {
475
- const groups = root.querySelectorAll(".resizable-group");
476
- groups.forEach((group) => {
477
- if (group.dataset.splitInitialized) return;
478
- const direction = group.dataset.direction || "horizontal";
479
- const children = Array.from(group.children).filter((el) => el.tagName === "DIV" || el.tagName === "SECTION" || el.tagName === "ASIDE");
480
- if (children.length < 2) return;
481
- let sizes;
482
- if (group.dataset.sizes) {
483
- sizes = group.dataset.sizes.split(",").map(Number);
484
- }
485
- let minSizes;
486
- if (group.dataset.minSizes) {
487
- minSizes = group.dataset.minSizes.split(",").map(Number);
488
- }
474
+ const initResizable = (group) => {
475
+ const direction = group.dataset.direction || "horizontal";
476
+ const children = Array.from(group.children).filter(
477
+ (el) => (el.tagName === "DIV" || el.tagName === "SECTION" || el.tagName === "ASIDE") && !el.classList.contains("gutter")
478
+ );
479
+ if (children.length < 2) return;
480
+ let sizes;
481
+ if (group.dataset.sizes) {
482
+ sizes = group.dataset.sizes.split(",").map(Number);
483
+ }
484
+ let minSizes;
485
+ if (group.dataset.minSizes) {
486
+ minSizes = group.dataset.minSizes.split(",").map(Number);
487
+ }
488
+ try {
489
489
  Split(children, {
490
490
  sizes: sizes || void 0,
491
- // Default is equal
492
491
  minSize: minSizes || 100,
493
492
  direction,
494
- gutterSize: 5,
495
- // 5px gutter
493
+ gutterSize: 1,
496
494
  cursor: direction === "horizontal" ? "col-resize" : "row-resize",
495
+ onDragEnd: (newSizes) => {
496
+ group.dispatchEvent(new CustomEvent("change", {
497
+ bubbles: true,
498
+ detail: { value: newSizes }
499
+ }));
500
+ },
497
501
  elementStyle: (dimension, size, gutterSize) => {
498
502
  return {
499
503
  "flex-basis": `calc(${size}% - ${gutterSize}px)`
@@ -505,29 +509,14 @@ const initResizable = (root = document) => {
505
509
  };
506
510
  }
507
511
  });
508
- group.dataset.splitInitialized = "true";
509
- });
512
+ } catch (e) {
513
+ console.error("Basecoat: Resizable initialization failed", e);
514
+ }
515
+ group.dataset.resizableInitialized = true;
516
+ group.dispatchEvent(new CustomEvent("basecoat:initialized"));
510
517
  };
511
- if (typeof window !== "undefined") {
512
- const observer = new MutationObserver((mutations) => {
513
- mutations.forEach((mutation) => {
514
- if (mutation.type === "childList") {
515
- mutation.addedNodes.forEach((node) => {
516
- if (node.nodeType === 1) {
517
- if (node.classList?.contains("resizable-group")) {
518
- initResizable(node.parentNode);
519
- } else if (node.querySelector?.(".resizable-group")) {
520
- initResizable(node);
521
- }
522
- }
523
- });
524
- }
525
- });
526
- });
527
- document.addEventListener("DOMContentLoaded", () => {
528
- initResizable();
529
- observer.observe(document.body, { childList: true, subtree: true });
530
- });
518
+ if (window.basecoat) {
519
+ window.basecoat.register("resizable", ".resizable-group:not([data-resizable-initialized])", initResizable);
531
520
  }
532
521
  export {
533
522
  initResizable