clarusjs 0.16.0 → 0.17.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 (62) hide show
  1. package/dist/check/generate.js +97 -8
  2. package/dist/cli/check.d.ts +3 -0
  3. package/dist/cli/check.js +55 -3
  4. package/dist/cli/explain.js +3 -0
  5. package/dist/cli/index.js +21 -3
  6. package/dist/cli/project.d.ts +5 -0
  7. package/dist/cli/project.js +1 -1
  8. package/dist/compiler/emit-client.js +33 -8
  9. package/dist/compiler/events.d.ts +9 -0
  10. package/dist/compiler/events.js +46 -0
  11. package/dist/compiler/html-expr.js +12 -7
  12. package/dist/compiler/index.js +32 -5
  13. package/dist/compiler/ir.d.ts +14 -0
  14. package/dist/compiler/ir.js +76 -10
  15. package/dist/compiler/lint.d.ts +6 -2
  16. package/dist/compiler/lint.js +114 -5
  17. package/dist/compiler/loops.d.ts +13 -0
  18. package/dist/compiler/loops.js +49 -0
  19. package/dist/compiler/parse.d.ts +23 -0
  20. package/dist/compiler/parse.js +149 -1
  21. package/dist/compiler/script.d.ts +2 -0
  22. package/dist/compiler/script.js +78 -1
  23. package/dist/forms/index.js +35 -15
  24. package/dist/forms/types.d.ts +7 -0
  25. package/dist/node/serve.d.ts +26 -0
  26. package/dist/node/serve.js +150 -0
  27. package/dist/runtime/dom.d.ts +32 -2
  28. package/dist/runtime/dom.js +164 -44
  29. package/dist/runtime/hot.js +3 -2
  30. package/dist/runtime/index.d.ts +1 -1
  31. package/dist/runtime/index.js +1 -1
  32. package/dist/runtime/loader.js +18 -6
  33. package/dist/runtime/motion.d.ts +59 -0
  34. package/dist/runtime/motion.js +339 -0
  35. package/dist/runtime/navigate.d.ts +2 -0
  36. package/dist/runtime/navigate.js +7 -1
  37. package/dist/runtime/navigation.d.ts +44 -0
  38. package/dist/runtime/navigation.js +69 -3
  39. package/dist/runtime/signals.d.ts +2 -0
  40. package/dist/runtime/signals.js +31 -0
  41. package/dist/seo.d.ts +140 -0
  42. package/dist/seo.js +64 -0
  43. package/dist/server/handler.d.ts +1 -0
  44. package/dist/server/handler.js +1 -0
  45. package/dist/server/head.js +5 -0
  46. package/dist/vite/cloudflare.d.ts +3 -0
  47. package/dist/vite/cloudflare.js +55 -0
  48. package/dist/vite/env.d.ts +19 -0
  49. package/dist/vite/env.js +34 -0
  50. package/dist/vite/headers.d.ts +27 -0
  51. package/dist/vite/headers.js +70 -0
  52. package/dist/vite/manifest.d.ts +11 -1
  53. package/dist/vite/manifest.js +10 -1
  54. package/dist/vite/netlify.d.ts +5 -2
  55. package/dist/vite/netlify.js +4 -2
  56. package/dist/vite/node.d.ts +4 -0
  57. package/dist/vite/node.js +9 -2
  58. package/dist/vite/plugin.d.ts +25 -2
  59. package/dist/vite/plugin.js +84 -9
  60. package/dist/vite/vercel.d.ts +3 -0
  61. package/dist/vite/vercel.js +53 -0
  62. package/package.json +14 -1
@@ -39,10 +39,23 @@ export declare function owner(node: Node): Element | null;
39
39
  export declare function provideOn(root: Element, cx: object): void;
40
40
  /** Slot ranges still open at `n`, among the siblings before it (#465). @internal */
41
41
  export declare function openSlots(n: Node): number;
