@wardic/dom 1.0.1 → 1.1.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 (57) hide show
  1. package/dist/Appear.d.ts +3 -2
  2. package/dist/Appear.d.ts.map +1 -1
  3. package/dist/Appear.js +4 -3
  4. package/dist/Appear.js.map +1 -1
  5. package/dist/FlushGate.d.ts.map +1 -1
  6. package/dist/FlushGate.js +4 -3
  7. package/dist/FlushGate.js.map +1 -1
  8. package/dist/TimelineValue.d.ts +18 -0
  9. package/dist/TimelineValue.d.ts.map +1 -0
  10. package/dist/TimelineValue.js +20 -0
  11. package/dist/TimelineValue.js.map +1 -0
  12. package/dist/index.d.ts +13 -5
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +10 -1
  15. package/dist/index.js.map +1 -1
  16. package/dist/internal/appear.d.ts +10 -4
  17. package/dist/internal/appear.d.ts.map +1 -1
  18. package/dist/internal/appear.js +18 -8
  19. package/dist/internal/appear.js.map +1 -1
  20. package/dist/internal/drag-defaults.js +4 -1
  21. package/dist/internal/drag-defaults.js.map +1 -1
  22. package/dist/internal/gate.d.ts +11 -0
  23. package/dist/internal/gate.d.ts.map +1 -1
  24. package/dist/internal/gate.js +114 -2
  25. package/dist/internal/gate.js.map +1 -1
  26. package/dist/internal/projection.d.ts.map +1 -1
  27. package/dist/internal/projection.js +8 -7
  28. package/dist/internal/projection.js.map +1 -1
  29. package/dist/internal/renderer.d.ts +13 -0
  30. package/dist/internal/renderer.d.ts.map +1 -1
  31. package/dist/internal/renderer.js +21 -0
  32. package/dist/internal/renderer.js.map +1 -1
  33. package/dist/internal/runner.d.ts +4 -3
  34. package/dist/internal/runner.d.ts.map +1 -1
  35. package/dist/internal/runner.js +19 -5
  36. package/dist/internal/runner.js.map +1 -1
  37. package/dist/internal/scroll.d.ts +17 -0
  38. package/dist/internal/scroll.d.ts.map +1 -1
  39. package/dist/internal/scroll.js +23 -15
  40. package/dist/internal/scroll.js.map +1 -1
  41. package/dist/internal/timeline.d.ts +56 -0
  42. package/dist/internal/timeline.d.ts.map +1 -0
  43. package/dist/internal/timeline.js +389 -0
  44. package/dist/internal/timeline.js.map +1 -0
  45. package/package.json +3 -3
  46. package/src/Appear.ts +4 -2
  47. package/src/FlushGate.ts +4 -3
  48. package/src/TimelineValue.ts +19 -0
  49. package/src/index.ts +15 -5
  50. package/src/internal/appear.ts +20 -8
  51. package/src/internal/drag-defaults.ts +4 -4
  52. package/src/internal/gate.ts +110 -2
  53. package/src/internal/projection.ts +8 -7
  54. package/src/internal/renderer.ts +22 -0
  55. package/src/internal/runner.ts +20 -6
  56. package/src/internal/scroll.ts +32 -15
  57. package/src/internal/timeline.ts +480 -0
@@ -65,6 +65,7 @@ import * as UndefinedOr from "effect/UndefinedOr"
65
65
  import * as Scope from "effect/Scope"
66
66
  import { reportFailure } from "./report.ts"
67
67
  import { TEMPLATE_KEY } from "./renderer.ts"
68
+ import type { TimelineValue } from "./timeline.ts"
68
69
  import { dashCase } from "./svg.ts"
69
70
 
70
71
  /**
@@ -146,6 +147,11 @@ const inlineValueOf = (target: unknown, key: string): string => {
146
147
  return style.getPropertyValue(dashCase(key))
147
148
  }
148
149
 
150
+ /** Each of `values` cancelled (#240). Per flush: a plain loop. */
151
+ const cancelEach = (values: Iterable<TimelineValue>): void => {
152
+ for (const value of values) value.cancel()
153
+ }
154
+
149
155
  /** A Flush gate: what a binding registers its elements with and writes their values into, one per provider. */
