@wardic/foldkit 0.0.0-stage → 1.2.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 (154) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +59 -2
  3. package/THIRD_PARTY_NOTICES.md +145 -0
  4. package/dist/AnimationControls.d.ts +37 -0
  5. package/dist/AnimationControls.d.ts.map +1 -0
  6. package/dist/AnimationControls.js +37 -0
  7. package/dist/AnimationControls.js.map +1 -0
  8. package/dist/AnimationScope.d.ts +20 -0
  9. package/dist/AnimationScope.d.ts.map +1 -0
  10. package/dist/AnimationScope.js +20 -0
  11. package/dist/AnimationScope.js.map +1 -0
  12. package/dist/DragControls.d.ts +25 -0
  13. package/dist/DragControls.d.ts.map +1 -0
  14. package/dist/DragControls.js +21 -0
  15. package/dist/DragControls.js.map +1 -0
  16. package/dist/Features.d.ts +36 -0
  17. package/dist/Features.d.ts.map +1 -0
  18. package/dist/Features.js +44 -0
  19. package/dist/Features.js.map +1 -0
  20. package/dist/Ward.d.ts +301 -0
  21. package/dist/Ward.d.ts.map +1 -0
  22. package/dist/Ward.js +6 -0
  23. package/dist/Ward.js.map +1 -0
  24. package/dist/Wardic.d.ts +363 -0
  25. package/dist/Wardic.d.ts.map +1 -0
  26. package/dist/Wardic.js +85 -0
  27. package/dist/Wardic.js.map +1 -0
  28. package/dist/index.d.ts +15 -0
  29. package/dist/index.d.ts.map +1 -0
  30. package/dist/index.js +20 -0
  31. package/dist/index.js.map +1 -0
  32. package/dist/internal/builder.d.ts +8 -0
  33. package/dist/internal/builder.d.ts.map +1 -0
  34. package/dist/internal/builder.js +242 -0
  35. package/dist/internal/builder.js.map +1 -0
  36. package/dist/internal/commands.d.ts +143 -0
  37. package/dist/internal/commands.d.ts.map +1 -0
  38. package/dist/internal/commands.js +242 -0
  39. package/dist/internal/commands.js.map +1 -0
  40. package/dist/internal/delivery.d.ts +44 -0
  41. package/dist/internal/delivery.d.ts.map +1 -0
  42. package/dist/internal/delivery.js +85 -0
  43. package/dist/internal/delivery.js.map +1 -0
  44. package/dist/internal/dev.d.ts +10 -0
  45. package/dist/internal/dev.d.ts.map +1 -0
  46. package/dist/internal/dev.js +10 -0
  47. package/dist/internal/dev.js.map +1 -0
  48. package/dist/internal/dev.production.d.ts +8 -0
  49. package/dist/internal/dev.production.d.ts.map +1 -0
  50. package/dist/internal/dev.production.js +8 -0
  51. package/dist/internal/dev.production.js.map +1 -0
  52. package/dist/internal/drag.d.ts +66 -0
  53. package/dist/internal/drag.d.ts.map +1 -0
  54. package/dist/internal/drag.js +71 -0
  55. package/dist/internal/drag.js.map +1 -0
  56. package/dist/internal/element.d.ts +59 -0
  57. package/dist/internal/element.d.ts.map +1 -0
  58. package/dist/internal/element.js +582 -0
  59. package/dist/internal/element.js.map +1 -0
  60. package/dist/internal/gestures.d.ts +62 -0
  61. package/dist/internal/gestures.d.ts.map +1 -0
  62. package/dist/internal/gestures.js +109 -0
  63. package/dist/internal/gestures.js.map +1 -0
  64. package/dist/internal/hydrate.d.ts +5 -0
  65. package/dist/internal/hydrate.d.ts.map +1 -0
  66. package/dist/internal/hydrate.js +27 -0
  67. package/dist/internal/hydrate.js.map +1 -0
  68. package/dist/internal/keys.d.ts +21 -0
  69. package/dist/internal/keys.d.ts.map +1 -0
  70. package/dist/internal/keys.js +12 -0
  71. package/dist/internal/keys.js.map +1 -0
  72. package/dist/internal/layout.d.ts +29 -0
  73. package/dist/internal/layout.d.ts.map +1 -0
  74. package/dist/internal/layout.js +88 -0
  75. package/dist/internal/layout.js.map +1 -0
  76. package/dist/internal/mount.d.ts +22 -0
  77. package/dist/internal/mount.d.ts.map +1 -0
  78. package/dist/internal/mount.js +100 -0
  79. package/dist/internal/mount.js.map +1 -0
  80. package/dist/internal/presence.d.ts +33 -0
  81. package/dist/internal/presence.d.ts.map +1 -0
  82. package/dist/internal/presence.js +338 -0
  83. package/dist/internal/presence.js.map +1 -0
  84. package/dist/internal/reactivity.d.ts +15 -0
  85. package/dist/internal/reactivity.d.ts.map +1 -0
  86. package/dist/internal/reactivity.js +26 -0
  87. package/dist/internal/reactivity.js.map +1 -0
  88. package/dist/internal/reorder.d.ts +47 -0
  89. package/dist/internal/reorder.d.ts.map +1 -0
  90. package/dist/internal/reorder.js +250 -0
  91. package/dist/internal/reorder.js.map +1 -0
  92. package/dist/internal/scope.d.ts +28 -0
  93. package/dist/internal/scope.d.ts.map +1 -0
  94. package/dist/internal/scope.js +44 -0
  95. package/dist/internal/scope.js.map +1 -0
  96. package/dist/internal/server.d.ts +13 -0
  97. package/dist/internal/server.d.ts.map +1 -0
  98. package/dist/internal/server.js +13 -0
  99. package/dist/internal/server.js.map +1 -0
  100. package/dist/internal/server.node.d.ts +8 -0
  101. package/dist/internal/server.node.d.ts.map +1 -0
  102. package/dist/internal/server.node.js +8 -0
  103. package/dist/internal/server.node.js.map +1 -0
  104. package/dist/internal/services.d.ts +222 -0
  105. package/dist/internal/services.d.ts.map +1 -0
  106. package/dist/internal/services.js +338 -0
  107. package/dist/internal/services.js.map +1 -0
  108. package/dist/internal/tree.d.ts +71 -0
  109. package/dist/internal/tree.d.ts.map +1 -0
  110. package/dist/internal/tree.js +207 -0
  111. package/dist/internal/tree.js.map +1 -0
  112. package/dist/internal/variant-node.d.ts +62 -0
  113. package/dist/internal/variant-node.d.ts.map +1 -0
  114. package/dist/internal/variant-node.js +279 -0
  115. package/dist/internal/variant-node.js.map +1 -0
  116. package/dist/internal/view-transition.d.ts +10 -0
  117. package/dist/internal/view-transition.d.ts.map +1 -0
  118. package/dist/internal/view-transition.js +36 -0
  119. package/dist/internal/view-transition.js.map +1 -0
  120. package/dist/internal/walk.d.ts +26 -0
  121. package/dist/internal/walk.d.ts.map +1 -0
  122. package/dist/internal/walk.js +162 -0
  123. package/dist/internal/walk.js.map +1 -0
  124. package/package.json +99 -4
  125. package/src/AnimationControls.ts +46 -0
  126. package/src/AnimationScope.ts +19 -0
  127. package/src/DragControls.ts +27 -0
  128. package/src/Features.ts +45 -0
  129. package/src/Ward.ts +434 -0
  130. package/src/Wardic.ts +410 -0
  131. package/src/index.ts +20 -0
  132. package/src/internal/builder.ts +262 -0
  133. package/src/internal/commands.ts +359 -0
  134. package/src/internal/delivery.ts +116 -0
  135. package/src/internal/dev.production.ts +7 -0
  136. package/src/internal/dev.ts +9 -0
  137. package/src/internal/drag.ts +136 -0
  138. package/src/internal/element.ts +668 -0
  139. package/src/internal/gestures.ts +184 -0
  140. package/src/internal/hydrate.ts +29 -0
  141. package/src/internal/keys.ts +19 -0
  142. package/src/internal/layout.ts +109 -0
  143. package/src/internal/mount.ts +124 -0
  144. package/src/internal/presence.ts +362 -0
  145. package/src/internal/reactivity.ts +45 -0
  146. package/src/internal/reorder.ts +313 -0
  147. package/src/internal/scope.ts +60 -0
  148. package/src/internal/server.node.ts +7 -0
  149. package/src/internal/server.ts +12 -0
  150. package/src/internal/services.ts +486 -0
  151. package/src/internal/tree.ts +230 -0
  152. package/src/internal/variant-node.ts +352 -0
  153. package/src/internal/view-transition.ts +43 -0
  154. package/src/internal/walk.ts +211 -0
