@wardic/core 1.1.0 → 1.1.2

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 (101) hide show
  1. package/dist/unstable/AnimationControls.d.ts +110 -0
  2. package/dist/unstable/AnimationControls.d.ts.map +1 -0
  3. package/dist/unstable/AnimationControls.js +107 -0
  4. package/dist/unstable/AnimationControls.js.map +1 -0
  5. package/dist/unstable/AppearHandoff.d.ts +53 -0
  6. package/dist/unstable/AppearHandoff.d.ts.map +1 -0
  7. package/dist/unstable/AppearHandoff.js +23 -0
  8. package/dist/unstable/AppearHandoff.js.map +1 -0
  9. package/dist/unstable/Config.d.ts +285 -0
  10. package/dist/unstable/Config.d.ts.map +1 -0
  11. package/dist/unstable/Config.js +3 -0
  12. package/dist/unstable/Config.js.map +1 -0
  13. package/dist/unstable/FlushGate.d.ts +222 -0
  14. package/dist/unstable/FlushGate.d.ts.map +1 -0
  15. package/dist/unstable/FlushGate.js +567 -0
  16. package/dist/unstable/FlushGate.js.map +1 -0
  17. package/dist/unstable/FrameTime.d.ts +21 -0
  18. package/dist/unstable/FrameTime.d.ts.map +1 -0
  19. package/dist/unstable/FrameTime.js +45 -0
  20. package/dist/unstable/FrameTime.js.map +1 -0
  21. package/dist/unstable/MeasuredPairs.d.ts +51 -0
  22. package/dist/unstable/MeasuredPairs.d.ts.map +1 -0
  23. package/dist/unstable/MeasuredPairs.js +20 -0
  24. package/dist/unstable/MeasuredPairs.js.map +1 -0
  25. package/dist/unstable/OwnValues.d.ts +29 -0
  26. package/dist/unstable/OwnValues.d.ts.map +1 -0
  27. package/dist/unstable/OwnValues.js +22 -0
  28. package/dist/unstable/OwnValues.js.map +1 -0
  29. package/dist/unstable/PresenceRecord.d.ts +176 -0
  30. package/dist/unstable/PresenceRecord.d.ts.map +1 -0
  31. package/dist/unstable/PresenceRecord.js +26 -0
  32. package/dist/unstable/PresenceRecord.js.map +1 -0
  33. package/dist/unstable/Reactivity.d.ts +29 -0
  34. package/dist/unstable/Reactivity.d.ts.map +1 -0
  35. package/dist/unstable/Reactivity.js +15 -0
  36. package/dist/unstable/Reactivity.js.map +1 -0
  37. package/dist/unstable/Report.d.ts +30 -0
  38. package/dist/unstable/Report.d.ts.map +1 -0
  39. package/dist/unstable/Report.js +66 -0
  40. package/dist/unstable/Report.js.map +1 -0
  41. package/dist/unstable/RetargetLegs.d.ts +189 -0
  42. package/dist/unstable/RetargetLegs.d.ts.map +1 -0
  43. package/dist/unstable/RetargetLegs.js +834 -0
  44. package/dist/unstable/RetargetLegs.js.map +1 -0
  45. package/dist/unstable/Spine.d.ts +696 -0
  46. package/dist/unstable/Spine.d.ts.map +1 -0
  47. package/dist/unstable/Spine.js +2617 -0
  48. package/dist/unstable/Spine.js.map +1 -0
  49. package/dist/unstable/Stops.d.ts +82 -0
  50. package/dist/unstable/Stops.d.ts.map +1 -0
  51. package/dist/unstable/Stops.js +149 -0
  52. package/dist/unstable/Stops.js.map +1 -0
  53. package/dist/unstable/Subject.d.ts +11 -0
  54. package/dist/unstable/Subject.d.ts.map +1 -0
  55. package/dist/unstable/Subject.js +12 -0
  56. package/dist/unstable/Subject.js.map +1 -0
  57. package/dist/unstable/Timelines.d.ts +51 -0
  58. package/dist/unstable/Timelines.d.ts.map +1 -0
  59. package/dist/unstable/Timelines.js +10 -0
  60. package/dist/unstable/Timelines.js.map +1 -0
  61. package/dist/unstable/VariableValues.d.ts +27 -0
  62. package/dist/unstable/VariableValues.d.ts.map +1 -0
  63. package/dist/unstable/VariableValues.js +19 -0
  64. package/dist/unstable/VariableValues.js.map +1 -0
  65. package/dist/unstable/VariantTarget.d.ts +106 -0
  66. package/dist/unstable/VariantTarget.d.ts.map +1 -0
  67. package/dist/unstable/VariantTarget.js +185 -0
  68. package/dist/unstable/VariantTarget.js.map +1 -0
  69. package/dist/unstable/VariantTree.d.ts +351 -0
  70. package/dist/unstable/VariantTree.d.ts.map +1 -0
  71. package/dist/unstable/VariantTree.js +362 -0
  72. package/dist/unstable/VariantTree.js.map +1 -0
  73. package/dist/unstable/WardicConfigValue.d.ts +89 -0
  74. package/dist/unstable/WardicConfigValue.d.ts.map +1 -0
  75. package/dist/unstable/WardicConfigValue.js +106 -0
  76. package/dist/unstable/WardicConfigValue.js.map +1 -0
  77. package/dist/unstable/WardicServices.d.ts +46 -0
  78. package/dist/unstable/WardicServices.d.ts.map +1 -0
  79. package/dist/unstable/WardicServices.js +72 -0
  80. package/dist/unstable/WardicServices.js.map +1 -0
  81. package/package.json +1 -1
  82. package/src/unstable/AnimationControls.ts +199 -0
  83. package/src/unstable/AppearHandoff.ts +81 -0
  84. package/src/unstable/Config.ts +326 -0
  85. package/src/unstable/FlushGate.ts +730 -0
  86. package/src/unstable/FrameTime.ts +52 -0
  87. package/src/unstable/MeasuredPairs.ts +56 -0
  88. package/src/unstable/OwnValues.ts +32 -0
  89. package/src/unstable/PresenceRecord.ts +202 -0
  90. package/src/unstable/Reactivity.ts +35 -0
  91. package/src/unstable/Report.ts +75 -0
  92. package/src/unstable/RetargetLegs.ts +1062 -0
  93. package/src/unstable/Spine.ts +3275 -0
  94. package/src/unstable/Stops.ts +221 -0
  95. package/src/unstable/Subject.ts +13 -0
  96. package/src/unstable/Timelines.ts +58 -0
  97. package/src/unstable/VariableValues.ts +27 -0
  98. package/src/unstable/VariantTarget.ts +246 -0
  99. package/src/unstable/VariantTree.ts +643 -0
  100. package/src/unstable/WardicConfigValue.ts +154 -0
  101. package/src/unstable/WardicServices.ts +125 -0
