what-core 0.14.0 → 0.15.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.
Files changed (145) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +64 -38
  3. package/a11y.d.ts +128 -0
  4. package/data.d.ts +294 -0
  5. package/dist/a11y.js +510 -0
  6. package/dist/a11y.min.js +413 -0
  7. package/dist/actions.js +158 -0
  8. package/dist/actions.min.js +112 -0
  9. package/dist/animation.js +481 -0
  10. package/dist/animation.min.js +295 -0
  11. package/dist/async.js +180 -0
  12. package/dist/async.min.js +134 -0
  13. package/dist/components.js +577 -0
  14. package/dist/components.min.js +413 -0
  15. package/dist/data.js +1212 -0
  16. package/dist/data.min.js +918 -0
  17. package/dist/diagnostics.js +187 -0
  18. package/dist/diagnostics.min.js +134 -0
  19. package/dist/dom.js +796 -0
  20. package/dist/dom.min.js +603 -0
  21. package/dist/entries/a11y.js +43 -0
  22. package/dist/entries/a11y.min.js +43 -0
  23. package/dist/entries/data.js +29 -0
  24. package/dist/entries/data.min.js +29 -0
  25. package/dist/entries/dom.js +45 -0
  26. package/dist/entries/dom.min.js +45 -0
  27. package/dist/entries/form.js +31 -0
  28. package/dist/entries/form.min.js +31 -0
  29. package/dist/entries/internal.js +123 -0
  30. package/dist/entries/internal.min.js +123 -0
  31. package/dist/entries/motion.js +23 -0
  32. package/dist/entries/motion.min.js +23 -0
  33. package/dist/entries/ui.js +25 -0
  34. package/dist/entries/ui.min.js +25 -0
  35. package/dist/env.js +5 -0
  36. package/dist/env.min.js +5 -0
  37. package/dist/errors.js +1518 -0
  38. package/dist/errors.min.js +548 -0
  39. package/dist/form.js +936 -0
  40. package/dist/form.min.js +703 -0
  41. package/dist/guardrails.js +84 -0
  42. package/dist/guardrails.min.js +55 -0
  43. package/dist/h.js +232 -0
  44. package/dist/h.min.js +171 -0
  45. package/dist/head.js +241 -0
  46. package/dist/head.min.js +207 -0
  47. package/dist/helpers.js +126 -0
  48. package/dist/helpers.min.js +98 -0
  49. package/dist/hooks.js +255 -0
  50. package/dist/hooks.min.js +153 -0
  51. package/dist/hot.js +215 -0
  52. package/dist/hot.min.js +24 -0
  53. package/dist/hydration-data.js +495 -0
  54. package/dist/hydration-data.min.js +371 -0
  55. package/dist/import-changes.js +273 -0
  56. package/dist/import-changes.min.js +272 -0
  57. package/dist/index.js +95 -0
  58. package/dist/index.min.js +95 -227
  59. package/dist/jsx-dev-runtime.js +10 -0
  60. package/dist/jsx-dev-runtime.min.js +10 -1
  61. package/dist/jsx-runtime.js +27 -0
  62. package/dist/jsx-runtime.min.js +15 -1
  63. package/dist/live.js +54 -0
  64. package/dist/live.min.js +49 -0
  65. package/dist/markup-rules.js +250 -0
  66. package/dist/markup-rules.min.js +213 -0
  67. package/dist/reactive.js +3334 -0
  68. package/dist/reactive.min.js +2033 -0
  69. package/dist/render.js +4746 -0
  70. package/dist/render.min.js +2797 -1
  71. package/dist/scheduler.js +225 -0
  72. package/dist/scheduler.min.js +162 -0
  73. package/dist/selector.js +11 -0
  74. package/dist/selector.min.js +10 -0
  75. package/dist/server-context.js +148 -0
  76. package/dist/server-context.min.js +121 -0
  77. package/dist/skeleton.js +342 -0
  78. package/dist/skeleton.min.js +310 -0
  79. package/dist/store.js +72 -0
  80. package/dist/store.min.js +50 -0
  81. package/dist/testing.js +720 -0
  82. package/dist/testing.min.js +458 -1
  83. package/dist/vnode-adoption.js +19 -0
  84. package/dist/vnode-adoption.min.js +19 -0
  85. package/dist/vnode-renderer.js +61 -0
  86. package/dist/vnode-renderer.min.js +47 -0
  87. package/dom.d.ts +44 -0
  88. package/form.d.ts +218 -0
  89. package/hot.d.ts +35 -0
  90. package/index.d.ts +421 -736
  91. package/internal.d.ts +284 -0
  92. package/jsx-runtime.d.ts +160 -137
  93. package/markup-rules.d.ts +83 -0
  94. package/motion.d.ts +156 -0
  95. package/package.json +117 -3
  96. package/render.d.ts +278 -29
  97. package/src/a11y.js +64 -59
  98. package/src/actions.js +226 -0
  99. package/src/animation.js +105 -57
  100. package/src/async.js +259 -0
  101. package/src/components.js +495 -144
  102. package/src/data.js +1410 -1159
  103. package/src/diagnostics.js +263 -0
  104. package/src/dom.js +1159 -872
  105. package/src/entries/a11y.js +24 -0
  106. package/src/entries/data.js +17 -0
  107. package/src/entries/dom.js +29 -0
  108. package/src/entries/form.js +16 -0
  109. package/src/entries/internal.js +63 -0
  110. package/src/entries/motion.js +13 -0
  111. package/src/entries/ui.js +13 -0
  112. package/src/env-development.js +12 -0
  113. package/src/env.js +33 -0
  114. package/src/errors.js +1144 -168
  115. package/src/form.js +723 -225
  116. package/src/guardrails.js +38 -126
  117. package/src/h.js +227 -152
  118. package/src/head.js +189 -19
  119. package/src/helpers.js +47 -75
  120. package/src/hooks.js +233 -248
  121. package/src/hot.js +316 -0
  122. package/src/hydration-data.js +859 -16
  123. package/src/import-changes.js +156 -0
  124. package/src/index.js +38 -188
  125. package/src/jsx-dev-runtime.js +7 -14
  126. package/src/jsx-runtime.js +25 -9
  127. package/src/live.js +80 -0
  128. package/src/markup-rules.js +387 -0
  129. package/src/reactive.js +4400 -862
  130. package/src/render.js +6502 -2570
  131. package/src/scheduler.js +56 -21
  132. package/src/selector.js +31 -0
  133. package/src/server-context.js +252 -22
  134. package/src/skeleton.js +22 -12
  135. package/src/store.js +17 -31
  136. package/src/testing.js +389 -63
  137. package/src/vnode-adoption.js +48 -0
  138. package/src/vnode-renderer.js +114 -0
  139. package/testing.d.ts +81 -1
  140. package/ui.d.ts +20 -0
  141. package/dist/chunk-6YOUZ6T4.min.js +0 -1
  142. package/dist/chunk-FN6HXME3.min.js +0 -11
  143. package/dist/chunk-VKZY7JPN.min.js +0 -1
  144. package/src/agent-context.js +0 -127
  145. package/src/warnings.js +0 -193