@@ -0,0 +1,230 @@
1
+ /**
2
+ * The tree, by DOM ancestry (ADR-0071 decisions 21 and 22, *Amendment (#791)* item 7): a `w.*`
3
+ * element's variant parent and its nearest `w.Config` are the nearest Wardic ancestors', found by
4
+ * walking `parentElement` to an element carrying a delivered config, which Foldkit's props module
5
+ * wrote at `create`, before the children exist. Foldkit runs a child's Mount before its parent's, so
6
+ * an element's node and config value are made by whichever first asks, cached by the element, and
7
+ * adopted by its own Mount when that runs.
8
+ *
9
+ * @module
10
+ */
11
+ import * as WardicConfigValue from "@wardic/core/unstable/WardicConfigValue"
12
+ import type * as VariantTree from "@wardic/core/unstable/VariantTree"
13
+ import * as LayoutGroup from "@wardic/dom/unstable/LayoutGroup"
14
+ import * as Arr from "effect/Array"
15
+ import * as Bool from "effect/Boolean"
16
+ import { constUndefined, constVoid, identity } from "effect/Function"
17
+ import * as Option from "effect/Option"
18
+ import * as Predicate from "effect/Predicate"
19
+ import * as UndefinedOr from "effect/UndefinedOr"
20
+ import type * as Ward from "../Ward.ts"
21
+ import { deliveredTo } from "./delivery.ts"
22
+ import { configOf } from "./element.ts"
23
+ import { recheck } from "./reactivity.ts"
24
+ import { type VariantNode, variantNodeOf } from "./variant-node.ts"
25
+
26
+ // Module state (#460, Q7): each element's node, held weakly by the element.
27
+ const nodes = new WeakMap<Element, VariantNode>()
28
+ // Module state (#460, Q7): each element's config value, held weakly by the element.
29
+ const values = new WeakMap<Element, WardicConfigValue.WardicConfigValue>()
30
+ // Module state (#460, Q7): the elements whose Mount has run, held weakly: what a later render's `w.Presence` is told by.
31
+ const mounted = new WeakSet<Element>()
32
+
33
+ /** The nearest ancestor of `element` that `has` what's looked for, or none at the top. Per element at mount, up the tree. */
34
+ export const nearestAncestor = (element: Element, has: (ancestor: Element) => boolean): Element | undefined => {
35
+ const ancestor = element.parentElement
36
+ if (Predicate.isNull(ancestor)) return undefined
37
+ if (has(ancestor)) return ancestor
38
+ return nearestAncestor(ancestor, has)
39
+ }
40
+
41
+ /** Whether an element carries a delivered config: a `w.*` element, or one with the bundle. */
42
+ const isWard = (element: Element): boolean => Predicate.isNotUndefined(deliveredTo(element))
43
+
44
+ /** Whether an element carries `w.Presence`. */
45
+ const isPresence = (element: Element): boolean => Predicate.isNotUndefined(configOf(deliveredTo(element)).presence)
46
+
47
+ /** The nearest Wardic ancestor of `element`: one carrying a delivered config. */
48
+ const nearestWard = (element: Element): Element | undefined => nearestAncestor(element, isWard)
49
+
50
+ /** Whether `node` is a `w.*` element or under one: a change of the Handle's own, not another app's under the body (decision 33). */
51
+ export const underWard = (node: Node): boolean => {
52
+ const element = Bool.match(node instanceof Element, { onTrue: () => node as Element, onFalse: () => node.parentElement })
53
+ if (Predicate.isNull(element)) return false
54
+ if (isWard(element)) return true
55
+ return Predicate.isNotUndefined(nearestWard(element))
56
+ }
57
+
58
+ /** The nearest `w.Presence` element above `element` (decision 29), found as the variant parent is. */
59
+ export const nearestPresenceElement = (element: Element): Element | undefined => nearestAncestor(element, isPresence)
60
+
61
+ /** Whether an element carries `w.LayoutGroup`. */
62
+ const isLayoutGroup = (element: Element): boolean => Predicate.isNotUndefined(configOf(deliveredTo(element)).layoutGroup)
63
+
64
+ // Module state (#460, Q7): each `w.LayoutGroup` element's id, held weakly, read once as the first `layoutId` under it mounts.
65
+ const groupIds = new WeakMap<Element, Option.Option<string>>()
66
+
67
+ /**
68
+ * The id of the **Layout group** `element` is in (#432, Motion's `LayoutGroup`): the nearest
69
+ * `w.LayoutGroup` above it, its own id joined with the group's above as its `inherit` says
70
+ * (`layoutGroupIdOf`), or none outside any. Read once, at mount, as Solid's is.
71
+ */
72
+ export const layoutGroupOf = (element: Element): string | undefined => UndefinedOr.match(nearestAncestor(element, isLayoutGroup), { onUndefined: constUndefined, onDefined: groupIdOf })
73
+
74
+ /** A `w.LayoutGroup` element's id: made at the first ask, from the group above it and its own options. */
75
+ const groupIdOf = (group: Element): string | undefined =>
76
+ UndefinedOr.match(groupIds.get(group), {
77
+ onDefined: Option.getOrUndefined,
78
+ onUndefined: () => {
79
+ const options = UndefinedOr.match(configOf(deliveredTo(group)).layoutGroup, { onUndefined: (): Ward.LayoutGroupOptions => ({}), onDefined: identity })
80
+ const id = LayoutGroup.layoutGroupIdOf(layoutGroupOf(group), options.id, options.inherit)
81
+ groupIds.set(group, Option.fromUndefinedOr(id))
82
+ return id
83
+ },
84
+ })
85
+
86
+ /** Mark `element`'s Mount as run: from now on, a `w.Presence` a render adds to it can't run (its Mount is made once). */
87
+ export const markMounted = (element: Element): void => {
88
+ mounted.add(element)
89
+ }
90
+
91
+ // Module state (#848): the elements whose own start has run, held weakly.
92
+ const started = new WeakSet<Element>()
93
+ // Module state (#848): the children waiting to start with each element, held weakly by it.
94
+ const waiting = new WeakMap<Element, Array<Waiting>>()
95
+
96
+ /** A start waiting its turn: a child's for its nearest `w.*` ancestor's here, or for its presence's delivery (`presence.ts`). */
97
+ export interface Waiting {
98
+ readonly element: Element
99
+ /** Starts the element; returns its release. */
100
+ readonly begin: () => () => void
101
+ /** The start's release, once it ran. */
102
+ release: (() => void) | undefined
103
+ /** Its Mount destroyed before it began. */
104
+ gone: boolean
105
+ }
106
+
107
+ /**
108
+ * A waiting start's release, at its Mount's destroy: gone from now on; the start's own release when it
109
+ * ran, else the node its Mount registered dropped, so no slot of a child that never came stays with
110
+ * its ancestor's orchestration.
111
+ */
112
+ export const releaseWaiting = (entry: Waiting): void => {
113
+ entry.gone = true
114
+ UndefinedOr.match(entry.release, { onUndefined: () => dropNode(entry.element), onDefined: (release) => release() })
115
+ }
116
+
117
+ /**
118
+ * `element`'s own start, `begin`, run now, then the starts of the children that waited for it, in their
119
+ * Mounts' order. A start that fails is the one element's (decision 13): the parent's failing still starts
120
+ * the children, and a child's failing starts its siblings, its defect thrown on a microtask for the
121
+ * page's error handling, as nothing of the parent's Mount is to blame. Returns the release.
122
+ */
123
+ const startNow = (element: Element, begin: () => () => void): (() => void) => {
124
+ let release: () => void = constVoid
125
+ try {
126
+ release = begin()
127
+ } finally {
128
+ started.add(element)
129
+ UndefinedOr.map(waiting.get(element), (children) => {
130
+ waiting.delete(element)
131
+ Arr.forEach(children, (child) => {
132
+ if (child.gone) return
133
+ try {
134
+ child.release = startNow(child.element, child.begin)
135
+ } catch (defect) {
136
+ queueMicrotask(() => {
137
+ throw defect
138
+ })
139
+ }
140
+ })
141
+ })
142
+ }
143
+ return () => {
144
+ started.delete(element)
145
+ release()
146
+ }
147
+ }
148
+
149
+ /**
150
+ * `begin` run now, or once the nearest `w.*` ancestor of `element` has started (#848): a tree mounted by
151
+ * one patch starts top-down, every child registered with its ancestor's orchestration before any reads
152
+ * its stagger slot. Returns the release.
153
+ */
154
+ export const startUnder = (element: Element, begin: () => () => void): (() => void) => {
155
+ const ancestor = nearestWard(element)
156
+ if (Predicate.isUndefined(ancestor)) return startNow(element, begin)
157
+ if (started.has(ancestor)) return startNow(element, begin)
158
+ const entry: Waiting = { element, begin, release: undefined, gone: false }
159
+ const children = UndefinedOr.match(waiting.get(ancestor), { onUndefined: (): Array<Waiting> => [], onDefined: identity })
160
+ children.push(entry)
161
+ waiting.set(ancestor, children)
162
+ return () => releaseWaiting(entry)
163
+ }
164
+
165
+ /** Whether `element`'s Mount has run. */
166
+ export const isMounted = (element: Element): boolean => mounted.has(element)
167
+
168
+ /** The variant context the nearest Wardic ancestor of `element` provides, or none at the top. */
169
+ const parentContextOf = (element: Element): VariantTree.WardicVariantContextValue | undefined =>
170
+ UndefinedOr.match(nearestWard(element), { onUndefined: constUndefined, onDefined: (ancestor) => nodeOf(ancestor).provided })
171
+
172
+ /** `element`'s variant node: made at the first ask (a child's, or its own Mount's), over its latest delivered config. */
173
+ export const nodeOf = (element: Element): VariantNode => {
174
+ const known = nodes.get(element)
175
+ if (Predicate.isNotUndefined(known)) return known
176
+ const node = variantNodeOf(() => configOf(deliveredTo(element)), parentContextOf(element), recheck)
177
+ nodes.set(element, node)
178
+ return node
179
+ }
180
+
181
+ /** Drop `element`'s node at its release: its registration with its ancestor given up. */
182
+ export const dropNode = (element: Element): void => {
183
+ const node = nodes.get(element)
184
+ if (Predicate.isUndefined(node)) return
185
+ nodes.delete(element)
186
+ node.dispose()
187
+ }
188
+
189
+ /**
190
+ * The `WardicConfigValue` in effect for `element` (#434, ADR-0062): its own `w.Config` over the nearest
191
+ * ancestor's, over the Handle's `config` (`root`). Each setting is read when an animation starts, so a
192
+ * change moves nothing by itself. An element with no `w.Config` of its own shares its ancestor's.
193
+ */
194
+ export const configValueOf = (element: Element, root: WardicConfigValue.WardicConfigValue | undefined): WardicConfigValue.WardicConfigValue | undefined => {
195
+ const own = (): Ward.ConfigOptions | undefined => configOf(deliveredTo(element)).config
196
+ const parent = UndefinedOr.match(nearestWard(element), { onUndefined: () => root, onDefined: (ancestor) => configValueOf(ancestor, root) })
197
+ if (Predicate.isUndefined(own())) return parent
198
+ const known = values.get(element)
199
+ if (Predicate.isNotUndefined(known)) return known
200
+ const value = configValueUnder(own, parent)
201
+ values.set(element, value)
202
+ return value
203
+ }
204
+
205
+ /**
206
+ * The `WardicConfigValue` of an element whose `w.Config` is `own()`, under `parent`'s (ADR-0062): each
207
+ * setting read when an animation starts, so a change moves nothing by itself. What {@link configValueOf}
208
+ * keeps for a mounted element, and the walk at a Provider element's build carries down a server render
209
+ * (#794, PR 2).
210
+ */
211
+ export const configValueUnder = (own: () => Ward.ConfigOptions | undefined, parent: WardicConfigValue.WardicConfigValue | undefined): WardicConfigValue.WardicConfigValue => ({
212
+ transition: () => WardicConfigValue.configTransitionOf(UndefinedOr.map(own(), (c) => c.transition), parent),
213
+ reducedMotion: () => WardicConfigValue.configReducedMotionOf(UndefinedOr.map(own(), (c) => c.reducedMotion), parent),
214
+ skipAnimations: () => WardicConfigValue.configSkipAnimationsOf(UndefinedOr.map(own(), (c) => c.skipAnimations), parent),
215
+ isValidProp: () => WardicConfigValue.configIsValidPropOf(undefined, parent),
216
+ // Where a drag's and a pan's points are (#793, Q9): the nearest `transformPagePoint` up the chain.
217
+ transformPagePoint: () => WardicConfigValue.configTransformPagePointOf(UndefinedOr.map(own(), (c) => c.transformPagePoint), parent),
218
+ })
219
+
220
+ /** The Handle's app-wide config (`Wardic.make({ config })`), as the root of every element's chain; none without one. */
221
+ export const rootConfigValueOf = (options: Ward.ConfigOptions | undefined): WardicConfigValue.WardicConfigValue | undefined => {
222
+ if (Predicate.isUndefined(options)) return undefined
223
+ return {
224
+ transition: () => WardicConfigValue.configTransitionOf(options.transition, undefined),
225
+ reducedMotion: () => options.reducedMotion,
226
+ skipAnimations: () => options.skipAnimations,
227
+ isValidProp: constUndefined,
228
+ transformPagePoint: () => options.transformPagePoint,
229
+ }
230
+ }
@@ -0,0 +1,352 @@
1
+ /**
2
+ * An element's place in variant orchestration on Foldkit (ADR-0007, #50; ADR-0071 decisions 17 and
3
+ * 21): Solid's `variant-node.ts` over the `Reactivity` port's getters, with no reactive graph. The
4
+ * rules it runs are core's `unstable/VariantTree`. What Solid keeps in memos is read on each ask here:
5
+ * the names handed down, the orchestration, the stagger; what a Solid effect did at a trigger (the
6
+ * slots marked stale, the names waiting again, `beforeChildren` waiting again) is done on the first
7
+ * read after the trigger key changes, so a child's Retarget in the same patch reads a fresh slot. A
8
+ * settle that changes a gate (`beforeChildren`, `afterChildren`) tells the port's watches to re-check,
9
+ * which is how a held Retarget is released.
10
+ *
11
+ * @module
12
+ */
13
+ import * as AnimationControls from "@wardic/core/unstable/AnimationControls"
14
+ import type * as WardConfig from "@wardic/dom/unstable/WardConfig"
15
+ import * as VariantTarget from "@wardic/core/unstable/VariantTarget"
16
+ import * as VariantTree from "@wardic/core/unstable/VariantTree"
17
+ import * as Arr from "effect/Array"
18
+ import * as Bool from "effect/Boolean"
19
+ import { constTrue, constUndefined, constVoid } from "effect/Function"
20
+ import * as MutableRef from "effect/MutableRef"
21
+ import * as Predicate from "effect/Predicate"
22
+ import * as UndefinedOr from "effect/UndefinedOr"
23
+ import type * as CoreConfig from "@wardic/core/unstable/Config"
24
+
25
+ /** An element's place in variant orchestration, on the client. */
26
+ export interface VariantNode {
27
+ /** What its children read: its own context, its parent's passed through, or none. */
28
+ readonly provided: VariantTree.WardicVariantContextValue | undefined
29
+ /** The context its spine inherits names and its default transition from: its parent's, for a Variant child. */
30
+ readonly inherited: VariantTree.WardicVariantContextValue | undefined
31
+ /** Its stagger delay in seconds, as a Variant child; `undefined` for any other element. */
32
+ readonly extraDelay: ((name: string | undefined) => number) | undefined
33
+ /** Whether its spine may start a Retarget: its ancestor's `beforeChildren` and its own `afterChildren` allow it. */
34
+ readonly canStart: () => boolean
35
+ /** Its spine's settle report on each frame: to its ancestor's registry, and to its own `beforeChildren`. */
36
+ readonly onFrame: (done: boolean) => void
37
+ /** A list's names orchestrated each on their own (#297's PR 2). Only an element that joins a provider or provides has one. */
38
+ readonly names: VariantTree.NameOrchestration | undefined
39
+ /**
40
+ * Which of its element's gestures are on (#291, #793), read into the names it hands its Variant
41
+ * children: bound by the element's Mount. Nothing for an element with no variants of its own, which
42
+ * hands down what it takes.
43
+ */
44
+ readonly bindGestures: (states: () => VariantTree.GestureStates) => void
45
+ /** Its registration with the ancestor it joined, given up: at its element's release. */
46
+ readonly dispose: () => void
47
+ /**
48
+ * A config as everything of the element reads it (#433; #798, Q4): with **Animation controls** as its
49
+ * first delivery's `animate`, the Controls change in effect as its entry (none before the first start
50
+ * or set, a later delivery's values dropped); else as delivered, controls a later delivery gives
51
+ * stripped. Built once, at the node's making.
52
+ */
53
+ readonly configured: <C extends Pick<CoreConfig.SpineConfig, "animate">>(config: C) => C
54
+ /** The Controls change in effect: its own controls', else the one its inherited context is in (core's `controlsChangeOf`), else none. */
55
+ readonly change: () => AnimationControls.ControlsChange | undefined
56
+ /** A start or set of its own controls: the change made its entry, which the next resolution reads. */
57
+ readonly setChange: (change: AnimationControls.ControlsChange) => void
58
+ }
59
+
60
+ /** What the orchestration reads of its node: its configured config, and the Controls change in effect. */
61
+ interface Read {
62
+ readonly config: () => VariantTree.VariantConfig
63
+ readonly change: () => AnimationControls.ControlsChange | undefined
64
+ }
65
+
66
+ /** A delivered config as the node reads it: the tree's keys, and `animate` as the spine types it, for the controls' forms. */
67
+ type Delivered = VariantTree.VariantConfig & Pick<CoreConfig.SpineConfig, "animate">
68
+
69
+ /** The orchestration an element with variants of its own runs for its Variant children. */
70
+ interface OwnOrchestration {
71
+ readonly context: VariantTree.WardicVariantContextValue
72
+ readonly canStart: () => boolean
73
+ readonly settled: (done: boolean) => void
74
+ readonly canStartFor: (name: string) => boolean
75
+ readonly settledFor: (names: ReadonlyArray<string>, done: boolean) => void
76
+ readonly bindGestures: (states: () => VariantTree.GestureStates) => void
77
+ }
78
+
79
+ /** No gesture on: what an element hands down before its Mount binds its gestures. */
80
+ const noGestures = (): VariantTree.GestureStates => VariantTree.NO_GESTURE_STATES
81
+
82
+ /** A registered Variant child's stagger slot, as its ancestor's registry holds it: set at each trigger. */
83
+ interface ChildRecord {
84
+ index: number
85
+ total: number
86
+ /** The names of a list whose keys it has settled since the list last changed (#297's PR 2). */
87
+ readonly names: Set<string>
88
+ }
89
+
90
+ /**
91
+ * The registry, stagger and `when` an element with variants of its own runs for its Variant children
92
+ * (ADR-0007), and the names it hands them. `notify` re-checks the port's watches: called when a gate
93
+ * a child waits on changes. `change` is the Controls change in effect (#433): a trigger even to the
94
+ * same name, and its override the children's orchestration while it's the latest.
95
+ */
96
+ const ownOrchestration = (
97
+ read: Read,
98
+ parent: VariantTree.WardicVariantContextValue | undefined,
99
+ inherited: VariantTree.WardicVariantContextValue | undefined,
100
+ notify: () => void,
101
+ ): OwnOrchestration => {
102
+ const cfg = read.config
103
+ const change = read.change
104
+ const config = cfg()
105
+ // Children in mount order (a Set keeps insertion order); the slots are rewritten at each trigger.
106
+ const records = new Set<ChildRecord>()
107
+ let unsettled = 0
108
+ // The names of a list whose own keys have settled since the list last changed (#297's PR 2).
109
+ const ownNames = new Set<string>()
110
+ // True once this element's own animation settles; false again at each trigger. Drives `beforeChildren`.
111
+ let ownSettled = false
112
+
113
+ // The names handed down. `animate`'s is read on each ask; `initial`'s is read once.
114
+ const variant = (): WardConfig.WardicVariantLabels | undefined => {
115
+ const c = cfg()
116
+ return VariantTree.handedDown(c, () => VariantTree.nameOf(c.animate), () => UndefinedOr.map(parent, (p) => p.variant()))
117
+ }
118
+ // What it hands down for its gestures (#291): which of its own are on, bound by its element's Mount (#793), and what it inherits, passed on.
119
+ const ownStates = MutableRef.make<() => VariantTree.GestureStates>(noGestures)
120
+ const gestures = (): VariantTree.GestureHandDown => VariantTree.handDownOf(cfg(), MutableRef.get(ownStates)(), VariantTree.handDownFrom(inherited))
121
+ const triggerKey = (): string => VariantTree.triggerKeyOf(variant(), gestures(), change())
122
+
123
+ // Snapshot-at-trigger (ADR-0007): each child's index and total are recomputed only when a trigger
124
+ // (this element's variant, or its gestures' names) changes, so a mount, unmount or reorder
125
+ // mid-transition doesn't reshuffle in-flight delays. The change is seen on the first read after it:
126
+ // a child the same patch Retargets reads its slot before anything else of this element's runs.
127
+ let stale = true
128
+ let lastKey: string | undefined
129
+ let at: VariantTree.ChangeAtTrigger | undefined
130
+ const checkTrigger = (): void => {
131
+ const next = triggerKey()
132
+ if (next === lastKey) return
133
+ lastKey = next
134
+ at = VariantTree.changeAtTrigger(change(), next, at)
135
+ stale = true
136
+ // Each name waits again (#297's PR 2): its keys, and its children's, settle anew.
137
+ ownNames.clear()
138
+ records.forEach((record) => record.names.clear())
139
+ // `beforeChildren` waits again at each trigger, until this element's own animation settles.
140
+ ownSettled = false
141
+ }
142
+ const fresh = (): void => {
143
+ checkTrigger()
144
+ if (!stale) return
145
+ stale = false
146
+ const n = records.size
147
+ let i = 0
148
+ records.forEach((record) => {
149
+ record.total = n
150
+ record.index = i
151
+ i++
152
+ })
153
+ }
154
+
155
+ const register = (): VariantTree.WardicChildRegistration => {
156
+ const record: ChildRecord = { index: 0, total: 1, names: new Set() }
157
+ let settled = false
158
+ records.add(record)
159
+ unsettled++
160
+ const setSettled = (done: boolean): void => {
161
+ if (done === settled) return
162
+ settled = done
163
+ const before = unsettled
164
+ unsettled += Bool.match(done, { onTrue: () => -1, onFalse: () => 1 })
165
+ // `afterChildren`'s gate changed: what waits on it re-checks.
166
+ if (before === 0 || unsettled === 0) notify()
167
+ }
168
+ const dispose = (): void => {
169
+ if (!records.delete(record)) return
170
+ if (!settled) {
171
+ unsettled--
172
+ if (unsettled === 0) notify()
173
+ }
174
+ stale = true
175
+ }
176
+ return {
177
+ index: () => {
178
+ fresh()
179
+ return record.index
180
+ },
181
+ total: () => {
182
+ fresh()
183
+ return record.total
184
+ },
185
+ setSettled,
186
+ setSettledFor: (name, done) => {
187
+ Bool.match(done, { onTrue: () => record.names.add(name), onFalse: () => record.names.delete(name) })
188
+ },
189
+ dispose,
190
+ }
191
+ }
192
+
193
+ // Its orchestration (#277, #291, Motion's): the own transition of its highest active gesture's name,
194
+ // else of its target (the variant it names), else its `transition`.
195
+ const orchestration = (): WardConfig.WardicTransition | undefined => {
196
+ checkTrigger()
197
+ return VariantTree.orchestrationAt(UndefinedOr.getOrThrow(at), () => {
198
+ const c = cfg()
199
+ const own = UndefinedOr.match(VariantTree.topGestureName(VariantTree.activeNamesOf(gestures())), {
200
+ onDefined: (name) => VariantTarget.ownTransitionOf(undefined, name, c.variants as WardConfig.WardicVariants | undefined, c.custom),
201
+ onUndefined: () => VariantTarget.ownTransitionOf(c.animate as VariantTarget.Entry, variant(), c.variants as WardConfig.WardicVariants | undefined, c.custom),
202
+ })
203
+ return VariantTarget.transitionOfTarget(own, c.transition)
204
+ })
205
+ }
206
+ const stagger = (): VariantTree.WardicStaggerConfig => VariantTree.staggerOf(orchestration())
207
+ // A list's names have each their own (#297's PR 2): none for the list as a whole.
208
+ const when = (): "beforeChildren" | "afterChildren" | undefined => {
209
+ if (Arr.isArray(variant())) return undefined
210
+ return UndefinedOr.map(orchestration(), (t) => t.when)
211
+ }
212
+ const initial = VariantTree.handedDown<WardConfig.WardicVariantLabels | false>(config, () => VariantTree.initialNameOf(config.initial), () => UndefinedOr.map(parent, (p) => p.initial))
213
+
214
+ // Each name's orchestration for the Variant children's keys of that name (#297): its own transition's,
215
+ // else the element's. Kept per name until the config changes.
216
+ let byName = new Map<string, VariantTree.NameConfig>()
217
+ let byNameFor: VariantTree.VariantConfig | undefined
218
+ const configOf = (name: string): VariantTree.NameConfig => {
219
+ checkTrigger()
220
+ const changed = VariantTree.changeConfigOf(UndefinedOr.getOrThrow(at))
221
+ if (Predicate.isNotUndefined(changed)) return changed
222
+ const c = cfg()
223
+ if (c !== byNameFor) {
224
+ byNameFor = c
225
+ byName = new Map()
226
+ }
227
+ const kept = byName.get(name)
228
+ if (Predicate.isNotUndefined(kept)) return kept
229
+ const made = VariantTree.nameConfigOf(VariantTarget.transitionOfTarget(VariantTarget.ownTransitionOf(undefined, name, c.variants as WardConfig.WardicVariants | undefined, c.custom), c.transition))
230
+ byName.set(name, made)
231
+ return made
232
+ }
233
+ const staggerFor = (name: string | undefined): VariantTree.WardicStaggerConfig => {
234
+ if (Predicate.isUndefined(name)) return stagger()
235
+ return configOf(name).stagger
236
+ }
237
+ const mayStartFor = (name: string): boolean => Bool.or(configOf(name).when !== "beforeChildren", ownNames.has(name))
238
+ const canStartFor = (name: string): boolean => {
239
+ if (configOf(name).when !== "afterChildren") return true
240
+ let all = true
241
+ records.forEach((record) => {
242
+ if (Bool.not(record.names.has(name))) all = false
243
+ })
244
+ return all
245
+ }
246
+ const settledFor = (names: ReadonlyArray<string>, done: boolean): void =>
247
+ Arr.forEach(names, (name) => {
248
+ Bool.match(done, { onTrue: () => ownNames.add(name), onFalse: () => ownNames.delete(name) })
249
+ })
250
+
251
+ const childrenMayStart = (): boolean => {
252
+ checkTrigger()
253
+ return when() !== "beforeChildren" || ownSettled
254
+ }
255
+ const allChildrenSettled = (): boolean => unsettled === 0
256
+ // The transition its children default to: its own, else the one it inherits.
257
+ const transition = (): WardConfig.WardicTransition | undefined => VariantTree.ownOrInherited(cfg().transition, inherited)
258
+
259
+ return {
260
+ context: { variant, initial, transition, gestures, register, stagger, staggerFor, mayStartFor, when, childrenMayStart, allChildrenSettled, change },
261
+ // It holds its own start for `afterChildren` (it owns the registry, so it reads it directly).
262
+ canStart: () => when() !== "afterChildren" || allChildrenSettled(),
263
+ settled: (done) => {
264
+ checkTrigger()
265
+ if (done === ownSettled) return
266
+ ownSettled = done
267
+ // `beforeChildren`'s gate changed: the children waiting on it re-check.
268
+ if (when() === "beforeChildren") notify()
269
+ },
270
+ canStartFor,
271
+ settledFor,
272
+ bindGestures: (states) => {
273
+ MutableRef.set(ownStates, states)
274
+ },
275
+ }
276
+ }
277
+
278
+ /**
279
+ * An element's place in variant orchestration on the client, from its delivered config (read on each
280
+ * ask) and the context the nearest Wardic ancestor provides. `notify` re-checks the port's watches.
281
+ * With **Animation controls** as its first delivery's `animate` (#433; #798, Q4), the config everything
282
+ * here reads has the Controls change in effect as its entry, none before the first; the change its
283
+ * children read is its own, else the one its inherited context is in, so a start above reaches a
284
+ * Variant child's own Variant children (core's `controlsChangeOf`, as Solid's node reads it).
285
+ */
286
+ export const variantNodeOf = (delivered: () => Delivered, parent: VariantTree.WardicVariantContextValue | undefined, notify: () => void): VariantNode => {
287
+ const own = MutableRef.make<AnimationControls.ControlsChange | undefined>(undefined)
288
+ const ownChange = (): AnimationControls.ControlsChange | undefined => MutableRef.get(own)
289
+ const controlled = AnimationControls.isAnimationControls(delivered().animate)
290
+ const configured: VariantNode["configured"] = Bool.match(controlled, {
291
+ onTrue: () => (c) => AnimationControls.controlledConfigOf(c, ownChange()),
292
+ onFalse: () => AnimationControls.uncontrolledOf,
293
+ })
294
+ const config = configured(delivered())
295
+ const inherited = VariantTree.inheritedContext(config, parent)
296
+ const change = VariantTree.controlsChangeOf(Bool.match(controlled, { onTrue: () => ownChange, onFalse: constUndefined }), inherited)
297
+ const read: Read = { config: () => configured(delivered()), change }
298
+ // A Variant child registers with the ancestor it joins, for its stagger slot and its `when`.
299
+ const joined = UndefinedOr.map(inherited, (context) => ({ context, registration: context.register() }))
300
+ const orchestration = Bool.match(VariantTree.hasOwnVariants(config), {
301
+ onFalse: constUndefined,
302
+ onTrue: () => ownOrchestration(read, parent, inherited, notify),
303
+ })
304
+ // The spine reports on every frame (the per-frame path): both reports resolved once, two plain calls a frame.
305
+ const reportJoined: (done: boolean) => void = UndefinedOr.match(joined, { onUndefined: () => constVoid, onDefined: ({ registration }) => registration.setSettled })
306
+ const reportOwn: (done: boolean) => void = UndefinedOr.match(orchestration, { onUndefined: () => constVoid, onDefined: (o) => o.settled })
307
+ return {
308
+ provided: UndefinedOr.match(orchestration, { onDefined: (o) => o.context, onUndefined: () => VariantTree.passedThrough(config, parent) }),
309
+ inherited,
310
+ extraDelay: UndefinedOr.map(joined, ({ context, registration }) => (name: string | undefined) => {
311
+ const s = VariantTree.staggerOfName(context, name)
312
+ const n = registration.total()
313
+ return VariantTree.childDelayOf(s, registration.index(), n)
314
+ }),
315
+ canStart: () =>
316
+ UndefinedOr.match(inherited, { onUndefined: constTrue, onDefined: (context) => context.childrenMayStart() }) &&
317
+ UndefinedOr.match(orchestration, { onUndefined: constTrue, onDefined: (o) => o.canStart() }),
318
+ onFrame: (done) => {
319
+ reportJoined(done)
320
+ reportOwn(done)
321
+ },
322
+ names: namesOf(joined, orchestration),
323
+ bindGestures: UndefinedOr.match(orchestration, { onUndefined: () => constVoid, onDefined: (o) => o.bindGestures }),
324
+ dispose: UndefinedOr.match(joined, { onUndefined: () => constVoid, onDefined: ({ registration }) => registration.dispose }),
325
+ configured,
326
+ change,
327
+ setChange: (made) => {
328
+ MutableRef.set(own, made)
329
+ },
330
+ }
331
+ }
332
+
333
+ /** Its spine's per-name orchestration (#297's PR 2), as Solid's. None for an element that joins nothing and provides nothing. */
334
+ const namesOf = (
335
+ joined: { readonly context: VariantTree.WardicVariantContextValue; readonly registration: VariantTree.WardicChildRegistration } | undefined,
336
+ own: OwnOrchestration | undefined,
337
+ ): VariantTree.NameOrchestration | undefined => {
338
+ if (Bool.and(Predicate.isUndefined(joined), Predicate.isUndefined(own))) return undefined
339
+ const tell = (names: ReadonlyArray<string>, done: boolean): void => {
340
+ UndefinedOr.map(joined, ({ registration }) => UndefinedOr.map(registration.setSettledFor, (set) => Arr.forEach(names, (name) => set(name, done))))
341
+ UndefinedOr.map(own, (o) => o.settledFor(names, done))
342
+ }
343
+ return {
344
+ mayStart: (name) =>
345
+ Bool.and(
346
+ UndefinedOr.match(joined, { onUndefined: constTrue, onDefined: ({ context }) => VariantTree.mayStartOf(context, name) }),
347
+ UndefinedOr.match(own, { onUndefined: constTrue, onDefined: (o) => o.canStartFor(name) }),
348
+ ),
349
+ moving: (names) => tell(names, false),
350
+ settled: (names) => tell(names, true),
351
+ }
352
+ }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * View Transitions on Foldkit (ADR-0071 decision 33; P10): `wardic.viewTransition(decide)` wraps the
3
+ * app's `viewTransition` predicate. Foldkit calls it before each live render; when it transitions the
4
+ * render (`true`, or `{ types }`), the wrapper arms the Handle's next layout delivery to run its pass as
5
+ * an Instant change, so layout moves and `layoutId` handovers land at once and the View Transition's
6
+ * morph carries the motion. Retargets in that render animate as usual. Where the document has no
7
+ * `startViewTransition`, Foldkit renders plainly and the wrapper arms nothing, so Wardic animates the
8
+ * moves itself. The decision passes through unchanged.
9
+ *
10
+ * @module
11
+ */
12
+ import * as Bool from "effect/Boolean"
13
+ import * as Predicate from "effect/Predicate"
14
+ import type * as Runtime from "foldkit/runtime"
15
+ import type { State } from "./services.ts"
16
+
17
+ /** Whether the document can transition a render: feature-detected once, at the first decision, as Foldkit's runtime detects it once. */
18
+ // Module state (#460, Q7): the detection, held once made; `undefined` until a decision asks.
19
+ let supported: boolean | undefined
20
+
21
+ const isSupported = (): boolean => {
22
+ if (Predicate.isNotUndefined(supported)) return supported
23
+ supported = Predicate.isFunction(document.startViewTransition)
24
+ return supported
25
+ }
26
+
27
+ /** Whether a decision transitions the render: `true`, or `{ types }`. */
28
+ const transitions = (decision: Runtime.ViewTransitionDecision): boolean => decision !== false
29
+
30
+ /**
31
+ * The Handle's `viewTransition` (above): `decide`, with a transitioning decision arming the next pass,
32
+ * whichever asks for it: the observer's delivery of the app's own changes, a `w.LayoutDependency`
33
+ * change, or `Wardic.animateLayout`. Every render decides afresh, so a transitioned render that
34
+ * changed nothing of the layout's leaves no arming behind.
35
+ */
36
+ export const viewTransitionOf =
37
+ (state: State) =>
38
+ <Model, Message>(decide: Runtime.ViewTransitionConfig<Model, Message>): Runtime.ViewTransitionConfig<Model, Message> =>
39
+ (context) => {
40
+ const decision = decide(context)
41
+ state.transitioning = Bool.and(transitions(decision), isSupported())
42
+ return decision
43
+ }