42
+ /** The DOM's shape changed: counts kept so far may be wrong. */
43
+ /** @internal Nodes were added or removed: cached slot ranges are stale. */
44
+ export declare const reshaped: () => void;
45
+ /**
46
+ * Run `fn` with openSlots() counts kept between calls; nothing outside this file may add or remove nodes meanwhile.
47
+ * @internal The loader (attach) and the forms engine (refresh) call this.
48
+ */
49
+ export declare function keepSlots<T>(fn: () => T): T;
42
50
  /** Hole markers in `ctx`, by index. @internal */
43
51
  export declare function holes(ctx: Ctx): Hole[];
44
52
  /** @if block markers in `ctx`, by index. @internal */
45
53
  export declare function blocks(ctx: Ctx): Block[];
54
+ /** Turn HTML into nodes and put them before `end`; returns the first and last inserted node. @internal */
55
+ export declare function insertHtml(html: string, end: Node): {
56
+ first: Node | null;
57
+ last: Node | null;
58
+ };
46
59
  /**
47
60
  * An @if block. `which()` picks the branch (-1 for none). The server-rendered branch is wired in place; a change
48
61
  * removes the old branch (disposing its effects and handlers), inserts the new branch's HTML and wires it.
@@ -63,6 +76,17 @@ export declare function awaited(fn: () => unknown): {
63
76
  };
64
77
  /** @internal Compiled components call this; apps don't. */
65
78
  export declare function region(ctx: Ctx, index: number, html: (cx?: object) => string, wire: () => void, cx?: object): void;
79
+ /**
80
+ * @transition around a @for (clarusjs/motion): `in` gets each new row's nodes (`initial` for the server's rows on
81
+ * attach), `out` a removed row's (instead of removing them), and the update runs inside `frame` so moves can be
82
+ * measured before and after.
83
+ * @internal
84
+ */
85
+ export type ForTransition = {
86
+ in(nodes: Node[], initial?: boolean): void;
87
+ out(nodes: Node[]): void;
88
+ frame(start: Node, end: Node, run: () => void): void;
89
+ };
66
90
  /**
67
91
  * A @for list. Rows the server rendered are adopted in place. When the list changes, rows are matched by key:
68
92
  * kept rows keep their DOM nodes (moved only when out of order) and get the new item and index through their
@@ -76,7 +100,7 @@ export declare function forBlock(ctx: Ctx, index: number, list: () => unknown, k
76
100
  }, empty: {
77
101
  html: () => string;
78
102
  wire: (ctx: Ctx) => void;
79
- } | null): void;
103
+ } | null, t?: ForTransition): void;
80
104
  /** Keep exactly one text node between the hole's markers, set to `fn()`. @internal */
81
105
  export declare function text(hole: Hole, fn: () => unknown): void;
82
106
  /**
@@ -86,7 +110,13 @@ export declare function text(hole: Hole, fn: () => unknown): void;
86
110
  * for elements in that row.
87
111
  * @internal Compiled components call this; apps don't.
88
112
  */
89
- export declare function on(ctx: Ctx, index: number, type: string, fn: (e: Event) => void): void;
113
+ export declare function on(ctx: Ctx, index: number, type: string, fn: (e: Event, el: Element) => void): void;
114
+ /**
115
+ * $values (#444): el's form's fields (el's, or its form's) as an object; a repeated name gives a list, and a
116
+ * submit's button counts.
117
+ * @internal Compiled handlers call this.
118
+ */
119
+ export declare function values(el: Element, e?: Event): Record<string, unknown>;
90
120
  /**
91
121
  * [use]="behaviour" (#464): call each behaviour (one, or a list) with the element marked data-cl-u="index", once; a
92
122
  * function it returns runs when the branch, row or island goes.
@@ -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, spreadAttrs } from './attrs.js';
4
- import { batch, effect, onDispose, scope, state, untrack } from './signals.js';
4
+ import { afterWiring, 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) {
@@ -46,6 +46,23 @@ function contextAt(node, root, own) {
46
46
  }
47
47
  /** Slot ranges still open at `n`, among the siblings before it (#465). @internal */