150
156
  export interface FlushGate {
151
157
  /**
@@ -192,6 +198,16 @@ export interface FlushGate {
192
198
  * keys asked for cost anything.
193
199
  */
194
200
  readonly valueOf: (id: string, key: string) => Atom.Atom<unknown>
201
+ /**
202
+ * Run `key`'s Timeline value on `id`'s element (#240), or stop it (`undefined`): a binding sets one
203
+ * while the key shows its Live value, and clears it when an entry of the Override stack takes the
204
+ * key. The gate goes on writing the key: at each flush that commits the element, before the commit,
205
+ * it starts the value's animation over it, or cancels it while something else adds to its property
206
+ * (a projection, a Transform template, another transform key off its identity), so a frame shows one
207
+ * or the other. A cleared or replaced one is cancelled at the next flush; a released key's, an
208
+ * unregistered element's and a disposed gate's, at once. Setting one schedules a flush.
209
+ */
210
+ readonly timeline: (id: string, key: string, timeline: TimelineValue | undefined) => void
195
211
  /**
196
212
  * Call `fn` once, after the gate's next flush: the end of the Frame loop's tick, for a call
197
213
  * made in one, else the next Refresh frame's, so what was written before it is on the elements.
@@ -275,6 +291,10 @@ export const makeFlushGate = (runtime: FlushGateRuntime, options: FlushGateOptio
275
291
  let recording: Set<string> | undefined
276
292
  // The entries committed since the last frame flush whose sink has an `onCommit` (#233).
277
293
  const committedSince = new Set<Entry>()
294
+ // Each element's Timeline values by key (#240), and those let go of since its last flush, cancelled at
295
+ // its next: only the elements that have one, so the per-frame path checks their sizes alone.
296
+ const timelines = new Map<Entry, Map<string, TimelineValue>>()
297
+ const endings = new Map<Entry, Array<TimelineValue>>()
278
298
  // A frame flush's entries, in order, prepared and waiting for their commit; and those whose
279
299
  // keys held changed, which the binding hears first. Reused each flush: the per-frame path.
280
300
  const prepared: Array<Entry> = []
@@ -321,6 +341,34 @@ export const makeFlushGate = (runtime: FlushGateRuntime, options: FlushGateOptio
321
341
  if ((added || Predicate.isNotUndefined(restore)) && Predicate.isNotUndefined(sink.onHeld)) held.push(entry)
322
342
  }
323
343
 
344
+ /**
345
+ * Before a flush's commits (#240): cancel the Timeline values each element committing now let go of,
346
+ * and sync the rest with its values, so each starts or stops as nothing else adds to its property.
347
+ * An entry is committing while it holds a prepared patch. Kept beside the entries, not on them, so an
348
+ * element with none pays nothing per frame: the flush checks two sizes. Per flush: plain loops.
349
+ */
350
+ const syncTimelines = (): void => {
351
+ for (const [entry, ended] of endings) {
352
+ if (Predicate.isUndefined(entry.patch)) continue
353
+ endings.delete(entry)
354
+ cancelEach(ended)
355
+ }
356
+ for (const [entry, byKey] of timelines) {
357
+ if (Predicate.isUndefined(entry.patch)) continue
358
+ for (const value of byKey.values()) value.sync(entry.values)
359
+ }
360
+ }
361
+
362
+ /** Every Timeline value of `entry`, cancelled at once: its element is gone, or the gate is. */
363
+ const cancelTimelines = (entry: Entry): void => {
364
+ const ended = endings.get(entry)
365
+ endings.delete(entry)
366
+ if (Predicate.isNotUndefined(ended)) cancelEach(ended)
367
+ const byKey = timelines.get(entry)
368
+ timelines.delete(entry)
369
+ if (Predicate.isNotUndefined(byKey)) cancelEach(byKey.values())
370
+ }
371
+
324
372
  /** Commit a prepared entry's patch onto its element. */
325
373
  const commitPrepared = (entry: Entry): void => {
326
374
  const patch = entry.patch
@@ -419,6 +467,7 @@ export const makeFlushGate = (runtime: FlushGateRuntime, options: FlushGateOptio
419
467
  // Every dirty element, once. No auto-reschedule — the next write() does. A write()
420
468
  // from an effect a sink's hook runs requests the next frame (it isn't stranded).
421
469
  for (const entry of accumulator.values()) if (entry.dirty) prepare(entry, prepared, heldChanged)
470
+ if (timelines.size > 0 || endings.size > 0) syncTimelines()
422
471
  commitAll(prepared, heldChanged)
423
472
  if (committedSince.size > 0) tellCommitted()
424
473
  } catch (defect) {
@@ -465,10 +514,23 @@ export const makeFlushGate = (runtime: FlushGateRuntime, options: FlushGateOptio
465
514
  }
466
515
 
467
516
  const register: FlushGate["register"] = (id, sink, values) => {
468
- const entry: Entry = { values: UndefinedOr.match(values, { onUndefined: () => ({}), onDefined: (start) => ({ ...start }) }), sink, dirty: false, committed: new Map(), outputs: outputsById.get(id), latest: undefined, failing: false, patch: undefined }
517
+ const entry: Entry = {
518
+ values: UndefinedOr.match(values, { onUndefined: () => ({}), onDefined: (start) => ({ ...start }) }),
519
+ sink,
520
+ dirty: false,
521
+ committed: new Map(),
522
+ outputs: outputsById.get(id),
523
+ latest: undefined,
524
+ failing: false,
525
+ patch: undefined,
526
+ }
527
+ // An entry the id had: its Timeline values end with it (#240).
528
+ const replaced = accumulator.get(id)
529
+ if (Predicate.isNotUndefined(replaced)) cancelTimelines(replaced)
469
530
  accumulator.set(id, entry)
470
531
  if (Predicate.isNotUndefined(sink.beforeFirstCommit)) firstCommits.add(id)
471
532
  return () => {
533
+ cancelTimelines(entry)
472
534
  accumulator.delete(id)
473
535
  firstCommits.delete(id)
474
536
  committedSince.delete(entry)
@@ -547,12 +609,55 @@ export const makeFlushGate = (runtime: FlushGateRuntime, options: FlushGateOptio
547
609
  const entry = accumulator.get(id)
548
610
  if (Predicate.isNotUndefined(entry) && entry.dirty) prepare(entry, entries, held)
549
611
  }
612
+ if (timelines.size > 0 || endings.size > 0) syncTimelines()
550
613
  commitAll(entries, held)
551
614
  }
552
615
 
616
+ // Per key per Retarget, as the Override stack takes a key and lets it go: statements (the hot path).
617
+ const timeline: FlushGate["timeline"] = (id, key, value) => {
618
+ const entry = accumulator.get(id)
619
+ if (Predicate.isUndefined(entry)) return
620
+ let byKey = timelines.get(entry)
621
+ let known: TimelineValue | undefined
622
+ if (Predicate.isNotUndefined(byKey)) known = byKey.get(key)
623
+ if (known === value) return
624
+ // The one it had is cancelled at the next flush, with the commit that draws the key without it.
625
+ if (Predicate.isNotUndefined(known)) {
626
+ const ending = endings.get(entry)
627
+ if (Predicate.isNotUndefined(ending)) ending.push(known)
628
+ if (Predicate.isUndefined(ending)) endings.set(entry, [known])
629
+ if (Predicate.isNotUndefined(byKey)) byKey.delete(key)
630
+ }
631
+ if (Predicate.isNotUndefined(value)) {
632
+ if (Predicate.isUndefined(byKey)) {
633
+ byKey = new Map()
634
+ timelines.set(entry, byKey)
635
+ }
636
+ byKey.set(key, value)
637
+ }
638
+ // An element left with none leaves the map, so the flush skips it.
639
+ if (Predicate.isNotUndefined(byKey) && byKey.size === 0) timelines.delete(entry)
640
+ entry.dirty = true
641
+ scheduleFlush()
642
+ }
643
+
644
+ /** `entry`'s Timeline values for `keys`, cancelled at once: those keys go back to the element's own style. */
645
+ const releaseTimelines = (entry: Entry, keys: ReadonlyArray<string>): void => {
646
+ const byKey = timelines.get(entry)
647
+ if (Predicate.isUndefined(byKey)) return
648
+ for (const key of keys) {
649
+ const value = byKey.get(key)
650
+ if (Predicate.isUndefined(value)) continue
651
+ byKey.delete(key)
652
+ value.cancel()
653
+ }
654
+ if (byKey.size === 0) timelines.delete(entry)
655
+ }
656
+
553
657
  const release: FlushGate["release"] = (id, keys, restore, keep) => {
554
658
  const entry = accumulator.get(id)
555
659
  if (Predicate.isUndefined(entry)) return
660
+ releaseTimelines(entry, keys)
556
661
  // A property another writer draws next: dropped from what's saved, so its drop-out writes nothing.
557
662
  if (Predicate.isNotUndefined(keep)) for (const property of keep) entry.committed.delete(property)
558
663
  let released = false
@@ -579,6 +684,9 @@ export const makeFlushGate = (runtime: FlushGateRuntime, options: FlushGateOptio
579
684
  cancelFrame(frame)
580
685
  scheduled = false
581
686
  }
687
+ for (const entry of accumulator.values()) cancelTimelines(entry)
688
+ timelines.clear()
689
+ endings.clear()
582
690
  accumulator.clear()
583
691
  firstCommits.clear()
584
692
  committedSince.clear()
@@ -592,7 +700,7 @@ export const makeFlushGate = (runtime: FlushGateRuntime, options: FlushGateOptio
592
700
  const ticks = Scope.forkUnsafe(runtime.scope, "sequential")
593
701
  runtime.runSync(afterTick(flushTicked).pipe(Scope.provide(ticks)))
594
702
 
595
- return { register, write, flushPass, release, valueOf, afterFlush, dispose }
703
+ return { register, write, flushPass, release, valueOf, afterFlush, timeline, dispose }
596
704
  }
597
705
 
598
706
 
@@ -71,6 +71,7 @@ import type * as Arc from "@wardic/core/Arc"
71
71
  import type * as Point from "@wardic/core/Point"
72
72
  import * as Probe from "@wardic/core/Probe"
73
73
  import * as Projection from "@wardic/core/Projection"
74
+ import * as Spans from "@wardic/core/Spans"
74
75
  import * as Renderer from "@wardic/core/Renderer"
75
76
  import * as Spring from "@wardic/core/Spring"
76
77
  import * as Machine from "@wardic/core/Machine"
@@ -1926,8 +1927,8 @@ export const makeProjectionSystem = (
1926
1927
  Effect.map(expire),
1927
1928
  Effect.flatMap((outcome) => Effect.annotateCurrentSpan(outcome)),
1928
1929
  Effect.tapCause((cause) => reportFailure(`wardic.projection: ${span} failed`, cause)),
1929
- // No call-site stack (ADR-0040, decision 2): pipeable, it's the third argument.
1930
- Effect.withSpan(span, { attributes }, { captureStackTrace: false }),
1930
+ // The app's to give, with no call-site stack (#746; ADR-0040, decision 2).
1931
+ Spans.withSpan(span, { attributes }),
1931
1932
  ),
1932
1933
  ),
1933
1934
  )
@@ -4349,7 +4350,7 @@ export const makeProjectionSystem = (
4349
4350
  blocked,
4350
4351
  refresh: refreshed,
4351
4352
  })
4352
- }).pipe(Effect.withSpan("wardic.projection.measure", undefined, { captureStackTrace: false }))
4353
+ }).pipe(Spans.withSpan("wardic.projection.measure"))
4353
4354
 
