@wardic/dom 1.0.0-rc.8 → 1.0.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 (86) hide show
  1. package/dist/Appear.d.ts +7 -1
  2. package/dist/Appear.d.ts.map +1 -1
  3. package/dist/Appear.js +7 -1
  4. package/dist/Appear.js.map +1 -1
  5. package/dist/DomBatch.d.ts +20 -0
  6. package/dist/DomBatch.d.ts.map +1 -0
  7. package/dist/DomBatch.js +23 -0
  8. package/dist/DomBatch.js.map +1 -0
  9. package/dist/DomInput.d.ts +2 -1
  10. package/dist/DomInput.d.ts.map +1 -1
  11. package/dist/DomInput.js +3 -1
  12. package/dist/DomInput.js.map +1 -1
  13. package/dist/KeyboardPress.d.ts +16 -0
  14. package/dist/KeyboardPress.d.ts.map +1 -0
  15. package/dist/KeyboardPress.js +17 -0
  16. package/dist/KeyboardPress.js.map +1 -0
  17. package/dist/ReorderController.d.ts +24 -4
  18. package/dist/ReorderController.d.ts.map +1 -1
  19. package/dist/ReorderController.js +38 -4
  20. package/dist/ReorderController.js.map +1 -1
  21. package/dist/index.d.ts +5 -3
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +3 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/internal/appear-slot.d.ts +23 -0
  26. package/dist/internal/appear-slot.d.ts.map +1 -0
  27. package/dist/internal/appear-slot.js +29 -0
  28. package/dist/internal/appear-slot.js.map +1 -0
  29. package/dist/internal/appear.d.ts +84 -16
  30. package/dist/internal/appear.d.ts.map +1 -1
  31. package/dist/internal/appear.js +302 -61
  32. package/dist/internal/appear.js.map +1 -1
  33. package/dist/internal/batch-writer.d.ts +16 -0
  34. package/dist/internal/batch-writer.d.ts.map +1 -0
  35. package/dist/internal/batch-writer.js +77 -0
  36. package/dist/internal/batch-writer.js.map +1 -0
  37. package/dist/internal/gate.d.ts +3 -2
  38. package/dist/internal/gate.d.ts.map +1 -1
  39. package/dist/internal/gate.js +5 -1
  40. package/dist/internal/gate.js.map +1 -1
  41. package/dist/internal/input.d.ts +0 -6
  42. package/dist/internal/input.d.ts.map +1 -1
  43. package/dist/internal/input.js +205 -23
  44. package/dist/internal/input.js.map +1 -1
  45. package/dist/internal/keyboard-press.d.ts +7 -0
  46. package/dist/internal/keyboard-press.d.ts.map +1 -0
  47. package/dist/internal/keyboard-press.js +45 -0
  48. package/dist/internal/keyboard-press.js.map +1 -0
  49. package/dist/internal/measure.d.ts +7 -0
  50. package/dist/internal/measure.d.ts.map +1 -1
  51. package/dist/internal/measure.js +19 -1
  52. package/dist/internal/measure.js.map +1 -1
  53. package/dist/internal/projection.d.ts +13 -5
  54. package/dist/internal/projection.d.ts.map +1 -1
  55. package/dist/internal/projection.js +271 -33
  56. package/dist/internal/projection.js.map +1 -1
  57. package/dist/internal/renderer.d.ts +18 -0
  58. package/dist/internal/renderer.d.ts.map +1 -1
  59. package/dist/internal/renderer.js +4 -2
  60. package/dist/internal/renderer.js.map +1 -1
  61. package/dist/internal/reorder.d.ts +28 -13
  62. package/dist/internal/reorder.d.ts.map +1 -1
  63. package/dist/internal/reorder.js +55 -26
  64. package/dist/internal/reorder.js.map +1 -1
  65. package/dist/internal/reveal.d.ts +7 -3
  66. package/dist/internal/reveal.d.ts.map +1 -1
  67. package/dist/internal/reveal.js +8 -3
  68. package/dist/internal/reveal.js.map +1 -1
  69. package/package.json +4 -4
  70. package/src/Appear.ts +12 -3
  71. package/src/DomBatch.ts +22 -0
  72. package/src/DomInput.ts +2 -1
  73. package/src/KeyboardPress.ts +16 -0
  74. package/src/ReorderController.ts +54 -4
  75. package/src/index.ts +5 -3
  76. package/src/internal/appear-slot.ts +32 -0
  77. package/src/internal/appear.ts +462 -67
  78. package/src/internal/batch-writer.ts +95 -0
  79. package/src/internal/gate.ts +7 -3
  80. package/src/internal/input.ts +209 -25
  81. package/src/internal/keyboard-press.ts +45 -0
  82. package/src/internal/measure.ts +17 -1
  83. package/src/internal/projection.ts +272 -38
  84. package/src/internal/renderer.ts +4 -2
  85. package/src/internal/reorder.ts +85 -39
  86. package/src/internal/reveal.ts +10 -4
@@ -24,7 +24,10 @@
24
24
  * coordinator. A running node whose box didn't change is left alone. A trigger
