@wardic/foldkit 1.3.1 → 1.3.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.
@@ -50,9 +50,10 @@ import { mounted as scopeMounted } from "./scope.ts"
50
50
  import { layoutOf } from "./layout.ts"
51
51
  import { nearestPresence } from "./presence.ts"
52
52
  import { groupDelivered, reorderItemOf } from "./reorder.ts"
53
- import { hydrationMounted, type Provider, reducedMotionUnder, type State } from "./services.ts"
53
+ import { hydrationMounted, type Provider, reducedMotionUnder, reporterOf, type State } from "./services.ts"
54
54
  import { configValueOf, dropNode, layoutGroupOf, nodeOf, startUnder } from "./tree.ts"
55
55
  import { DEV } from "@wardic/foldkit/internal/dev"
56
+ import { IS_SERVER } from "@wardic/foldkit/internal/server"
56
57
 
57
58
  /** An element's config as the spine reads it: what the view delivered, with its merged `style`. */
58
59
  export type Config = Ward.Config<unknown> & WardConfig.SpineConfig
@@ -69,6 +70,9 @@ export const configOf = (delivered: unknown): Config => {
69
70
  /** What a Retarget throwing inside the patch is reported as: contained at the binding's edge (decision 13), the element left where it is. */
70
71
  const RETARGET_FAILED = "wardic.retarget: a w.* element's Retarget failed; it stays where it is"
71
72
 
73
+ /** What a `w.*` element's Mount or start failing is reported as: contained at the binding's edge (decision 13), the element left at its Starting style. */
74
+ export const MOUNT_FAILED = "wardic.mount: a w.* element's mount failed; it stays at its Starting style"
75
+
72
76
  // Module state (#460, Q7): an id counter, only ever compared for distinctness.
73
77
  let ids = 0
74
78
 
@@ -94,8 +98,13 @@ const devDiagnostics = (): Spine.SpineDiagnostics | undefined => {
94
98
  * `w.Provider()` element's build gives both (#794, Q7), and its Mount writes them for an element with
95
99
  * no Provider element above.
96
100
  */
97
- export const startingStyleOf = (tag: string | undefined, config: Config, registry: () => AtomRegistry.AtomRegistry, inherited?: VariantTree.WardicVariantContextValue, held = false): Style.Style =>
98
- startingStyleFrom(tag, config, registry, Spine.startOf(AnimationControls.uncontrolledOf(config), inherited, held, reactivity.untrack))
101
+ export const startingStyleOf = (tag: string | undefined, config: Config, registry: () => AtomRegistry.AtomRegistry, inherited?: VariantTree.WardicVariantContextValue, held = false): Style.Style => {
102
+ const start = Spine.startOf(AnimationControls.uncontrolledOf(config), inherited, held, reactivity.untrack)
103
+ // A server renders it once: a variant that throws is logged there, its values left out of the markup.
104
+ // A client's mount reports it instead, since each render resolves it again (#979).
105
+ if (IS_SERVER && Option.isSome(start.failure)) Spine.variantFailedOnServer(start.failure.value)
106
+ return startingStyleFrom(tag, config, registry, start)
107
+ }
99
108
 
100
109
  /** The element's Starting style (above) drawn from `start`, its own: what the walk draws beside its Appear entry, which reads `start` too (#794, PR 2). */
101
110
  export const startingStyleFrom = (tag: string | undefined, config: Config, registry: () => AtomRegistry.AtomRegistry, start: Spine.Start): Style.Style =>
@@ -301,344 +310,394 @@ export const mountElement = (provider: Provider, state: State, element: HTMLElem
301
310
  UndefinedOr.map(context, (ctx) => UndefinedOr.map(ctx.made, (made) => made(element, config.key, Predicate.isNotUndefined(config.layoutId))))
302
311
 
303
312
  const begin = (): (() => void) => {
304
- // Its Live values in `style` (#130, ADR-0043): routed keys the spine follows through the gate.
305
- const live = LiveStyle.createLiveStyle({ registry: registryNow, onDroppedTransform: droppedTransformWarning(provider, id), reactivity, ownValues: provider.valueReads.ownValues })
306
- live.css(config.style)
307
- // Where it starts (ADR-0003), read once: the config the view rendered it with, its inherited `initial`
308
- // included (#50), at its `animate` under a presence's `initial: false` (#134).
309
- const start = Spine.startOf(view, inherited, PresenceRecord.presenceBlocksInitial(context), reactivity.untrack)
310
- const snapshot = Spine.snapshotOf(start)
311
- // Its Transform template (#443), drawn with its first write: the one at mount, then each other one.
312
- const templateAtMount = config.transformTemplate
313
- // The keys its Starting style set, switched to the user's in its first commit. A Variant child's
314
- // inherited Starting style wasn't rendered by the view: written now, inside the patch (P2). So is a
315
- // child of a `w.Presence({ initial: false })`: the view rendered it at its `Animate` (the walk at a
316
- // Provider element's build, #794), where only the presence's first render starts it there (#134).
317
- const starting = Style.starting(tag, ElementSetup.drawnAtRest(start.initialValues, false, templateAtMount)).style
318
- if (Bool.or(Predicate.isNotUndefined(inherited), UndefinedOr.match(presence, { onUndefined: constFalse, onDefined: (p) => p.holds() }))) writeStartingStyle(element, starting)
319
- const startingKeys = Rec.keys(starting)
320
- // Its Appear animation, when it hydrates one the server rendered (ADR-0044 decision 6; #794, Q10):
321
- // found by the slot its inline `animation` names, which the hydration render kept, taken over by its
322
- // first Retarget that starts, as far in as the CSS has got, and removed in its first commit, the
323
- // Handoff. Asked at every mount, a string read: an element a client render made names no slot. One
324
- // found keeps hydration's window open until that commit, so no render in between strips the slot.
325
- const handoff = Option.getOrUndefined(AppearAnimations.handoffOf(element))
326
- const released = hydrationMounted(state, Predicate.isNotUndefined(handoff))
327
- const sink: FlushGate.Sink = ElementSetup.releasingAppear(
328
- Spine.withOnUpdate(
329
- {
330
- target: element,
331
- beforeFirstCommit: () => {
332
- switchToUserStyle(element, tag, startingKeys, config, registryNow)
333
- released()
313
+ // What its release undoes, each set as it's made, in the order the release runs them (#997, ADR-0074):
314
+ // a start that throws runs them on what it had made by then, its setter gone with the rest, so it stays
315
+ // at its Starting style and no later delivery reaches it.
316
+ let unsubscribe: () => void = constVoid
317
+ let releaseHydration: () => void = constVoid
318
+ let unwatch: () => void = constVoid
319
+ let unfollowText: () => void = constVoid
320
+ let unfollowTemplate: () => void = constVoid
321
+ let unregisterAnimatable: () => void = constVoid
322
+ let uninstall: () => void = constVoid
323
+ let stopRechecks: () => void = constVoid
324
+ let releaseScope: () => void = constVoid
325
+ let disposeStartsDrag: () => void = constVoid
326
+ let disposeItem: () => void = constVoid
327
+ let disposeDrag: () => void = constVoid
328
+ let disposeGestures: () => void = constVoid
329
+ let disposeLayoutNode: () => void = constVoid
330
+ let unregisterLayoutValues: () => void = constVoid
331
+ let disposeSpine: () => void = constVoid
332
+ let unregisterGate: () => void = constVoid
333
+ // Its presence record's, run only when its start throws: else the presence keeps it, to drive its exit.
334
+ let unregisterRecord: () => void = constVoid
335
+ const releaseMade = (): void => {
336
+ unsubscribe()
337
+ releaseHydration()
338
+ unwatch()
339
+ unfollowText()
340
+ unfollowTemplate()
341
+ unregisterAnimatable()
342
+ uninstall()
343
+ stopRechecks()
344
+ releaseScope()
345
+ disposeStartsDrag()
346
+ disposeItem()
347
+ disposeDrag()
348
+ disposeGestures()
349
+ disposeLayoutNode()
350
+ unregisterLayoutValues()
351
+ disposeSpine()
352
+ unregisterGate()
353
+ }
354
+ try {
355
+ // Its Live values in `style` (#130, ADR-0043): routed keys the spine follows through the gate.
356
+ const live = LiveStyle.createLiveStyle({ registry: registryNow, onDroppedTransform: droppedTransformWarning(provider, id), reactivity, ownValues: provider.valueReads.ownValues })
357
+ live.css(config.style)
358
+ // Where it starts (ADR-0003), read once: the config the view rendered it with, its inherited `initial`
359
+ // included (#50), at its `animate` under a presence's `initial: false` (#134).
360
+ const start = Spine.startOf(view, inherited, PresenceRecord.presenceBlocksInitial(context), reactivity.untrack)
361
+ // A variant whose resolver throws is this element's alone: reported once at its mount and once a run
362
+ // of throws until it resolves, the element keeping its last target, and nothing thrown into the
363
+ // render, which would crash the app (#979; ADR-0074). Each render's Starting style resolves it silently.
364
+ const variantFailures = Spine.variantFailuresOf(provider.runtime)
365
+ Spine.reportStart(variantFailures, start)
366
+ const snapshot = Spine.snapshotOf(start)
367
+ // Its Transform template (#443), drawn with its first write: the one at mount, then each other one.
368
+ const templateAtMount = config.transformTemplate
369
+ // The keys its Starting style set, switched to the user's in its first commit. A Variant child's
370
+ // inherited Starting style wasn't rendered by the view: written now, inside the patch (P2). So is a
371
+ // child of a `w.Presence({ initial: false })`: the view rendered it at its `Animate` (the walk at a
372
+ // Provider element's build, #794), where only the presence's first render starts it there (#134).
373
+ const starting = Style.starting(tag, ElementSetup.drawnAtRest(start.initialValues, false, templateAtMount)).style
374
+ if (Bool.or(Predicate.isNotUndefined(inherited), UndefinedOr.match(presence, { onUndefined: constFalse, onDefined: (p) => p.holds() }))) writeStartingStyle(element, starting)
375
+ const startingKeys = Rec.keys(starting)
376
+ // Its Appear animation, when it hydrates one the server rendered (ADR-0044 decision 6; #794, Q10):
377
+ // found by the slot its inline `animation` names, which the hydration render kept, taken over by its
378
+ // first Retarget that starts, as far in as the CSS has got, and removed in its first commit, the
379
+ // Handoff. Asked at every mount, a string read: an element a client render made names no slot. One
380
+ // found keeps hydration's window open until that commit, so no render in between strips the slot.
381
+ const handoff = Option.getOrUndefined(AppearAnimations.handoffOf(element))
382
+ releaseHydration = hydrationMounted(state, Predicate.isNotUndefined(handoff))
383
+ const sink: FlushGate.Sink = ElementSetup.releasingAppear(
384
+ Spine.withOnUpdate(
385
+ {
386
+ target: element,
387
+ beforeFirstCommit: () => {
388
+ switchToUserStyle(element, tag, startingKeys, config, registryNow)
389
+ releaseHydration()
390
+ },
334
391
  },
392
+ view.onUpdate,
393
+ configNow,
394
+ ),
395
+ handoff,
396
+ element,
397
+ )
398
+ unregisterGate = provider.gate.register(id, sink, ElementSetup.registeredValuesOf(templateAtMount))
399
+ // The user's style now, for its Base values (ADR-0022): the config's, as the view last delivered it.
400
+ const styleNow = (): object | string | undefined => config.style
401
+ const links = ElementSetup.startLinksOf(element, styleNow, live)
402
+ // Reduced motion (ADR-0029) and skipped animations (#468) under its nearest `w.Config`, read at each Retarget.
403
+ const reducedMotion = reducedMotionUnder(provider, configValue)
404
+ const skipped = WardicConfigValue.skipAnimationsOf(configValue)
405
+ const skipAnimations = (): boolean => paused() || skipped()
406
+ // Where its drag's and its pan's points are (#793, Q9): its nearest `w.Config`'s `transformPagePoint`, read at each down.
407
+ const pagePoint = (): WardConfig.WardicTransformPoint | undefined => UndefinedOr.match(configValue, { onUndefined: constUndefined, onDefined: (v) => v.transformPagePoint() })
408
+ // Its presence record (ADR-0007): what its presence drives its exit with after Foldkit removes it,
409
+ // resolved from its `Exit` as it leaves; the presence owns the registration. And where it starts when
410
+ // it takes over from a leaver under its key (#286).
411
+ const exitConfig = { current: Spine.exitConfigOf(view, inherited) }
412
+ const record = UndefinedOr.map(context, (ctx) => {
413
+ const rec = Spine.presenceRecordOf(element, start, (custom, inheritedNames) => Spine.exitTargetOf(exitConfig.current, custom, snapshot, inheritedNames), links, Spine.presenceTreeOf(view), snapshot, reducedMotion, skipAnimations)
414
+ unregisterRecord = ctx.register(rec)
415
+ return rec
416
+ })
417
+ const taken = PresenceRecord.presenceSeedOf(context, element)
418
+ const recordFrame = Spine.recordFrameOf(record)
419
+ // Its gestures (#793; decision 23): each started as a delivery wants it, its entry in the Override
420
+ // stack re-resolved as it flips, and its Variant children told through the port's watches (#291).
421
+ const gestures = gesturesOf({
422
+ provider,
423
+ element,
424
+ config: configNow,
425
+ dispatch,
426
+ onChange: () => {
427
+ retarget()
428
+ recheck()
335
429
  },
336
- view.onUpdate,
337
- configNow,
338
- ),
339
- handoff,
340
- element,
341
- )
342
- const unregister = provider.gate.register(id, sink, ElementSetup.registeredValuesOf(templateAtMount))
343
- // The user's style now, for its Base values (ADR-0022): the config's, as the view last delivered it.
344
- const styleNow = (): object | string | undefined => config.style
345
- const links = ElementSetup.startLinksOf(element, styleNow, live)
346
- // Reduced motion (ADR-0029) and skipped animations (#468) under its nearest `w.Config`, read at each Retarget.
347
- const reducedMotion = reducedMotionUnder(provider, configValue)
348
- const skipped = WardicConfigValue.skipAnimationsOf(configValue)
349
- const skipAnimations = (): boolean => paused() || skipped()
350
- // Where its drag's and its pan's points are (#793, Q9): its nearest `w.Config`'s `transformPagePoint`, read at each down.
351
- const pagePoint = (): WardConfig.WardicTransformPoint | undefined => UndefinedOr.match(configValue, { onUndefined: constUndefined, onDefined: (v) => v.transformPagePoint() })
352
- // Its presence record (ADR-0007): what its presence drives its exit with after Foldkit removes it,
353
- // resolved from its `Exit` as it leaves; the presence owns the registration. And where it starts when
354
- // it takes over from a leaver under its key (#286).
355
- const exitConfig = { current: Spine.exitConfigOf(view, inherited) }
356
- const record = UndefinedOr.map(context, (ctx) => {
357
- const rec = Spine.presenceRecordOf(element, start, (custom, inheritedNames) => Spine.exitTargetOf(exitConfig.current, custom, snapshot, inheritedNames), links, Spine.presenceTreeOf(view), snapshot, reducedMotion, skipAnimations)
358
- ctx.register(rec)
359
- return rec
360
- })
361
- const taken = PresenceRecord.presenceSeedOf(context, element)
362
- const recordFrame = Spine.recordFrameOf(record)
363
- // Its gestures (#793; decision 23): each started as a delivery wants it, its entry in the Override
364
- // stack re-resolved as it flips, and its Variant children told through the port's watches (#291).
365
- const gestures = gesturesOf({
366
- provider,
367
- element,
368
- config: configNow,
369
- dispatch,
370
- onChange: () => {
371
- retarget()
372
- recheck()
373
- },
374
- id,
375
- pagePoint,
376
- // A Reorder item's keys are its Keyboard reorder's (ADR-0037): its press starts from no key.
377
- keysTaken: () => Predicate.isNotUndefined(MutableRef.get(item)),
378
- })
379
- node.bindGestures(gestures.states)
380
- // A Reorder item (decision 28): joined to the group above it, which decides its drag's axis and hears
381
- // its samples; its keyboard lift turns `WhileDrag` on. Joined now, or as a load brings the Features (Q15).
382
- const item = MutableRef.make(reorderItemOf(provider, element, id, () => config, dispatch, gestures.lift))
383
- // Its spine's options, with what its drag writes as its track starts (ADR-0004; #793, PR 2): its
384
- // axis, its track and whether it drives a layout node, which the spine reads where it uses them.
385
- // The axis is seeded from the config at mount too, as Solid's: the spine registers its drag hook (a
386
- // Batch member's hold, #665) at make from it, so a `Drag` a later render adds registers none.
387
- const spineOptions: Spine.ElementSpineOptions & SpineDragOptions = {
388
- runtime: provider.runtime,
389
- gate: provider.gate,
390
- id,
391
- element,
392
- tag,
393
- config: configNow,
394
- parent: inherited,
395
- canStart: node.canStart,
396
- extraDelay: node.extraDelay,
397
- onFrame: node.onFrame,
398
- byName: node.names,
399
- reducedMotion,
400
- skipAnimations,
401
- positionalKeys: provider.positionalKeys,
402
- defaultTransition: provider.defaultTransition,
403
- ownValues: provider.valueReads.ownValues,
404
- measuredPairs: provider.valueReads.measuredPairs,
405
- variableValues: provider.valueReads.variableValues,
406
- start,
407
- snapshot,
408
- reuse: Spine.makeReuse(),
409
- links,
410
- styleNow,
411
- live,
412
- builtFeatures: provider.registry.built,
413
- presence: record,
414
- projected: false,
415
- dragAxis: DragDefaults.dragAxisOf(view, { dragAxis: UndefinedOr.map(MutableRef.get(item), (joined) => joined.dragAxis) }),
416
- drag: undefined,
417
- handoff,
418
- // Motion's onAnimationStart / onAnimationComplete (decision 23), as Messages through `dispatch`.
419
- animation: Spine.animationCallbacks(provider.runtime, id),
420
- diagnostics: devDiagnostics(),
421
- }
422
- const spine = Spine.makeElementSpine(spineOptions)
423
- UndefinedOr.map(taken, (seed) => spine.seed(seed))
424
- // The `StartsDrag` element (Q8): its downs start its controls' drags, from the first delivery that gives them.
425
- const startsDrag = startsDragOf(element, () => config)
426
- // Its Animation scope (#798, Q7 and Q9): the scope's element from now, and the animations started in it stopped at its release.
427
- const releaseScope = UndefinedOr.match(config.animationScope, { onUndefined: () => constVoid, onDefined: (scope) => scopeMounted(scope, element) })
428
- // Its layout (ADR-0009, ADR-0010; decision 30): one projection node for `Layout`, `LayoutId`,
429
- // `LayoutScroll` or `LayoutRoot`, on the provider's layout, built at the first; and, with a layout
430
- // built, its values by element, for a twin's flight to follow (ADR-0012 *Amendment (#137)*). Each
431
- // joined at mount, or as a load brings the Feature (#793, Q15).
432
- // Its config as dom's layout code reads it: the layout keys alone.
433
- const wardConfig = (): WardConfig.WardConfig => view as WardConfig.WardConfig
434
- const builtProjection = (): ProjectionSystem.Shape | undefined => Keys.projectionIn(provider.registry.built())
435
- const layoutNode = MutableRef.make<LayoutNode | undefined>(undefined)
436
- const unregisterValues = MutableRef.make<(() => void) | undefined>(undefined)
437
- const joinLayout = (): void => {
438
- if (Bool.and(Predicate.isUndefined(MutableRef.get(layoutNode)), Feature.wantsLayoutNode(wardConfig()))) {
439
- MutableRef.set(layoutNode, UndefinedOr.map(layoutOf(provider, state), (made) =>
440
- made.projection.register(
441
- made.nodes.registrationOf(
442
- made.nodes.optionsOf({
443
- id,
444
- element,
445
- config: wardConfig,
446
- parent: inherited,
447
- snapshot,
448
- // What its release runs on (ADR-0016, *Amendment (#140)*; Q10): the element's drag transition, read now.
449
- dragTransition: DragTransition.dragTransitionOf(view.dragElastic, view.dragTransition),
450
- // Raised while dragged as a Reorder item (ADR-0020), over the rest.
451
- raiseWhileDragged: Predicate.isNotUndefined(MutableRef.get(item)),
452
- taken,
453
- layoutGroup: layoutGroupOf(element),
454
- reducedMotion,
455
- skipAnimations,
456
- heldTransform: ElementSetup.heldTransformFor(snapshot, templateAtMount, () => view.transformTemplate),
457
- }),
458
- ),
459
- )))
460
- registerItemNode()
430
+ id,
431
+ pagePoint,
432
+ // A Reorder item's keys are its Keyboard reorder's (ADR-0037): its press starts from no key.
433
+ keysTaken: () => Predicate.isNotUndefined(MutableRef.get(item)),
434
+ })
435
+ disposeGestures = gestures.dispose
436
+ node.bindGestures(gestures.states)
437
+ node.reportsTo(variantFailures)
438
+ // A Reorder item (decision 28): joined to the group above it, which decides its drag's axis and hears
439
+ // its samples; its keyboard lift turns `WhileDrag` on. Joined now, or as a load brings the Features (Q15).
440
+ const item = MutableRef.make(reorderItemOf(provider, element, id, () => config, dispatch, gestures.lift))
441
+ disposeItem = () => UndefinedOr.map(MutableRef.get(item), (joined) => joined.dispose())
442
+ // Its spine's options, with what its drag writes as its track starts (ADR-0004; #793, PR 2): its
443
+ // axis, its track and whether it drives a layout node, which the spine reads where it uses them.
444
+ // The axis is seeded from the config at mount too, as Solid's: the spine registers its drag hook (a
445
+ // Batch member's hold, #665) at make from it, so a `Drag` a later render adds registers none.
446
+ const spineOptions: Spine.ElementSpineOptions & SpineDragOptions = {
447
+ runtime: provider.runtime,
448
+ gate: provider.gate,
449
+ id,
450
+ element,
451
+ tag,
452
+ config: configNow,
453
+ parent: inherited,
454
+ canStart: node.canStart,
455
+ extraDelay: node.extraDelay,
456
+ onFrame: node.onFrame,
457
+ byName: node.names,
458
+ reducedMotion,
459
+ skipAnimations,
460
+ positionalKeys: provider.positionalKeys,
461
+ defaultTransition: provider.defaultTransition,
462
+ ownValues: provider.valueReads.ownValues,
463
+ measuredPairs: provider.valueReads.measuredPairs,
464
+ variableValues: provider.valueReads.variableValues,
465
+ start,
466
+ snapshot,
467
+ reuse: Spine.makeReuse(),
468
+ links,
469
+ styleNow,
470
+ live,
471
+ builtFeatures: provider.registry.built,
472
+ presence: record,
473
+ projected: false,
474
+ dragAxis: DragDefaults.dragAxisOf(view, { dragAxis: UndefinedOr.map(MutableRef.get(item), (joined) => joined.dragAxis) }),
475
+ drag: undefined,
476
+ handoff,
477
+ // Motion's onAnimationStart / onAnimationComplete (decision 23), as Messages through `dispatch`.
478
+ animation: Spine.animationCallbacks(provider.runtime, id),
479
+ diagnostics: devDiagnostics(),
461
480
  }
462
- if (Predicate.isUndefined(MutableRef.get(unregisterValues))) MutableRef.set(unregisterValues, UndefinedOr.map(builtProjection(), (projection) => projection.registerValues(element, () => snapshot.values)))
463
- }
464
- /** A Reorder item's node, registered with its group once both are there (ADR-0020): its box, its drag offset and its lift. */
465
- const registerItemNode = (): void => {
466
- UndefinedOr.map(MutableRef.get(layoutNode), (made) => UndefinedOr.map(MutableRef.get(item), (joined) => joined.onLayoutHandle(made)))
467
- }
468
- /** The item joined as a load brings the Features (Q15): its group made by it if its own Mount hasn't, its node registered. */
469
- const joinItem = (): void => {
470
- if (Predicate.isNotUndefined(MutableRef.get(item))) return
471
- MutableRef.set(item, reorderItemOf(provider, element, id, () => config, dispatch, gestures.lift))
472
- registerItemNode()
473
- }
474
- joinLayout()
475
- // Its drag (ADR-0004; #793, PR 2): dom's track, started as a delivery wants it, driving the layout
476
- // node it has then; its samples and its hold on `x`/`y` to the gestures, its release's end too.
477
- const drag = dragOf({
478
- provider,
479
- element,
480
- id,
481
- config: configNow,
482
- snapshot,
483
- recordFrame,
484
- live,
485
- spine: spineOptions,
486
- layoutNode: () => MutableRef.get(layoutNode),
487
- // A Reorder item's axis at each gesture's start, and its samples to its group before the callbacks (ADR-0020).
488
- itemAxis: () => UndefinedOr.map(MutableRef.get(item), (joined) => joined.dragAxis),
489
- pagePoint,
490
- skipAnimations,
491
- onDragging: gestures.dragHeld,
492
- onSample: (sample) => {
493
- UndefinedOr.map(MutableRef.get(item), (joined) => joined.onDragSample(sample))
494
- gestures.dragged(sample)
495
- },
496
- onReleaseEnd: gestures.released,
497
- })
498
- // A load's Features arriving (Q15): what the element wants of them starts then; an item joins its
499
- // group first, and a `layout` from the same load makes its node before the drag's track drives it (#582).
500
- const stopRechecks = provider.onFeatures(() => {
501
- joinItem()
502
- joinLayout()
503
- drag.check()
504
- gestures.check()
505
- })
506
- // Its `LayoutDependency`, compared per delivery as Motion's `prevProps.layoutDependency !==` is: a
507
- // change asks for the node's measure, or, on an element with no node (a group's), the whole tree's.
508
- let dependency = config.layoutDependency
509
- const dependencyChanged = (): void => {
510
- const next = config.layoutDependency
511
- if (Equal.equals(dependency, next)) return
512
- dependency = next
513
- const ask = (): void => {
514
- UndefinedOr.match(MutableRef.get(layoutNode), {
515
- onDefined: (node) => node.notify(),
516
- onUndefined: () => UndefinedOr.map(builtProjection(), (projection) => projection.notify()),
517
- })
481
+ const spine = Spine.makeElementSpine(spineOptions)
482
+ disposeSpine = spine.dispose
483
+ UndefinedOr.map(taken, (seed) => spine.seed(seed))
484
+ // The `StartsDrag` element (Q8): its downs start its controls' drags, from the first delivery that gives them.
485
+ const startsDrag = startsDragOf(element, () => config)
486
+ disposeStartsDrag = startsDrag.dispose
487
+ // Its Animation scope (#798, Q7 and Q9): the scope's element from now, and the animations started in it stopped at its release.
488
+ releaseScope = UndefinedOr.match(config.animationScope, { onUndefined: () => constVoid, onDefined: (scope) => scopeMounted(scope, element) })
489
+ // Its layout (ADR-0009, ADR-0010; decision 30): one projection node for `Layout`, `LayoutId`,
490
+ // `LayoutScroll` or `LayoutRoot`, on the provider's layout, built at the first; and, with a layout
491
+ // built, its values by element, for a twin's flight to follow (ADR-0012 *Amendment (#137)*). Each
492
+ // joined at mount, or as a load brings the Feature (#793, Q15).
493
+ // Its config as dom's layout code reads it: the layout keys alone.
494
+ const wardConfig = (): WardConfig.WardConfig => view as WardConfig.WardConfig
495
+ const builtProjection = (): ProjectionSystem.Shape | undefined => Keys.projectionIn(provider.registry.built())
496
+ const layoutNode = MutableRef.make<LayoutNode | undefined>(undefined)
497
+ const unregisterValues = MutableRef.make<(() => void) | undefined>(undefined)
498
+ disposeLayoutNode = () => UndefinedOr.map(MutableRef.get(layoutNode), (made) => made.dispose())
499
+ unregisterLayoutValues = () => UndefinedOr.map(MutableRef.get(unregisterValues), (unregisterNow) => unregisterNow())
500
+ const joinLayout = (): void => {
501
+ if (Bool.and(Predicate.isUndefined(MutableRef.get(layoutNode)), Feature.wantsLayoutNode(wardConfig()))) {
502
+ MutableRef.set(layoutNode, UndefinedOr.map(layoutOf(provider, state), (made) =>
503
+ made.projection.register(
504
+ made.nodes.registrationOf(
505
+ made.nodes.optionsOf({
506
+ id,
507
+ element,
508
+ config: wardConfig,
509
+ parent: inherited,
510
+ snapshot,
511
+ // What its release runs on (ADR-0016, *Amendment (#140)*; Q10): the element's drag transition, read now.
512
+ dragTransition: DragTransition.dragTransitionOf(view.dragElastic, view.dragTransition),
513
+ // Raised while dragged as a Reorder item (ADR-0020), over the rest.
514
+ raiseWhileDragged: Predicate.isNotUndefined(MutableRef.get(item)),
515
+ taken,
516
+ layoutGroup: layoutGroupOf(element),
517
+ reducedMotion,
518
+ skipAnimations,
519
+ heldTransform: ElementSetup.heldTransformFor(snapshot, templateAtMount, () => view.transformTemplate),
520
+ }),
521
+ ),
522
+ )))
523
+ registerItemNode()
524
+ }
525
+ if (Predicate.isUndefined(MutableRef.get(unregisterValues))) MutableRef.set(unregisterValues, UndefinedOr.map(builtProjection(), (projection) => projection.registerValues(element, () => snapshot.values)))
518
526
  }
519
- // A View Transition's render (decision 33): the pass it asks for is an Instant change of the layout.
520
- if (state.transitioning) {
521
- state.transitioning = false
522
- UndefinedOr.match(builtProjection(), { onUndefined: ask, onDefined: (projection) => projection.instant(ask) })
523
- return
527
+ /** A Reorder item's node, registered with its group once both are there (ADR-0020): its box, its drag offset and its lift. */
528
+ const registerItemNode = (): void => {
529
+ UndefinedOr.map(MutableRef.get(layoutNode), (made) => UndefinedOr.map(MutableRef.get(item), (joined) => joined.onLayoutHandle(made)))
524
530
  }
525
- ask()
526
- }
527
- // The Override stack resolved (ADR-0007), compared by value (#470): a delivery that resolves the same
528
- // targets tells nothing, as a `transition` change alone doesn't in Motion; a gesture's flip
529
- // re-resolves it with the entries on now.
530
- let last: Spine.Resolution | undefined
531
- const retarget = (): void => {
532
- const next = Spine.resolutionOf(view, inherited, start.initialLink, gestures.active(), snapshot, node.change())
533
- if (Predicate.isNotUndefined(last) && Spine.sameResolution(last, next)) return
534
- last = next
535
- spine.retarget(next)
536
- }
537
- // Its Delivery (decision 20): each render's fresh config, inside the patch, Retargets at once (#791, Q14),
538
- // after the binding's hint (decision 32), so the Retarget's legs take their first step in this frame.
539
- // A defect there is Foldkit's patch's otherwise, which would stop the whole app (decision 13): reported,
540
- // and the element stays where it is.
541
- const uninstall = installSetter(element, (delivered) => {
542
- config = configOf(delivered)
543
- if (DEV) warnChangedControls(provider, id, controls, config)
544
- view = viewOf()
545
- // A Reorder group's values that changed (decision 28): its controller told, before the pass that lays the items out in them.
546
- groupDelivered(element)
547
- exitConfig.current = Spine.exitConfigOf(view, inherited)
548
- patchTouched()
549
- hintFrameUnless(provider.loop, paused)
550
- try {
551
- live.css(config.style)
552
- dependencyChanged()
553
- // A gesture this render wants starts now (ADR-0047 decision 5): a callback added later starts its recognizer, a `Drag` its track.
531
+ /** The item joined as a load brings the Features (Q15): its group made by it if its own Mount hasn't, its node registered. */
532
+ const joinItem = (): void => {
533
+ if (Predicate.isNotUndefined(MutableRef.get(item))) return
534
+ MutableRef.set(item, reorderItemOf(provider, element, id, () => config, dispatch, gestures.lift))
535
+ registerItemNode()
536
+ }
537
+ joinLayout()
538
+ // Its drag (ADR-0004; #793, PR 2): dom's track, started as a delivery wants it, driving the layout
539
+ // node it has then; its samples and its hold on `x`/`y` to the gestures, its release's end too.
540
+ const drag = dragOf({
541
+ provider,
542
+ element,
543
+ id,
544
+ config: configNow,
545
+ snapshot,
546
+ recordFrame,
547
+ live,
548
+ spine: spineOptions,
549
+ layoutNode: () => MutableRef.get(layoutNode),
550
+ // A Reorder item's axis at each gesture's start, and its samples to its group before the callbacks (ADR-0020).
551
+ itemAxis: () => UndefinedOr.map(MutableRef.get(item), (joined) => joined.dragAxis),
552
+ pagePoint,
553
+ skipAnimations,
554
+ onDragging: gestures.dragHeld,
555
+ onSample: (sample) => {
556
+ UndefinedOr.map(MutableRef.get(item), (joined) => joined.onDragSample(sample))
557
+ gestures.dragged(sample)
558
+ },
559
+ onReleaseEnd: gestures.released,
560
+ })
561
+ disposeDrag = drag.dispose
562
+ // A load's Features arriving (Q15): what the element wants of them starts then; an item joins its
563
+ // group first, and a `layout` from the same load makes its node before the drag's track drives it (#582).
564
+ stopRechecks = provider.onFeatures(() => {
565
+ joinItem()
566
+ joinLayout()
554
567
  drag.check()
555
568
  gestures.check()
556
- startsDrag.check()
557
- retarget()
558
- } catch (error) {
559
- provider.runtime.runFork(Report.failure(RETARGET_FAILED, Cause.die(error)))
569
+ })
570
+ // Its `LayoutDependency`, compared per delivery as Motion's `prevProps.layoutDependency !==` is: a
571
+ // change asks for the node's measure, or, on an element with no node (a group's), the whole tree's.
572
+ let dependency = config.layoutDependency
573
+ const dependencyChanged = (): void => {
574
+ const next = config.layoutDependency
575
+ if (Equal.equals(dependency, next)) return
576
+ dependency = next
577
+ const ask = (): void => {
578
+ UndefinedOr.match(MutableRef.get(layoutNode), {
579
+ onDefined: (node) => node.notify(),
580
+ onUndefined: () => UndefinedOr.map(builtProjection(), (projection) => projection.notify()),
581
+ })
582
+ }
583
+ // A View Transition's render (decision 33): the pass it asks for is an Instant change of the layout.
584
+ if (state.transitioning) {
585
+ state.transitioning = false
586
+ UndefinedOr.match(builtProjection(), { onUndefined: ask, onDefined: (projection) => projection.instant(ask) })
587
+ return
588
+ }
589
+ ask()
560
590
  }
561
- })
562
- // Its Live values followed from its first routed key on (#130), its text (decision 16), and its
563
- // Transform template after the one at mount (#443), which its presence record draws its exit through.
564
- spine.follow({ resolved: () => UndefinedOr.match(last, { onUndefined: () => Spine.NO_VALUES, onDefined: (resolution) => resolution.resolved }), dragging: gestures.dragging })
565
- const unfollowText = followText(element, config.text, registry)
566
- const unfollowTemplate = UndefinedOr.match(templateAtMount, {
567
- onUndefined: () => constVoid,
568
- onDefined: (template) => {
569
- recordFrame(ElementSetup.registeredValuesOf(template) as Values.Values)
570
- return ElementSetup.followTemplate(template, () => view.transformTemplate, reactivity.watch, (next) => {
571
- provider.gate.write(id, { ...snapshot.values, [DomRenderer.TEMPLATE_KEY]: next })
572
- recordFrame({ [DomRenderer.TEMPLATE_KEY]: next } as unknown as Values.Values)
573
- })
574
- },
575
- })
576
- // Its Imperative animations (#433) go through its spine while it's mounted.
577
- const unregisterAnimatable = Imperative.registerAnimatable(element, spine)
578
- // What a Variant child hears without a delivery of its own (P17): the name its ancestor hands down and
579
- // the gestures it hands on (#291), as one trigger key, and its `when` gate opening, re-checked once a
580
- // patch, at each settle that changes a gate (decision 17) and at each gesture flip above it.
581
- let unwatch: () => void = constVoid
582
- if (Predicate.isNotUndefined(node.names)) {
583
- const unwatchName = UndefinedOr.match(inherited, {
591
+ // The Override stack resolved (ADR-0007), compared by value (#470): a delivery that resolves the same
592
+ // targets tells nothing, as a `transition` change alone doesn't in Motion; a gesture's flip
593
+ // re-resolves it with the entries on now.
594
+ let last: Spine.Resolution | undefined
595
+ const retarget = (): void => {
596
+ const next = Spine.keptOf(variantFailures, Spine.resolutionOf(view, inherited, start.initialLink, gestures.active(), snapshot, node.change()))
597
+ if (Predicate.isNotUndefined(last) && Spine.sameResolution(last, next)) return
598
+ last = next
599
+ spine.retarget(next)
600
+ }
601
+ // Its Delivery (decision 20): each render's fresh config, inside the patch, Retargets at once (#791, Q14),
602
+ // after the binding's hint (decision 32), so the Retarget's legs take their first step in this frame.
603
+ // A defect there is Foldkit's patch's otherwise, which would stop the whole app (decision 13): reported,
604
+ // and the element stays where it is.
605
+ uninstall = installSetter(element, (delivered) => {
606
+ config = configOf(delivered)
607
+ if (DEV) warnChangedControls(provider, id, controls, config)
608
+ view = viewOf()
609
+ // A Reorder group's values that changed (decision 28): its controller told, before the pass that lays the items out in them.
610
+ groupDelivered(element)
611
+ exitConfig.current = Spine.exitConfigOf(view, inherited)
612
+ patchTouched()
613
+ hintFrameUnless(provider.loop, paused)
614
+ try {
615
+ live.css(config.style)
616
+ dependencyChanged()
617
+ // A gesture this render wants starts now (ADR-0047 decision 5): a callback added later starts its recognizer, a `Drag` its track.
618
+ drag.check()
619
+ gestures.check()
620
+ startsDrag.check()
621
+ retarget()
622
+ } catch (error) {
623
+ provider.runtime.runFork(Report.failure(RETARGET_FAILED, Cause.die(error)))
624
+ }
625
+ })
626
+ // Its Live values followed from its first routed key on (#130), its text (decision 16), and its
627
+ // Transform template after the one at mount (#443), which its presence record draws its exit through.
628
+ spine.follow({ resolved: () => UndefinedOr.match(last, { onUndefined: () => Spine.NO_VALUES, onDefined: (resolution) => resolution.resolved }), dragging: gestures.dragging })
629
+ unfollowText = followText(element, config.text, registry)
630
+ unfollowTemplate = UndefinedOr.match(templateAtMount, {
584
631
  onUndefined: () => constVoid,
585
- // Its ancestor's Controls change is a trigger too (#433), even to the same name.
586
- onDefined: (parentContext) => {
587
- const inheritedChange = VariantTree.controlsChangeOf(undefined, parentContext)
588
- return reactivity.watch(() => VariantTree.triggerKeyOf(parentContext.variant(), VariantTree.handDownFrom(parentContext), inheritedChange()), retarget)
632
+ onDefined: (template) => {
633
+ recordFrame(ElementSetup.registeredValuesOf(template) as Values.Values)
634
+ return ElementSetup.followTemplate(template, () => view.transformTemplate, reactivity.watch, (next) => {
635
+ provider.gate.write(id, { ...snapshot.values, [DomRenderer.TEMPLATE_KEY]: next })
636
+ recordFrame({ [DomRenderer.TEMPLATE_KEY]: next } as unknown as Values.Values)
637
+ })
589
638
  },
590
639
  })
591
- const unwatchGate = reactivity.watch(node.canStart, (open) => {
592
- if (Bool.and(open, Predicate.isNotUndefined(last))) spine.retarget(UndefinedOr.getOrThrow(last))
593
- })
594
- unwatch = () => {
595
- unwatchName()
596
- unwatchGate()
640
+ // Its Imperative animations (#433) go through its spine while it's mounted.
641
+ unregisterAnimatable = Imperative.registerAnimatable(element, spine)
642
+ // What a Variant child hears without a delivery of its own (P17): the name its ancestor hands down and
643
+ // the gestures it hands on (#291), as one trigger key, and its `when` gate opening, re-checked once a
644
+ // patch, at each settle that changes a gate (decision 17) and at each gesture flip above it.
645
+ if (Predicate.isNotUndefined(node.names)) {
646
+ const unwatchName = UndefinedOr.match(inherited, {
647
+ onUndefined: () => constVoid,
648
+ // Its ancestor's Controls change is a trigger too (#433), even to the same name.
649
+ onDefined: (parentContext) => {
650
+ const inheritedChange = VariantTree.controlsChangeOf(undefined, parentContext)
651
+ return reactivity.watch(() => VariantTree.triggerKeyOf(parentContext.variant(), VariantTree.handDownFrom(parentContext), inheritedChange()), retarget)
652
+ },
653
+ })
654
+ unwatch = unwatchName
655
+ const unwatchGate = reactivity.watch(node.canStart, (open) => {
656
+ if (Bool.and(open, Predicate.isNotUndefined(last))) spine.retarget(UndefinedOr.getOrThrow(last))
657
+ })
658
+ unwatch = () => {
659
+ unwatchName()
660
+ unwatchGate()
661
+ }
597
662
  }
598
- }
599
- // Its Animation controls reach it from its mount to its release (#798, Q4): each change sets its
600
- // entry, re-resolves it on the change's override and tells the watches, its Variant children's
601
- // triggers among them; their stop ends its legs where they are.
602
- const unsubscribe = UndefinedOr.match(controls, {
603
- onUndefined: () => constVoid,
604
- onDefined: (c) =>
605
- AnimationControls.subscribe(
606
- c,
607
- AnimationControls.controlledElement(
608
- (change) => {
609
- node.setChange(change)
610
- view = viewOf()
611
- retarget()
612
- recheck()
613
- },
614
- { custom: () => config.custom, snapshot },
615
- spine.stop,
663
+ // Its Animation controls reach it from its mount to its release (#798, Q4): each change sets its
664
+ // entry, re-resolves it on the change's override and tells the watches, its Variant children's
665
+ // triggers among them; their stop ends its legs where they are.
666
+ unsubscribe = UndefinedOr.match(controls, {
667
+ onUndefined: () => constVoid,
668
+ onDefined: (c) =>
669
+ AnimationControls.subscribe(
670
+ c,
671
+ AnimationControls.controlledElement(
672
+ (change) => {
673
+ node.setChange(change)
674
+ view = viewOf()
675
+ retarget()
676
+ recheck()
677
+ },
678
+ { custom: () => config.custom, snapshot },
679
+ spine.stop,
680
+ ),
616
681
  ),
617
- ),
618
- })
619
- // Its drag and its gestures wanted at mount, then its first Retarget: from `initial` to `animate`.
620
- drag.check()
621
- gestures.check()
622
- startsDrag.check()
623
- retarget()
682
+ })
683
+ // Its drag and its gestures wanted at mount, then its first Retarget: from `initial` to `animate`.
684
+ drag.check()
685
+ gestures.check()
686
+ startsDrag.check()
687
+ retarget()
688
+ } catch (defect) {
689
+ // Its start's failure is its own (#997, ADR-0074), wherever it runs (its Mount's, its parent's, its
690
+ // presence's delivery): what it made released, its presence record too, reported once with its id,
691
+ // and never thrown on. Its node reports it settled, as its spine never will, so a parent waiting
692
+ // `afterChildren` goes on. Its release then drops what remains, its node in the tree.
693
+ releaseMade()
694
+ unregisterRecord()
695
+ node.onFrame(true)
696
+ reporterOf(provider, id)(MOUNT_FAILED, Cause.die(defect))
697
+ return () => dropNode(element)
698
+ }
624
699
  return () => {
625
- unsubscribe()
626
- released()
627
- unwatch()
628
- unfollowText()
629
- unfollowTemplate()
630
- unregisterAnimatable()
631
- uninstall()
632
- stopRechecks()
633
- releaseScope()
634
- startsDrag.dispose()
635
- UndefinedOr.map(MutableRef.get(item), (joined) => joined.dispose())
636
- drag.dispose()
637
- gestures.dispose()
638
- UndefinedOr.map(MutableRef.get(layoutNode), (node) => node.dispose())
639
- UndefinedOr.map(MutableRef.get(unregisterValues), (unregister) => unregister())
640
- spine.dispose()
641
- unregister()
700
+ releaseMade()
642
701
  dropNode(element)
643
702
  }
644
703
  }