4354
4355
  /**
4355
4356
  * Whether a trigger now comes after its frame's tick and before its paint, known from where it runs
@@ -4527,7 +4528,7 @@ export const makeProjectionSystem = (
4527
4528
  // A trigger from its flush is the end-of-task pass's, which the call queues.
4528
4529
  triggeredDuringPass = false
4529
4530
  return Effect.annotateCurrentSpan({ nodes: nodes.size, active: active.size })
4530
- }).pipe(Effect.withSpan("wardic.projection.declared", undefined, { captureStackTrace: false }))
4531
+ }).pipe(Spans.withSpan("wardic.projection.declared"))
4531
4532
 
4532
4533
  /**
4533
4534
  * The Measurements a task's first **Declared layout change** takes (#4): a Measure pass already due
@@ -5631,13 +5632,13 @@ export const makeProjectionSystem = (
5631
5632
  */
5632
5633
  const handleLeave: ProjectionSystem["handleLeave"] = (el, onDone) =>
5633
5634
  runtime.runSync(
5634
- Effect.sync(() => doHandleLeave(el, onDone)).pipe(Effect.withSpan("wardic.projection.handleLeave", undefined, { captureStackTrace: false })),
5635
+ Effect.sync(() => doHandleLeave(el, onDone)).pipe(Spans.withSpan("wardic.projection.handleLeave")),
5635
5636
  )
