clarusjs 0.8.1 → 0.10.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/dist/check/generate.d.ts +20 -4
- package/dist/check/generate.js +157 -21
- package/dist/cli/check.d.ts +13 -0
- package/dist/cli/check.js +65 -18
- package/dist/cli/index.js +9 -3
- package/dist/cli/mapper.d.ts +25 -0
- package/dist/cli/mapper.js +65 -0
- package/dist/cli/project.d.ts +5 -0
- package/dist/cli/project.js +21 -0
- package/dist/compiler/emit-client.js +33 -9
- package/dist/compiler/emit-server.js +3 -2
- package/dist/compiler/errors.d.ts +14 -1
- package/dist/compiler/html-expr.d.ts +21 -0
- package/dist/compiler/html-expr.js +29 -3
- package/dist/compiler/index.d.ts +28 -3
- package/dist/compiler/index.js +36 -24
- package/dist/compiler/ir.d.ts +19 -1
- package/dist/compiler/ir.js +261 -11
- package/dist/compiler/parse.d.ts +52 -1
- package/dist/compiler/parse.js +145 -6
- package/dist/compiler/script.d.ts +22 -1
- package/dist/compiler/script.js +108 -5
- package/dist/fetch/index.d.ts +149 -0
- package/dist/fetch/index.js +312 -0
- package/dist/forms/index.d.ts +26 -0
- package/dist/forms/index.js +374 -0
- package/dist/forms/types.d.ts +111 -0
- package/dist/forms/types.js +1 -0
- package/dist/runtime/attrs.js +10 -2
- package/dist/runtime/dom.d.ts +26 -11
- package/dist/runtime/dom.js +102 -14
- package/dist/runtime/env.d.ts +14 -0
- package/dist/runtime/env.js +14 -0
- package/dist/runtime/html.d.ts +9 -7
- package/dist/runtime/html.js +12 -7
- package/dist/runtime/index.d.ts +3 -2
- package/dist/runtime/index.js +3 -2
- package/dist/runtime/loader.d.ts +1 -1
- package/dist/runtime/loader.js +36 -5
- package/dist/runtime/navigate.d.ts +5 -0
- package/dist/runtime/navigation.d.ts +85 -13
- package/dist/runtime/navigation.js +77 -11
- package/dist/runtime/router.js +10 -1
- package/dist/runtime/serialize.d.ts +1 -1
- package/dist/runtime/serialize.js +1 -1
- package/dist/runtime/server.d.ts +9 -2
- package/dist/runtime/server.js +23 -5
- package/dist/runtime/signals.d.ts +98 -3
- package/dist/runtime/signals.js +112 -6
- package/dist/server/handler.d.ts +14 -1
- package/dist/server/handler.js +2 -2
- package/dist/server/head.d.ts +4 -1
- package/dist/server/manifest.d.ts +14 -1
- package/dist/server/views.d.ts +64 -6
- package/dist/server/views.js +46 -0
- package/dist/vite/build.d.ts +1 -1
- package/dist/vite/manifest.d.ts +12 -0
- package/dist/vite/plugin.d.ts +30 -2
- package/dist/vite/plugin.js +17 -2
- package/dist/vite/sitemap.d.ts +1 -0
- package/package.json +19 -1
package/dist/runtime/dom.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Attach helpers: find markers in server HTML and keep them updated. Browser only. Never re-renders existing HTML;
|
|
2
2
|
// only @if branches and @for rows that did not exist on the server are rendered here.
|
|
3
3
|
import { attrValue } from './attrs.js';
|
|
4
|
-
import { effect, onDispose, scope, state, untrack } from './signals.js';
|
|
4
|
+
import { batch, effect, onDispose, scope, state, untrack } from './signals.js';
|
|
5
5
|
const rootOf = (ctx) => ('root' in ctx ? ctx.root : ctx);
|
|
6
6
|
/** Is `node` inside `ctx` (always true for a whole island)? */
|
|
7
7
|
function inside(ctx, node) {
|
|
@@ -67,11 +67,11 @@ function scan(ctx) {
|
|
|
67
67
|
}
|
|
68
68
|
return { holes, blocks, fors, regions };
|
|
69
69
|
}
|
|
70
|
-
/** Hole markers in `ctx`, by index. */
|
|
70
|
+
/** Hole markers in `ctx`, by index. @internal */
|
|
71
71
|
export function holes(ctx) {
|
|
72
72
|
return scan(ctx).holes;
|
|
73
73
|
}
|
|
74
|
-
/** @if block markers in `ctx`, by index. */
|
|
74
|
+
/** @if block markers in `ctx`, by index. @internal */
|
|
75
75
|
export function blocks(ctx) {
|
|
76
76
|
return scan(ctx).blocks;
|
|
77
77
|
}
|
|
@@ -101,6 +101,7 @@ function rangeNodes(start, end) {
|
|
|
101
101
|
/**
|
|
102
102
|
* An @if block. `which()` picks the branch (-1 for none). The server-rendered branch is wired in place; a change
|
|
103
103
|
* removes the old branch (disposing its effects and handlers), inserts the new branch's HTML and wires it.
|
|
104
|
+
* @internal Compiled components call this; apps don't.
|
|
104
105
|
*/
|
|
105
106
|
export function block(ctx, index, which, branches) {
|
|
106
107
|
const b = blocks(ctx)[index];
|
|
@@ -133,6 +134,12 @@ export function block(ctx, index, which, branches) {
|
|
|
133
134
|
* values; when anything it read changes, the old HTML is disposed and replaced, and `wire()` wires the island's own
|
|
134
135
|
* holes and events in the child's slot content again. A stateful child is remounted with its new props.
|
|
135
136
|
*/
|
|
137
|
+
/** The one <cl-island> among `nodes` (blank text aside), or null. */
|
|
138
|
+
const onlyIsland = (nodes) => {
|
|
139
|
+
const kept = nodes.filter((n) => n.nodeType !== 3 || n.textContent.trim());
|
|
140
|
+
return kept.length === 1 && kept[0].localName === 'cl-island' ? kept[0] : null;
|
|
141
|
+
};
|
|
142
|
+
/** @internal Compiled components call this; apps don't. */
|
|
136
143
|
export function region(ctx, index, html, wire) {
|
|
137
144
|
const r = scan(ctx).regions[index];
|
|
138
145
|
if (!r)
|
|
@@ -144,6 +151,16 @@ export function region(ctx, index, html, wire) {
|
|
|
144
151
|
const next = html();
|
|
145
152
|
untrack(() => {
|
|
146
153
|
if (!first) {
|
|
154
|
+
// A nested island with new props is updated in place (DOM and state kept), not replaced.
|
|
155
|
+
const t = document.createElement('template');
|
|
156
|
+
t.innerHTML = next;
|
|
157
|
+
const between = [];
|
|
158
|
+
for (let n = r.start.nextSibling; n && n !== r.end; n = n.nextSibling)
|
|
159
|
+
between.push(n);
|
|
160
|
+
const old = onlyIsland(between);
|
|
161
|
+
const fresh = onlyIsland([...t.content.childNodes]);
|
|
162
|
+
if (old && fresh && globalThis.__clUpdate?.(old, fresh))
|
|
163
|
+
return;
|
|
147
164
|
dispose?.();
|
|
148
165
|
while (r.start.nextSibling && r.start.nextSibling !== r.end)
|
|
149
166
|
r.start.nextSibling.remove();
|
|
@@ -159,6 +176,7 @@ export function region(ctx, index, html, wire) {
|
|
|
159
176
|
* kept rows keep their DOM nodes (moved only when out of order) and get the new item and index through their
|
|
160
177
|
* signals; new rows are rendered and wired; removed rows are disposed and removed. `@empty` content is shown while
|
|
161
178
|
* the list is empty.
|
|
179
|
+
* @internal Compiled components call this; apps don't.
|
|
162
180
|
*/
|
|
163
181
|
export function forBlock(ctx, index, list, keyOf, row, empty) {
|
|
164
182
|
const f = scan(ctx).fors[index];
|
|
@@ -168,10 +186,11 @@ export function forBlock(ctx, index, list, keyOf, row, empty) {
|
|
|
168
186
|
let rows = [];
|
|
169
187
|
let emptyRange = null;
|
|
170
188
|
let first = true;
|
|
189
|
+
const count = state(0); // the list's length, for rows using $count, $last…
|
|
171
190
|
const wireRow = (range, key, item, i) => {
|
|
172
191
|
const itemS = state(item);
|
|
173
192
|
const indexS = state(i);
|
|
174
|
-
const dispose = scope(() => row.wire({ root, ...range }, itemS, indexS));
|
|
193
|
+
const dispose = scope(() => row.wire({ root, ...range }, itemS, indexS, count));
|
|
175
194
|
return { ...range, key, item: itemS, index: indexS, dispose };
|
|
176
195
|
};
|
|
177
196
|
const wireEmpty = (range) => ({ ...range, dispose: empty ? scope(() => empty.wire({ root, ...range })) : () => { } });
|
|
@@ -182,7 +201,8 @@ export function forBlock(ctx, index, list, keyOf, row, empty) {
|
|
|
182
201
|
});
|
|
183
202
|
effect(() => {
|
|
184
203
|
const items = [...(list() ?? [])];
|
|
185
|
-
untrack(() => {
|
|
204
|
+
untrack(() => batch(() => {
|
|
205
|
+
count.set(items.length); // with the rows' new indices below, in one batch: $last doesn't flip on the way
|
|
186
206
|
// Keys, made unique: a repeated key would make two rows share one identity.
|
|
187
207
|
const seen = new Set();
|
|
188
208
|
const keys = items.map((item, i) => {
|
|
@@ -213,7 +233,7 @@ export function forBlock(ctx, index, list, keyOf, row, empty) {
|
|
|
213
233
|
kept.index.set(i);
|
|
214
234
|
return kept;
|
|
215
235
|
}
|
|
216
|
-
const { first: start, last: end } = insertHtml(`<!--[r-->${row.html(item, i)}<!--]-->`, f.end);
|
|
236
|
+
const { first: start, last: end } = insertHtml(`<!--[r-->${row.html(item, i, items)}<!--]-->`, f.end);
|
|
217
237
|
return wireRow({ start: start, end: end }, keys[i], item, i);
|
|
218
238
|
});
|
|
219
239
|
for (const r of old.values()) {
|
|
@@ -239,10 +259,10 @@ export function forBlock(ctx, index, list, keyOf, row, empty) {
|
|
|
239
259
|
const { first: start, last: end } = insertHtml(`<!--[e-->${empty.html()}<!--]-->`, f.end);
|
|
240
260
|
emptyRange = wireEmpty({ start: start, end: end });
|
|
241
261
|
}
|
|
242
|
-
});
|
|
262
|
+
}));
|
|
243
263
|
});
|
|
244
264
|
}
|
|
245
|
-
/** Keep exactly one text node between the hole's markers, set to `fn()`. */
|
|
265
|
+
/** Keep exactly one text node between the hole's markers, set to `fn()`. @internal */
|
|
246
266
|
export function text(hole, fn) {
|
|
247
267
|
effect(() => {
|
|
248
268
|
const value = fn();
|
|
@@ -260,12 +280,16 @@ export function text(hole, fn) {
|
|
|
260
280
|
}
|
|
261
281
|
const registry = new WeakMap();
|
|
262
282
|
// These don't bubble, so they are caught on the way down instead.
|
|
263
|
-
const CAPTURE = new Set([
|
|
283
|
+
const CAPTURE = new Set([
|
|
284
|
+
'focus', 'blur', 'mouseenter', 'mouseleave', 'pointerenter', 'pointerleave', 'load', 'error', 'scroll', 'toggle',
|
|
285
|
+
'invalid', 'close', 'cancel', 'play', 'pause', 'ended', 'timeupdate', 'volumechange', 'loadedmetadata', 'canplay',
|
|
286
|
+
]);
|
|
264
287
|
/**
|
|
265
288
|
* Register a handler for elements marked `data-cl-e="index"` inside `ctx`.
|
|
266
289
|
* One listener per island per event type; it walks from the target up to the island root, running handlers
|
|
267
290
|
* innermost first and stopping when one calls `stopPropagation()`. A handler registered for a @for row only fires
|
|
268
291
|
* for elements in that row.
|
|
292
|
+
* @internal Compiled components call this; apps don't.
|
|
269
293
|
*/
|
|
270
294
|
export function on(ctx, index, type, fn) {
|
|
271
295
|
const root = rootOf(ctx);
|
|
@@ -277,7 +301,8 @@ export function on(ctx, index, type, fn) {
|
|
|
277
301
|
const own = (handlers = new Map());
|
|
278
302
|
types.set(type, own);
|
|
279
303
|
root.addEventListener(type, (e) => {
|
|
280
|
-
|
|
304
|
+
// An event that doesn't bubble (mouseenter, focus) is its target's alone, not its ancestors'.
|
|
305
|
+
for (let el = e.target; el && el !== root.parentElement; el = e.bubbles ? el.parentElement : null) {
|
|
281
306
|
const i = el.getAttribute?.('data-cl-e');
|
|
282
307
|
if (i === null || i === undefined || el.closest('cl-island') !== root)
|
|
283
308
|
continue;
|
|
@@ -297,17 +322,80 @@ export function on(ctx, index, type, fn) {
|
|
|
297
322
|
onDispose(() => { const at = list.indexOf(handler); if (at !== -1)
|
|
298
323
|
list.splice(at, 1); }); // a removed branch or row stops listening
|
|
299
324
|
}
|
|
300
|
-
/**
|
|
301
|
-
export function
|
|
325
|
+
/** The element matching `selector` in `ctx`: this island's (not a nested island's), and in a row, that row's. */
|
|
326
|
+
export function marked(ctx, selector) {
|
|
302
327
|
const root = rootOf(ctx);
|
|
303
|
-
const
|
|
328
|
+
const island = root.closest('cl-island');
|
|
329
|
+
return [...root.querySelectorAll(selector)].find((e) => e.closest('cl-island') === island && inside(ctx, e));
|
|
330
|
+
}
|
|
331
|
+
/** Keep attribute `name` of the element marked `data-cl-a="index"` in `ctx` in sync with `fn()`. @internal */
|
|
332
|
+
export function attr(ctx, index, name, fn, tag) {
|
|
333
|
+
const el = marked(ctx, `[data-cl-a="${index}"]`);
|
|
304
334
|
if (!el)
|
|
305
335
|
return;
|
|
306
336
|
effect(() => {
|
|
307
|
-
const
|
|
337
|
+
const raw = fn();
|
|
338
|
+
// <select multiple> bound to an array: each option selected when the array has its value.
|
|
339
|
+
if (name === 'value' && Array.isArray(raw) && el.tagName === 'SELECT' && el.multiple) {
|
|
340
|
+
const values = raw.map(String); // option values are text: [1, 2] selects "1" and "2"
|
|
341
|
+
for (const o of el.options)
|
|
342
|
+
o.selected = values.includes(o.value);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
const v = attrValue(name, raw, tag);
|
|
308
346
|
if (v === null)
|
|
309
347
|
el.removeAttribute(name);
|
|
310
348
|
else
|
|
311
349
|
el.setAttribute(name, v === true ? '' : v);
|
|
350
|
+
// A form field shows its property once someone has typed or clicked in it, not the attribute: set both.
|
|
351
|
+
if (name === 'value' && 'value' in el) {
|
|
352
|
+
const field = el;
|
|
353
|
+
const next = v === null || v === true ? '' : v;
|
|
354
|
+
// A number field showing the same number keeps what was typed ("1.0" isn't rewritten to "1" mid-typing).
|
|
355
|
+
if (field.value !== next && !(/^(number|range)$/.test(field.type) && field.value !== '' && Number(field.value) === Number(raw)))
|
|
356
|
+
field.value = next;
|
|
357
|
+
}
|
|
358
|
+
if (name === 'checked' && 'checked' in el)
|
|
359
|
+
el.checked = v !== null;
|
|
312
360
|
});
|
|
313
361
|
}
|
|
362
|
+
/**
|
|
363
|
+
* A @model input: writing it asks the parent island to update its state (a bubbling, cancelable cl:model event from
|
|
364
|
+
* the island); when no parent takes it (a top-level island), the input keeps the value itself. New props from the
|
|
365
|
+
* parent set it with own().
|
|
366
|
+
* @internal Compiled components call this; apps don't.
|
|
367
|
+
*/
|
|
368
|
+
export function modelInput(root, name, initial) {
|
|
369
|
+
const value = state(initial);
|
|
370
|
+
return {
|
|
371
|
+
initial,
|
|
372
|
+
get: value.get,
|
|
373
|
+
own: value.set,
|
|
374
|
+
set(next) {
|
|
375
|
+
if (root.dispatchEvent(new CustomEvent('cl:model', { detail: { name, value: next }, bubbles: true, cancelable: true })))
|
|
376
|
+
value.set(next);
|
|
377
|
+
},
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
/** [(name)]="…" on a child island in region `index`: the child's writes to its @model name arrive here. @internal */
|
|
381
|
+
export function model(ctx, index, name, set) {
|
|
382
|
+
const r = scan(ctx).regions[index];
|
|
383
|
+
if (!r)
|
|
384
|
+
return;
|
|
385
|
+
const host = r.start.parentNode;
|
|
386
|
+
const listen = (event) => {
|
|
387
|
+
const detail = event.detail;
|
|
388
|
+
const from = event.target;
|
|
389
|
+
// Only from inside this region: 4 = the target follows the start marker, 2 = it precedes the end marker.
|
|
390
|
+
if (detail.name !== name || !(r.start.compareDocumentPosition(from) & 4) || !(r.end.compareDocumentPosition(from) & 2))
|
|
391
|
+
return;
|
|
392
|
+
// Only the child island this region binds: an island nested in it, which nobody bound, keeps its own value.
|
|
393
|
+
if (from.parentElement?.closest('cl-island') !== rootOf(ctx))
|
|
394
|
+
return;
|
|
395
|
+
event.preventDefault();
|
|
396
|
+
event.stopPropagation();
|
|
397
|
+
set(detail.value);
|
|
398
|
+
};
|
|
399
|
+
host.addEventListener('cl:model', listen);
|
|
400
|
+
onDispose(() => host.removeEventListener('cl:model', listen));
|
|
401
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* True in the browser, false on the server. A component's script runs in both (the server renders it, the browser
|
|
3
|
+
* attaches it), so code that needs the DOM, timers that keep running or listeners on `window` goes under
|
|
4
|
+
* `if (browser) { … }`. A window without a document (some server runtimes define one) is the server.
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* if (browser) {
|
|
8
|
+
* const timer = setInterval(tick, 1000)
|
|
9
|
+
* onDispose(() => clearInterval(timer))
|
|
10
|
+
* }
|
|
11
|
+
* ```
|
|
12
|
+
* @see https://clarusjs.com/reference/runtime#browser
|
|
13
|
+
*/
|
|
14
|
+
export declare const browser: boolean;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* True in the browser, false on the server. A component's script runs in both (the server renders it, the browser
|
|
3
|
+
* attaches it), so code that needs the DOM, timers that keep running or listeners on `window` goes under
|
|
4
|
+
* `if (browser) { … }`. A window without a document (some server runtimes define one) is the server.
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* if (browser) {
|
|
8
|
+
* const timer = setInterval(tick, 1000)
|
|
9
|
+
* onDispose(() => clearInterval(timer))
|
|
10
|
+
* }
|
|
11
|
+
* ```
|
|
12
|
+
* @see https://clarusjs.com/reference/runtime#browser
|
|
13
|
+
*/
|
|
14
|
+
export const browser = typeof window !== 'undefined' && typeof document !== 'undefined';
|
package/dist/runtime/html.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** The mark of h()'s output (clarusjs/views). Symbol.for: two copies of the module agree. @internal */
|
|
2
|
+
export declare const HTML: unique symbol;
|
|
3
|
+
/** Escape a value for HTML text or a double-quoted attribute. null/undefined render as ''. @internal */
|
|
2
4
|
export declare function escape(value: unknown): string;
|
|
3
|
-
/** A value as JSON for a <script> (JSON-LD): <, >, & and line separators escaped, so it can't end the script. */
|
|
5
|
+
/** A value as JSON for a <script> (JSON-LD): <, >, & and line separators escaped, so it can't end the script. @internal */
|
|
4
6
|
export declare function json(value: unknown): string;
|
|
5
|
-
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. */
|
|
7
|
+
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. @internal */
|
|
6
8
|
export declare function attrHtml(name: string, value: unknown, tag?: string): string;
|
|
7
|
-
/** An @if block inside an island: markers record which branch (k, or -1 for none) was rendered. */
|
|
9
|
+
/** An @if block inside an island: markers record which branch (k, or -1 for none) was rendered. @internal */
|
|
8
10
|
export declare function blockHtml(index: number, k: number, branches: (() => string)[]): string;
|
|
9
|
-
/** A @for list inside an island: one marker pair around the list, one per row, one around @empty content. */
|
|
10
|
-
export declare function forHtml(index: number, list: unknown, row: (item: any, i: number) => string, empty: (() => string) | null): string;
|
|
11
|
-
/** A child component inside an island that the browser may re-render: markers around its HTML. */
|
|
11
|
+
/** A @for list inside an island: one marker pair around the list, one per row, one around @empty content. @internal */
|
|
12
|
+
export declare function forHtml(index: number, list: unknown, row: (item: any, i: number, list: unknown[]) => string, empty: (() => string) | null): string;
|
|
13
|
+
/** A child component inside an island that the browser may re-render: markers around its HTML. @internal */
|
|
12
14
|
export declare function regionHtml(index: number, html: () => string): string;
|
package/dist/runtime/html.js
CHANGED
|
@@ -1,34 +1,39 @@
|
|
|
1
1
|
// HTML-string helpers used by compiled code on the server (render) and in the browser (branch HTML for @if).
|
|
2
2
|
import { attrValue } from './attrs.js';
|
|
3
|
+
/** The mark of h()'s output (clarusjs/views). Symbol.for: two copies of the module agree. @internal */
|
|
4
|
+
export const HTML = Symbol.for('clarusjs.html');
|
|
3
5
|
const ENTITIES = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
|
4
|
-
/** Escape a value for HTML text or a double-quoted attribute. null/undefined render as ''. */
|
|
6
|
+
/** Escape a value for HTML text or a double-quoted attribute. null/undefined render as ''. @internal */
|
|
5
7
|
export function escape(value) {
|
|
6
8
|
if (value == null)
|
|
7
9
|
return '';
|
|
10
|
+
// h()'s output (clarusjs/views): HTML built from escaped parts, marked with a symbol data can't carry (JSON can't).
|
|
11
|
+
if (typeof value === 'object' && value[HTML] === true)
|
|
12
|
+
return value.html;
|
|
8
13
|
return String(value).replace(/[&<>"']/g, (ch) => ENTITIES[ch]);
|
|
9
14
|
}
|
|
10
|
-
/** A value as JSON for a <script> (JSON-LD): <, >, & and line separators escaped, so it can't end the script. */
|
|
15
|
+
/** A value as JSON for a <script> (JSON-LD): <, >, & and line separators escaped, so it can't end the script. @internal */
|
|
11
16
|
export function json(value) {
|
|
12
17
|
return (JSON.stringify(value) ?? 'null').replace(/[<>&\u2028\u2029]/g, (ch) => `\\u${ch.charCodeAt(0).toString(16).padStart(4, '0')}`);
|
|
13
18
|
}
|
|
14
|
-
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. */
|
|
19
|
+
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. @internal */
|
|
15
20
|
export function attrHtml(name, value, tag) {
|
|
16
21
|
const v = attrValue(name, value, tag);
|
|
17
22
|
return v === null ? '' : v === true ? ` ${name}` : ` ${name}="${escape(v)}"`;
|
|
18
23
|
}
|
|
19
|
-
/** An @if block inside an island: markers record which branch (k, or -1 for none) was rendered. */
|
|
24
|
+
/** An @if block inside an island: markers record which branch (k, or -1 for none) was rendered. @internal */
|
|
20
25
|
export function blockHtml(index, k, branches) {
|
|
21
26
|
return `<!--[b${index}:${k}-->${k >= 0 ? branches[k]() : ''}<!--]-->`;
|
|
22
27
|
}
|
|
23
|
-
/** A @for list inside an island: one marker pair around the list, one per row, one around @empty content. */
|
|
28
|
+
/** A @for list inside an island: one marker pair around the list, one per row, one around @empty content. @internal */
|
|
24
29
|
export function forHtml(index, list, row, empty) {
|
|
25
30
|
const items = [...(list ?? [])];
|
|
26
31
|
const body = items.length
|
|
27
|
-
? items.map((item, i) => `<!--[r-->${row(item, i)}<!--]-->`).join('')
|
|
32
|
+
? items.map((item, i) => `<!--[r-->${row(item, i, items)}<!--]-->`).join('')
|
|
28
33
|
: empty ? `<!--[e-->${empty()}<!--]-->` : '';
|
|
29
34
|
return `<!--[f${index}-->${body}<!--]-->`;
|
|
30
35
|
}
|
|
31
|
-
/** A child component inside an island that the browser may re-render: markers around its HTML. */
|
|
36
|
+
/** A child component inside an island that the browser may re-render: markers around its HTML. @internal */
|
|
32
37
|
export function regionHtml(index, html) {
|
|
33
38
|
return `<!--[c${index}-->${html()}<!--]-->`;
|
|
34
39
|
}
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { state, derived, shared, effect, scope, onDispose, untrack, type Signal, type ReadSignal } from './signals.ts';
|
|
2
|
-
export { holes, blocks, text, on, attr, block, forBlock, region, type Hole, type Block, type Ctx } from './dom.ts';
|
|
1
|
+
export { state, derived, shared, effect, scope, onDispose, untrack, batch, type Signal, type ReadSignal } from './signals.ts';
|
|
2
|
+
export { holes, blocks, text, on, attr, block, forBlock, region, model, modelInput, type Hole, type Block, type Ctx } from './dom.ts';
|
|
3
3
|
export { escape, attrHtml, blockHtml, forHtml, regionHtml } from './html.ts';
|
|
4
4
|
export { parse as parseIslandData } from './serialize.ts';
|
|
5
|
+
export { browser } from './env.ts';
|
package/dist/runtime/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Browser runtime used by compiled attach() functions.
|
|
2
|
-
export { state, derived, shared, effect, scope, onDispose, untrack } from './signals.js';
|
|
3
|
-
export { holes, blocks, text, on, attr, block, forBlock, region } from './dom.js';
|
|
2
|
+
export { state, derived, shared, effect, scope, onDispose, untrack, batch } from './signals.js';
|
|
3
|
+
export { holes, blocks, text, on, attr, block, forBlock, region, model, modelInput } from './dom.js';
|
|
4
4
|
export { escape, attrHtml, blockHtml, forHtml, regionHtml } from './html.js';
|
|
5
5
|
export { parse as parseIslandData } from './serialize.js';
|
|
6
|
+
export { browser } from './env.js';
|
package/dist/runtime/loader.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
type IslandModule = {
|
|
2
|
-
attach(root: Element, props: Record<string, unknown>, state?: Record<string, unknown>): void;
|
|
2
|
+
attach(root: Element, props: Record<string, unknown>, state?: Record<string, unknown>): void | ((props: Record<string, unknown>) => void);
|
|
3
3
|
};
|
|
4
4
|
type Load = (src: string) => Promise<IslandModule>;
|
|
5
5
|
/**
|
package/dist/runtime/loader.js
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
// Island loader: finds <cl-island> elements, loads each one's chunk when its attach mode says so, calls attach(),
|
|
2
2
|
// and replays what the visitor did before that. Browser only.
|
|
3
|
-
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
3
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx, extraExtensions, ignoreCase) {
|
|
4
4
|
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
5
|
+
if (extraExtensions) {
|
|
6
|
+
for (var extension in extraExtensions) {
|
|
7
|
+
var suffix = path.slice(-extension.length);
|
|
8
|
+
if (suffix === extension || ignoreCase && suffix.toLowerCase() === extension.toLowerCase()) {
|
|
9
|
+
var output = extraExtensions[extension];
|
|
10
|
+
return output === extension ? path : path.slice(0, -extension.length) + output;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
5
14
|
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
6
15
|
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
7
16
|
});
|
|
@@ -12,6 +21,7 @@ import { parse } from './serialize.js';
|
|
|
12
21
|
import { scope } from './signals.js';
|
|
13
22
|
// Islands scheduled or loading. `data-cl-ready` is set only after attach() succeeds.
|
|
14
23
|
const loading = new WeakSet();
|
|
24
|
+
const updates = new WeakMap();
|
|
15
25
|
// Each attached island's disposer: everything its attach() wired (effects, listeners) is released by it.
|
|
16
26
|
const attached = new Map();
|
|
17
27
|
let lastLoad = (src) => import(__rewriteRelativeImportExtension(/* @vite-ignore */ src));
|
|
@@ -103,7 +113,12 @@ async function attachOne(el, load, recorded) {
|
|
|
103
113
|
return;
|
|
104
114
|
}
|
|
105
115
|
try {
|
|
106
|
-
attached.set(el, scope(() =>
|
|
116
|
+
attached.set(el, scope(() => {
|
|
117
|
+
// An island with inputs returns how to update them: kept for a parent island that passes new props.
|
|
118
|
+
const update = mod.attach(el, props, state);
|
|
119
|
+
if (typeof update === 'function')
|
|
120
|
+
updates.set(el, update);
|
|
121
|
+
}));
|
|
107
122
|
el.setAttribute('data-cl-ready', '');
|
|
108
123
|
}
|
|
109
124
|
catch (err) {
|
|
@@ -175,10 +190,26 @@ function arrive() {
|
|
|
175
190
|
region.textContent = '';
|
|
176
191
|
setTimeout(() => { region.textContent = document.title; }, 50);
|
|
177
192
|
}
|
|
178
|
-
//
|
|
179
|
-
//
|
|
180
|
-
// islands that attach right away are attached before afterSwap and afterNavigate run.
|
|
193
|
+
// A parent island re-rendered this nested island with new props (`next`, not in the page): they go to it in place, so
|
|
194
|
+
// its DOM and state are kept. False when it can't take them (not attached yet, or another component).
|
|
181
195
|
;
|
|
196
|
+
globalThis.__clUpdate = (el, next) => {
|
|
197
|
+
const update = updates.get(el);
|
|
198
|
+
// The same component: the same name and the same source (two panel.tscl files share a name).
|
|
199
|
+
if (!update || el.getAttribute('c') !== next.getAttribute('c') || el.getAttribute('src') !== next.getAttribute('src'))
|
|
200
|
+
return false;
|
|
201
|
+
const p = next.getAttribute('p') ?? '{}';
|
|
202
|
+
el.setAttribute('p', p);
|
|
203
|
+
// Its paths for the forms engine (data-cl-path-<model>) move with it: a row removed above shifts the indexes.
|
|
204
|
+
for (const { name } of [...el.attributes])
|
|
205
|
+
if (name.startsWith('data-cl-path-'))
|
|
206
|
+
el.removeAttribute(name);
|
|
207
|
+
for (const { name, value } of [...next.attributes])
|
|
208
|
+
if (name.startsWith('data-cl-path-'))
|
|
209
|
+
el.setAttribute(name, value);
|
|
210
|
+
update(parse(p));
|
|
211
|
+
return true;
|
|
212
|
+
};
|
|
182
213
|
globalThis.__clAttach = (root, navigated) => {
|
|
183
214
|
const done = attachAll(root);
|
|
184
215
|
if (navigated)
|
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
/** `signal`: aborts when the navigation does (pass it to the page's requests). */
|
|
3
3
|
export type Render = (url: URL, gate: () => Promise<boolean> | boolean, signal?: AbortSignal) => Promise<unknown>;
|
|
4
4
|
/** A navigation in progress, as the in-between hooks and the navigating store see it. */
|
|
5
|
+
/**
|
|
6
|
+
* A navigation in progress, as the `navigating` store and the swap hooks see it: where it comes from and goes, its
|
|
7
|
+
* type (push, replace, reload, traverse), its phase, and a signal that aborts when a newer navigation takes over.
|
|
8
|
+
* @see https://clarusjs.com/reference/navigation#navigating
|
|
9
|
+
*/
|
|
5
10
|
export type Transition = {
|
|
6
11
|
from: URL;
|
|
7
12
|
to: URL;
|
|
@@ -4,6 +4,15 @@ export type { Transition };
|
|
|
4
4
|
/**
|
|
5
5
|
* The current page's URL, as a store: `let here = shared(currentUrl)` in an island's script. `null` on the server
|
|
6
6
|
* (what the server renders), so pass the page's path as a prop when the server HTML needs it.
|
|
7
|
+
* @example
|
|
8
|
+
* ```tscl
|
|
9
|
+
* <script>
|
|
10
|
+
* import { currentUrl } from 'clarusjs/navigation'
|
|
11
|
+
* let here = shared(currentUrl)
|
|
12
|
+
* </script>
|
|
13
|
+
* <a href="/guide" [class]="here?.pathname === '/guide' ? 'active' : ''">Guide</a>
|
|
14
|
+
* ```
|
|
15
|
+
* @see https://clarusjs.com/reference/navigation#currenturl
|
|
7
16
|
* @see https://clarusjs.com/guide/fragment-navigation
|
|
8
17
|
*/
|
|
9
18
|
export declare const currentUrl: Signal<URL | null>;
|
|
@@ -11,8 +20,15 @@ export declare const currentUrl: Signal<URL | null>;
|
|
|
11
20
|
* Go to `to`, as a link click would: the page's navigator swaps or renders it. Resolves once the new page is shown,
|
|
12
21
|
* and also when the navigation is cancelled or a newer one replaces it (so check `currentUrl` when it matters);
|
|
13
22
|
* rejects when it fails.
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* async function save() {
|
|
26
|
+
* await post(form)
|
|
27
|
+
* await navigate('/account', { replace: true })
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
* @see https://clarusjs.com/reference/navigation#navigate
|
|
14
31
|
* @see https://clarusjs.com/guide/fragment-navigation
|
|
15
|
-
* @see https://clarusjs.com/guide/client-routes
|
|
16
32
|
*/
|
|
17
33
|
export declare function navigate(to: string | URL, options?: {
|
|
18
34
|
replace?: boolean;
|
|
@@ -22,15 +38,32 @@ export declare function navigate(to: string | URL, options?: {
|
|
|
22
38
|
* Stops the navigation in progress, like the browser's stop button: its requests are cancelled and, where the URL was
|
|
23
39
|
* held while loading (push and replace in browsers with precommit handlers), page and URL stay as they were. Hooks
|
|
24
40
|
* see it through `t.signal`. Like the stop button, it also stops the page's own loading (images still arriving).
|
|
25
|
-
* @
|
|
41
|
+
* @example
|
|
42
|
+
* ```tscl
|
|
43
|
+
* <button (click)="abort()">Stop loading</button>
|
|
44
|
+
* ```
|
|
45
|
+
* @see https://clarusjs.com/reference/navigation#abort
|
|
26
46
|
*/
|
|
27
47
|
export declare function abort(): void;
|
|
28
|
-
/**
|
|
48
|
+
/**
|
|
49
|
+
* Back one entry, like the browser's back button.
|
|
50
|
+
* @example
|
|
51
|
+
* ```tscl
|
|
52
|
+
* <button (click)="back()">Back</button>
|
|
53
|
+
* ```
|
|
54
|
+
* @see https://clarusjs.com/reference/navigation#back
|
|
55
|
+
*/
|
|
29
56
|
export declare function back(): void;
|
|
30
57
|
/**
|
|
31
58
|
* Before any navigation (links, forms, back/forward, `navigate()`): `nav.cancel()` stops it, for example to keep
|
|
32
59
|
* unsaved work. Back/forward can only be cancelled where the browser allows it.
|
|
33
|
-
* @
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* beforeNavigate((to, nav) => {
|
|
63
|
+
* if (dirty && !confirm('Leave without saving?')) nav.cancel()
|
|
64
|
+
* })
|
|
65
|
+
* ```
|
|
66
|
+
* @see https://clarusjs.com/reference/navigation#beforenavigate
|
|
34
67
|
*/
|
|
35
68
|
export declare function beforeNavigate(fn: (to: URL, nav: {
|
|
36
69
|
type: string;
|
|
@@ -38,51 +71,90 @@ export declare function beforeNavigate(fn: (to: URL, nav: {
|
|
|
38
71
|
}) => void): void;
|
|
39
72
|
/**
|
|
40
73
|
* After a navigation within the page finished: the new page is in and its islands attached.
|
|
41
|
-
* @
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* afterNavigate((to) => analytics.page(to.pathname))
|
|
77
|
+
* ```
|
|
78
|
+
* @see https://clarusjs.com/reference/navigation#afternavigate
|
|
42
79
|
*/
|
|
43
80
|
export declare function afterNavigate(fn: (to: URL) => void): void;
|
|
44
81
|
/**
|
|
45
82
|
* A navigation failed (a fragment or a client page could not be loaded). The page then loads normally.
|
|
46
|
-
* @
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* onNavigateError((to, error) => report(error, { url: to.href }))
|
|
86
|
+
* ```
|
|
87
|
+
* @see https://clarusjs.com/reference/navigation#onnavigateerror
|
|
47
88
|
*/
|
|
48
89
|
export declare function onNavigateError(fn: (to: URL, error: unknown) => void): void;
|
|
49
90
|
/**
|
|
50
91
|
* The navigation in progress, as a store (`let nav = shared(navigating)`): `null` when idle, else where it goes and
|
|
51
92
|
* its phase, `'loading'` (the next page is fetched) or `'swapping'` (it replaces this one). For CSS alone,
|
|
52
93
|
* `<html data-cl-navigating>` carries the phase.
|
|
53
|
-
* @
|
|
94
|
+
* @example
|
|
95
|
+
* ```tscl
|
|
96
|
+
* <script>
|
|
97
|
+
* import { navigating } from 'clarusjs/navigation'
|
|
98
|
+
* let nav = shared(navigating)
|
|
99
|
+
* </script>
|
|
100
|
+
* @if (nav) { <div class="progress" role="status">Loading {{ nav.to.pathname }}…</div> }
|
|
101
|
+
* ```
|
|
102
|
+
* @see https://clarusjs.com/reference/navigation#navigating
|
|
54
103
|
*/
|
|
55
104
|
export declare const navigating: Signal<Transition | null>;
|
|
56
105
|
/**
|
|
57
106
|
* Page changes run as view transitions where the browser has them (`document.startViewTransition`), so CSS
|
|
58
107
|
* `::view-transition-*` rules animate them; elsewhere they stay instant. Call it once, from any island's script.
|
|
59
108
|
* `beforeSwap` hooks run before the browser's snapshot, `afterSwap` hooks once the new page is in.
|
|
60
|
-
* @
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* // in the layout's island script
|
|
112
|
+
* viewTransitions()
|
|
113
|
+
* ```
|
|
114
|
+
* @see https://clarusjs.com/reference/navigation#viewtransitions
|
|
61
115
|
*/
|
|
62
116
|
export declare function viewTransitions(): void;
|
|
63
117
|
/**
|
|
64
118
|
* Runs once the next page is loaded, before it replaces this one (animate out, save state). Return a promise to
|
|
65
119
|
* make the navigation wait; `t.signal` aborts when a newer navigation takes over. A hook that throws makes the page
|
|
66
120
|
* load normally.
|
|
67
|
-
* @
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* beforeSwap(() => document.querySelector('main')?.animate({ opacity: [1, 0] }, 150).finished)
|
|
124
|
+
* ```
|
|
125
|
+
* @see https://clarusjs.com/reference/navigation#beforeswap
|
|
68
126
|
*/
|
|
69
127
|
export declare function beforeSwap(fn: (t: Transition) => unknown): void;
|
|
70
128
|
/**
|
|
71
129
|
* Runs once the new page is in and its islands attached (animate in, move focus). Return a promise to make the
|
|
72
130
|
* navigation wait for it.
|
|
73
|
-
* @
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* afterSwap(() => document.querySelector('h1')?.focus())
|
|
134
|
+
* ```
|
|
135
|
+
* @see https://clarusjs.com/reference/navigation#afterswap
|
|
74
136
|
*/
|
|
75
137
|
export declare function afterSwap(fn: (t: Transition) => unknown): void;
|
|
76
|
-
/**
|
|
138
|
+
/**
|
|
139
|
+
* Route → params. Filled in by the generated .clarusjs/routes.d.ts (declaration merging; see `clarusjs check`).
|
|
140
|
+
* @see https://clarusjs.com/reference/navigation#href
|
|
141
|
+
*/
|
|
77
142
|
export interface Routes {
|
|
78
143
|
}
|
|
79
|
-
/**
|
|
144
|
+
/**
|
|
145
|
+
* Without the generated routes any path is accepted; with them, only real routes.
|
|
146
|
+
* @see https://clarusjs.com/reference/navigation#href
|
|
147
|
+
*/
|
|
80
148
|
export type RouteName = [keyof Routes] extends [never] ? string : Extract<keyof Routes, string>;
|
|
81
149
|
type ParamsOf<R> = R extends keyof Routes ? Routes[R] : Record<string, string>;
|
|
82
150
|
/**
|
|
83
151
|
* A page's URL from its route, as its file names it, with the site's base: `href('/releases/[version]',
|
|
84
152
|
* { version: '0.8.0' })` is `'/releases/0.8.0'`. With .clarusjs/routes.d.ts (written by `clarusjs check`), a route
|
|
85
153
|
* that doesn't exist or a missing param is a type error. Params are encoded.
|
|
86
|
-
* @
|
|
154
|
+
* @example
|
|
155
|
+
* ```tscl
|
|
156
|
+
* <a [href]="href('/releases/[version]', { version: '0.8.0' })">0.8.0</a>
|
|
157
|
+
* ```
|
|
158
|
+
* @see https://clarusjs.com/reference/navigation#href
|
|
87
159
|
*/
|
|
88
160
|
export declare function href<R extends RouteName>(route: R, ...params: [keyof Routes] extends [never] ? [Record<string, string>?] : keyof ParamsOf<R> extends never ? [] : [ParamsOf<R>]): string;
|