@@ -0,0 +1,643 @@
1
+ /**
2
+ * The rules of an element's place in variant orchestration (ADR-0007, #50), on every binding: the
3
+ * names an element hands its children and inherits from its parent, the values it starts from, and
4
+ * what a server render builds. Each binding's variant node runs the orchestration over them, with its
5
+ * own reactivity.
6
+ *
7
+ * Motion's rule (CONTEXT.md, **Variant child**):
8
+ * - An element that names its own variants, a name on any variant prop (`initial`, `animate`, the
9
+ * `while*` gestures, `exit`; #618), hands its children its `initial` and `animate` names, and an
10
+ * `initial={false}` with them (#134). Any other passes its parent's through, unless it sets
11
+ * `inherit={false}`, which passes none.
12
+ * - A **Variant child** (`variants`, no names of its own, `inherit` not `false`) inherits its
13
+ * ancestor's names and default transition, takes a stagger slot among the ancestor's variant
14
+ * children, and counts for the ancestor's `when`.
15
+ * - An element with variants of its own (`variants`, or a name) is what its Variant children
16
+ * join: it owns their registry, their stagger and their `when`. Any other element passes its
17
+ * parent's context through, so a Variant child joins its nearest such ancestor; it renders no
18
+ * Provider unless it sets `inherit={false}` ({@link changesContext}).
19
+ *
20
+ * Unstable, for Wardic's own packages: public, with no semver promise.
21
+ *
22
+ * @module
23
+ */
24
+ // Unstable: ADR-0071, decision 5. #672: solid's `variant-tree.ts`, moved for the spine to read an
25
+ // element's place in variant orchestration in core.
26
+ import * as Stagger from "../Stagger.ts"
27
+ import type * as Values from "../Values.ts"
28
+ import * as Stops from "./Stops.ts"
29
+ import * as Arr from "effect/Array"
30
+ import * as Bool from "effect/Boolean"
31
+ import { constTrue, constUndefined, identity } from "effect/Function"
32
+ import * as Predicate from "effect/Predicate"
33
+ import * as UndefinedOr from "effect/UndefinedOr"
34
+ import * as AnimationControls from "./AnimationControls.ts"
35
+ import type * as Config from "./Config.ts"
36
+ import type * as Reactivity from "./Reactivity.ts"
37
+ import * as VariantTarget from "./VariantTarget.ts"
38
+
39
+ /**
40
+ * The handle a child receives from {@link WardicVariantContextValue.register}
41
+ * when it mounts under a `ward.Provider` (ADR-0007, step 21b).
42
+ *
43
+ * - `index` / `total` — the child's SNAPSHOT position + sibling count for the
44
+ * CURRENT transition. The Provider rewrites these only at the trigger (the
45
+ * parent variant changing), never mid-transition; the child reads them to feed
46
+ * core `stagger(amount, { from })(index, total)`.
47
+ * - `setSettled` — the child reports its own settle state (true when its spine
48
+ * settles, false while animating), feeding the parent's `allChildrenSettled`.
49
+ * - `dispose` — removes this record from the registry on unmount.
50
+ */
51
+ export interface WardicChildRegistration {
52
+ readonly index: Reactivity.Accessor<number>
53
+ readonly total: Reactivity.Accessor<number>
54
+ readonly setSettled: (settled: boolean) => void
55
+ /**
56
+ * The child's keys of one of a list's names settled, or moving again (#297's PR 2): for that name's
57
+ * own `afterChildren`. Optional: left out, nothing per name.
58
+ */
59
+ readonly setSettledFor?: (name: string, settled: boolean) => void
60
+ readonly dispose: () => void
61
+ }
62
+
63
+ /**
64
+ * A list's names orchestrated each on their own (#297's PR 2, Motion's `animateVariant` a name): whether
65
+ * a name's keys may start (its provider's `beforeChildren` for that name, and its own `afterChildren`),
66
+ * and what a spine tells as a list's names start moving and settle. Read by polling: a group waiting
67
+ * on a name's gate reads it each tick.
68
+ */
69
+ export interface NameOrchestration {
70
+ readonly mayStart: (name: string) => boolean
71
+ readonly moving: (names: ReadonlyArray<string>) => void
72
+ readonly settled: (names: ReadonlyArray<string>) => void
73
+ }
74
+
75
+ /** The resolved stagger config the Provider exposes to its children. */
76
+ export interface WardicStaggerConfig {
77
+ /** Seconds between siblings (core `stagger` amount). */
78
+ readonly amount: number
79
+ /** Fixed offset added to every child's computed delay, or a function that decides it whole (#436). */
80
+ readonly delayChildren: number | Config.WardicStagger
81
+ /** Origin the stagger distance grows outward from (core option). */
82
+ readonly from: Stagger.Origin
83
+ /** `1` forward (index = position), `-1` reverses the effective index. */
84
+ readonly direction: 1 | -1
85
+ }
86
+
87
+ /**
88
+ * The value carried down the tree by its binding's variant context.
89
+ *
90
+ * - `variant` — the parent instance's CURRENT variant NAME (reactive). It is
91
+ * `undefined` when the parent's resolved `animate` is a `Values` map rather
92
+ * than a name (propagation is by name only — a values map has no name to
93
+ * inherit).
94
+ * - `initial` — the parent's `initial` NAME, or `false`, read once (#50, #134).
95
+ *
96
+ * Every Wardic element provides one (#50): its own if it has variants of its own, else its
97
+ * parent's, passed through (`variant-node.ts`).
98
+ * - `transition` — the parent's transition, inherited as the DEFAULT for
99
+ * children that supply no local `transition`.
100
+ *
101
+ * Designed to extend without breaking: 21b adds optional `registry` / `stagger`
102
+ * / `when` members; readers consume only the fields they know.
103
+ */
104
+ /** A gesture layer, in Motion's priority, lowest first (#291): `whileInView` … `whileDrag`. */
105
+ export type GestureLayer = "inView" | "focused" | "hovered" | "pressed" | "dragged"
106
+
107
+ /** The gesture layers in Motion's priority, lowest first. */
108
+ export const GESTURE_LAYERS: ReadonlyArray<GestureLayer> = ["inView", "focused", "hovered", "pressed", "dragged"]
109
+
110
+ /** The variant names an element hands its Variant children for its active gestures (#291): a name or a list each (#297). */
111
+ export type GestureNames = Readonly<Partial<Record<GestureLayer, Config.WardicVariantLabels | undefined>>>
112
+
113
+ /** No gesture on: what an element with none active hands down. */
114
+ export const NO_GESTURE_NAMES: GestureNames = {}
115
+
116
+ /** Two {@link GestureNames} the same: the same names for each gesture, a list name by name (#297). */
117
+ export const sameGestureNames = (a: GestureNames, b: GestureNames): boolean =>
118
+ GESTURE_LAYERS.every((layer) => VariantTarget.sameLabels(a[layer], b[layer]))
119
+
120
+ /** Whether each gesture is on (#291): a spine's, bound to its variant node. */
121
+ export type GestureStates = Readonly<Record<GestureLayer, boolean>>
122
+
123
+ /** No gesture on. */
124
+ export const NO_GESTURE_STATES: GestureStates = { inView: false, focused: false, hovered: false, pressed: false, dragged: false }
125
+
126
+ /**
127
+ * What an element hands its Variant children for its gestures (#291, Motion's `setActive` and variant
128
+ * context): whether each is on (its own, or its own provider's), and each one's label (its own
129
+ * `while*`, if a name, else the one it took). A Variant child's gesture is on while its provider's is.
130
+ */
131
+ export interface GestureHandDown {
132
+ readonly on: GestureStates
133
+ readonly names: GestureNames
134
+ }
135
+
136
+ /** Nothing on, no labels: what an element hands down with no provider and no gesture. */
137
+ export const NO_HAND_DOWN: GestureHandDown = { on: NO_GESTURE_STATES, names: NO_GESTURE_NAMES }
138
+
139
+ /** Two hand-downs the same: each gesture on alike, with the same label. */
140
+ export const sameHandDown = (a: GestureHandDown, b: GestureHandDown): boolean =>
141
+ GESTURE_LAYERS.every((layer) => Bool.and(a.on[layer] === b.on[layer], VariantTarget.sameLabels(a.names[layer], b.names[layer])))
142
+
143
+ /** What a context hands down, nothing without a context or its gestures. */
144
+ export const handDownFrom = (context: WardicVariantContextValue | undefined): GestureHandDown => {
145
+ // Per Retarget, per element: guards, nothing made (most elements have no provider).
146
+ if (Predicate.isUndefined(context)) return NO_HAND_DOWN
147
+ const gestures = context.gestures
148
+ if (Predicate.isUndefined(gestures)) return NO_HAND_DOWN
149
+ return gestures()
150
+ }
151
+
152
+ /** The gesture props a variant node reads (#291). */
153
+ export interface GestureConfig {
154
+ readonly whileInView?: object | Config.WardicVariantLabels | undefined
155
+ readonly whileFocus?: object | Config.WardicVariantLabels | undefined
156
+ readonly whileHover?: object | Config.WardicVariantLabels | undefined
157
+ readonly whileTap?: object | Config.WardicVariantLabels | undefined
158
+ readonly whileDrag?: object | Config.WardicVariantLabels | undefined
159
+ }
160
+
161
+ /** Each gesture on: its own, or its provider's. */
162
+ export const gesturesOn = (own: GestureStates, above: GestureStates): GestureStates => ({
163
+ inView: Bool.or(own.inView, above.inView),
164
+ focused: Bool.or(own.focused, above.focused),
165
+ hovered: Bool.or(own.hovered, above.hovered),
166
+ pressed: Bool.or(own.pressed, above.pressed),
167
+ dragged: Bool.or(own.dragged, above.dragged),
168
+ })
169
+
170
+ /** A gesture's label (#291): its own `while*`, if a name or a list (#297); with none of its own, the one it took. */
171
+ const labelOf = (own: object | Config.WardicVariantLabels | undefined, inherited: Config.WardicVariantLabels | undefined): Config.WardicVariantLabels | undefined => {
172
+ if (Predicate.isNotUndefined(own)) return nameOf(own)
173
+ return inherited
174
+ }
175
+
176
+ /** What an element hands down (#291), from its config, its own states, and what it takes. */
177
+ export const handDownOf = (config: GestureConfig, own: GestureStates, above: GestureHandDown): GestureHandDown => ({
178
+ on: gesturesOn(own, above.on),
179
+ names: {
180
+ inView: labelOf(config.whileInView, above.names.inView),
181
+ focused: labelOf(config.whileFocus, above.names.focused),
182
+ hovered: labelOf(config.whileHover, above.names.hovered),
183
+ pressed: labelOf(config.whileTap, above.names.pressed),
184
+ dragged: labelOf(config.whileDrag, above.names.dragged),
185
+ },
186
+ })
187
+
188
+ /** The name each gesture's layer resolves while it's on (#291): its label then; off, none. */
189
+ export const activeNamesOf = (handDown: GestureHandDown): GestureNames => ({
190
+ inView: activeName(handDown, "inView"),
191
+ focused: activeName(handDown, "focused"),
192
+ hovered: activeName(handDown, "hovered"),
193
+ pressed: activeName(handDown, "pressed"),
194
+ dragged: activeName(handDown, "dragged"),
195
+ })
196
+
197
+ /** A gesture's label while it's on. */
198
+ const activeName = (handDown: GestureHandDown, layer: GestureLayer): Config.WardicVariantLabels | undefined => {
199
+ if (Bool.not(handDown.on[layer])) return undefined
200
+ return handDown.names[layer]
201
+ }
202
+
203
+ /**
204
+ * A key for a variant name and the active gestures' names: what a provider's orchestration snapshots
205
+ * at (#291), a list's names each (#297). A **Controls change** (#433) is one too, to the same name.
206
+ */
207
+ export const triggerKeyOf = (variant: Config.WardicVariantLabels | undefined, handDown: GestureHandDown, change: AnimationControls.ControlsChange | undefined): string =>
208
+ [keyOfLabels(variant), ...GESTURE_LAYERS.map((layer) => keyOfLabels(activeName(handDown, layer))), UndefinedOr.match(change, { onUndefined: () => "", onDefined: (c) => `${c.serial}` })].join("|")
209
+
210
+ /**
211
+ * The **Controls change** a provider's latest trigger came with (#433): its orchestration is that
212
+ * change's override until a trigger without one. `previous` is the last answer.
213
+ */
214
+ export interface ChangeAtTrigger {
215
+ readonly change: AnimationControls.ControlsChange | undefined
216
+ readonly key: string
217
+ /** The change that orchestrates now: the one the latest trigger came with, or none. */
218
+ readonly by: AnimationControls.ControlsChange | undefined
219
+ }
220
+
221
+ /** {@link ChangeAtTrigger} after a trigger `key` with `change` in effect. */
222
+ export const changeAtTrigger = (change: AnimationControls.ControlsChange | undefined, key: string, previous: ChangeAtTrigger | undefined): ChangeAtTrigger => {
223
+ if (Predicate.isUndefined(previous)) return { change, key, by: change }
224
+ if (change !== previous.change) return { change, key, by: change }
225
+ if (key !== previous.key) return { change, key, by: undefined }
226
+ return previous
227
+ }
228
+
229
+ /** Each Controls change's override as a name's orchestration (#433), made once a change. */
230
+ // Module state (#460, Q7): a cache held weakly by the object it's for, so it goes with it.
231
+ const CHANGE_CONFIGS = new WeakMap<AnimationControls.ControlsChange, NameConfig>()
232
+
233
+ /**
234
+ * The orchestration every name of a list takes at a trigger a Controls change came with (#433): its
235
+ * override's, or `undefined` for none (each name its own).
236
+ */
237
+ export const changeConfigOf = (at: ChangeAtTrigger): NameConfig | undefined => {
238
+ const by = at.by
239
+ if (Predicate.isUndefined(by)) return undefined
240
+ const override = by.override
241
+ if (Predicate.isUndefined(override)) return undefined
242
+ const known = CHANGE_CONFIGS.get(by)
243
+ if (Predicate.isNotUndefined(known)) return known
244
+ const made = nameConfigOf(override)
245
+ CHANGE_CONFIGS.set(by, made)
246
+ return made
247
+ }
248
+
249
+ /** A provider's orchestration at a trigger (#433): the override of the change it came with, else its own. */
250
+ export const orchestrationAt = (at: ChangeAtTrigger, own: () => Config.WardicTransition | undefined): Config.WardicTransition | undefined =>
251
+ UndefinedOr.match(UndefinedOr.map(at.by, (by) => by.override), { onUndefined: own, onDefined: identity })
252
+
253
+ /** Labels as one key: a list's names apart by a character no name has in practice. */
254
+ const keyOfLabels = (labels: Config.WardicVariantLabels | undefined): string => Arr.join(VariantTarget.labelsOf(labels), "\u0001")
255
+
256
+ /** The highest-priority active gesture's name, or list (#297), if any hands one down. */
257
+ export const topGestureName = (names: GestureNames): Config.WardicVariantLabels | undefined =>
258
+ UndefinedOr.match(names.dragged, {
259
+ onDefined: identity,
260
+ onUndefined: () =>
261
+ UndefinedOr.match(names.pressed, {
262
+ onDefined: identity,
263
+ onUndefined: () =>
264
+ UndefinedOr.match(names.hovered, {
265
+ onDefined: identity,
266
+ onUndefined: () => UndefinedOr.match(names.focused, { onDefined: identity, onUndefined: () => names.inView }),
267
+ }),
268
+ }),
269
+ })
270
+
271
+ /**
272
+ * What a variant parent hands its children: `variant()`, its current name or names; `initial`;
273
+ * `transition()`; `when()` and its stagger; and the registration its children make, in mount order,
274
+ * for that orchestration.
275
+ *
276
+ * @see https://wardic.dev/reference/solid/advanced/variant-context
277
+ */
278
+ export interface WardicVariantContextValue {
279
+ /** The parent's current variant NAME, or list of names, reactive (undefined if it's a values map). */
280
+ readonly variant: Reactivity.Accessor<Config.WardicVariantLabels | undefined>
281
+ /**
282
+ * The parent's `initial` variant NAME, or `false`, read once (undefined if it's a values map or
283
+ * absent): a Variant child with no `initial` of its own starts from it, or, for `false`,
284
+ * at its `animate` (Motion's `getCurrentTreeVariants`).
285
+ */
286
+ readonly initial: Config.WardicVariantLabels | false | undefined
287
+ /** The parent's transition, inherited as the default for children. */
288
+ readonly transition: Reactivity.Accessor<Config.WardicTransition | undefined>
289
+ /**
290
+ * What the parent hands down for its gestures (Motion's): whether each is on, and its label.
291
+ * A Variant child's gesture is on while the parent's is, on its own `while*`, else that label.
292
+ * Optional: left out, nothing.
293
+ */
294
+ readonly gestures?: Reactivity.Accessor<GestureHandDown>
295
+ /**
296
+ * Register this child with the parent's ordered registry (call on mount).
297
+ * Returns the child's reactive snapshot index/total + a settle reporter +
298
+ * a disposer. Children register in mount order; the parent snapshots that
299
+ * order at each change it hands down.
300
+ */
301
+ // ADR-0007: snapshot at trigger.
302
+ readonly register: () => WardicChildRegistration
303
+ /**
304
+ * The parent's resolved stagger config (from `config.transition`): amount =
305
+ * `staggerChildren`, plus `delayChildren` / `from` / `direction`.
306
+ */
307
+ readonly stagger: Reactivity.Accessor<WardicStaggerConfig>
308
+ /**
309
+ * The parent's stagger config for one of its names (Motion's `animateVariant` a name): that
310
+ * name's own transition's, else the parent's `transition`. A Variant child's keys of that name take
311
+ * their delay from it. `undefined` asks for `stagger`. Optional: left out, `stagger` for all.
312
+ */
313
+ readonly staggerFor?: (name: string | undefined) => WardicStaggerConfig
314
+ /**
315
+ * For a list: whether a Variant child's keys of `name` may start, that name's own
316
+ * `beforeChildren`: true unless it has one and this element's keys of that name haven't settled
317
+ * since its list last changed. Optional: left out, always.
318
+ */
319
+ readonly mayStartFor?: (name: string) => boolean
320
+ /** The parent's `when` mode, or `undefined` (default concurrent); a list's names have each their own. */
321
+ readonly when: Reactivity.Accessor<"beforeChildren" | "afterChildren" | undefined>
322
+ /**
323
+ * Whether children may START their inherited-variant Retarget yet. True
324
+ * immediately UNLESS `when === "beforeChildren"`, in which case it flips true
325
+ * once the parent's own variant animation settles.
326
+ */
327
+ readonly childrenMayStart: Reactivity.Accessor<boolean>
328
+ /** Whether EVERY Variant child has reported its spine as settled. */
329
+ readonly allChildrenSettled: Reactivity.Accessor<boolean>
330
+ /**
331
+ * The **Controls change** in effect: its own controls' latest, else the one it inherits. A
332
+ * Variant child joins it, and runs its first Retarget for it on its override. Optional: none.
333
+ */
334
+ readonly change?: Reactivity.Accessor<AnimationControls.ControlsChange | undefined>
335
+ /**
336
+ * During a server render, the parent's Appear group: its Variant
337
+ * children join it in render order, for their stagger and `when` before hydration. Opaque here:
338
+ * the server's Appear entry writes it and reads it, and the variant tree passes it on.
339
+ */
340
+ // #672 (PR4-Q4): `unknown`, so the variant tree names none of the server sampler's types.
341
+ readonly appearGroup?: unknown
342
+ }
343
+
344
+ /** The part of an element's config its place in variant orchestration reads. */
345
+ export interface VariantConfig extends GestureConfig {
346
+ readonly initial?: object | Config.WardicVariantLabels | false | undefined
347
+ readonly animate?: object | Config.WardicVariantLabels | undefined
348
+ readonly exit?: object | Config.WardicVariantLabels | undefined
349
+ readonly variants?: object | undefined
350
+ readonly inherit?: boolean | undefined
351
+ readonly transition?: Config.WardicTransition | undefined
352
+ /** What its dynamic variants resolve with (#277). */
353
+ readonly custom?: unknown
354
+ /**
355
+ * Whether its `animate` is **Animation controls**' (#433): it names its own variants whatever its
356
+ * entry is, as Motion's `isControllingVariants`, so it hands down the names its controls set.
357
+ */
358
+ readonly controlled?: boolean | undefined
359
+ }
360
+
361
+ /** An `initial` an element hands its children: a variant name or list (#297), `false` (#134), or `undefined` for a values map or nothing. */
362
+ export const initialNameOf = (entry: object | Config.WardicVariantLabels | false | undefined): Config.WardicVariantLabels | false | undefined =>
363
+ Bool.match(entry === false, { onTrue: () => false as const, onFalse: () => nameOf(entry as object | Config.WardicVariantLabels | undefined) })
364
+
365
+ /** A variant name or a list of names (#297), or `undefined` for a values map or nothing. */
366
+ export const nameOf = (entry: object | Config.WardicVariantLabels | undefined): Config.WardicVariantLabels | undefined =>
367
+ Bool.match(VariantTarget.isLabels(entry), { onTrue: () => entry as Config.WardicVariantLabels, onFalse: constUndefined })
368
+
369
+ /**
370
+ * Whether `config` names its own variants: any of its variant props, `initial`, `animate`, the five
371
+ * `while*` and `exit`, is a name or a list, or its `animate` is **Animation controls** (#433), as given
372
+ * or as its binding marks the config it reads (Motion's `isControllingVariants`, `isVariantLabel`).
373
+ */
374
+ // #618 (decision 8): Motion's `variantProps` (motion-dom 14.6 render/utils/variant-props.mjs); only
375
+ // `initial` and `animate` counted before. Statements, not a loop over the keys: it's read per change.
376
+ export const namesOwnVariants = (config: VariantConfig): boolean =>
377
+ config.controlled === true ||
378
+ AnimationControls.isAnimationControls(config.animate) ||
379
+ VariantTarget.isLabels(config.initial) ||
380
+ VariantTarget.isLabels(config.animate) ||
381
+ VariantTarget.isLabels(config.whileInView) ||
382
+ VariantTarget.isLabels(config.whileFocus) ||
383
+ VariantTarget.isLabels(config.whileHover) ||
384
+ VariantTarget.isLabels(config.whileTap) ||
385
+ VariantTarget.isLabels(config.whileDrag) ||
386
+ VariantTarget.isLabels(config.exit)
387
+
388
+ /** Whether an element has variants of its own, so Variant children join it (Motion's `isVariantNode`). */
389
+ export const hasOwnVariants = (config: VariantConfig): boolean =>
390
+ Predicate.isNotUndefined(config.variants) || namesOwnVariants(config)
391
+
392
+ /**
393
+ * Whether an element changes what its children read, so it renders its variant context's
394
+ * Provider (#50): it has variants of its own, or it sets `inherit={false}`. Any other element hands
395
+ * its children its parent's context as it is, so it renders none. Decided once, at setup, from the
396
+ * same props on a server and on the client, so the nesting never changes after mount.
397
+ */
398
+ export const changesContext = (config: VariantConfig): boolean => hasOwnVariants(config) || config.inherit === false
399
+
400
+ /**
401
+ * The **Controls change** in effect for an element (#433): its own controls', else the one the context
402
+ * it inherits from is in, else none. Per mount, for every element (#460): guards, nothing built.
403
+ */
404
+ export const controlsChangeOf = (
405
+ controls: Reactivity.Accessor<AnimationControls.ControlsChange | undefined> | undefined,
406
+ inherited: WardicVariantContextValue | undefined,
407
+ ): Reactivity.Accessor<AnimationControls.ControlsChange | undefined> => {
408
+ if (Predicate.isNotUndefined(controls)) return controls
409
+ if (Predicate.isUndefined(inherited)) return constUndefined
410
+ const change = inherited.change
411
+ if (Predicate.isUndefined(change)) return constUndefined
412
+ return change
413
+ }
414
+
415
+ /** Whether an element is a **Variant child** (CONTEXT.md). */
416
+ export const isVariantChild = (config: VariantConfig): boolean =>
417
+ Predicate.isNotUndefined(config.variants) && !namesOwnVariants(config) && config.inherit !== false
418
+
419
+ /** The context an element inherits names and its default transition from: its parent's, for a Variant child. */
420
+ export const inheritedContext = (
421
+ config: VariantConfig,
422
+ parent: WardicVariantContextValue | undefined,
423
+ ): WardicVariantContextValue | undefined => {
424
+ // Per element at mount (#540): statements.
425
+ if (isVariantChild(config)) return parent
426
+ return undefined
427
+ }
428
+
429
+ /**
430
+ * The `initial` an element's Starting style resolves: its own, else the `initial` name (or `false`) of
431
+ * the context it inherits from (#50, #134). Read once, as every `initial` is.
432
+ */
433
+ export const initialOf = (
434
+ config: VariantConfig,
435
+ inherited: WardicVariantContextValue | undefined,
436
+ ): object | Config.WardicVariantLabels | false | undefined =>
437
+ UndefinedOr.match(config.initial, {
438
+ onDefined: (initial) => initial,
439
+ onUndefined: () => UndefinedOr.match(inherited, { onUndefined: constUndefined, onDefined: (context) => context.initial }),
440
+ })
441
+
442
+ /** The `animate` an element starts at with `initial={false}`: its own, else the name its context hands down (#134). Read once, untracked. */
443
+ const animateOf = (
444
+ config: VariantConfig,
445
+ inherited: WardicVariantContextValue | undefined,
446
+ untrack: Reactivity.Reactivity["untrack"],
447
+ ): object | Config.WardicVariantLabels | undefined =>
448
+ UndefinedOr.match(config.animate, {
449
+ onDefined: (animate) => animate,
450
+ onUndefined: () => UndefinedOr.match(inherited, { onUndefined: constUndefined, onDefined: (context) => untrack(context.variant) }),
451
+ })
452
+
453
+ /**
454
+ * Whether an element starts at its `animate`, with nothing animating at mount (#134, Motion's
455
+ * `isInitialAnimationBlocked`): its `initial` (own or inherited) is `false`, or it renders with an
456
+ * `<AnimatePresence initial={false}>`'s first render (`presenceBlocks`).
457
+ */
458
+ export const startsAtAnimate = (
459
+ config: VariantConfig,
460
+ inherited: WardicVariantContextValue | undefined,
461
+ presenceBlocks: boolean,
462
+ ): boolean => presenceBlocks || initialOf(config, inherited) === false
463
+
464
+ /**
465
+ * Whether an element's mount tells nothing (#448, Motion's `animation-state.ts`): it
466
+ * {@link startsAtAnimate}, or its `initial` (own, else inherited) is the same name, list or values map
467
+ * as its `animate` (own, else inherited). Its first Retarget, landing with nothing to animate, is told
468
+ * to no one. Read once, untracked.
469
+ */
470
+ export const startsQuiet = (
471
+ config: VariantConfig,
472
+ inherited: WardicVariantContextValue | undefined,
473
+ presenceBlocks: boolean,
474
+ untrack: Reactivity.Reactivity["untrack"],
475
+ ): boolean => {
476
+ if (startsAtAnimate(config, inherited, presenceBlocks)) return true
477
+ const initial = initialOf(config, inherited)
478
+ if (Predicate.isUndefined(initial)) return false
479
+ // The same map is the same entry, as the same name or list is.
480
+ return VariantTarget.sameLabels(initial as Config.WardicVariantLabels, animateOf(config, inherited, untrack) as Config.WardicVariantLabels)
481
+ }
482
+
483
+ /**
484
+ * The values an element starts from, its Starting style's input (ADR-0003): its `initial` (own, else
485
+ * inherited, #50), or, when it {@link startsAtAnimate}, its `animate` (own, else inherited), as
486
+ * Motion's `initial={false}` (#134). Read once: an inherited name is read through `untrack`.
487
+ */
488
+ export const startingValuesOf = (
489
+ config: VariantConfig,
490
+ inherited: WardicVariantContextValue | undefined,
491
+ presenceBlocks: boolean,
492
+ untrack: Reactivity.Reactivity["untrack"],
493
+ ): Values.Values => {
494
+ const atAnimate = startsAtAnimate(config, inherited, presenceBlocks)
495
+ const animate = animateOf(config, inherited, untrack)
496
+ const initial = initialOf(config, inherited)
497
+ // Per element at mount (#540): statements. Starting at `animate` resolves as `initial={false}`,
498
+ // and its first target is `animate`.
499
+ let from = initial
500
+ let first = initial
501
+ if (atAnimate) {
502
+ from = false
503
+ first = animate
504
+ }
505
+ const values = Stops.resolveInitial({
506
+ initial: from,
507
+ animate,
508
+ // Dynamic variants resolved first (#277): `@wardic/dom`'s Starting style reads values maps only.
509
+ variants: VariantTarget.startingVariantsOf(config.variants, config.custom) as Readonly<Record<string, object | undefined>> | undefined,
510
+ })
511
+ // The first target's Transition end (#428, Motion's `makeLatestValues`): set at once, so a server
512
+ // renders it, and a hydration finds it.
513
+ if (Predicate.isUndefined(first) || first === false) return values
514
+ const end = VariantTarget.resolveTarget(first as VariantTarget.Entry, config.variants as Config.WardicVariants | undefined, config.custom, VariantTarget.NO_VARIANT_STATE).transitionEnd
515
+ if (Predicate.isUndefined(end)) return values
516
+ return { ...values, ...end }
517
+ }
518
+
519
+ /**
520
+ * A name an element hands its children (Motion's `getCurrentTreeVariants`): `own` if it names its own
521
+ * variants, else `fromParent` unless it sets `inherit={false}`.
522
+ */
523
+ export const handedDown = <A extends Config.WardicVariantLabels | false>(config: VariantConfig, own: () => A | undefined, fromParent: () => A | undefined): A | undefined =>
524
+ Bool.match(namesOwnVariants(config), {
525
+ onTrue: own,
526
+ onFalse: () => Bool.match(config.inherit === false, { onTrue: constUndefined, onFalse: fromParent }),
527
+ })
528
+
529
+ /** An element's own transition, else the one it inherits. */
530
+ export const ownOrInherited = (own: Config.WardicTransition | undefined, inherited: WardicVariantContextValue | undefined): Config.WardicTransition | undefined =>
531
+ UndefinedOr.match(own, { onUndefined: () => UndefinedOr.map(inherited, (context) => context.transition()), onDefined: identity })
532
+
533
+ /** The stagger config an element's `transition` gives its Variant children. */
534
+ export const staggerOf = (transition: Config.WardicTransition | undefined): WardicStaggerConfig =>
535
+ UndefinedOr.match(transition, { onUndefined: () => staggerWith({}), onDefined: staggerWith })
536
+
537
+ /** A context's stagger config for one of its names (#297): its `staggerFor`'s, else its whole orchestration's. */
538
+ export const staggerOfName = (context: WardicVariantContextValue, name: string | undefined): WardicStaggerConfig => {
539
+ const of = context.staggerFor
540
+ if (Predicate.isUndefined(of)) return context.stagger()
541
+ return of(name)
542
+ }
543
+
544
+ /** Whether a context lets a Variant child's keys of a list's name start (#297's PR 2): its `mayStartFor`, else always. */
545
+ export const mayStartOf = (context: WardicVariantContextValue, name: string): boolean => {
546
+ const of = context.mayStartFor
547
+ if (Predicate.isUndefined(of)) return true
548
+ return of(name)
549
+ }
550
+
551
+ /** A child's place in a stagger, turned by its `staggerDirection`: `-1` counts from the last. */
552
+ export const directed = (index: number, total: number, direction: 1 | -1): number => {
553
+ if (direction === -1) return total - 1 - index
554
+ return index
555
+ }
556
+
557
+ /**
558
+ * A child's delay in a stagger (ADR-0007): its `delayChildren` plus its place from the origin, turned
559
+ * by its `staggerDirection`. A function `delayChildren` (#436, Motion's `calcChildStagger`) decides it
560
+ * whole, from the child's index and the count. Every stagger, a Variant child's, a presence's exit's
561
+ * and a server's Appear timing, is placed by it.
562
+ */
563
+ export const childDelayOf = (s: WardicStaggerConfig, index: number, total: number): number => {
564
+ if (Predicate.isFunction(s.delayChildren)) return s.delayChildren(index, total)
565
+ return s.delayChildren + Stagger.make(s.amount, { from: s.from })(directed(index, total, s.direction), total)
566
+ }
567
+
568
+ /** A name's orchestration (#297): its stagger config, and its own `when`, from its own transition, else the element's. */
569
+ export interface NameConfig {
570
+ readonly stagger: WardicStaggerConfig
571
+ readonly when: "beforeChildren" | "afterChildren" | undefined
572
+ }
573
+
574
+ /** A {@link NameConfig} from the transition a name orchestrates on. */
575
+ export const nameConfigOf = (transition: Config.WardicTransition | undefined): NameConfig => ({
576
+ stagger: staggerOf(transition),
577
+ when: UndefinedOr.map(transition, (t) => t.when),
578
+ })
579
+
580
+ /** The stagger config of a transition's stagger keys, each defaulted. */
581
+ export const staggerWith = ({ staggerChildren = 0, delayChildren = 0, from = "first", staggerDirection = 1 }: Config.WardicTransition): WardicStaggerConfig => ({
582
+ amount: staggerChildren,
583
+ delayChildren,
584
+ from,
585
+ direction: staggerDirection,
586
+ })
587
+
588
+ /**
589
+ * Two stagger configs the same field by field: a memo of one holds while its orchestration changes to
590
+ * a transition with the same stagger (#460: a Variant child with no `press` variant, its parent
591
+ * pressed, made a new, equal config at each press and release, `UNSTABLE_MEMO_OUTPUT`).
592
+ */
593
+ export const sameStagger = (a: WardicStaggerConfig, b: WardicStaggerConfig): boolean =>
594
+ a.amount === b.amount && a.delayChildren === b.delayChildren && a.from === b.from && a.direction === b.direction
595
+
596
+ /** What an element provides when it has no variants of its own: its parent's context, unless `inherit={false}`. */
597
+ export const passedThrough = (
598
+ config: VariantConfig,
599
+ parent: WardicVariantContextValue | undefined,
600
+ ): WardicVariantContextValue | undefined => Bool.match(config.inherit === false, { onTrue: constUndefined, onFalse: () => parent })
601
+
602
+ /** A registration that does nothing: a server render has no orchestration to join. */
603
+ export const INERT_REGISTRATION: WardicChildRegistration = Object.freeze({
604
+ index: () => 0,
605
+ total: () => 1,
606
+ setSettled: () => {},
607
+ dispose: () => {},
608
+ })
609
+
610
+ /** An element's place in variant orchestration during a server render (ADR-0021). */
611
+ export interface ServerVariantNode {
612
+ /** What its children read: the same context the client builds, with inert orchestration. */
613
+ readonly provided: WardicVariantContextValue | undefined
614
+ /** The context it inherits from: its parent's, for a Variant child. */
615
+ readonly inherited: WardicVariantContextValue | undefined
616
+ }
617
+
618
+ /**
619
+ * A binding's variant node during a server render: the same names, read once, and orchestration
620
+ * that does nothing. No runtime, no signals.
621
+ */
622
+ export const serverVariantNode = (config: VariantConfig, parent: WardicVariantContextValue | undefined): ServerVariantNode => {
623
+ const inherited = inheritedContext(config, parent)
624
+ const own = (): WardicVariantContextValue => {
625
+ const variant = handedDown(config, () => nameOf(config.animate), () => UndefinedOr.map(parent, (p) => p.variant()))
626
+ const transition = ownOrInherited(config.transition, inherited)
627
+ const stagger = staggerOf(config.transition)
628
+ return {
629
+ variant: () => variant,
630
+ initial: handedDown<Config.WardicVariantLabels | false>(config, () => initialNameOf(config.initial), () => UndefinedOr.map(parent, (p) => p.initial)),
631
+ transition: () => transition,
632
+ register: () => INERT_REGISTRATION,
633
+ stagger: () => stagger,
634
+ when: () => UndefinedOr.map(config.transition, (t) => t.when),
635
+ childrenMayStart: constTrue,
636
+ allChildrenSettled: constTrue,
637
+ }
638
+ }
639
+ return {
640
+ provided: Bool.match(hasOwnVariants(config), { onTrue: own, onFalse: () => passedThrough(config, parent) }),
641
+ inherited,
642
+ }
643
+ }