5636
5637
 
5637
5638
  const handBackWithin: ProjectionSystem["handBackWithin"] = (el, onDone) =>
5638
5639
  runtime.runSync(
5639
5640
  Effect.sync(() => Option.fromUndefinedOr(doHandBackWithin(el, onDone))).pipe(
5640
- Effect.withSpan("wardic.projection.handBackWithin", undefined, { captureStackTrace: false }),
5641
+ Spans.withSpan("wardic.projection.handBackWithin"),
5641
5642
  ),
5642
5643
  )
5643
5644
 
@@ -5683,7 +5684,7 @@ export const makeProjectionSystem = (
5683
5684
  finishLeaver(node)
5684
5685
  return Option.some(fade)
5685
5686
  })
5686
- ).pipe(Effect.withSpan("wardic.projection.dropLeaver", undefined, { captureStackTrace: false })),
5687
+ ).pipe(Spans.withSpan("wardic.projection.dropLeaver")),
5687
5688
  )
5688
5689
 
5689
5690
  /** Whether a page box takes space in the flow (#261): read the first time it's asked, then kept. */
@@ -22,8 +22,10 @@ import type * as Point from "@wardic/core/Point"
22
22
  import * as Projection from "@wardic/core/Projection"
23
23
  import * as Renderer from "@wardic/core/Renderer"