package/dist/head.js ADDED
@@ -0,0 +1,241 @@
1
+ // src/head.js
2
+ import { getServerContext, isServerRender } from "./server-context.js";
3
+ import { SAFE_ATTR_NAME, escapeHtml } from "./markup-rules.js";
4
+ import { isVNode } from "./vnode-renderer.js";
5
+ import { _headChildren } from "./render.js";
6
+ var headState = {
7
+ title: null,
8
+ metas: /* @__PURE__ */ new Map(),
9
+ links: /* @__PURE__ */ new Map()
10
+ };
11
+ var Head = /* @__PURE__ */ Object.assign(function Head2(props) {
12
+ const { title, meta, link, children } = readHeadChildren(props);
13
+ if (isServerRender()) {
14
+ const ctx = getServerContext();
15
+ if (ctx && ctx.head) writeToSink(ctx.head, { title, meta, link });
16
+ return children ?? null;
17
+ }
18
+ if (title) {
19
+ document.title = title;
20
+ headState.title = title;
21
+ }
22
+ if (meta) {
23
+ for (const attrs of Array.isArray(meta) ? meta : [meta]) {
24
+ const key = attrs.name || attrs.property || attrs.httpEquiv || JSON.stringify(attrs);
25
+ setHeadTag("meta", key, attrs);
26
+ }
27
+ }
28
+ if (link) {
29
+ for (const attrs of Array.isArray(link) ? link : [link]) {
30
+ const key = attrs.rel + (attrs.href || "");
31
+ setHeadTag("link", key, attrs);
32
+ }
33
+ }
34
+ return children || null;
35
+ }, { _deferChildren: true });
36
+ function readHeadChildren({ title, meta, link, children }) {
37
+ const compiled = typeof children === "function" && children._lazyChildren;
38
+ const found = { title, meta: toList(meta), link: toList(link), rest: [] };
39
+ collectHeadChildren(compiled ? _headChildren(children) : children, found);
40
+ const rest = found.rest;
41
+ return { title: found.title, meta: found.meta, link: found.link, children: rest.length > 1 ? rest : rest[0] };
42
+ }
43
+ function toList(value) {
44
+ return value == null ? [] : Array.isArray(value) ? [...value] : [value];
45
+ }
46
+ function collectHeadChildren(child, found) {
47
+ if (child == null || typeof child === "boolean") return;
48
+ if (Array.isArray(child)) {
49
+ for (const c of child) collectHeadChildren(c, found);
50
+ return;
51
+ }
52
+ const tag = readHeadTag(child);
53
+ if (tag === null) found.rest.push(child);
54
+ else if (tag.name === "title") found.title = tag.text;
55
+ else found[tag.name].push(tag.attrs);
56
+ }
57
+ function readHeadTag(child) {
58
+ if (typeof child !== "object") return null;
59
+ if (isServerMarkup(child)) return isHeadTag(child.rootTag()) ? readHeadMarkup(markupText(child.parts(null))) : null;
60
+ if (isVNode(child) && typeof child.tag === "string") {
61
+ if (!isHeadTag(child.tag)) return null;
62
+ const attrs = {};
63
+ for (const [k, v] of Object.entries(child.props || {})) {
64
+ if (k !== "children" && k !== "ref") attrs[propName(k)] = typeof v === "function" ? v() : v;
65
+ }
66
+ return { name: child.tag, attrs, text: textOf(child.children) };
67
+ }
68
+ if (child.nodeType === 1 && isHeadTag(child.localName)) {
69
+ const attrs = {};
70
+ for (const a of child.attributes) attrs[propName(a.name)] = a.value;
71
+ return { name: child.localName, attrs, text: child.textContent };
72
+ }
73
+ return null;
74
+ }
75
+ function isHeadTag(name) {
76
+ return name === "title" || name === "meta" || name === "link";
77
+ }
78
+ function propName(name) {
79
+ return name === "http-equiv" ? "httpEquiv" : name;
80
+ }
81
+ function textOf(value) {
82
+ if (value == null || typeof value === "boolean") return "";
83
+ if (typeof value === "function") return textOf(value());
84
+ if (Array.isArray(value)) return value.map(textOf).join("");
85
+ return String(value);
86
+ }
87
+ var HTML_CHECKS = /* @__PURE__ */ Symbol.for("what-server.html.v3");
88
+ function isServerMarkup(value) {
89
+ const checks = (
90
+ /** @type {any} */
91
+ globalThis[HTML_CHECKS]
92
+ );
93
+ if (checks === void 0) return false;
94
+ for (const is of checks) if (is(value)) return true;
95
+ return false;
96
+ }
97
+ var TITLE_MARKUP = /^<title>([^<]*)<\/title>$/;
98
+ var VOID_MARKUP = /^<(meta|link)((?:\s[^>]*)?)>$/;
99
+ var ATTR_MARKUP = /\s([^\s="'>/]+)(?:="([^"]*)")?/g;
100
+ function markupText(parts) {
101
+ if (typeof parts === "string") return parts;
102
+ if (!Array.isArray(parts)) return null;
103
+ let out = "";
104
+ for (const part of parts) {
105
+ if (typeof part === "string") out += part;
106
+ else if (typeof part === "function") {
107
+ const value = part();
108
+ if (value != null && typeof value === "object") return null;
109
+ out += escapeText(textOf(value));
110
+ } else if (part !== null && typeof part === "object" && typeof part.parts === "function") {
111
+ const text = markupText(part.parts(null));
112
+ if (text === null) return null;
113
+ out += text;
114
+ } else return null;
115
+ }
116
+ return out;
117
+ }
118
+ function escapeText(str) {
119
+ return str.replace(/[&<>"]/g, (c) => c === "&" ? "&amp;" : c === "<" ? "&lt;" : c === ">" ? "&gt;" : "&quot;");
120
+ }
121
+ function readHeadMarkup(markup) {
122
+ if (typeof markup !== "string") return null;
123
+ const title = TITLE_MARKUP.exec(markup);
124
+ if (title) return { name: "title", attrs: {}, text: unescapeHtml(title[1]) };
125
+ const tag = VOID_MARKUP.exec(markup);
126
+ if (!tag) return null;
127
+ const attrs = {};
128
+ for (const [, name, value] of tag[2].matchAll(ATTR_MARKUP)) attrs[propName(name)] = unescapeHtml(value ?? "");
129
+ return { name: tag[1], attrs, text: "" };
130
+ }
131
+ var ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', "#39": "'" };
132
+ function unescapeHtml(str) {
133
+ return str.replace(/&(amp|lt|gt|quot|#39);/g, (_, e) => ENTITIES[e]);
134
+ }
135
+ function metaKey(attrs) {
136
+ return attrs.name || attrs.property || attrs.httpEquiv || JSON.stringify(attrs);
137
+ }
138
+ function writeToSink(sink, { title, meta, link }) {
139
+ if (title != null) sink.title = title;
140
+ if (meta) {
141
+ for (const attrs of Array.isArray(meta) ? meta : [meta]) {
142
+ sink.metas.set(metaKey(attrs), attrs);
143
+ }
144
+ }
145
+ if (link) {
146
+ for (const attrs of Array.isArray(link) ? link : [link]) {
147
+ sink.links.set(attrs.rel + (attrs.href || ""), attrs);
148
+ }
149
+ }
150
+ }
151
+ function beginHeadCollection() {
152
+ return { title: null, metas: /* @__PURE__ */ new Map(), links: /* @__PURE__ */ new Map(), hoisted: (
153
+ /** @type {Map<string, string> | undefined} */
154
+ void 0
155
+ ) };
156
+ }
157
+ function endHeadCollection(sink) {
158
+ if (!sink) return "";
159
+ let out = "";
160
+ if (sink.title != null) out += `<title>${escapeHtml(String(sink.title))}</title>`;
161
+ for (const attrs of sink.metas.values()) out += renderHeadTag("meta", attrs);
162
+ for (const attrs of sink.links.values()) out += renderHeadTag("link", attrs);
163
+ if (sink.hoisted !== void 0) for (const html of sink.hoisted.values()) out += html;
164
+ return out;
165
+ }
166
+ function isSafeAttrName(name) {
167
+ if (!SAFE_ATTR_NAME.test(name)) return false;
168
+ const lower = name.toLowerCase();
169
+ return !(lower.startsWith("on") && lower.length > 2);
170
+ }
171
+ function attrName(key) {
172
+ return key === "httpEquiv" ? "http-equiv" : key;
173
+ }
174
+ function renderHeadTag(tag, attrs) {
175
+ let s = `<${tag}`;
176
+ for (const [k, v] of Object.entries(attrs)) {
177
+ if (v == null || v === false) continue;
178
+ const name = attrName(k);
179
+ if (!isSafeAttrName(name)) continue;
180
+ s += ` ${name}="${escapeHtml(String(v))}"`;
181
+ }
182
+ return s + ">";
183
+ }
184
+ function escapeSelectorValue(key) {
185
+ const s = String(key);
186
+ if (typeof CSS !== "undefined" && typeof CSS.escape === "function") return CSS.escape(s);
187
+ return s.replace(/[^a-zA-Z0-9_-]/g, (c) => `\\${/** @type {number} */
188
+ c.codePointAt(0).toString(16)} `);
189
+ }
190
+ function setHeadTag(tag, key, attrs) {
191
+ const existing = document.head.querySelector(`[data-what-head="${escapeSelectorValue(key)}"]`) || serverTag(tag, attrs);
192
+ if (existing) {
193
+ existing.setAttribute("data-what-head", key);
194
+ updateElement(existing, attrs);
195
+ return;
196
+ }
197
+ const el = document.createElement(tag);
198
+ el.setAttribute("data-what-head", key);
199
+ for (const [k, v] of Object.entries(attrs)) {
200
+ if (!isSafeAttrName(attrName(k))) continue;
201
+ el.setAttribute(attrName(k), v);
202
+ }
203
+ document.head.appendChild(el);
204
+ }
205
+ function serverTag(tag, attrs) {
206
+ for (let el = document.head.firstElementChild; el; el = el.nextElementSibling) {
207
+ if (el.localName === tag && !el.hasAttribute("data-what-head") && sameTag(tag, el, attrs)) return el;
208
+ }
209
+ return null;
210
+ }
211
+ function sameTag(tag, el, attrs) {
212
+ if (tag === "link") {
213
+ return el.getAttribute("rel") === String(attrs.rel) && el.getAttribute("href") === (attrs.href == null ? null : String(attrs.href));
214
+ }
215
+ if (attrs.name) return el.getAttribute("name") === String(attrs.name);
216
+ if (attrs.property) return el.getAttribute("property") === String(attrs.property);
217
+ if (attrs.httpEquiv) return el.getAttribute("http-equiv") === String(attrs.httpEquiv);
218
+ const written = Object.entries(attrs).filter(([k, v]) => v != null && v !== false && isSafeAttrName(attrName(k)));
219
+ return el.attributes.length === written.length && written.every(([k, v]) => el.getAttribute(attrName(k)) === String(v));
220
+ }
221
+ function updateElement(el, attrs) {
222
+ for (const [k, v] of Object.entries(attrs)) {
223
+ const name = attrName(k);
224
+ if (!isSafeAttrName(name)) continue;
225
+ if (el.getAttribute(name) !== v) {
226
+ el.setAttribute(name, v);
227
+ }
228
+ }
229
+ }
230
+ function clearHead() {
231
+ const tags = document.head.querySelectorAll("[data-what-head]");
232
+ for (const tag of tags) tag.remove();
233
+ headState.metas.clear();
234
+ headState.links.clear();
235
+ }
236
+ export {
237
+ Head,
238
+ beginHeadCollection,
239
+ clearHead,
240
+ endHeadCollection
241
+ };
@@ -0,0 +1,207 @@
1
+ // src/head.js
2
+ import { getServerContext as T, isServerRender as x } from "./server-context.min.js";
3
+ import { SAFE_ATTR_NAME as v, escapeHtml as h } from "./markup-rules.min.js";
4
+ import { isVNode as C } from "./vnode-renderer.min.js";
5
+ import { _headChildren as E } from "./render.min.js";
6
+ var p = {
7
+ title: null,
8
+ metas: /* @__PURE__ */ new Map(),
9
+ links: /* @__PURE__ */ new Map()
10
+ }, F = /* @__PURE__ */ Object.assign(function(n) {
11
+ let { title: e, meta: r, link: o, children: i } = H(n);
12
+ if (x()) {
13
+ let u = T();
14
+ return u && u.head && K(u.head, { title: e, meta: r, link: o }), i ?? null;
15
+ }
16
+ if (e && (document.title = e, p.title = e), r)
17
+ for (let u of Array.isArray(r) ? r : [r]) {
18
+ let s = u.name || u.property || u.httpEquiv || JSON.stringify(u);
19
+ A("meta", s, u);
20
+ }
21
+ if (o)
22
+ for (let u of Array.isArray(o) ? o : [o]) {
23
+ let s = u.rel + (u.href || "");
24
+ A("link", s, u);
25
+ }
26
+ return i || null;
27
+ }, { _deferChildren: !0 });
28
+ function H({ title: t, meta: n, link: e, children: r }) {
29
+ let o = typeof r == "function" && r._lazyChildren, i = { title: t, meta: g(n), link: g(e), rest: [] };
30
+ S(o ? E(r) : r, i);
31
+ let u = i.rest;
32
+ return { title: i.title, meta: i.meta, link: i.link, children: u.length > 1 ? u : u[0] };
33
+ }
34
+ function g(t) {
35
+ return t == null ? [] : Array.isArray(t) ? [...t] : [t];
36
+ }
37
+ function S(t, n) {
38
+ if (t == null || typeof t == "boolean") return;
39
+ if (Array.isArray(t)) {
40
+ for (let r of t) S(r, n);
41
+ return;
42
+ }
43
+ let e = q(t);
44
+ e === null ? n.rest.push(t) : e.name === "title" ? n.title = e.text : n[e.name].push(e.attrs);
45
+ }
46
+ function q(t) {
47
+ if (typeof t != "object") return null;
48
+ if (M(t)) return c(t.rootTag()) ? k(b(t.parts(null))) : null;
49
+ if (C(t) && typeof t.tag == "string") {
50
+ if (!c(t.tag)) return null;
51
+ let n = {};
52
+ for (let [e, r] of Object.entries(t.props || {}))
53
+ e !== "children" && e !== "ref" && (n[m(e)] = typeof r == "function" ? r() : r);
54
+ return { name: t.tag, attrs: n, text: a(t.children) };
55
+ }
56
+ if (t.nodeType === 1 && c(t.localName)) {
57
+ let n = {};
58
+ for (let e of t.attributes) n[m(e.name)] = e.value;
59
+ return { name: t.localName, attrs: n, text: t.textContent };
60
+ }
61
+ return null;
62
+ }
63
+ function c(t) {
64
+ return t === "title" || t === "meta" || t === "link";
65
+ }
66
+ function m(t) {
67
+ return t === "http-equiv" ? "httpEquiv" : t;
68
+ }
69
+ function a(t) {
70
+ return t == null || typeof t == "boolean" ? "" : typeof t == "function" ? a(t()) : Array.isArray(t) ? t.map(a).join("") : String(t);
71
+ }
72
+ var w = /* @__PURE__ */ Symbol.for("what-server.html.v3");
73
+ function M(t) {
74
+ let n = (
75
+ /** @type {any} */
76
+ globalThis[w]
77
+ );
78
+ if (n === void 0) return !1;
79
+ for (let e of n) if (e(t)) return !0;
80
+ return !1;
81
+ }
82
+ var N = /^<title>([^<]*)<\/title>$/, _ = /^<(meta|link)((?:\s[^>]*)?)>$/, j = /\s([^\s="'>/]+)(?:="([^"]*)")?/g;
83
+ function b(t) {
84
+ if (typeof t == "string") return t;
85
+ if (!Array.isArray(t)) return null;
86
+ let n = "";
87
+ for (let e of t)
88
+ if (typeof e == "string") n += e;
89
+ else if (typeof e == "function") {
90
+ let r = e();
91
+ if (r != null && typeof r == "object") return null;
92
+ n += O(a(r));
93
+ } else if (e !== null && typeof e == "object" && typeof e.parts == "function") {
94
+ let r = b(e.parts(null));
95
+ if (r === null) return null;
96
+ n += r;
97
+ } else return null;
98
+ return n;
99
+ }
100
+ function O(t) {
101
+ return t.replace(/[&<>"]/g, (n) => n === "&" ? "&amp;" : n === "<" ? "&lt;" : n === ">" ? "&gt;" : "&quot;");
102
+ }
103
+ function k(t) {
104
+ if (typeof t != "string") return null;
105
+ let n = N.exec(t);
106
+ if (n) return { name: "title", attrs: {}, text: d(n[1]) };
107
+ let e = _.exec(t);
108
+ if (!e) return null;
109
+ let r = {};
110
+ for (let [, o, i] of e[2].matchAll(j)) r[m(o)] = d(i ?? "");
111
+ return { name: e[1], attrs: r, text: "" };
112
+ }
113
+ var $ = { amp: "&", lt: "<", gt: ">", quot: '"', "#39": "'" };
114
+ function d(t) {
115
+ return t.replace(/&(amp|lt|gt|quot|#39);/g, (n, e) => $[e]);
116
+ }
117
+ function R(t) {
118
+ return t.name || t.property || t.httpEquiv || JSON.stringify(t);
119
+ }
120
+ function K(t, { title: n, meta: e, link: r }) {
121
+ if (n != null && (t.title = n), e)
122
+ for (let o of Array.isArray(e) ? e : [e])
123
+ t.metas.set(R(o), o);
124
+ if (r)
125
+ for (let o of Array.isArray(r) ? r : [r])
126
+ t.links.set(o.rel + (o.href || ""), o);
127
+ }
128
+ function W() {
129
+ return { title: null, metas: /* @__PURE__ */ new Map(), links: /* @__PURE__ */ new Map(), hoisted: (
130
+ /** @type {Map<string, string> | undefined} */
131
+ void 0
132
+ ) };
133
+ }
134
+ function Z(t) {
135
+ if (!t) return "";
136
+ let n = "";
137
+ t.title != null && (n += `<title>${h(String(t.title))}</title>`);
138
+ for (let e of t.metas.values()) n += y("meta", e);
139
+ for (let e of t.links.values()) n += y("link", e);
140
+ if (t.hoisted !== void 0) for (let e of t.hoisted.values()) n += e;
141
+ return n;
142
+ }
143
+ function l(t) {
144
+ if (!v.test(t)) return !1;
145
+ let n = t.toLowerCase();
146
+ return !(n.startsWith("on") && n.length > 2);
147
+ }
148
+ function f(t) {
149
+ return t === "httpEquiv" ? "http-equiv" : t;
150
+ }
151
+ function y(t, n) {
152
+ let e = `<${t}`;
153
+ for (let [r, o] of Object.entries(n)) {
154
+ if (o == null || o === !1) continue;
155
+ let i = f(r);
156
+ l(i) && (e += ` ${i}="${h(String(o))}"`);
157
+ }
158
+ return e + ">";
159
+ }
160
+ function I(t) {
161
+ let n = String(t);
162
+ return typeof CSS < "u" && typeof CSS.escape == "function" ? CSS.escape(n) : n.replace(/[^a-zA-Z0-9_-]/g, (e) => `\\${/** @type {number} */
163
+ e.codePointAt(0).toString(16)} `);
164
+ }
165
+ function A(t, n, e) {
166
+ let r = document.head.querySelector(`[data-what-head="${I(n)}"]`) || L(t, e);
167
+ if (r) {
168
+ r.setAttribute("data-what-head", n), U(r, e);
169
+ return;
170
+ }
171
+ let o = document.createElement(t);
172
+ o.setAttribute("data-what-head", n);
173
+ for (let [i, u] of Object.entries(e))
174
+ l(f(i)) && o.setAttribute(f(i), u);
175
+ document.head.appendChild(o);
176
+ }
177
+ function L(t, n) {
178
+ for (let e = document.head.firstElementChild; e; e = e.nextElementSibling)
179
+ if (e.localName === t && !e.hasAttribute("data-what-head") && P(t, e, n)) return e;
180
+ return null;
181
+ }
182
+ function P(t, n, e) {
183
+ if (t === "link")
184
+ return n.getAttribute("rel") === String(e.rel) && n.getAttribute("href") === (e.href == null ? null : String(e.href));
185
+ if (e.name) return n.getAttribute("name") === String(e.name);
186
+ if (e.property) return n.getAttribute("property") === String(e.property);
187
+ if (e.httpEquiv) return n.getAttribute("http-equiv") === String(e.httpEquiv);
188
+ let r = Object.entries(e).filter(([o, i]) => i != null && i !== !1 && l(f(o)));
189
+ return n.attributes.length === r.length && r.every(([o, i]) => n.getAttribute(f(o)) === String(i));
190
+ }
191
+ function U(t, n) {
192
+ for (let [e, r] of Object.entries(n)) {
193
+ let o = f(e);
194
+ l(o) && t.getAttribute(o) !== r && t.setAttribute(o, r);
195
+ }
196
+ }
197
+ function B() {
198
+ let t = document.head.querySelectorAll("[data-what-head]");
199
+ for (let n of t) n.remove();
200
+ p.metas.clear(), p.links.clear();
201
+ }
202
+ export {
203
+ F as Head,
204
+ W as beginHeadCollection,
205
+ B as clearHead,
206
+ Z as endHeadCollection
207
+ };
@@ -0,0 +1,126 @@
1
+ // src/helpers.js
2
+ import { signal, effect, onCleanup } from "./reactive.js";
3
+ import { _vnode } from "./h.js";
4
+ import { createPortalDOM } from "./dom.js";
5
+ import { report } from "./diagnostics.js";
6
+ import { styleText } from "./markup-rules.js";
7
+ function cls(...args) {
8
+ const classes = [];
9
+ const add = (arg) => {
10
+ if (!arg) return;
11
+ if (typeof arg === "string") {
12
+ classes.push(arg);
13
+ } else if (Array.isArray(arg)) {
14
+ for (const item of arg) add(item);
15
+ } else if (typeof arg === "object") {
16
+ for (const [key, val] of Object.entries(arg)) {
17
+ if (val) classes.push(key);
18
+ }
19
+ }
20
+ };
21
+ for (const arg of args) add(arg);
22
+ return classes.join(" ");
23
+ }
24
+ function style(obj) {
25
+ if (typeof obj === "string") return obj;
26
+ return styleText(obj);
27
+ }
28
+ function debounce(fn, ms) {
29
+ let timer;
30
+ return (...args) => {
31
+ clearTimeout(timer);
32
+ timer = setTimeout(() => fn(...args), ms);
33
+ };
34
+ }
35
+ function throttle(fn, ms) {
36
+ let last = 0;
37
+ return (...args) => {
38
+ const now = Date.now();
39
+ if (now - last >= ms) {
40
+ last = now;
41
+ fn(...args);
42
+ }
43
+ };
44
+ }
45
+ function useMediaQuery(query) {
46
+ if (typeof window === "undefined") return signal(false);
47
+ const mq = window.matchMedia(query);
48
+ const s = signal(mq.matches);
49
+ const handler = (e) => s.set(e.matches);
50
+ mq.addEventListener("change", handler);
51
+ onCleanup(() => mq.removeEventListener("change", handler));
52
+ return s;
53
+ }
54
+ function useLocalStorage(key, initial) {
55
+ let stored;
56
+ try {
57
+ const raw = localStorage.getItem(key);
58
+ stored = raw !== null ? JSON.parse(raw) : initial;
59
+ } catch {
60
+ stored = initial;
61
+ }
62
+ const s = signal(stored);
63
+ effect(() => {
64
+ try {
65
+ localStorage.setItem(key, JSON.stringify(s()));
66
+ } catch (e) {
67
+ if ((process.env.NODE_ENV !== "production")) report("ERR_STORAGE_FAILED", { data: { key, operation: "write", reason: e?.message || String(e) } });
68
+ }
69
+ });
70
+ let storageHandler = null;
71
+ if (typeof window !== "undefined") {
72
+ storageHandler = (e) => {
73
+ if (e.key === key && e.newValue !== null) {
74
+ try {
75
+ s.set(JSON.parse(e.newValue));
76
+ } catch (err) {
77
+ if ((process.env.NODE_ENV !== "production")) report("ERR_STORAGE_FAILED", { data: { key, operation: "read", reason: err?.message || String(err) } });
78
+ }
79
+ }
80
+ };
81
+ window.addEventListener("storage", storageHandler);
82
+ }
83
+ onCleanup(() => {
84
+ if (storageHandler) window.removeEventListener("storage", storageHandler);
85
+ });
86
+ return s;
87
+ }
88
+ var Portal = /* @__PURE__ */ Object.assign(function Portal2({ target, children }) {
89
+ if (typeof document === "undefined") return null;
90
+ const container = typeof target === "string" ? document.querySelector(target) : target ?? document.body;
91
+ if (!container) {
92
+ if ((process.env.NODE_ENV !== "production") && typeof target === "string") report("ERR_PORTAL_TARGET_MISSING", { data: { selector: target } });
93
+ return null;
94
+ }
95
+ return _vnode("__portal", { container, mount: createPortalDOM }, Array.isArray(children) ? children : [children]);
96
+ }, { _deferChildren: true });
97
+ function useClickOutside(ref, handler) {
98
+ if (typeof document === "undefined") return;
99
+ const listener = (e) => {
100
+ const el = ref && typeof ref === "object" && "current" in ref ? ref.current : ref;
101
+ if (!el || el.contains(e.target)) return;
102
+ handler(e);
103
+ };
104
+ document.addEventListener("mousedown", listener);
105
+ document.addEventListener("touchstart", listener);
106
+ onCleanup(() => {
107
+ document.removeEventListener("mousedown", listener);
108
+ document.removeEventListener("touchstart", listener);
109
+ });
110
+ }
111
+ function transitionClass(name, active) {
112
+ return {
113
+ class: active ? `${name}-enter ${name}-enter-active` : `${name}-leave ${name}-leave-active`
114
+ };
115
+ }
116
+ export {
117
+ Portal,
118
+ cls,
119
+ debounce,
120
+ style,
121
+ throttle,
122
+ transitionClass,
123
+ useClickOutside,
124
+ useLocalStorage,
125
+ useMediaQuery
126
+ };
@@ -0,0 +1,98 @@
1
+ // src/helpers.js
2
+ import { signal as i, effect as a, onCleanup as c } from "./reactive.min.js";
3
+ import { _vnode as u } from "./h.min.js";
4
+ import { createPortalDOM as f } from "./dom.min.js";
5
+ import { styleText as l } from "./markup-rules.min.js";
6
+ function E(...e) {
7
+ let n = [], r = (t) => {
8
+ if (t) {
9
+ if (typeof t == "string")
10
+ n.push(t);
11
+ else if (Array.isArray(t))
12
+ for (let o of t) r(o);
13
+ else if (typeof t == "object")
14
+ for (let [o, s] of Object.entries(t))
15
+ s && n.push(o);
16
+ }
17
+ };
18
+ for (let t of e) r(t);
19
+ return n.join(" ");
20
+ }
21
+ function v(e) {
22
+ return typeof e == "string" ? e : l(e);
23
+ }
24
+ function w(e, n) {
25
+ let r;
26
+ return (...t) => {
27
+ clearTimeout(r), r = setTimeout(() => e(...t), n);
28
+ };
29
+ }
30
+ function g(e, n) {
31
+ let r = 0;
32
+ return (...t) => {
33
+ let o = Date.now();
34
+ o - r >= n && (r = o, e(...t));
35
+ };
36
+ }
37
+ function h(e) {
38
+ if (typeof window > "u") return i(!1);
39
+ let n = window.matchMedia(e), r = i(n.matches), t = (o) => r.set(o.matches);
40
+ return n.addEventListener("change", t), c(() => n.removeEventListener("change", t)), r;
41
+ }
42
+ function S(e, n) {
43
+ let r;
44
+ try {
45
+ let s = localStorage.getItem(e);
46
+ r = s !== null ? JSON.parse(s) : n;
47
+ } catch {
48
+ r = n;
49
+ }
50
+ let t = i(r);
51
+ a(() => {
52
+ try {
53
+ localStorage.setItem(e, JSON.stringify(t()));
54
+ } catch {
55
+ }
56
+ });
57
+ let o = null;
58
+ return typeof window < "u" && (o = (s) => {
59
+ if (s.key === e && s.newValue !== null)
60
+ try {
61
+ t.set(JSON.parse(s.newValue));
62
+ } catch {
63
+ }
64
+ }, window.addEventListener("storage", o)), c(() => {
65
+ o && window.removeEventListener("storage", o);
66
+ }), t;
67
+ }
68
+ var L = /* @__PURE__ */ Object.assign(function({ target: n, children: r }) {
69
+ if (typeof document > "u") return null;
70
+ let t = typeof n == "string" ? document.querySelector(n) : n ?? document.body;
71
+ return t ? u("__portal", { container: t, mount: f }, Array.isArray(r) ? r : [r]) : null;
72
+ }, { _deferChildren: !0 });
73
+ function x(e, n) {
74
+ if (typeof document > "u") return;
75
+ let r = (t) => {
76
+ let o = e && typeof e == "object" && "current" in e ? e.current : e;
77
+ !o || o.contains(t.target) || n(t);
78
+ };
79
+ document.addEventListener("mousedown", r), document.addEventListener("touchstart", r), c(() => {
80
+ document.removeEventListener("mousedown", r), document.removeEventListener("touchstart", r);
81
+ });
82
+ }
83
+ function A(e, n) {
84
+ return {
85
+ class: n ? `${e}-enter ${e}-enter-active` : `${e}-leave ${e}-leave-active`
86
+ };
87
+ }
88
+ export {
89
+ L as Portal,
90
+ E as cls,
91
+ w as debounce,
92
+ v as style,
93
+ g as throttle,
94
+ A as transitionClass,
95
+ x as useClickOutside,
96
+ S as useLocalStorage,
97
+ h as useMediaQuery
98
+ };