48
48
  export function openSlots(n) {
49
+ if (slots) {
50
+ // While keepSlots() runs: from the nearest sibling on the left already counted, then rightwards, keeping each count. A list's
51
+ // rows then cost one pass, not one walk back per row. Left to right it's a depth: [s opens, ]s closes an open one.
52
+ const todo = [];
53
+ let s = n.previousSibling;
54
+ while (s && !slots.has(s)) {
55
+ todo.push(s);
56
+ s = s.previousSibling;
57
+ }
58
+ let depth = s ? step(slots.get(s), s) : 0;
59
+ for (let i = todo.length - 1; i >= 0; i--) {
60
+ slots.set(todo[i], depth);
61
+ depth = step(depth, todo[i]);
62
+ }
63
+ slots.set(n, depth);
64
+ return depth;
65
+ }
49
66
  let open = 0;
50
67
  let closed = 0;
51
68
  for (let s = n.previousSibling; s; s = s.previousSibling) {
@@ -59,6 +76,28 @@ export function openSlots(n) {
59
76
  }
60
77
  return open;
61
78
  }
79
+ // The slot ranges open before each node (#589 review), kept while keepSlots() runs: an island’s attach, a forms refresh.
80
+ let slots = null;
81
+ const step = (depth, n) => (n.nodeType !== 8 ? depth : n.data === '[s' ? depth + 1 : n.data === ']s' && depth ? depth - 1 : depth);
82
+ /** The DOM's shape changed: counts kept so far may be wrong. */
83
+ /** @internal Nodes were added or removed: cached slot ranges are stale. */
84
+ export const reshaped = () => { if (slots)
85
+ slots = new WeakMap(); };
86
+ /**
87
+ * Run `fn` with openSlots() counts kept between calls; nothing outside this file may add or remove nodes meanwhile.
88
+ * @internal The loader (attach) and the forms engine (refresh) call this.
89
+ */
90
+ export function keepSlots(fn) {
91
+ if (slots)
92
+ return fn();
93
+ slots = new WeakMap();
94
+ try {
95
+ return fn();
96
+ }
97
+ finally {
98
+ slots = null;
99
+ }
100
+ }
62
101
  /** The island a context's root stands for: itself when it is one, else the island it is in (null: none). */
63
102
  const islandOf = (root) => (root.localName === 'cl-island' ? root : owner(root));
64
103
  /**
@@ -110,11 +149,12 @@ function scan(ctx) {
110
149
  };
111
150
  // One pass (#465): `level` counts the islands entered below this one, minus the slot ranges entered (each moves its
112
151
  // content one island out), so the island's own comments are at 0 and nothing below 0 is (as owner() says, linearly).
113
- const visit = (parent, level) => {
152
+ // The siblings from `first` up to `stop` (not included), and what they hold.
153
+ const visit = (first, stop, level) => {
114
154
  let ranges = 0;
115
- for (let n = parent.firstChild; n; n = n.nextSibling) {
155
+ for (let n = first; n && n !== stop; n = n.nextSibling) {
116
156
  if (n.nodeType === 1)
117
- level < 0 || visit(n, level + (n.localName === 'cl-island' ? 1 : 0));
157
+ level < 0 || visit(n.firstChild, null, level + (n.localName === 'cl-island' ? 1 : 0));
118
158
  else if (n.nodeType !== 8)
119
159
  continue;
120
160
  else if (n.data === '[s') {
@@ -127,11 +167,17 @@ function scan(ctx) {
127
167
  level++;
128
168
  }
129
169
  }
130
- else if (!level && inside(ctx, n))
170
+ else if (!level && (near || inside(ctx, n)))
131
171
  take(n);
132
172
  }
133
173
  };
134
- visit(root, 0);
174
+ // A row whose markers are siblings: only between them, not the whole island once per row. Otherwise (a <tr> row
175
+ // straight in a <table>: the parser puts a <tbody> between them) the whole island, kept to the row's range.
176
+ const near = 'root' in ctx && ctx.start.parentNode === ctx.end.parentNode;
177
+ if (near)
178
+ visit(ctx.start.nextSibling, ctx.end, 0);
179
+ else
180
+ visit(root.firstChild, null, 0);
135
181
  return { holes, blocks, fors, regions };
136
182
  }
137
183
  /** Hole markers in `ctx`, by index. @internal */
@@ -142,13 +188,14 @@ export function holes(ctx) {
142
188
  export function blocks(ctx) {
143
189
  return scan(ctx).blocks;
144
190
  }
145
- /** Turn HTML into nodes and put them before `end`; returns the first and last inserted node. */
146
- function insertHtml(html, end) {
191
+ /** Turn HTML into nodes and put them before `end`; returns the first and last inserted node. @internal */
192
+ export function insertHtml(html, end) {
147
193
  const t = document.createElement('template');
148
194
  t.innerHTML = html;
149
195
  const first = t.content.firstChild;
150
196
  const last = t.content.lastChild;
151
197
  end.parentNode.insertBefore(t.content, end);
198
+ reshaped();
152
199
  // Child islands in the new HTML attach like the ones on the page (the loader registers this hook).
153
200
  const attachIslands = globalThis.__clAttach;
154
201
  if (attachIslands && end.parentNode)
@@ -187,6 +234,7 @@ export function block(ctx, index, which, branches) {
187
234
  dispose?.();
188
235
  while (b.start.nextSibling && b.start.nextSibling !== b.end)
189
236
  b.start.nextSibling.remove();
237
+ reshaped();
190
238
  if (k >= 0)
191
239
  insertHtml(branches[k].html(), b.end);
192
240
  current = k;
@@ -255,6 +303,7 @@ export function region(ctx, index, html, wire, cx) {
255
303
  dispose?.();
256
304
  while (r.start.nextSibling && r.start.nextSibling !== r.end)
257
305
  r.start.nextSibling.remove();
306
+ reshaped();
258
307
  insertHtml(next, r.end);
259
308
  }
260
309
  first = false;
@@ -269,7 +318,7 @@ export function region(ctx, index, html, wire, cx) {
269
318
  * the list is empty.
270
319
  * @internal Compiled components call this; apps don't.
271
320
  */
272
- export function forBlock(ctx, index, list, keyOf, row, empty) {
321
+ export function forBlock(ctx, index, list, keyOf, row, empty, t) {
273
322
  const f = scan(ctx).fors[index];
274
323
  if (!f)
275
324
  return;
@@ -310,40 +359,79 @@ export function forBlock(ctx, index, list, keyOf, row, empty) {
310
359
  });
311
360
  if (first) {
312
361
  first = false;
313
- rows = f.rows.map((range, i) => wireRow(range, keys[i], items[i], i));
362
+ if (f.rows.length === items.length) {
363
+ rows = f.rows.map((range, i) => wireRow(range, keys[i], items[i], i));
364
+ if (f.empty)
365
+ emptyRange = wireEmpty(f.empty);
366
+ if (t)
367
+ for (const r of rows)
368
+ t.in(rangeNodes(r.start, r.end), true);
369
+ return;
370
+ }
371
+ // The browser's list isn't the one the server rendered (it read something the server couldn't: the URL,
372
+ // storage): drop the server's rows and render the browser's below, as for any change.
373
+ for (const range of f.rows)
374
+ for (const node of rangeNodes(range.start, range.end))
375
+ node.parentNode?.removeChild(node);
314
376
  if (f.empty)
315
377
  emptyRange = wireEmpty(f.empty);
316
- return;
378
+ reshaped();
317
379
  }
318
- const old = new Map(rows.map((r) => [r.key, r]));
319
- const next = items.map((item, i) => {
320
- const kept = old.get(keys[i]);
321
- if (kept) {
322
- old.delete(keys[i]);
323
- kept.item.set(item);
324
- kept.index.set(i);
325
- return kept;
380
+ // With @transition, the update runs inside t.frame (clarusjs/motion measures rows before and after it).
381
+ const update = () => {
382
+ const added = [];
383
+ const old = new Map(rows.map((r) => [r.key, r]));
384
+ const next = items.map((item, i) => {
385
+ const kept = old.get(keys[i]);
386
+ if (kept) {
387
+ old.delete(keys[i]);
388
+ kept.item.set(item);
389
+ kept.index.set(i);
390
+ return kept;
391
+ }
392
+ const { first: start, last: end } = insertHtml(`<!--[r-->${row.html(item, i, items)}<!--]-->`, f.end);
393
+ const r = wireRow({ start: start, end: end }, keys[i], item, i);
394
+ added.push(r);
395
+ return r;
396
+ });
397
+ for (const r of old.values()) {
398
+ r.dispose();
399
+ // With @transition, a removed row leaves first (clarusjs/motion), else it goes now.
400
+ if (t)
401
+ t.out(rangeNodes(r.start, r.end));
402
+ else
403
+ for (const n of rangeNodes(r.start, r.end))
404
+ n.parentNode?.removeChild(n);
326
405
  }
327
- const { first: start, last: end } = insertHtml(`<!--[r-->${row.html(item, i, items)}<!--]-->`, f.end);
328
- return wireRow({ start: start, end: end }, keys[i], item, i);
329
- });
330
- for (const r of old.values()) {
331
- r.dispose();
332
- for (const n of rangeNodes(r.start, r.end))
333
- n.parentNode?.removeChild(n);
334
- }
335
- // Put rows in order, moving only those that are not already right after the previous one.
336
- let anchor = f.start;
337
- for (const r of next) {
338
- if (anchor.nextSibling !== r.start)
339
- anchor.after(...rangeNodes(r.start, r.end));
340
- anchor = r.end;
341
- }
342
- rows = next;
406
+ reshaped();
407
+ // Put rows in order, moving only those that are not already right after the previous one.
408
+ let anchor = f.start;
409
+ for (const r of next) {
410
+ // With @transition, leaving rows (no markers left) sit between live ones: stepped over, they keep their place.
411
+ let at = anchor.nextSibling;
412
+ if (t)
413
+ while (at && at.nodeType !== 8)
414
+ at = at.nextSibling;
415
+ if (at !== r.start) {
416
+ anchor.after(...rangeNodes(r.start, r.end));
417
+ reshaped();
418
+ }
419
+ anchor = r.end;
420
+ }
421
+ rows = next;
422
+ if (t)
423
+ for (const r of added)
424
+ t.in(rangeNodes(r.start, r.end));
425
+ };
426
+ if (t)
427
+ t.frame(f.start, f.end, update);
428
+ else
429
+ update();
343
430
  if (items.length && emptyRange) {
344
431
  emptyRange.dispose();
345
432
  for (const n of rangeNodes(emptyRange.start, emptyRange.end))
346
433
  n.parentNode?.removeChild(n);
434
+ reshaped();
347
435
  emptyRange = null;
348
436
  }
349
437
  else if (!items.length && !emptyRange && empty) {
@@ -399,7 +487,7 @@ export function on(ctx, index, type, fn) {
399
487
  continue;
400
488
  for (const h of [...(own.get(Number(i)) ?? [])])
401
489
  if (inside(h.ctx, el))
402
- h.fn(e);
490
+ h.fn(e, el);
403
491
  if (e.cancelBubble)
404
492
  break;
405
493
  }
@@ -413,6 +501,22 @@ export function on(ctx, index, type, fn) {
413
501
  onDispose(() => { const at = list.indexOf(handler); if (at !== -1)
414
502
  list.splice(at, 1); }); // a removed branch or row stops listening
415
503
  }
504
+ /**
505
+ * $values (#444): el's form's fields (el's, or its form's) as an object; a repeated name gives a list, and a
506
+ * submit's button counts.
507
+ * @internal Compiled handlers call this.
508
+ */
509
+ export function values(el, e) {
510
+ const form = el instanceof HTMLFormElement ? el : el.form ?? el.closest('form');
511
+ if (!form)
512
+ return {};
513
+ const data = new FormData(form, e?.submitter);
514
+ // own properties: a field named __proto__ stays a field
515
+ return Object.fromEntries([...new Set(data.keys())].map((k) => {
516
+ const all = data.getAll(k);
517
+ return [k, all.length > 1 ? all : all[0]];
518
+ }));
519
+ }
416
520
  /**
417
521
  * [use]="behaviour" (#464): call each behaviour (one, or a list) with the element marked data-cl-u="index", once; a
418
522
  * function it returns runs when the branch, row or island goes.
@@ -422,19 +526,35 @@ export function use(ctx, index, get) {
422
526
  const el = marked(ctx, `[data-cl-u="${index}"]`);
423
527
  if (!el)
424
528
  return;
425
- for (const fn of [untrack(get)].flat()) {
426
- if (typeof fn !== 'function')
427
- continue;
428
- const dispose = fn(el);
429
- if (typeof dispose === 'function')
430
- onDispose(dispose);
431
- }
529
+ // After the wiring around it (#550), if its element is still there.
530
+ afterWiring(() => {
531
+ if (!el.isConnected)
532
+ return;
533
+ for (const fn of [untrack(get)].flat()) {
534
+ if (typeof fn !== 'function')
535
+ continue;
536
+ const dispose = fn(el);
537
+ if (typeof dispose === 'function')
538
+ onDispose(dispose);
539
+ }
540
+ });
432
541
  }
433
542
  /** The element matching `selector` in `ctx`: this island's (not a nested island's), and in a row, that row's. */
434
543
  export function marked(ctx, selector) {
435
544
  const root = rootOf(ctx);
436
545
  const island = islandOf(root);
437
- return [...root.querySelectorAll(selector)].find((e) => owner(e) === island && inside(ctx, e));
546
+ const mine = (e) => owner(e) === island;
547
+ if (!('root' in ctx) || ctx.start.parentNode !== ctx.end.parentNode)
548
+ return [...root.querySelectorAll(selector)].find((e) => mine(e) && inside(ctx, e));
549
+ // A row with sibling markers: the elements between them, not the whole island's once per row.
550
+ for (let n = ctx.start.nextSibling; n && n !== ctx.end; n = n.nextSibling) {
551
+ if (n.nodeType !== 1)
552
+ continue;
553
+ const found = (n.matches(selector) ? [n] : []).concat([...n.querySelectorAll(selector)]).find(mine);
554
+ if (found)
555
+ return found;
556
+ }
557
+ return undefined;
438
558
  }
439
559
  /**
440
560
  * A reactive spread (#405): sets the attributes the object has, removes the ones it no longer has (only those it set:
@@ -8,7 +8,8 @@ export async function swap(url) {
8
8
  const next = new URL(url, location.href);
9
9
  const pathOf = (el) => new URL(el.getAttribute('src') ?? '', location.href).pathname;
10
10
  // The outermost islands of this component: one nested in another is swapped with it.
11
- const mine = (root) => [...root.querySelectorAll('cl-island')].filter((el) => pathOf(el) === next.pathname && !el.parentElement?.closest(`cl-island[src="${CSS.escape(el.getAttribute('src'))}"]`));
11
+ // A copy still leaving (@transition: inert) isn't the page's any more: not counted.
12
+ const mine = (root) => [...root.querySelectorAll('cl-island')].filter((el) => pathOf(el) === next.pathname && !el.closest('[inert]') && !el.parentElement?.closest(`cl-island[src="${CSS.escape(el.getAttribute('src'))}"]`));
12
13
  const old = mine(document);
13
14
  if (!old.length)
14
15
  return;
@@ -28,7 +29,7 @@ export async function swap(url) {
28
29
  swapped.replaceChildren(...[...fresh[i].childNodes].map((n) => document.importNode(n, true)));
29
30
  swapped.setAttribute('s', fresh[i].getAttribute('s') ?? '{}');
30
31
  // State: what the server rendered with, then what the running version had, island by island in page order.
31
- const before = [el, ...el.querySelectorAll('cl-island')];
32
+ const before = [el, ...[...el.querySelectorAll('cl-island')].filter((x) => !x.closest('[inert]'))];
32
33
  const after = [swapped, ...swapped.querySelectorAll('cl-island')];
33
34
  if (before.length === after.length) {
34
35
  before.forEach((b, j) => {
@@ -1,5 +1,5 @@
1
1
  export { state, derived, shared, effect, scope, onDispose, untrack, batch, type Signal, type ReadSignal } from './signals.ts';
2
- export { holes, blocks, text, on, attr, spread, block, awaited, forBlock, region, model, modelInput, emit, listen, declare, use, provideOn, type Hole, type Block, type Ctx } from './dom.ts';
2
+ export { holes, blocks, text, on, attr, spread, block, awaited, forBlock, region, model, modelInput, emit, listen, declare, use, values, provideOn, type Hole, type Block, type Ctx } from './dom.ts';
3
3
  export { escape, attrHtml, spreadHtml, blockHtml, forHtml, regionHtml } from './html.ts';
4
4
  export { parse as parseIslandData } from './serialize.ts';
5
5
  export { classValue, styleValue } from './attrs.ts';
@@ -1,6 +1,6 @@
1
1
  // Browser runtime used by compiled attach() functions.
2
2
  export { state, derived, shared, effect, scope, onDispose, untrack, batch } from './signals.js';
3
- export { holes, blocks, text, on, attr, spread, block, awaited, forBlock, region, model, modelInput, emit, listen, declare, use, provideOn } from './dom.js';
3
+ export { holes, blocks, text, on, attr, spread, block, awaited, forBlock, region, model, modelInput, emit, listen, declare, use, values, provideOn } from './dom.js';
4
4
  export { escape, attrHtml, spreadHtml, blockHtml, forHtml, regionHtml } from './html.js';
5
5
  export { parse as parseIslandData } from './serialize.js';
6
6
  export { classValue, styleValue } from './attrs.js';
@@ -17,7 +17,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
17
17
  }
18
18
  return path;
19
19
  };
20
- import { owner } from './dom.js';
20
+ import { keepSlots, owner } from './dom.js';
21
21
  import { parse } from './serialize.js';
22
22
  import { scope } from './signals.js';
23
23
  // Islands scheduled or loading. `data-cl-ready` is set only after attach() succeeds.
@@ -120,9 +120,9 @@ async function attachOne(el, load, recorded) {
120
120
  let state;
121
121
  try {
122
122
  mod = await load(el.getAttribute('src'));
123
- props = parse(el.getAttribute('p') ?? '{}');
124
- const s = el.getAttribute('s');
125
- state = s === null ? undefined : parse(s);
123
+ // Waiting (idle, visible, interaction) or loading while it started leaving (@transition: inert): it's going.
124
+ if (el.closest('[inert]'))
125
+ return;
126
126
  // Islands in it holding its content attach first (#465): content it re-renders there reads what they provide. One
127
127
  // failing to load fails this one too (both retry). ponytail: later ones (idle, visible, interaction) aren't waited for.
128
128
  for (const inner of el.querySelectorAll('cl-island')) {
@@ -132,6 +132,10 @@ async function attachOne(el, load, recorded) {
132
132
  if (!pending.has(inner))
133
133
  throw 0;
134
134
  }
135
+ // Read last: a parent may have passed new props meanwhile (#524).
136
+ props = parse(el.getAttribute('p') ?? '{}');
137
+ const s = el.getAttribute('s');
138
+ state = s === null ? undefined : parse(s);
135
139
  }
136
140
  catch (err) {
137
141
  recorded.stop();
@@ -144,7 +148,7 @@ async function attachOne(el, load, recorded) {
144
148
  rendered.set(el, rootAttrs(el));
145
149
  attached.set(el, scope(() => {
146
150
  // An island with inputs returns how to update them: kept for a parent island that passes new props.
147
- const update = mod.attach(el, props, state);
151
+ const update = keepSlots(() => mod.attach(el, props, state)); // openSlots counts kept while it wires (dom.ts)
148
152
  if (typeof update === 'function')
149
153
  updates.set(el, update);
150
154
  }));
@@ -230,7 +234,10 @@ globalThis.__clUpdate = (el, next) => {
230
234
  // The same component: the same name and the same source (two panel.tscl files share a name). An attached island
231
235
  // without inputs has nothing to update (#465: re-rendered because the content it holds changed, which its parent
232
236
  // wires in place).
233
- if (!attached.has(el) || el.getAttribute('c') !== next.getAttribute('c') || el.getAttribute('src') !== next.getAttribute('src'))
237
+ if (el.getAttribute('c') !== next.getAttribute('c') || el.getAttribute('src') !== next.getAttribute('src'))
238
+ return false;
239
+ // Not attached yet but on its way: it takes the new props and attaches with them (#524).
240
+ if (!attached.has(el) && !loading.has(el))
234
241
  return false;
235
242
  const p = next.getAttribute('p') ?? '{}';
236
243
  el.setAttribute('p', p);
@@ -241,6 +248,11 @@ globalThis.__clUpdate = (el, next) => {
241
248
  for (const { name, value } of [...next.attributes])
242
249
  if (name.startsWith('data-cl-path-'))
243
250
  el.setAttribute(name, value);
251
+ if (!attached.has(el)) {
252
+ // And the markup rendered for them, which it adopts.
253
+ el.replaceChildren(...next.childNodes);
254
+ return true;
255
+ }
244
256
  const root = el.firstElementChild;
245
257
  const before = rendered.get(el);
246
258
  const after = rootAttrs(next);
@@ -0,0 +1,59 @@
1
+ import { type Ctx } from './dom.ts';
2
+ import type { Signal } from './signals.ts';
3
+ /**
4
+ * What `@transition (name, options)` takes after the name.
5
+ * @example
6
+ * <script>
7
+ * const slide = { mode: 'out-in', duration: { enter: 200, leave: 150 } }
8
+ * </script>
9
+ * @transition ('slide', slide) { @if (step === 1) { <p>One</p> } @else { <p>Two</p> } }
10
+ * @see https://clarusjs.com/guide/animations#transition
11
+ */
12
+ export type TransitionOptions = {
13
+ /** Default: the old branch leaves while the new one enters. `out-in`: the new one comes once the old one is gone. `in-out`: the new one enters first, then the old one leaves. */
14
+ mode?: 'out-in' | 'in-out';
15
+ /** Run enter on the branch the server rendered, when the island attaches. */
16
+ appear?: boolean;
17
+ /** Milliseconds to wait instead of the element's animations (Vue's `:duration`). Not waited under reduced motion. */
18
+ duration?: number | {
19
+ enter?: number;
20
+ leave?: number;
21
+ };
22
+ /** `false`: no classes, only the hooks. */
23
+ css?: false;
24
+ onBeforeEnter?(el: Element): void;
25
+ /** With a second parameter, the enter waits until `done()` is called. */
26
+ onEnter?(el: Element, done: () => void): void;
27
+ onAfterEnter?(el: Element): void;
28
+ /** The branch changed while this element was still entering: it leaves from where it is. */
29
+ onEnterCancelled?(el: Element): void;
30
+ onBeforeLeave?(el: Element): void;
31
+ /** With a second parameter, the leave waits until `done()` is called. */
32
+ onLeave?(el: Element, done: () => void): void;
33
+ onAfterLeave?(el: Element): void;
34
+ };
35
+ type Branch = {
36
+ html: () => string;
37
+ wire: () => void;
38
+ };
39
+ /**
40
+ * The @if block of `@transition (name, options) { @if … }`: as dom.ts's block(), with enter and leave transitions.
41
+ * @internal Compiled components call this; apps don't.
42
+ */
43
+ export declare function transitionBlock(ctx: Ctx, index: number, which: () => number, branches: Branch[], name: string, options?: TransitionOptions): void;
44
+ /** A row's template, as the compiler passes it to forBlock. */
45
+ type RowParts = {
46
+ html: (item: any, i: number, list: unknown[]) => string;
47
+ wire: (ctx: Ctx, item: Signal<any>, index: Signal<number>, count: Signal<number>) => void;
48
+ };
49
+ /**
50
+ * The @for block of `@transition (name, options) { @for … }`: dom.ts's forBlock(), with rows entering and leaving
51
+ * with the classes, and rows that move sliding into place (FLIP: measured before and after the update, put back with
52
+ * an inverse transform, then `<name>-move` and the transform off, so its CSS transition animates the move).
53
+ * @internal Compiled components call this; apps don't.
54
+ */
55
+ export declare function transitionFor(ctx: Ctx, index: number, list: () => unknown, keyOf: (item: any, i: number) => unknown, row: RowParts, empty: {
56
+ html: () => string;
57
+ wire: (ctx: Ctx) => void;
58
+ } | null, name: string, options?: TransitionOptions): void;
59
+ export {};