24
24
  import { dashCase, isSvgInner, setSvgAttrs, splitPatch } from "./svg.ts"
25
+ import * as Arr from "effect/Array"
25
26
  import * as Bool from "effect/Boolean"
26
27
  import * as Layer from "effect/Layer"
28
+ import * as Option from "effect/Option"
27
29
  import * as Predicate from "effect/Predicate"
28
30
  import * as UndefinedOr from "effect/UndefinedOr"
29
31
 
@@ -74,6 +76,18 @@ const isIdentity = (value: unknown, identity: number): boolean => {
74
76
  return Predicate.isString(value) && Number.parseFloat(value) === identity
75
77
  }
76
78
 
79
+ /**
80
+ * Whether `key`'s `value` adds to the element's `transform` (#240): a transform key off its identity, a
81
+ * `transform` string, the projection's value or a Transform template. A Timeline value drawing
82
+ * `transform` runs only while nothing else does. Per frame: statements.
83
+ */
84
+ export const addsToTransform = (key: string, value: unknown): boolean => {
85
+ if (key === Projection.KEY || key === TEMPLATE_KEY || key === "transform") return true
86
+ const identity = BaseValue.TRANSFORM_IDENTITY[key]
87
+ if (Predicate.isUndefined(identity)) return false
88
+ return !isIdentity(value, identity)
89
+ }
90
+
77
91
  /** `value` when it's a number, else `fallback`. Per frame: an early return, no Option. */