25
25
  * during the pass's own flush repeats it (at most 5 rounds). Every node the pass
26
26
  * seeded takes its first step at the Frame loop's next tick, whose end flushes the
27
- * gate, so it moves in that Refresh frame whatever the Clock's state (ADR-0033).
27
+ * gate, so it moves in that Refresh frame whatever the Clock's state (ADR-0033). A
28
+ * pass that runs after its frame's tick, before the paint (a trigger during a tick,
29
+ * or the engine's observer delivery), takes the first step of the nodes it seeded
30
+ * itself, so they move in that frame too (ADR-0018 *Amendment (#673)*).
28
31
  * - COORDINATOR (one run on the Environment's Frame loop while >=1 node animates,
29
32
  * Q4; ADR-0027, #75): each tick steps every active node's progress spring, then
30
33
  * `deriveAll()` walks the active nodes TOP-DOWN (ancestors first) computing
@@ -59,6 +62,7 @@
59
62
  * `measure.ts` is the port's surface, and #16 maps what this file still does inline.
60
63
  */
61
64
  import * as Box from "@wardic/core/Box";
65
+ import * as Clock from "@wardic/core/Clock";
62
66
  import * as FrameLoop from "@wardic/core/FrameLoop";
63
67
  import * as Inertia from "@wardic/core/Inertia";
64
68
  import * as Microtask from "@wardic/core/Microtask";
@@ -86,10 +90,10 @@ import * as Scope from "effect/Scope";
86
90
  import * as Str from "effect/String";
87
91
  import * as UndefinedOr from "effect/UndefinedOr";
88
92
  import * as AtomRegistry from "effect/reactivity/AtomRegistry";
89
- import { holdTransform, isHoldable, isScrollRoot, measureDocumentScroll, measureScroll, readBoxSize, readLayoutBox, releaseTransforms, scrollStyleFrom, takesFlowSpace, transformStyleFrom, transitionRunning, contains, readCustomBases as readCustomBasesOf, readOpacity, readStickyShifts, } from "./measure.js";
93
+ import { holdTransform, isHoldable, isScrollRoot, measureDocumentScroll, measureScroll, readBoxSize, readLayoutBox, releaseTransforms, scrollStyleFrom, takesFlowSpace, transformStyleFrom, transitionRunning, contains, readCustomBases as readCustomBasesOf, frameTime, readOpacity, readStickyShifts, } from "./measure.js";
90
94
  import { DEFAULT_DRAG_TRANSITION, DEFAULT_LAYOUT_TRANSITION } from "./projection-defaults.js";
91
95
  import { counted, watchPage } from "./content-watch.js";
92
- import { pending as appearRuns } from "./appear.js";
96
+ import { pending as appearRuns } from "./appear-slot.js";
93
97
  import { debugOf, reportFailure } from "./report.js";
94
98
  import { blendRadius, borderRadiusCorrector, isUnitScale, SCALE_CORRECTORS, scaleRadius, ZERO_RADIUS } from "./scale-correction.js";
95
99
  import { dashCase } from "./svg.js";
@@ -364,6 +368,11 @@ const landedFlight = () => ({ position: 1, elapsed: 0, spring: Spring.init({ fro
364
368
  const MIN_CURVE_DISTANCE = 20;
365
369
  /** No links: an ancestor that doesn't move the twin, or the top of the chain. */
366
370
  const NO_LINKS = [];
371
+ /**
372
+ * The map of an ancestor that drew no transform at the pass (#687): a Wardic element at identity,
373
+ * linked so a translate or scale it starts after the pass is followed.
374
+ */
375
+ const IDENTITY_MAP = { sx: 1, sy: 1, tx: 0, ty: 0 };
367
376
  /** A translate `(tx, ty)` and a scale `(sx, sy)` about `origin`, as CSS composes them: `p → origin + t + s·(p − origin)`. Read phase. */
368
377
  const mapAbout = (origin, tx, ty, sx, sy) => ({
369
378
  sx,
@@ -567,13 +576,14 @@ const NO_OWN_TRANSLATE = () => NO_TRANSLATE;
567
576
  /** No values registered for an element (#443): an app's corrector reads its base then. */
568
577
  const NO_VALUES = {};
569
578
  /**
570
- * The transform a Measure pass holds `node` at (#443): its Transform template's, `""` (cleared) for a
571
- * node with none. Per pass per node: a field read and an early return.
579
+ * The transform a Measure pass holds `node` at (#443): its Transform template's, `none` for a node
580
+ * with none (Motion's `resetTransform`), which is what an at-rest node already draws (#674). Per pass
581
+ * per node: a field read and an early return.
572
582
  */
573
583
  const heldTransformFor = (node) => {
574
584
  const held = node.heldTransform;
575
585
  if (Predicate.isUndefined(held))
576
- return "";
586
+ return "none";
577
587
  return held();
578
588
  };
579
589
  /** No own rotation or opacity of its own: a node the binding gives none, or a leaving one (*Amendment (#166)*). */
@@ -784,6 +794,9 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
784
794
  // The coordinator's run on the Frame loop (#75): at most one, from the first node that
785
795
  // animates until none does. In the same Scope, so disposing the system interrupts it.
786
796
  const coordinator = runtime.runSync(FiberHandle.make().pipe(Scope.provide(scope)));
797
+ // The Environment's Frame loop: the coordinator runs on it, the engine's hook is on its ticks, and a
798
+ // trigger reads whether its Late window is open (#673, #680).
799
+ const loop = runtime.runSync(Effect.service(FrameLoop.FrameLoop));
787
800
  // Whether the coordinator's step is running: a stop from inside it settles the run.
788
801
  let inStep = false;
789
802
  // The turn a turning flight's curve gives at its target just placed (#438), for `derive` to draw:
@@ -795,6 +808,17 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
795
808
  let passQueued = false;
796
809
  let inPass = false;
797
810
  let triggeredDuringPass = false;
811
+ // An after-tick pass (ADR-0018 *Amendment (#673)*): whether the queued pass runs after its frame's
812
+ // tick, before the paint, as every trigger for it did (one during a Frame loop tick, its hooks
813
+ // included; one during the engine's observer delivery; one during an after-tick pass's own step);
814
+ // whether the observer is delivering; and whether an after-tick pass's step is being applied.
815
+ let passAfterTick = false;
816
+ let inDelivery = false;
817
+ let inAfterTickStep = false;
818
+ // The frame time and dt of the Frame loop's last tick, from the engine's `afterTick` hook: an
819
+ // after-tick pass in that frame steps at its dt. `-2` before any, which no frame time equals.
820
+ let tickFrame = -2;
821
+ let tickDt = 0;
798
822
  // Whether a **Declared layout change** in this task has taken its Measurements (#4): the end-of-task
799
823
  // pass, which animates from them, clears it.
800
824
  let declared = false;
@@ -1045,6 +1069,19 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
1045
1069
  return undefined;
1046
1070
  return stack[i - 1];
1047
1071
  };
1072
+ /**
1073
+ * The newest twin below `stack[from]` that isn't leaving, with its index: the one a Lead that leaves
1074
+ * hands over to (#682). Motion's `NodeStack.relegate` promotes only a member still present, so a
1075
+ * twin on a page that is leaving too never takes over. `undefined` when every one is leaving.
1076
+ */
1077
+ const presentBelow = (stack, from) => {
1078
+ for (let i = from - 1; i >= 0; i--) {
1079
+ const node = stack[i];
1080
+ if (Predicate.isNotUndefined(node) && Bool.not(node.leaving))
1081
+ return [node, i];
1082
+ }
1083
+ return undefined;
1084
+ };
1048
1085
  /** Whether `node` is a Follower: in a `layoutId` stack but NOT its (newest-live) Lead. */
1049
1086
  const isFollower = (node) => Predicate.isNotUndefined(node.layoutId) && leadOf(node.layoutId) !== node;
1050
1087
  /**
@@ -1582,8 +1619,9 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
1582
1619
  */
1583
1620
  const layoutTargetOf = (node, box) => {
1584
1621
  // ISOLATED (ADR-0013, *Amendment (#33)*): a layoutRoot sits at its new box, its projection
1585
- // counter-acting its ancestors' layout animations; only their transforms carry it.
1586
- if (node.layoutRoot)
1622
+ // counter-acting its ancestors' layout animations; only their transforms carry it. Not one with no
1623
+ // layout of its own (Layout mode `false`, #691): it rides them, as Motion's does.
1624
+ if (node.isolating)
1587
1625
  return offsetBox(box, carryOf(node));
1588
1626
  const rp = relativeParentOf(node);
1589
1627
  const rpBox = boxOf(rp);
@@ -1994,6 +2032,40 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
1994
2032
  return undefined;
1995
2033
  return UndefinedOr.map(linksAt(el, reads, shift, documentScroll), (own) => Arr.appendAll(above, own));
1996
2034
  };
2035
+ /**
2036
+ * The Wardic elements above `el` up to `<body>` that draw no transform now (#687), into `out`: not a
2037
+ * projection node (linked for its own scale already), not one this pass read for its transform
2038
+ * (`reads`), not one a running transition holds. `walked` keeps a walk from going over what an
2039
+ * earlier twin's walked. Touches no DOM but the parent links. Read phase only.
2040
+ */
2041
+ const untransformedAbove = (el, reads, walked, out) => {
2042
+ let at = el;
2043
+ while (Predicate.isNotNull(at)) {
2044
+ if (isDocumentTop(at))
2045
+ return;
2046
+ // Walked from another twin, and so everything above it too.
2047
+ if (walked.has(at))
2048
+ return;
2049
+ walked.add(at);
2050
+ const candidate = at;
2051
+ at = at.parentElement;
2052
+ if (elementToNode.has(candidate))
2053
+ continue;
2054
+ if (Bool.not(elementValues.has(candidate)))
2055
+ continue;
2056
+ if (reads.has(candidate))
2057
+ continue;
2058
+ if (passUnheld.has(candidate))
2059
+ continue;
2060
+ out.push(candidate);
2061
+ }
2062
+ };
2063
+ /** `el`'s transform origin on the page: its box as read now, and its computed `transform-origin`. Read phase only. */
2064
+ const pageOriginOf = (el) => {
2065
+ const box = el.getBoundingClientRect();
2066
+ const origin = transformOriginOf(el);
2067
+ return { x: box.left + origin.x, y: box.top + origin.y };
2068
+ };
1997
2069
  /**
1998
2070
  * The ancestors of twin `node` whose transforms move it, outermost first, in its frame (ADR-0012,
1999
2071
  * *Amendment (#137)*): each element this pass found moving it, and each projection node above it,
@@ -2681,17 +2753,26 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
2681
2753
  // so this is still the minimal set, not a widening.
2682
2754
  // A templated element is held at its Transform template's with no transform of Wardic's (#443,
2683
2755
  // Motion's `transformTemplate(values, "")`), so a static part of it, such as centring, is measured.
2756
+ // A node that already draws what it's held at, as every at-rest node does (`none`), isn't written
2757
+ // or restored (#674, Motion's `resetTransform` skips it too): `undefined` in `savedTransforms`.
2684
2758
  const savedTransforms = [];
2685
2759
  for (const node of ordered) {
2686
2760
  // Its app's `onBeforeLayoutMeasure` (#443), before its transform is held for the read.
2687
2761
  if (Predicate.isNotUndefined(node.onBeforeMeasure))
2688
2762
  callMeasureHook(node.onBeforeMeasure, "onBeforeLayoutMeasure");
2689
- savedTransforms.push(node.element.style.transform);
2690
- node.element.style.transform = heldTransformFor(node);
2763
+ const own = node.element.style.transform;
2764
+ const held = heldTransformFor(node);
2691
2765
  // A server's Appear animation not yet handed off (#245, ADR-0044 *Amendment (#245)*): its CSS
2692
2766
  // transform beats an inline one, but not an important one. Its timeline runs on; the restore
2693
2767
  // below gives the old value back, at its own priority.
2694
- if (appearRuns(node.element))
2768
+ const appearing = appearRuns(node.element);
2769
+ if (Bool.and(own === held, Bool.not(appearing))) {
2770
+ savedTransforms.push(undefined);
2771
+ continue;
2772
+ }
2773
+ savedTransforms.push(own);
2774
+ node.element.style.transform = held;
2775
+ if (appearing)
2695
2776
  node.element.style.setProperty("transform", "none", "important");
2696
2777
  }
2697
2778
  // --- READ: no DOM writes until the next banner. ---------------------------
@@ -2795,13 +2876,27 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
2795
2876
  holds = Arr.map(wanted, ([el, style]) => holdTransform(el, style));
2796
2877
  // --- READ: each one's box, held, and so its transform origin on the page. ------
2797
2878
  Arr.forEach(wanted, ([el], k) => {
2798
- const box = el.getBoundingClientRect();
2799
- const origin = transformOriginOf(el);
2879
+ const origin = pageOriginOf(el);
2800
2880
  ancestorReads.set(el, {
2801
- origin: { x: box.left + origin.x, y: box.top + origin.y },
2802
- pass: UndefinedOr.map(passTransforms[k], (m) => mapAbout({ x: box.left + origin.x, y: box.top + origin.y }, m.e, m.f, m.a, m.d)),
2881
+ origin,
2882
+ pass: UndefinedOr.map(passTransforms[k], (m) => mapAbout(origin, m.e, m.f, m.a, m.d)),
2803
2883
  });
2804
2884
  });
2885
+ // Each moved twin's Wardic ancestors at identity (#687): read as the others are, so its chain links
2886
+ // them, and a translate or scale one starts after this pass is followed. Only when a twin moved.
2887
+ let walked;
2888
+ for (let i = 0; i < ordered.length; i++) {
2889
+ if (Predicate.isUndefined(ordered[i].layoutId))
2890
+ continue;
2891
+ if (Bool.not(moved[i]))
2892
+ continue;
2893
+ if (Predicate.isUndefined(walked))
2894
+ walked = new Set();
2895
+ const untransformed = [];
2896
+ untransformedAbove(ordered[i].element.parentElement, ancestorReads, walked, untransformed);
2897
+ for (const el of untransformed)
2898
+ ancestorReads.set(el, { origin: pageOriginOf(el), pass: IDENTITY_MAP });
2899
+ }
2805
2900
  // --- READ: those nodes' boxes again, with nothing transformed. ----------
2806
2901
  if (Arr.isArrayNonEmpty(wanted)) {
2807
2902
  for (const i of under) {
@@ -2824,8 +2919,11 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
2824
2919
  // replaced could only ever strand one), and the gate's `flush` has no
2825
2920
  // `finally` of its own, so it would stay latched mid-flush.
2826
2921
  releaseTransforms(holds);
2827
- for (let i = 0; i < ordered.length; i++)
2828
- ordered[i].element.style.transform = savedTransforms[i];
2922
+ for (let i = 0; i < ordered.length; i++) {
2923
+ const saved = savedTransforms[i];
2924
+ if (Predicate.isNotUndefined(saved))
2925
+ ordered[i].element.style.transform = saved;
2926
+ }
2829
2927
  }
2830
2928
  // Each moved twin's paint deltas, between its box and its box as painted, both in its
2831
2929
  // Scroll-stable frame (the fold is the same shift for the two). Before the loop below
@@ -2878,6 +2976,13 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
2878
2976
  dropBox(node);
2879
2977
  continue;
2880
2978
  }
2979
+ // Whether a layoutRoot isolates its subtree (ADR-0013, *Amendment (#33)*): from the first pass that
2980
+ // finds it a layout of its own, and from then on, as Motion's (its `didUpdate` handler, attached once
2981
+ // `layout` or `layoutId` is set, at mount or a later change, is never taken off). One only a
2982
+ // `layoutRoot` makes (Layout mode `false`) is its subtree's frame and no more (#691). Read here, and
2983
+ // only until then: a read each frame cost bench:solid's page scene 7–12% at 330 roots, and each pass 5%.
2984
+ if (node.layoutRoot && !node.isolating)
2985
+ node.isolating = node.layoutMode() !== false;
2881
2986
  // Store the box in the SCROLL-STABLE frame (ADR-0012), so a scroll change
2882
2987
  // between triggers doesn't leak into the delta. Every scroll offset was
2883
2988
  // sampled in the read phase, so the whole ancestor chain is available.
@@ -3021,10 +3126,12 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
3021
3126
  // moves from where the leaver painted, as a layout change, no handover: rebased as a
3022
3127
  // Switch seed is, and drawn there on its first frame, its own scale divided out. A Switch
3023
3128
  // seed the same leaving twin left is passed over: the key decides where it comes from.
3129
+ // Shaped by its Layout mode before the check, as a restart's is: one that leaves nothing to
3130
+ // move, a node with no layout of its own (Layout mode `false`, #691) among them, starts none.
3024
3131
  if (Predicate.isNotUndefined(enteredFrom)) {
3025
- const origin = undrawnFor(node, unpaintedOf(node, offsetUnlessZero(enteredFrom, rootFrameShift(node, documentScroll))));
3132
+ const origin = shapedOrigin(node, undrawnFor(node, unpaintedOf(node, offsetUnlessZero(enteredFrom, rootFrameShift(node, documentScroll)))), newBox);
3026
3133
  if (Bool.not(boxesEqual(origin, newBox))) {
3027
- node.origin = shapedOrigin(node, origin, newBox);
3134
+ node.origin = origin;
3028
3135
  node.layoutBox = newBox;
3029
3136
  node.flight = startFlight(node);
3030
3137
  node.animating = true;
@@ -3130,7 +3237,9 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
3130
3237
  * of `sink.flushPass(round)` until a round ends with no new trigger (a trigger
3131
3238
  * during a round comes from an effect in its flush), at most {@link MAX_ROUNDS};
3132
3239
  * past the cap the rest runs as a fresh Measure pass in the next task. The nodes it
3133
- * seeded take their first step at the Frame loop's next tick (ADR-0033). Traced as one
3240
+ * seeded take their first step at the Frame loop's next tick (ADR-0033), or, in a pass that runs
3241
+ * after its frame's tick, in the pass itself, after its rounds (ADR-0018 *Amendment (#673)*): how
3242
+ * many is its span's `stepped`. Traced as one
3134
3243
  * `wardic.projection.measure` span annotated with the
3135
3244
  * node counts, how many nodes it seeded, how many it gave a slot-only update in a
3136
3245
  * dragged subtree (`dragBlocked`), how many rounds it ran, and whether a window
@@ -3138,6 +3247,8 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
3138
3247
  */
3139
3248
  const measurePass = Effect.suspend(() => {
3140
3249
  passQueued = false;
3250
+ const afterTick = passAfterTick;
3251
+ passAfterTick = false;
3141
3252
  // A Declared layout change's Measurements serve this pass, and the next task's call takes its own.
3142
3253
  declared = false;
3143
3254
  const seeded = new Set();
@@ -3169,6 +3280,10 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
3169
3280
  // The nodes whose own size this pass took (#453): the next delivery finds its own.
3170
3281
  ownSized.clear();
3171
3282
  }
3283
+ // After its frame's tick, the nodes it seeded take their first step now, painted in this frame (#673).
3284
+ let stepped = 0;
3285
+ if (afterTick)
3286
+ stepped = stepAfterTick(seeded);
3172
3287
  for (const listener of passEndListeners) {
3173
3288
  try {
3174
3289
  listener();
@@ -3189,12 +3304,75 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
3189
3304
  nodes: nodes.size,
3190
3305
  active: active.size,
3191
3306
  seeded: seeded.size,
3307
+ stepped,
3192
3308
  dragBlocked: dragBlocked.size,
3193
3309
  rounds,
3194
3310
  blocked,
3195
3311
  refresh: refreshed,
3196
3312
  });
3197
3313
  }).pipe(Effect.withSpan("wardic.projection.measure", undefined, { captureStackTrace: false }));
3314
+ /**
3315
+ * Whether a trigger now comes after its frame's tick and before its paint, known from where it runs
3316
+ * (ADR-0018 *Amendment (#673)*): in the Frame loop's Late window, from a tick's first step through the
3317
+ * microtasks its frame's code queued (the gate's flush and the app effects in it, the engine's
3318
+ * callbacks, Solid 2's effect flush; ADR-0027 *Amendment (#680)*); during the engine's observer
3319
+ * delivery, which follows a frame's rAF callbacks; or during an after-tick pass's own step. App code in
3320
+ * a later rAF callback, or in an app observer, can't be told from a task after the paint, so its pass
3321
+ * holds a frame as before.
3322
+ */
3323
+ const afterTickNow = () => {
3324
+ if (inDelivery)
3325
+ return true;
3326
+ if (inAfterTickStep)
3327
+ return true;
3328
+ return loop.inLateWindow();
3329
+ };
3330
+ /**
3331
+ * An after-tick pass's step (ADR-0018 *Amendment (#673)*): each node it seeded that this frame's tick
3332
+ * didn't step, still flying, takes one step, and their group is derived and applied at once, so the
3333
+ * frame paints the first step and not the origin. At the dt of the tick that ran in this frame, as
3334
+ * every other node advanced by; `NOMINAL_DT` when none did (a Cold start). Its start callbacks follow,
3335
+ * after the frame is in the DOM (ADR-0039). A flight this step brings to rest lands at the next tick.
3336
+ * How many it stepped: the pass's `stepped`.
3337
+ */
3338
+ const stepAfterTick = (seeded) => {
3339
+ if (seeded.size === 0)
3340
+ return 0;
3341
+ // Inside the frame here, where the timeline's time is the frame's timestamp exactly (#673's probe).
3342
+ const at = frameTime();
3343
+ let dt = Clock.NOMINAL_DT;
3344
+ if (tickFrame === at)
3345
+ dt = Math.min(tickDt, Machine.MAX_DT);
3346
+ const stepped = new Set();
3347
+ for (const node of seeded) {
3348
+ if (node.steppedAt === at)
3349
+ continue;
3350
+ if (!active.has(node))
3351
+ continue;
3352
+ if (Predicate.isUndefined(node.flight))
3353
+ continue;
3354
+ if (node.dragging)
3355
+ continue;
3356
+ stepFlight(node, dt);
3357
+ noteFlight(node, false);
3358
+ node.steppedAt = at;
3359
+ stepped.add(node);
3360
+ }
3361
+ if (stepped.size === 0)
3362
+ return 0;
3363
+ inAfterTickStep = true;
3364
+ try {
3365
+ sink.flushPass(() => deriveGroup(groupOf(stepped)));
3366
+ }
3367
+ finally {
3368
+ inAfterTickStep = false;
3369
+ }
3370
+ if (Arr.isArrayNonEmpty(flightStarts)) {
3371
+ for (const node of flightStarts.splice(0))
3372
+ callAppIfAny(node.onLayoutAnimationStart(), "onLayoutAnimationStart");
3373
+ }
3374
+ return stepped.size;
3375
+ };
3198
3376
  /**
3199
3377
  * Queue the Measure pass for the end of the current task (one per task, however
3200
3378
  * many triggers). A trigger while a pass runs its rounds repeats the pass instead.
@@ -3208,9 +3386,15 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
3208
3386
  triggeredDuringPass = true;
3209
3387
  return;
3210
3388
  }
3211
- if (passQueued)
3389
+ // A pass runs after the tick only when every trigger for it did (#673).
3390
+ const after = afterTickNow();
3391
+ if (passQueued) {
3392
+ if (!after)
3393
+ passAfterTick = false;
3212
3394
  return;
3395
+ }
3213
3396
  passQueued = true;
3397
+ passAfterTick = after;
3214
3398
  FiberSet.addUnsafe(taskFibers, runtime.runFork(Microtask.next.pipe(Effect.andThen(measurePass), Effect.tapCause((cause) => reportFailure("wardic.projection: measure pass failed", cause)))));
3215
3399
  };
3216
3400
  /** Call an app's `callback` (#191): one that throws is reported, and the engine goes on. */
@@ -3271,7 +3455,10 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
3271
3455
  * whose flights have all ended. The Frame loop calls its hooks in the order they were added, and the
3272
3456
  * provider adds its gate's first, so the frame is in the DOM. Per frame: nothing to do is two reads.
3273
3457
  */
3274
- const afterTick = () => {
3458
+ const afterTick = (dt) => {
3459
+ // This tick's frame and dt, for an after-tick pass in the same frame (#673): one read a tick.
3460
+ tickFrame = frameTime();
3461
+ tickDt = dt;
3275
3462
  if (Arr.isArrayNonEmpty(flightStarts)) {
3276
3463
  for (const node of flightStarts.splice(0))
3277
3464
  callAppIfAny(node.onLayoutAnimationStart(), "onLayoutAnimationStart");
@@ -3287,7 +3474,7 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
3287
3474
  checkSettlings();
3288
3475
  return undefined;
3289
3476
  };
3290
- runtime.runSync(FrameLoop.FrameLoop.use((loop) => loop.afterTick(afterTick)).pipe(Scope.provide(scope)));
3477
+ runtime.runSync(loop.afterTick(afterTick).pipe(Scope.provide(scope)));
3291
3478
  /**
3292
3479
  * A **Declared layout change**'s Measurements (#4, ADR-0038): one slot-only round over every node at
3293
3480
  * the call, before its callback runs, so the end-of-task pass animates what the callback moves from
@@ -3520,10 +3707,13 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
3520
3707
  // 0) The first frame after a change: things there kept where they were seen (#480).
3521
3708
  if (seenKeeps.size + unseen.size > 0)
3522
3709
  settleSeen();
3710
+ // This frame's time, stamped on each node it steps, so a pass after it doesn't step one again (#673).
3711
+ const at = frameTime();
3523
3712
  // 1) Step every active node's springs (no derive yet — the tree-aware derive needs
3524
3713
  // ALL springs advanced before computing parent-first): its layout animation's
3525
3714
  // progress, and its Release offset's two axes. It settles when both are at rest.
3526
3715
  for (const node of active) {
3716
+ node.steppedAt = at;
3527
3717
  // A dragging node (ADR-0016) follows the pointer — kept active + derived each frame,
3528
3718
  // never settled. A layout animation its grab left running goes on under it.
3529
3719
  if (node.dragging) {
@@ -3616,6 +3806,8 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
3616
3806
  // The running nodes placed in these landing nodes' frames go on from where they paint (#144).
3617
3807
  landFrames(settled);
3618
3808
  for (const node of settled) {
3809
+ // Landed: a pass after this tick that seeds it again steps it, its last step being its landing's (#673).
3810
+ node.steppedAt = Number.NaN;
3619
3811
  node.releasing = false;
3620
3812
  node.release = undefined;
3621
3813
  node.animating = false;
@@ -4017,6 +4209,23 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
4017
4209
  return;
4018
4210
  runtime.runFork(FiberHandle.clear(coordinator));
4019
4211
  };
4212
+ /**
4213
+ * A disposed Lead's hand-over (#682): the newest twin in `stack` that isn't leaving becomes the Lead,
4214
+ * promoted from `leaver`'s box. With none, nothing takes over, and every twin left, each leaving,
4215
+ * ends its handover now.
4216
+ */
4217
+ const handOverFrom = (leaver, stack) => {
4218
+ const next = presentBelow(stack, stack.length);
4219
+ if (Predicate.isNotUndefined(next)) {
4220
+ const [twin, i] = next;
4221
+ stack.splice(i, 1);
4222
+ stack.push(twin);
4223
+ promoteFrom(twin, leaver);
4224
+ return;
4225
+ }
4226
+ for (const twin of [...stack])
4227
+ finishLeaver(twin);
4228
+ };
4020
4229
  /** Promote `node` to an animating Lead from `fromBox` → its own box (un-hide + move home). */
4021
4230
  const promoteFrom = (node, from) => {
4022
4231
  const ownBox = node.retained;
@@ -4125,6 +4334,7 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
4125
4334
  origin: undefined,
4126
4335
  layoutBox: recent.box,
4127
4336
  flight: undefined,
4337
+ steppedAt: Number.NaN,
4128
4338
  animating: false,
4129
4339
  parent: undefined,
4130
4340
  depth: 0,
@@ -4156,6 +4366,7 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
4156
4366
  passDelta: IDENTITY_DELTA,
4157
4367
  passPainted: undefined,
4158
4368
  layoutRoot: false,
4369
+ isolating: false,
4159
4370
  layoutAnchor,
4160
4371
  dragging: false,
4161
4372
  grabBox: undefined,
@@ -4177,17 +4388,21 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
4177
4388
  // later dispose then SKIPS removal), demote it to a Follower, and promote the twin.
4178
4389
  if (Predicate.isNotUndefined(live)) {
4179
4390
  const stack = stacks.get(layoutId);
4391
+ if (Predicate.isUndefined(stack))
4392
+ return false;
4180
4393
  const isLead = leadOf(layoutId) === live;
4181
- const twin = Bool.match(isLead, { onTrue: () => priorLeadOf(live), onFalse: () => leadOf(layoutId) });
4182
- if (Predicate.isUndefined(twin) || twin === live || Predicate.isUndefined(stack))
4394
+ // A Lead hands over only to a twin that isn't leaving (#682): with none, a lone leave, and it
4395
+ // goes on as the Lead while presence runs its own exit, as Motion's relegate leaves it.
4396
+ const below = Bool.match(isLead, { onTrue: () => presentBelow(stack, stack.indexOf(live)), onFalse: () => undefined });
4397
+ const twin = Bool.match(isLead, { onTrue: () => UndefinedOr.map(below, ([node]) => node), onFalse: () => leadOf(layoutId) });
4398
+ if (Predicate.isUndefined(twin) || twin === live)
4183
4399
  return false; // lone leave
4184
4400
  live.leaving = true;
4185
4401
  live.onLeaveDone = onDone;
4186
- if (isLead) {
4187
- // Swap so the twin becomes the Lead (last); el becomes a Follower below it.
4188
- const last = stack.length - 1;
4189
- stack[last - 1] = live;
4190
- stack[last] = twin;
4402
+ if (Predicate.isNotUndefined(below)) {
4403
+ // The twin becomes the Lead (last); el stays below it, a Follower.
4404
+ stack.splice(below[1], 1);
4405
+ stack.push(twin);
4191
4406
  promoteFrom(twin, live);
4192
4407
  }
4193
4408
  // After a possible swap, re-read the (new) Lead — never assert it exists.
@@ -4204,6 +4419,9 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
4204
4419
  const lead = leadOf(layoutId);
4205
4420
  if (Predicate.isUndefined(lead) || lead.element === el || Predicate.isUndefined(recent))
4206
4421
  return false;
4422
+ // No twin to come back to but one leaving itself (#682).
4423
+ if (lead.leaving)
4424
+ return false;
4207
4425
  const last = recent.last;
4208
4426
  if (Predicate.isUndefined(last))
4209
4427
  return false;
@@ -4279,6 +4497,9 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
4279
4497
  return;
4280
4498
  if (lead.element === inner)
4281
4499
  return;
4500
+ // Not onto a Lead that is leaving itself (#682): it has no handover to ride.
4501
+ if (lead.leaving)
4502
+ return;
4282
4503
  if (Bool.not(takesOver(lead)))
4283
4504
  return;
4284
4505
  const twin = makeLeavingNode(inner, held.layoutId, last, () => undefined, undefined, chainOf(held));
@@ -4565,6 +4786,17 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
4565
4786
  * Motion's projection never sees it; beside a real resize, the pass gives them a slot-only update.
4566
4787
  */
4567
4788
  const onObserved = (entries) => {
4789
+ // A pass this delivery queues runs after its frame's tick, before its paint (#673).
4790
+ inDelivery = true;
4791
+ try {
4792
+ delivered(entries);
4793
+ }
4794
+ finally {
4795
+ inDelivery = false;
4796
+ }
4797
+ };
4798
+ /** {@link onObserved}'s delivery, read. */
4799
+ const delivered = (entries) => {
4568
4800
  // A node's own resize runs a pass, which reads the page as well.
4569
4801
  const unmeasured = Arr.filter(entries, Predicate.not(alreadyMeasured));
4570
4802
  if (Arr.isArrayNonEmpty(unmeasured)) {
@@ -4632,6 +4864,7 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
4632
4864
  origin: undefined,
4633
4865
  layoutBox: undefined,
4634
4866
  flight: undefined,
4867
+ steppedAt: Number.NaN,
4635
4868
  animating: false,
4636
4869
  parent: undefined,
4637
4870
  depth: 0,
@@ -4661,6 +4894,8 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
4661
4894
  passDelta: IDENTITY_DELTA,
4662
4895
  passPainted: undefined,
4663
4896
  layoutRoot: opts.layoutRoot === true,
4897
+ // Set at its first Measure pass, before any frame derives it.
4898
+ isolating: false,
4664
4899
  layoutAnchor: opts.layoutAnchor,
4665
4900
  dragging: false,
4666
4901
  grabBox: undefined,
@@ -4880,8 +5115,11 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
4880
5115
  active.delete(node);
4881
5116
  deleteNode(node);
4882
5117
  painted.delete(node);
4883
- // Stack relegation / promotion (ADR-0010): a leaving Lead promotes the new
4884
- // newest node, animating it FROM the leaver's box (un-hide + move home).
5118
+ // Stack relegation / promotion (ADR-0010): a Lead that leaves promotes the newest twin
5119
+ // that isn't leaving, animating it FROM the leaver's box (un-hide + move home), as Motion's
5120
+ // relegate does. With none, every twin left is leaving with its page: nothing takes over, and
5121
+ // the leaver stays where it's drawn, and their handovers end now, so their pages go once
5122
+ // their own exits do (#682).
4885
5123
  if (Predicate.isNotUndefined(node.layoutId)) {
4886
5124
  const stack = stacks.get(node.layoutId);
4887
5125
  if (Predicate.isNotUndefined(stack)) {
@@ -4892,7 +5130,7 @@ export const makeProjectionSystem = (runtime, sink, options = {}) => {
4892
5130
  if (stack.length === 0)
4893
5131
  stacks.delete(node.layoutId);
4894
5132
  if (stack.length > 0 && wasLead)
4895
- promoteFrom(stack[stack.length - 1], node);
5133
+ handOverFrom(node, stack);
4896
5134
  }
4897
5135
  }
4898
5136
  if (active.size === 0)