what-core 0.13.11 → 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.
- package/LICENSE +21 -0
- package/README.md +64 -38
- package/a11y.d.ts +128 -0
- package/data.d.ts +294 -0
- package/dist/a11y.js +510 -0
- package/dist/a11y.min.js +413 -0
- package/dist/actions.js +158 -0
- package/dist/actions.min.js +112 -0
- package/dist/animation.js +481 -0
- package/dist/animation.min.js +295 -0
- package/dist/async.js +180 -0
- package/dist/async.min.js +134 -0
- package/dist/components.js +577 -0
- package/dist/components.min.js +413 -0
- package/dist/data.js +1212 -0
- package/dist/data.min.js +918 -0
- package/dist/diagnostics.js +187 -0
- package/dist/diagnostics.min.js +134 -0
- package/dist/dom.js +796 -0
- package/dist/dom.min.js +603 -0
- package/dist/entries/a11y.js +43 -0
- package/dist/entries/a11y.min.js +43 -0
- package/dist/entries/data.js +29 -0
- package/dist/entries/data.min.js +29 -0
- package/dist/entries/dom.js +45 -0
- package/dist/entries/dom.min.js +45 -0
- package/dist/entries/form.js +31 -0
- package/dist/entries/form.min.js +31 -0
- package/dist/entries/internal.js +123 -0
- package/dist/entries/internal.min.js +123 -0
- package/dist/entries/motion.js +23 -0
- package/dist/entries/motion.min.js +23 -0
- package/dist/entries/ui.js +25 -0
- package/dist/entries/ui.min.js +25 -0
- package/dist/env.js +5 -0
- package/dist/env.min.js +5 -0
- package/dist/errors.js +1518 -0
- package/dist/errors.min.js +548 -0
- package/dist/form.js +936 -0
- package/dist/form.min.js +703 -0
- package/dist/guardrails.js +84 -0
- package/dist/guardrails.min.js +55 -0
- package/dist/h.js +232 -0
- package/dist/h.min.js +171 -0
- package/dist/head.js +241 -0
- package/dist/head.min.js +207 -0
- package/dist/helpers.js +126 -0
- package/dist/helpers.min.js +98 -0
- package/dist/hooks.js +255 -0
- package/dist/hooks.min.js +153 -0
- package/dist/hot.js +215 -0
- package/dist/hot.min.js +24 -0
- package/dist/hydration-data.js +495 -0
- package/dist/hydration-data.min.js +371 -0
- package/dist/import-changes.js +273 -0
- package/dist/import-changes.min.js +272 -0
- package/dist/index.js +95 -0
- package/dist/index.min.js +95 -227
- package/dist/jsx-dev-runtime.js +10 -0
- package/dist/jsx-dev-runtime.min.js +10 -1
- package/dist/jsx-runtime.js +27 -0
- package/dist/jsx-runtime.min.js +15 -1
- package/dist/live.js +54 -0
- package/dist/live.min.js +49 -0
- package/dist/markup-rules.js +250 -0
- package/dist/markup-rules.min.js +213 -0
- package/dist/reactive.js +3334 -0
- package/dist/reactive.min.js +2033 -0
- package/dist/render.js +4746 -0
- package/dist/render.min.js +2797 -1
- package/dist/scheduler.js +225 -0
- package/dist/scheduler.min.js +162 -0
- package/dist/selector.js +11 -0
- package/dist/selector.min.js +10 -0
- package/dist/server-context.js +148 -0
- package/dist/server-context.min.js +121 -0
- package/dist/skeleton.js +342 -0
- package/dist/skeleton.min.js +310 -0
- package/dist/store.js +72 -0
- package/dist/store.min.js +50 -0
- package/dist/testing.js +720 -0
- package/dist/testing.min.js +458 -1
- package/dist/vnode-adoption.js +19 -0
- package/dist/vnode-adoption.min.js +19 -0
- package/dist/vnode-renderer.js +61 -0
- package/dist/vnode-renderer.min.js +47 -0
- package/dom.d.ts +44 -0
- package/form.d.ts +218 -0
- package/hot.d.ts +35 -0
- package/index.d.ts +421 -736
- package/internal.d.ts +284 -0
- package/jsx-runtime.d.ts +160 -137
- package/markup-rules.d.ts +83 -0
- package/motion.d.ts +156 -0
- package/package.json +117 -3
- package/render.d.ts +278 -29
- package/src/a11y.js +64 -59
- package/src/actions.js +226 -0
- package/src/animation.js +105 -57
- package/src/async.js +259 -0
- package/src/components.js +495 -144
- package/src/data.js +1410 -1159
- package/src/diagnostics.js +263 -0
- package/src/dom.js +1159 -872
- package/src/entries/a11y.js +24 -0
- package/src/entries/data.js +17 -0
- package/src/entries/dom.js +29 -0
- package/src/entries/form.js +16 -0
- package/src/entries/internal.js +63 -0
- package/src/entries/motion.js +13 -0
- package/src/entries/ui.js +13 -0
- package/src/env-development.js +12 -0
- package/src/env.js +33 -0
- package/src/errors.js +1144 -168
- package/src/form.js +723 -225
- package/src/guardrails.js +38 -126
- package/src/h.js +227 -152
- package/src/head.js +189 -19
- package/src/helpers.js +47 -75
- package/src/hooks.js +233 -248
- package/src/hot.js +316 -0
- package/src/hydration-data.js +859 -16
- package/src/import-changes.js +156 -0
- package/src/index.js +38 -188
- package/src/jsx-dev-runtime.js +7 -14
- package/src/jsx-runtime.js +25 -9
- package/src/live.js +80 -0
- package/src/markup-rules.js +387 -0
- package/src/reactive.js +4400 -862
- package/src/render.js +6502 -2570
- package/src/scheduler.js +56 -21
- package/src/selector.js +31 -0
- package/src/server-context.js +252 -22
- package/src/skeleton.js +22 -12
- package/src/store.js +17 -31
- package/src/testing.js +389 -63
- package/src/vnode-adoption.js +48 -0
- package/src/vnode-renderer.js +114 -0
- package/testing.d.ts +81 -1
- package/ui.d.ts +20 -0
- package/dist/chunk-6YOUZ6T4.min.js +0 -1
- package/dist/chunk-FN6HXME3.min.js +0 -11
- package/dist/chunk-VKZY7JPN.min.js +0 -1
- package/src/agent-context.js +0 -127
- package/src/warnings.js +0 -193
package/src/head.js
CHANGED
|
@@ -8,7 +8,10 @@
|
|
|
8
8
|
// The server and client use IDENTICAL dedup keys so the rendered head matches
|
|
9
9
|
// what the client would produce — no hydration mismatch.
|
|
10
10
|
|
|
11
|
-
import { getServerContext } from './server-context.js';
|
|
11
|
+
import { getServerContext, isServerRender } from './server-context.js';
|
|
12
|
+
import { SAFE_ATTR_NAME, escapeHtml } from './markup-rules.js';
|
|
13
|
+
import { isVNode } from './vnode-renderer.js';
|
|
14
|
+
import { _headChildren } from './render.js';
|
|
12
15
|
|
|
13
16
|
const headState = {
|
|
14
17
|
title: null,
|
|
@@ -18,9 +21,19 @@ const headState = {
|
|
|
18
21
|
|
|
19
22
|
// --- Head component ---
|
|
20
23
|
// Use in any component to set head tags. Last one wins for title/meta.
|
|
24
|
+
// Compiled children arrive unbuilt (`_deferChildren`, dom.js
|
|
25
|
+
// _installLazyChildren) and Head builds them where it stands: while
|
|
26
|
+
// hydrating, any other child claims the server's nodes there, and a <title>,
|
|
27
|
+
// <meta> or <link> child, which is never rendered where it stands, is built
|
|
28
|
+
// on the client (readHeadChildren, render.js _headChildren).
|
|
21
29
|
|
|
22
|
-
export
|
|
23
|
-
|
|
30
|
+
export const Head = /* @__PURE__ */ Object.assign(function Head(props) {
|
|
31
|
+
const { title, meta, link, children } = readHeadChildren(props);
|
|
32
|
+
// Decided by the render scope, not by `typeof document`: under a DOM shim
|
|
33
|
+
// (jsdom, happy-dom, a Workers polyfill) a server render would otherwise
|
|
34
|
+
// write one request's title into the process-wide document and send a page
|
|
35
|
+
// with no <title>.
|
|
36
|
+
if (isServerRender()) {
|
|
24
37
|
// Server: collect into the active render context's sink (if a render is
|
|
25
38
|
// collecting head). No active context => no-op (renderToString body-only).
|
|
26
39
|
const ctx = getServerContext();
|
|
@@ -48,6 +61,143 @@ export function Head({ title, meta, link, children }) {
|
|
|
48
61
|
}
|
|
49
62
|
|
|
50
63
|
return children || null;
|
|
64
|
+
}, { _deferChildren: true });
|
|
65
|
+
|
|
66
|
+
// --- Head children ---
|
|
67
|
+
// <title>, <meta> and <link> children are the props they spell, as React 19
|
|
68
|
+
// hoists the same three: <Head><title>Hi</title></Head> sets the title, and a
|
|
69
|
+
// child shares the props' dedup keys (later wins, so a child beats a prop of
|
|
70
|
+
// the same Head). Any other child renders in place. A child is an h() or
|
|
71
|
+
// JSX-runtime vnode, a DOM element (compiled JSX), or in a server render the
|
|
72
|
+
// markup of what-compiler's server target (what-server's ssr-runtime).
|
|
73
|
+
// Compiled children come as their factory, built where Head stands.
|
|
74
|
+
|
|
75
|
+
function readHeadChildren({ title, meta, link, children }) {
|
|
76
|
+
const compiled = typeof children === 'function' && children._lazyChildren;
|
|
77
|
+
const found = { title, meta: toList(meta), link: toList(link), rest: [] };
|
|
78
|
+
collectHeadChildren(compiled ? _headChildren(children) : children, found);
|
|
79
|
+
const rest = found.rest;
|
|
80
|
+
return { title: found.title, meta: found.meta, link: found.link, children: rest.length > 1 ? rest : rest[0] };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function toList(value) {
|
|
84
|
+
return value == null ? [] : Array.isArray(value) ? [...value] : [value];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function collectHeadChildren(child, found) {
|
|
88
|
+
if (child == null || typeof child === 'boolean') return;
|
|
89
|
+
if (Array.isArray(child)) {
|
|
90
|
+
for (const c of child) collectHeadChildren(c, found);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const tag = readHeadTag(child);
|
|
94
|
+
if (tag === null) found.rest.push(child);
|
|
95
|
+
else if (tag.name === 'title') found.title = tag.text;
|
|
96
|
+
else found[tag.name].push(tag.attrs);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** @returns {{ name: string, attrs: Record<string, any>, text: string } | null} */
|
|
100
|
+
function readHeadTag(child) {
|
|
101
|
+
if (typeof child !== 'object') return null;
|
|
102
|
+
// Only markup that opens with a head tag is rendered to be read: any other
|
|
103
|
+
// child renders where it stands, once (its attributes and holes are
|
|
104
|
+
// evaluated when it is rendered).
|
|
105
|
+
if (isServerMarkup(child)) return isHeadTag(child.rootTag()) ? readHeadMarkup(markupText(child.parts(null))) : null;
|
|
106
|
+
if (isVNode(child) && typeof child.tag === 'string') {
|
|
107
|
+
if (!isHeadTag(child.tag)) return null;
|
|
108
|
+
const attrs = {};
|
|
109
|
+
for (const [k, v] of Object.entries(child.props || {})) {
|
|
110
|
+
if (k !== 'children' && k !== 'ref') attrs[propName(k)] = typeof v === 'function' ? v() : v;
|
|
111
|
+
}
|
|
112
|
+
return { name: child.tag, attrs, text: textOf(child.children) };
|
|
113
|
+
}
|
|
114
|
+
if (child.nodeType === 1 && isHeadTag(child.localName)) {
|
|
115
|
+
const attrs = {};
|
|
116
|
+
for (const a of child.attributes) attrs[propName(a.name)] = a.value;
|
|
117
|
+
return { name: child.localName, attrs, text: child.textContent };
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function isHeadTag(name) {
|
|
123
|
+
return name === 'title' || name === 'meta' || name === 'link';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// The props spell http-equiv as httpEquiv, and metaKey() reads that spelling.
|
|
127
|
+
function propName(name) {
|
|
128
|
+
return name === 'http-equiv' ? 'httpEquiv' : name;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function textOf(value) {
|
|
132
|
+
if (value == null || typeof value === 'boolean') return '';
|
|
133
|
+
if (typeof value === 'function') return textOf(value());
|
|
134
|
+
if (Array.isArray(value)) return value.map(textOf).join('');
|
|
135
|
+
return String(value);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Markup is compiled JSX only when one of what-server's brand checks
|
|
139
|
+
// recognises it (packages/server/src/compiled.js keeps them in this
|
|
140
|
+
// process-wide set), so an object that merely looks like markup is a child.
|
|
141
|
+
const HTML_CHECKS = Symbol.for('what-server.html.v3');
|
|
142
|
+
|
|
143
|
+
function isServerMarkup(value) {
|
|
144
|
+
const checks = /** @type {any} */ (globalThis)[HTML_CHECKS];
|
|
145
|
+
if (checks === undefined) return false;
|
|
146
|
+
for (const is of checks) if (is(value)) return true;
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// The server target compiles <title>, <meta> and <link> to markup it escaped
|
|
151
|
+
// and quoted itself, so one tag with double-quoted attributes is all there is
|
|
152
|
+
// to read. Its parts are one string unless it holds something rendered on
|
|
153
|
+
// placement (a reactive child, a component), which renders in place.
|
|
154
|
+
const TITLE_MARKUP = /^<title>([^<]*)<\/title>$/;
|
|
155
|
+
const VOID_MARKUP = /^<(meta|link)((?:\s[^>]*)?)>$/;
|
|
156
|
+
const ATTR_MARKUP = /\s([^\s="'>/]+)(?:="([^"]*)")?/g;
|
|
157
|
+
|
|
158
|
+
// A head tag's parts as one string: a hole that reads a value (a signal, a
|
|
159
|
+
// live prop: `<title>{props.title}</title>`, `content={'about ' + name()}`)
|
|
160
|
+
// is a part the server evaluates where it places it, so it is evaluated here,
|
|
161
|
+
// once, as that render would. A part rendered on placement that is not text
|
|
162
|
+
// (a component, an element) leaves the tag to render in place (null).
|
|
163
|
+
function markupText(parts) {
|
|
164
|
+
if (typeof parts === 'string') return parts;
|
|
165
|
+
if (!Array.isArray(parts)) return null;
|
|
166
|
+
let out = '';
|
|
167
|
+
for (const part of parts) {
|
|
168
|
+
if (typeof part === 'string') out += part;
|
|
169
|
+
else if (typeof part === 'function') {
|
|
170
|
+
const value = part();
|
|
171
|
+
if (value != null && typeof value === 'object') return null;
|
|
172
|
+
out += escapeText(textOf(value));
|
|
173
|
+
} else if (part !== null && typeof part === 'object' && typeof part.parts === 'function') {
|
|
174
|
+
const text = markupText(part.parts(null));
|
|
175
|
+
if (text === null) return null;
|
|
176
|
+
out += text;
|
|
177
|
+
} else return null;
|
|
178
|
+
}
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function escapeText(str) {
|
|
183
|
+
return str.replace(/[&<>"]/g, (c) => (c === '&' ? '&' : c === '<' ? '<' : c === '>' ? '>' : '"'));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function readHeadMarkup(markup) {
|
|
187
|
+
if (typeof markup !== 'string') return null;
|
|
188
|
+
const title = TITLE_MARKUP.exec(markup);
|
|
189
|
+
if (title) return { name: 'title', attrs: {}, text: unescapeHtml(title[1]) };
|
|
190
|
+
const tag = VOID_MARKUP.exec(markup);
|
|
191
|
+
if (!tag) return null;
|
|
192
|
+
const attrs = {};
|
|
193
|
+
for (const [, name, value] of tag[2].matchAll(ATTR_MARKUP)) attrs[propName(name)] = unescapeHtml(value ?? '');
|
|
194
|
+
return { name: tag[1], attrs, text: '' };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', '#39': "'" };
|
|
198
|
+
|
|
199
|
+
function unescapeHtml(str) {
|
|
200
|
+
return str.replace(/&(amp|lt|gt|quot|#39);/g, (_, e) => ENTITIES[e]);
|
|
51
201
|
}
|
|
52
202
|
|
|
53
203
|
// --- Server-side head collection ---
|
|
@@ -72,7 +222,7 @@ function writeToSink(sink, { title, meta, link }) {
|
|
|
72
222
|
|
|
73
223
|
/** Create a fresh head sink for one render. */
|
|
74
224
|
export function beginHeadCollection() {
|
|
75
|
-
return { title: null, metas: new Map(), links: new Map() };
|
|
225
|
+
return { title: null, metas: new Map(), links: new Map(), hoisted: /** @type {Map<string, string> | undefined} */ (undefined) };
|
|
76
226
|
}
|
|
77
227
|
|
|
78
228
|
/** Serialize a head sink into escaped <head> HTML (title + meta + link). */
|
|
@@ -82,16 +232,15 @@ export function endHeadCollection(sink) {
|
|
|
82
232
|
if (sink.title != null) out += `<title>${escapeHtml(String(sink.title))}</title>`;
|
|
83
233
|
for (const attrs of sink.metas.values()) out += renderHeadTag('meta', attrs);
|
|
84
234
|
for (const attrs of sink.links.values()) out += renderHeadTag('link', attrs);
|
|
235
|
+
if (sink.hoisted !== undefined) for (const html of sink.hoisted.values()) out += html;
|
|
85
236
|
return out;
|
|
86
237
|
}
|
|
87
238
|
|
|
88
239
|
// Head attributes usually come from CMS/SEO data, so attribute NAMES must be
|
|
89
240
|
// validated, not merely escaped: a key of `x" onload="alert(1)` would otherwise
|
|
90
|
-
// break out of the quoted value and inject a live handler.
|
|
91
|
-
//
|
|
92
|
-
//
|
|
93
|
-
const SAFE_ATTR_NAME = /^[a-zA-Z_:][a-zA-Z0-9:._-]*$/;
|
|
94
|
-
|
|
241
|
+
// break out of the quoted value and inject a live handler. what-server's name
|
|
242
|
+
// rule, plus a case-insensitive `on*` refusal so `onLoad` cannot slip past an
|
|
243
|
+
// uppercase-tolerant name pattern.
|
|
95
244
|
function isSafeAttrName(name) {
|
|
96
245
|
if (!SAFE_ATTR_NAME.test(name)) return false;
|
|
97
246
|
const lower = name.toLowerCase();
|
|
@@ -113,15 +262,6 @@ function renderHeadTag(tag, attrs) {
|
|
|
113
262
|
return s + '>';
|
|
114
263
|
}
|
|
115
264
|
|
|
116
|
-
function escapeHtml(str) {
|
|
117
|
-
return str
|
|
118
|
-
.replace(/&/g, '&')
|
|
119
|
-
.replace(/</g, '<')
|
|
120
|
-
.replace(/>/g, '>')
|
|
121
|
-
.replace(/"/g, '"')
|
|
122
|
-
.replace(/'/g, ''');
|
|
123
|
-
}
|
|
124
|
-
|
|
125
265
|
// --- Client DOM helpers ---
|
|
126
266
|
|
|
127
267
|
// Dedup keys come from user-supplied attrs (and fall back to JSON.stringify,
|
|
@@ -135,8 +275,10 @@ function escapeSelectorValue(key) {
|
|
|
135
275
|
}
|
|
136
276
|
|
|
137
277
|
function setHeadTag(tag, key, attrs) {
|
|
138
|
-
const existing = document.head.querySelector(`[data-what-head="${escapeSelectorValue(key)}"]`)
|
|
278
|
+
const existing = document.head.querySelector(`[data-what-head="${escapeSelectorValue(key)}"]`)
|
|
279
|
+
|| serverTag(tag, attrs);
|
|
139
280
|
if (existing) {
|
|
281
|
+
existing.setAttribute('data-what-head', key);
|
|
140
282
|
updateElement(existing, attrs);
|
|
141
283
|
return;
|
|
142
284
|
}
|
|
@@ -150,6 +292,34 @@ function setHeadTag(tag, key, attrs) {
|
|
|
150
292
|
document.head.appendChild(el);
|
|
151
293
|
}
|
|
152
294
|
|
|
295
|
+
// The tag the server already wrote for these attributes, if it did: a
|
|
296
|
+
// server-rendered document (renderToStringWithHead, renderDocument) and a
|
|
297
|
+
// streamed boundary's late head carry no data-what-head, so a client <Head>
|
|
298
|
+
// that only looked for it added a second copy of every tag when the page
|
|
299
|
+
// hydrated. The identity is the one the server deduplicates by (writeToSink)
|
|
300
|
+
// and the streamed head is applied with: a meta's name, property or
|
|
301
|
+
// http-equiv, a link's rel and href, and for any other meta all of its
|
|
302
|
+
// attributes.
|
|
303
|
+
function serverTag(tag, attrs) {
|
|
304
|
+
for (let el = document.head.firstElementChild; el; el = el.nextElementSibling) {
|
|
305
|
+
if (el.localName === tag && !el.hasAttribute('data-what-head') && sameTag(tag, el, attrs)) return el;
|
|
306
|
+
}
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function sameTag(tag, el, attrs) {
|
|
311
|
+
if (tag === 'link') {
|
|
312
|
+
return el.getAttribute('rel') === String(attrs.rel)
|
|
313
|
+
&& el.getAttribute('href') === (attrs.href == null ? null : String(attrs.href));
|
|
314
|
+
}
|
|
315
|
+
if (attrs.name) return el.getAttribute('name') === String(attrs.name);
|
|
316
|
+
if (attrs.property) return el.getAttribute('property') === String(attrs.property);
|
|
317
|
+
if (attrs.httpEquiv) return el.getAttribute('http-equiv') === String(attrs.httpEquiv);
|
|
318
|
+
const written = Object.entries(attrs).filter(([k, v]) => v != null && v !== false && isSafeAttrName(attrName(k)));
|
|
319
|
+
return el.attributes.length === written.length
|
|
320
|
+
&& written.every(([k, v]) => el.getAttribute(attrName(k)) === String(v));
|
|
321
|
+
}
|
|
322
|
+
|
|
153
323
|
function updateElement(el, attrs) {
|
|
154
324
|
for (const [k, v] of Object.entries(attrs)) {
|
|
155
325
|
const name = attrName(k);
|
package/src/helpers.js
CHANGED
|
@@ -1,56 +1,40 @@
|
|
|
1
1
|
// What Framework - Helpers & Utilities
|
|
2
2
|
// Commonly needed patterns, zero overhead.
|
|
3
3
|
|
|
4
|
-
import { signal, effect, __DEV__ } from './reactive.js';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export function each(list, fn, keyFn) {
|
|
10
|
-
if (!_eachWarned) {
|
|
11
|
-
_eachWarned = true;
|
|
12
|
-
console.warn('[what] each() is deprecated. Use the <For> component or Array.map() instead.');
|
|
13
|
-
}
|
|
14
|
-
if (!list || list.length === 0) return [];
|
|
15
|
-
return list.map((item, index) => {
|
|
16
|
-
const vnode = fn(item, index);
|
|
17
|
-
if (keyFn && vnode && typeof vnode === 'object') {
|
|
18
|
-
vnode.key = keyFn(item, index);
|
|
19
|
-
}
|
|
20
|
-
return vnode;
|
|
21
|
-
});
|
|
22
|
-
}
|
|
4
|
+
import { signal, effect, onCleanup, __DEV__ } from './reactive.js';
|
|
5
|
+
import { _vnode } from './h.js';
|
|
6
|
+
import { createPortalDOM } from './dom.js';
|
|
7
|
+
import { report } from './diagnostics.js';
|
|
8
|
+
import { styleText } from './markup-rules.js';
|
|
23
9
|
|
|
24
10
|
// --- cls(...args) ---
|
|
25
11
|
// Conditional class names. Like clsx but tiny.
|
|
26
12
|
// cls('base', condition && 'active', { hidden: isHidden, bold: isBold })
|
|
27
13
|
export function cls(...args) {
|
|
28
14
|
const classes = [];
|
|
29
|
-
|
|
30
|
-
if (!arg)
|
|
15
|
+
const add = (arg) => {
|
|
16
|
+
if (!arg) return;
|
|
31
17
|
if (typeof arg === 'string') {
|
|
32
18
|
classes.push(arg);
|
|
19
|
+
} else if (Array.isArray(arg)) {
|
|
20
|
+
for (const item of arg) add(item);
|
|
33
21
|
} else if (typeof arg === 'object') {
|
|
34
22
|
for (const [key, val] of Object.entries(arg)) {
|
|
35
23
|
if (val) classes.push(key);
|
|
36
24
|
}
|
|
37
25
|
}
|
|
38
|
-
}
|
|
26
|
+
};
|
|
27
|
+
for (const arg of args) add(arg);
|
|
39
28
|
return classes.join(' ');
|
|
40
29
|
}
|
|
41
30
|
|
|
42
31
|
// --- style(obj) ---
|
|
43
|
-
//
|
|
32
|
+
// A style object as CSS text, exactly as the server writes the style prop
|
|
33
|
+
// (markup-rules.js styleText): a declaration the prop would drop is left out.
|
|
34
|
+
// A string is returned as it is.
|
|
44
35
|
export function style(obj) {
|
|
45
36
|
if (typeof obj === 'string') return obj;
|
|
46
|
-
return
|
|
47
|
-
.filter(([, v]) => v != null && v !== '')
|
|
48
|
-
.map(([k, v]) => `${camelToKebab(k)}:${v}`)
|
|
49
|
-
.join(';');
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function camelToKebab(str) {
|
|
53
|
-
return str.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
37
|
+
return styleText(obj);
|
|
54
38
|
}
|
|
55
39
|
|
|
56
40
|
// --- debounce ---
|
|
@@ -75,11 +59,6 @@ export function throttle(fn, ms) {
|
|
|
75
59
|
};
|
|
76
60
|
}
|
|
77
61
|
|
|
78
|
-
// Component context ref — injected by dom.js to avoid circular imports
|
|
79
|
-
/** @type {(() => any) | null} */
|
|
80
|
-
let _getCurrentComponentRef = null;
|
|
81
|
-
export function _setComponentRef(fn) { _getCurrentComponentRef = fn; }
|
|
82
|
-
|
|
83
62
|
// --- useMediaQuery ---
|
|
84
63
|
// Reactive media query. Returns a signal. Cleans up listener on component unmount.
|
|
85
64
|
export function useMediaQuery(query) {
|
|
@@ -89,12 +68,8 @@ export function useMediaQuery(query) {
|
|
|
89
68
|
const handler = (e) => s.set(e.matches);
|
|
90
69
|
mq.addEventListener('change', handler);
|
|
91
70
|
|
|
92
|
-
//
|
|
93
|
-
|
|
94
|
-
if (ctx) {
|
|
95
|
-
ctx._cleanupCallbacks = ctx._cleanupCallbacks || [];
|
|
96
|
-
ctx._cleanupCallbacks.push(() => mq.removeEventListener('change', handler));
|
|
97
|
-
}
|
|
71
|
+
// Stop listening when the owner (the component using it) goes away.
|
|
72
|
+
onCleanup(() => mq.removeEventListener('change', handler));
|
|
98
73
|
|
|
99
74
|
return s;
|
|
100
75
|
}
|
|
@@ -113,11 +88,11 @@ export function useLocalStorage(key, initial) {
|
|
|
113
88
|
const s = signal(stored);
|
|
114
89
|
|
|
115
90
|
// Sync to localStorage on changes
|
|
116
|
-
|
|
91
|
+
effect(() => {
|
|
117
92
|
try {
|
|
118
93
|
localStorage.setItem(key, JSON.stringify(s()));
|
|
119
94
|
} catch (e) {
|
|
120
|
-
if (__DEV__)
|
|
95
|
+
if (__DEV__) report('ERR_STORAGE_FAILED', { data: { key, operation: 'write', reason: e?.message || String(e) } });
|
|
121
96
|
}
|
|
122
97
|
});
|
|
123
98
|
|
|
@@ -128,46 +103,47 @@ export function useLocalStorage(key, initial) {
|
|
|
128
103
|
storageHandler = (e) => {
|
|
129
104
|
if (e.key === key && e.newValue !== null) {
|
|
130
105
|
try { s.set(JSON.parse(e.newValue)); } catch (err) {
|
|
131
|
-
if (__DEV__)
|
|
106
|
+
if (__DEV__) report('ERR_STORAGE_FAILED', { data: { key, operation: 'read', reason: err?.message || String(err) } });
|
|
132
107
|
}
|
|
133
108
|
}
|
|
134
109
|
};
|
|
135
110
|
window.addEventListener('storage', storageHandler);
|
|
136
111
|
}
|
|
137
112
|
|
|
138
|
-
//
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
dispose();
|
|
144
|
-
if (storageHandler) window.removeEventListener('storage', storageHandler);
|
|
145
|
-
});
|
|
146
|
-
}
|
|
113
|
+
// The sync effect belongs to the owner it is created under; the listener
|
|
114
|
+
// goes with that owner too.
|
|
115
|
+
onCleanup(() => {
|
|
116
|
+
if (storageHandler) window.removeEventListener('storage', storageHandler);
|
|
117
|
+
});
|
|
147
118
|
|
|
148
119
|
return s;
|
|
149
120
|
}
|
|
150
121
|
|
|
151
122
|
// --- portal ---
|
|
152
|
-
// Render children into a different DOM container
|
|
153
|
-
|
|
154
|
-
|
|
123
|
+
// Render children into a different DOM container: `target`, an element or a
|
|
124
|
+
// selector, document.body when there is none. Compiled children arrive
|
|
125
|
+
// unbuilt (`_deferChildren`, see dom.js _installLazyChildren) and are built in
|
|
126
|
+
// the container: while hydrating, children built where the portal is placed
|
|
127
|
+
// would claim the server's nodes there. The server renders nothing for it.
|
|
128
|
+
export const Portal = /* @__PURE__ */ Object.assign(function Portal({ target, children }) {
|
|
155
129
|
if (typeof document === 'undefined') return null;
|
|
156
|
-
const container = typeof target === 'string'
|
|
157
|
-
|
|
158
|
-
: target;
|
|
159
|
-
|
|
130
|
+
const container = typeof target === 'string' ? document.querySelector(target) : target ?? document.body;
|
|
131
|
+
if (!container) {
|
|
132
|
+
if (__DEV__ && typeof target === 'string') report('ERR_PORTAL_TARGET_MISSING', { data: { selector: target } });
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
160
135
|
// The DOM reconciler will mount children here
|
|
161
|
-
return
|
|
162
|
-
}
|
|
136
|
+
return _vnode('__portal', { container, mount: createPortalDOM }, Array.isArray(children) ? children : [children]);
|
|
137
|
+
}, { _deferChildren: true });
|
|
163
138
|
|
|
164
139
|
// --- useClickOutside ---
|
|
165
140
|
// Detect clicks outside a ref'd element. Essential for dropdowns, modals, popovers.
|
|
166
141
|
export function useClickOutside(ref, handler) {
|
|
167
142
|
if (typeof document === 'undefined') return;
|
|
168
143
|
|
|
144
|
+
// A ref object is read at each press: null until it is attached.
|
|
169
145
|
const listener = (e) => {
|
|
170
|
-
const el = ref.current
|
|
146
|
+
const el = ref && typeof ref === 'object' && 'current' in ref ? ref.current : ref;
|
|
171
147
|
if (!el || el.contains(e.target)) return;
|
|
172
148
|
handler(e);
|
|
173
149
|
};
|
|
@@ -175,19 +151,15 @@ export function useClickOutside(ref, handler) {
|
|
|
175
151
|
document.addEventListener('mousedown', listener);
|
|
176
152
|
document.addEventListener('touchstart', listener);
|
|
177
153
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
document.removeEventListener('mousedown', listener);
|
|
183
|
-
document.removeEventListener('touchstart', listener);
|
|
184
|
-
});
|
|
185
|
-
}
|
|
154
|
+
onCleanup(() => {
|
|
155
|
+
document.removeEventListener('mousedown', listener);
|
|
156
|
+
document.removeEventListener('touchstart', listener);
|
|
157
|
+
});
|
|
186
158
|
}
|
|
187
159
|
|
|
188
|
-
// ---
|
|
189
|
-
//
|
|
190
|
-
export function
|
|
160
|
+
// --- transitionClass(name, active) ---
|
|
161
|
+
// Enter/leave classes for a CSS transition, as props to spread on the element.
|
|
162
|
+
export function transitionClass(name, active) {
|
|
191
163
|
return {
|
|
192
164
|
class: active ? `${name}-enter ${name}-enter-active` : `${name}-leave ${name}-leave-active`,
|
|
193
165
|
};
|