78
92
  const numberOr = (value: unknown, fallback: number): number => {
79
93
  if (Predicate.isNumber(value)) return value
@@ -197,6 +211,14 @@ const formatTransformValue = (key: string, value: unknown): string => {
197
211
  return String(value)
198
212
  }
199
213
 
214
+ /**
215
+ * `key`'s own transform function for `value`, drawn even at its identity: a Timeline value's keyframe
216
+ * (#240), so each blends with its neighbours as one function, as the gate's values do. `None` for a key
217
+ * that isn't a transform key.
218
+ */
219
+ export const transformFunctionOf = (key: string, value: unknown): Option.Option<string> =>
220
+ Option.map(Arr.findFirst(TRANSFORM_FUNCS, (f) => f.key === key), ({ fn }) => `${fn}(${formatTransformValue(key, value)})`)
221
+
200
222
  /** A transform key's default unit; none for a key the table doesn't name. */
201
223
  // Exported for the Batch writer (#665).
202
224
  export const unitOf = (key: string): TransformUnit => {
@@ -15,8 +15,7 @@
15
15
  import * as Bool from "effect/Boolean"
16
16
  import * as Context from "effect/Context"
17
17
  import * as Effect from "effect/Effect"
18
- import type * as Fiber from "effect/Fiber"
19
- import * as FiberSet from "effect/FiberSet"
18
+ import * as Fiber from "effect/Fiber"
20
19
  import * as Scheduler from "effect/Scheduler"
21
20
  import * as Scope from "effect/Scope"
22
21
 
@@ -33,8 +32,9 @@ export interface Runner<R> {
33
32
  }
34
33
 
35
34
  /**
36
- * A runner over the current context, without a sync scheduler, forking into a `FiberSet` in the
37
- * current Scope. It forks with that context itself (`Effect.runForkWith`), not through
35
+ * A runner over the current context, without a sync scheduler, holding its forks in a set of its own
36
+ * until the current Scope closes and interrupts them (#746: what a `FiberSet` held, so an app ships no
37
+ * FiberSet); a fork after that is interrupted at once. It forks with that context itself (`Effect.runForkWith`), not through
38
38
  * `FiberSet.runtime`, which reads the context again in the current fiber, where the building call's
39
39
  * scheduler still is: `Effect.provideContext` merges into a fiber's context, it doesn't replace it.
40
40
  */
@@ -43,11 +43,25 @@ export interface Runner<R> {
43
43
  export const makeRunner = <R>(): Effect.Effect<Runner<R>, never, R | Scope.Scope> =>
44
44
  Effect.gen(function* () {
45
45
  const context = withoutSyncScheduler(yield* Effect.context<R | Scope.Scope>())
46
- const fibers = yield* FiberSet.make()
46
+ const fibers = new Set<Fiber.Fiber<unknown, unknown>>()
47
+ let closed = false
48
+ yield* Effect.addFinalizer(() =>
49
+ Effect.suspend(() => {
50
+ closed = true
51
+ return Fiber.interruptAll(fibers)
52
+ }),
53
+ )
47
54
  const fork = Effect.runForkWith(context)
48
55
  const runFork = <A, E>(effect: Effect.Effect<A, E, R>): Fiber.Fiber<A, E> => {
49
56
  const fiber = fork(effect)
50
- FiberSet.addUnsafe(fibers, fiber)
57
+ if (closed) {
58
+ fiber.interruptUnsafe()
59
+ return fiber
60
+ }
61
+ fibers.add(fiber)
62
+ fiber.addObserver(() => {
63
+ if (!closed) fibers.delete(fiber)
64
+ })
51
65
  return fiber
52
66
  }
53
67
  return { runSync: Effect.runSyncWith(context), runFork, scope: Context.get(context, Scope.Scope) }
@@ -31,6 +31,7 @@ import * as SharedSource from "@wardic/core/SharedSource"
31
31
  import * as Arr from "effect/Array"
32
32
  import * as Bool from "effect/Boolean"
33
33
  import * as Effect from "effect/Effect"
34
+ import { identity } from "effect/Function"
34
35
  import * as Iter from "effect/Iterable"
35
36
  import * as Layer from "effect/Layer"
36
37
  import * as Match from "effect/Match"
@@ -111,8 +112,8 @@ const pairOf: (stop: ScrollOffset.Stop) => Pair = Match.type<ScrollOffset.Stop>(
111
112
  Match.orElse((pair) => pair as Pair),
112
113
  )
113
114
 
114
- /** A stop as a scroll position: where the target's edge meets the container's. */
115
- const positionOf = (stop: ScrollOffset.Stop, containerLength: number, targetLength: number, targetInset: number): number => {
115
+ /** A stop as a scroll position: where the target's edge meets the container's. A Timeline value probes it for its range (#240). */
116
+ export const positionOf = (stop: ScrollOffset.Stop, containerLength: number, targetLength: number, targetInset: number): number => {
116
117
  const [target, container] = pairOf(stop)
117
118
  return targetInset + edgeOffset({ edge: target, length: targetLength }) - edgeOffset({ edge: container, length: containerLength })
118
119
  }
@@ -186,7 +187,8 @@ const insetOf = (element: Element | null, container: Element, x: number, y: numb
186
187
  return { x, y }
187
188
  }
188
189
 
189
- const isHTMLElement = (e: Element): e is HTMLElement => e instanceof HTMLElement
190
+ /** Whether `u` is an HTML element: what a Timeline value's target must be too (#240). */
191
+ export const isHTMLElement = (u: unknown): u is HTMLElement => u instanceof HTMLElement
190
192
 
191
193
  /**
192
194
  * `x`, `y` from `parent`, where the offset chain left `container`, made `container`'s: its own offset
@@ -219,17 +221,30 @@ const sizeOf = (target: Element): { readonly width: number; readonly height: num
219
221
  /** A container's progress on one axis with no target or offset: Motion's `progress(0, length, current)`, 1 for no range. */
220
222
  const plainProgress = (current: number, length: number): number => Bool.match(length === 0, { onTrue: () => 1, onFalse: () => current / length })
221
223
 
224
+ /** `options`' offset, the DOM's (core passes it through as the Environment's own): `All` with a target and none. */
225
+ export const offsetOf = (options: ScrollSource.Options): ScrollOffset.ScrollOffset =>
226
+ UndefinedOr.match(options.offset as ScrollOffset.ScrollOffset | undefined, { onUndefined: () => ScrollOffset.All, onDefined: identity })
227
+
228
+ /**
229
+ * Whether `options`' progress on `axis` runs through its offsets: with a target or an offset, on the
230
+ * options' own axis (`"y"` when left out). Else it's the container's plain progress. A Timeline value
231
+ * reads it as a reader does (#240).
232
+ */
233
+ export const resolvesOn = (options: ScrollSource.Options, axis: "x" | "y"): boolean =>
234
+ Bool.and(
235
+ Bool.or(Predicate.isNotUndefined(options.target), Predicate.isNotUndefined(options.offset)),
236
+ UndefinedOr.match(options.axis, { onUndefined: () => "y", onDefined: identity }) === axis,
237
+ )
238
+
239
+ /** An element's client size on `axis`: a container's length, as a reader and a Timeline value read it. */
240
+ export const clientLengthOf = (element: Element, axis: "x" | "y"): number =>
241
+ Bool.match(axis === "y", { onTrue: () => element.clientHeight, onFalse: () => element.clientWidth })
242
+
222
243
  /** What a reader measures, for `options` on `element`. Reads layout: called in events, never per frame. */
223
244
  const measureOf = (element: Element, options: ScrollSource.Options): (() => ScrollSource.Scroll) => {
224
245
  const { axis = "y" } = options
225
- // The DOM's offset (core passes it through as the Environment's own); `All` with a target and none.
226
- const offset = UndefinedOr.match(options.offset as ScrollOffset.ScrollOffset | undefined, {
227
- onUndefined: () => ScrollOffset.All,
228
- onDefined: (given) => given,
229
- })
246
+ const offset = offsetOf(options)
230
247
  const target = UndefinedOr.match(options.target as Element | undefined, { onUndefined: () => element, onDefined: (given) => given })
231
- // A target's progress, or an offset's through the container itself; else the plain one.
232
- const resolves = Bool.or(Predicate.isNotUndefined(options.target), Predicate.isNotUndefined(options.offset))
233
248
  const onAxis = (current: number): number => {
234
249
  const own = target === element
235
250
  const inset = Bool.match(own, { onTrue: (): Point => ({ x: 0, y: 0 }), onFalse: () => insetOf(target, element, 0, 0) })
@@ -238,20 +253,22 @@ const measureOf = (element: Element, options: ScrollSource.Options): (() => Scro
238
253
  onFalse: () => sizeOf(target),
239
254
  })
240
255
  const y = axis === "y"
241
- const containerLength = Bool.match(y, { onTrue: () => element.clientHeight, onFalse: () => element.clientWidth })
256
+ const containerLength = clientLengthOf(element, axis)
242
257
  const targetLength = Bool.match(y, { onTrue: () => size.height, onFalse: () => size.width })
243
258
  const targetInset = Bool.match(y, { onTrue: () => inset.y, onFalse: () => inset.x })
244
259
  return progressThrough(Arr.map(offset, (stop) => positionOf(stop, containerLength, targetLength, targetInset)), current)
245
260
  }
261
+ // A target's progress, or an offset's through the container itself, on its axis; else the plain one.
262
+ const onX = resolvesOn(options, "x")
263
+ const onY = resolvesOn(options, "y")
246
264
  return () => {
247
265
  const x = Math.abs(element.scrollLeft)
248
266
  const y = Math.abs(element.scrollTop)
249
267
  const xLength = element.scrollWidth - element.clientWidth
250
268
  const yLength = element.scrollHeight - element.clientHeight
251
- const resolvesOn = (on: "x" | "y"): boolean => Bool.and(resolves, axis === on)
252
269
  return {
253
- x: { current: x, length: xLength, progress: Bool.match(resolvesOn("x"), { onTrue: () => onAxis(x), onFalse: () => plainProgress(x, xLength) }) },
254
- y: { current: y, length: yLength, progress: Bool.match(resolvesOn("y"), { onTrue: () => onAxis(y), onFalse: () => plainProgress(y, yLength) }) },
270
+ x: { current: x, length: xLength, progress: Bool.match(onX, { onTrue: () => onAxis(x), onFalse: () => plainProgress(x, xLength) }) },
271
+ y: { current: y, length: yLength, progress: Bool.match(onY, { onTrue: () => onAxis(y), onFalse: () => plainProgress(y, yLength) }) },
255
272
  }
256
273
  }
257
274
  }
@@ -271,7 +288,7 @@ const sameScroll = (a: ScrollSource.Scroll, b: ScrollSource.Scroll): boolean =>
271
288
  )
272
289
 
273
290
  /** The page's scroller: `document.scrollingElement`, else the root element. */
274
- const pageOf = (): Element => Option.getOrElse(Option.fromNullOr(document.scrollingElement), () => document.documentElement)
291
+ export const pageOf = (): Element => Option.getOrElse(Option.fromNullOr(document.scrollingElement), () => document.documentElement)
275
292
 
276
293
  /** A container's listener set, opened for its first reader. */
277
294
  const open = (element: Element): Container => {