@wardic/dom 1.1.4 → 1.3.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 (33) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.js +1 -1
  3. package/dist/internal/default-transition.d.ts +1 -1
  4. package/dist/internal/default-transition.d.ts.map +1 -1
  5. package/dist/internal/default-transition.js +4 -20
  6. package/dist/internal/default-transition.js.map +1 -1
  7. package/dist/internal/renderer.d.ts.map +1 -1
  8. package/dist/internal/renderer.js +3 -0
  9. package/dist/internal/renderer.js.map +1 -1
  10. package/dist/unstable/BatchMembers.d.ts +2 -2
  11. package/dist/unstable/BatchMembers.d.ts.map +1 -1
  12. package/dist/unstable/BatchMembers.js +32 -271
  13. package/dist/unstable/BatchMembers.js.map +1 -1
  14. package/dist/unstable/Imperative.d.ts +13 -136
  15. package/dist/unstable/Imperative.d.ts.map +1 -1
  16. package/dist/unstable/Imperative.js +59 -721
  17. package/dist/unstable/Imperative.js.map +1 -1
  18. package/dist/unstable/PointerValues.d.ts +34 -0
  19. package/dist/unstable/PointerValues.d.ts.map +1 -0
  20. package/dist/unstable/PointerValues.js +81 -0
  21. package/dist/unstable/PointerValues.js.map +1 -0
  22. package/dist/unstable/PresenceController.d.ts +8 -0
  23. package/dist/unstable/PresenceController.d.ts.map +1 -1
  24. package/dist/unstable/PresenceController.js +34 -13
  25. package/dist/unstable/PresenceController.js.map +1 -1
  26. package/package.json +3 -3
  27. package/src/index.ts +1 -1
  28. package/src/internal/default-transition.ts +5 -27
  29. package/src/internal/renderer.ts +3 -0
  30. package/src/unstable/BatchMembers.ts +35 -270
  31. package/src/unstable/Imperative.ts +69 -917
  32. package/src/unstable/PointerValues.ts +115 -0
  33. package/src/unstable/PresenceController.ts +42 -11
@@ -0,0 +1,115 @@
1
+ /**
2
+ * The pointer over an element as **Live values**, for every DOM binding: where it is in the element's
3
+ * pixels and how far across the element's box, the box's size, and whether it's inside, each an Atom
4
+ * a binding's element binds, set within the pointer's own events and read until the Scope ends.
5
+ * Plain pointer events, not the hover gesture: a finger counts only when asked (it never hovers),
6
+ * and a drag suppresses nothing. Foldkit's `Wardic.pointer` reads it on the Handle's registry.
7
+ *
8
+ * Unstable, for Wardic's own packages: public, with no semver promise.
9
+ *
10
+ * @module
11
+ */
12
+ // Unstable: ADR-0071, decision 5. #833: the storefront's spotlight and magnifier each listened to the pointer
13
+ // and did the arithmetic themselves, where the Solid ward's `when("hover")` is its own per-move listener; core's
14
+ // recognizer knows hover's edges alone, and a per-move Message on Foldkit would render per move (decision 23).
15
+ import * as Arr from "effect/Array"
16
+ import * as Bool from "effect/Boolean"
17
+ import * as Effect from "effect/Effect"
18
+ import { identity } from "effect/Function"
19
+ import * as Num from "effect/Number"
20
+ import * as Atom from "effect/reactivity/Atom"
21
+ import * as AtomRegistry from "effect/reactivity/AtomRegistry"
22
+ import type * as Scope from "effect/Scope"
23
+ import * as UndefinedOr from "effect/UndefinedOr"
24
+
25
+ /** A pointer's type, as its events name it. */
26
+ export type PointerType = "mouse" | "pen" | "touch"
27
+
28
+ /** What `pointer` takes: the pointer types that count, a mouse and a pen unless given, since a finger never hovers. */
29
+ export interface Options {
30
+ readonly types?: ReadonlyArray<PointerType> | undefined
31
+ }
32
+
33
+ /**
34
+ * The pointer over an element: its place in the element's pixels, from the box's top left; its
35
+ * progress across the box, from 0 at the left or top edge to 1 at the right or bottom; the box's
36
+ * size, as last measured at a pointer event; and whether it's inside: on since the pointer came in
37
+ * or a finger pressed, off at its leave. A leave keeps the place: what returns to rest with it is a
38
+ * derived Atom over `inside`. A finger's capture keeps `inside` on while it moves off the box, until
39
+ * it lifts, and the place then runs past the box while the progress stays clamped.
40
+ */
41
+ export interface PointerValues {
42
+ readonly x: Atom.Atom<number>
43
+ readonly y: Atom.Atom<number>
44
+ readonly xProgress: Atom.Atom<number>
45
+ readonly yProgress: Atom.Atom<number>
46
+ readonly width: Atom.Atom<number>
47
+ readonly height: Atom.Atom<number>
48
+ readonly inside: Atom.Atom<boolean>
49
+ }
50
+
51
+ const MOUSE_AND_PEN: ReadonlyArray<PointerType> = ["mouse", "pen"]
52
+
53
+ /** The events that place the pointer: its entry, each move, and a press, which is how a finger arrives. */
54
+ const PLACING: ReadonlyArray<string> = ["pointerenter", "pointermove", "pointerdown"]
55
+
56
+ const UNIT = { minimum: 0, maximum: 1 } as const
57
+
58
+ /** `offset` as progress across `length`: 0 for a box with none. */
59
+ const progressOf = (offset: number, length: number): number => {
60
+ if (length <= 0) return 0
61
+ return Num.clamp(offset / length, UNIT)
62
+ }
63
+
64
+ /**
65
+ * The pointer over `element` as {@link PointerValues}, on the Environment's `AtomRegistry`, until the
66
+ * Scope ends: its Atoms are mounted on the registry while read, so a set holds before anything binds
67
+ * them, and its listeners are the element's own.
68
+ */
69
+ export const pointer: (element: Element, options?: Options) => Effect.Effect<PointerValues, never, AtomRegistry.AtomRegistry | Scope.Scope> = Effect.fnUntraced(
70
+ function* (element: Element, options: Options = {}) {
71
+ const registry = yield* AtomRegistry.AtomRegistry
72
+ const types = UndefinedOr.match(options.types, { onUndefined: () => MOUSE_AND_PEN, onDefined: identity })
73
+ const x = Atom.make(0)
74
+ const y = Atom.make(0)
75
+ const xProgress = Atom.make(0)
76
+ const yProgress = Atom.make(0)
77
+ const width = Atom.make(0)
78
+ const height = Atom.make(0)
79
+ const inside = Atom.make(false)
80
+ // Mounted while read, not kept alive, as the scroll reader's Atom is: a kept-alive Atom's node never leaves the registry.
81
+ const unmounts = [registry.mount(x), registry.mount(y), registry.mount(xProgress), registry.mount(yProgress), registry.mount(width), registry.mount(height), registry.mount(inside)]
82
+ const counts = (event: Event): boolean => Arr.contains(types, (event as PointerEvent).pointerType as PointerType)
83
+ const place = (event: Event): void => {
84
+ if (Bool.not(counts(event))) return
85
+ const { clientX, clientY } = event as PointerEvent
86
+ const box = element.getBoundingClientRect()
87
+ const left = clientX - box.left
88
+ const top = clientY - box.top
89
+ registry.set(x, left)
90
+ registry.set(y, top)
91
+ registry.set(xProgress, progressOf(left, box.width))
92
+ registry.set(yProgress, progressOf(top, box.height))
93
+ registry.set(width, box.width)
94
+ registry.set(height, box.height)
95
+ registry.set(inside, true)
96
+ }
97
+ const leave = (event: Event): void => {
98
+ if (Bool.not(counts(event))) return
99
+ registry.set(inside, false)
100
+ }
101
+ yield* Effect.acquireRelease(
102
+ Effect.sync(() => {
103
+ Arr.forEach(PLACING, (type) => element.addEventListener(type, place))
104
+ element.addEventListener("pointerleave", leave)
105
+ }),
106
+ () =>
107
+ Effect.sync(() => {
108
+ Arr.forEach(PLACING, (type) => element.removeEventListener(type, place))
109
+ element.removeEventListener("pointerleave", leave)
110
+ Arr.forEach(unmounts, (unmount) => unmount())
111
+ }),
112
+ )
113
+ return { x, y, xProgress, yProgress, width, height, inside }
114
+ },
115
+ )
@@ -185,18 +185,18 @@ const exitRunOf = (
185
185
  const key = leg.key
186
186
  return Effect.suspend(() =>
187
187
  leg.machine().pipe(
188
- Machine.run((frame) => {
188
+ Machine.run((frame, dt) => {
189
189
  velocities[key] = UndefinedOr.match(frame.velocity, { onUndefined: () => 0, onDefined: identity })
190
- return commitFrame({ ...frame, value: { [key]: frame.value } })
190
+ return commitFrame({ ...frame, value: { [key]: frame.value } }, dt)
191
191
  }),
192
192
  ))
193
193
  }
194
194
  if (leg._tag === "Numbers") {
195
195
  return Effect.suspend(() =>
196
196
  leg.machine().pipe(
197
- Machine.run((frame) => {
197
+ Machine.run((frame, dt) => {
198
198
  velocitiesInto(velocities, frame)
199
- return commitFrame(frame)
199
+ return commitFrame(frame, dt)
200
200
  }),
201
201
  ))
202
202
  }
@@ -359,14 +359,23 @@ const pxOf = (value: string): number => {
359
359
  return px
360
360
  }
361
361
 
362
+ /** Whether `el` is popped at all: an HTML element, or an outermost `<svg>` (#744, beyond Motion's). An SVG element inside an `<svg>` runs its exit in place (#702), as Motion's. */
363
+ const isPopped = (el: Element): el is HTMLElement | SVGSVGElement => Bool.or(isHTMLElement(el), isSvgRoot(el))
364
+
365
+ /** `el`'s gaps now, as its kind reads them (below): `undefined` for an element that isn't popped. */
366
+ const gapsNowOf = (el: Element): EdgeGaps | undefined => {
367
+ if (isHTMLElement(el)) return htmlGapsOf(el)
368
+ if (isSvgRoot(el)) return svgGapsOf(el)
369
+ return undefined
370
+ }
371
+
362
372
  /**
363
- * Where `el` is now, to pop it (#277): `undefined` for one that isn't popped: an SVG element inside an
364
- * `<svg>`, which runs its exit in place (#702), as Motion's, and one with no size of its own. An outermost
365
- * `<svg>` is popped (#744), beyond Motion's.
373
+ * Where `el` is now, to pop it (#277): `undefined` for one that isn't popped, or with no size of its own.
374
+ * With `ahead`, its gaps as a binding's snapshot measured them before its framework's change (#840), the
375
+ * box is at those, not at the gaps the change has left it with.
366
376
  */
367
- const popBoxOf = (el: Element, anchors: PopAnchors): PopBox | undefined => {
368
- if (isHTMLElement(el)) return pinBoxOf(el, anchors, htmlGapsOf)
369
- if (isSvgRoot(el)) return pinBoxOf(el, anchors, svgGapsOf)
377
+ const popBoxOf = (el: Element, anchors: PopAnchors, ahead: EdgeGaps | undefined): PopBox | undefined => {
378
+ if (isPopped(el)) return pinBoxOf(el, anchors, (measured) => UndefinedOr.match(ahead, { onUndefined: () => gapsNowOf(measured), onDefined: identity }))
370
379
  return undefined
371
380
  }
372
381
 
@@ -649,6 +658,14 @@ export interface PresenceController {
649
658
  * the change is applied; {@link present} is then told what it showed.
650
659
  */
651
660
  readonly shown: (resolved: unknown) => unknown
661
+ /**
662
+ * `children` measured where they are now, for the next change's leavers: {@link shown} pins a
663
+ * `popLayout` leaver among them at the box measured here, not where the change has moved it by then,
664
+ * and spends the snapshot, whatever its mode. For a binding whose change reaches the controller after
665
+ * its framework has applied it; one that resolves a change first needs none. Measuring an outermost
666
+ * `<svg>` child puts an element in and out beside it, which an observer of the children sees.
667
+ */
668
+ readonly snapshot: (children: ReadonlyArray<Element>) => void
652
669
  /**
653
670
  * `propagate` (#277's PR 5c): every child present leaves, as if the change took them all, and
654
671
  * `done` is called once none is left (at once with none).
@@ -775,6 +792,9 @@ export const makePresence = (options: PresenceOptions): PresenceController => {
775
792
  // and kept until it's gone. Whether this change popped one, so the projection is told.
776
793
  const pops = new Map<Element, PopBox>()
777
794
  let popped = false
795
+ // A binding's snapshot of its children's gaps from before its framework's change (#840): what this
796
+ // change's leavers are pinned at, where the change has moved them by the time the binding sees it.
797
+ const ahead = new Map<Element, EdgeGaps>()
778
798
  // Whether this change moved a leaver from an earlier one to its place (#675), so the projection is
779
799
  // told: a twin leaver crossfading in the flow is drawn on its Lead from where it now is.
780
800
  let moved = false
@@ -1270,10 +1290,18 @@ export const makePresence = (options: PresenceOptions): PresenceController => {
1270
1290
  Arr.forEach(previous, (placed) => {
1271
1291
  if (kept.has(placed.el)) return
1272
1292
  if (Bool.not(retains(placed.el))) return
1273
- UndefinedOr.map(popBoxOf(placed.el, anchors), (box) => pops.set(placed.el, box))
1293
+ // At the snapshot's gaps when a binding took one (#840): a `bottom` or `right` gap is from the containing block's far edge as it was then, as Motion's `getSnapshotBeforeUpdate`'s.
1294
+ UndefinedOr.map(popBoxOf(placed.el, anchors, ahead.get(placed.el)), (box) => pops.set(placed.el, box))
1274
1295
  })
1275
1296
  }
1276
1297
 
1298
+ /** `children` measured where they are now, for the next change's leavers ({@link PresenceController.snapshot}). */
1299
+ const snapshot = (children: ReadonlyArray<Element>): void => {
1300
+ // #840: Foldkit's binding, whose delivery comes a microtask after the patch; Solid's resolves a change before the DOM moves and never calls it.
1301
+ ahead.clear()
1302
+ Arr.forEach(children, (el) => UndefinedOr.map(gapsNowOf(el), (gaps) => ahead.set(el, gaps)))
1303
+ }
1304
+
1277
1305
  /** Put a retained child back where it was, if a framework took it out. */
1278
1306
  const putBack = (placed: Placed): void => {
1279
1307
  if (placed.el.parentNode !== null) return
@@ -1668,6 +1696,8 @@ export const makePresence = (options: PresenceOptions): PresenceController => {
1668
1696
  // `popLayout` (#277): each child leaving in this change is measured where it is, before the change
1669
1697
  // moves it, as Motion's `getSnapshotBeforeUpdate`.
1670
1698
  if (mode === "popLayout") measureLeavers(resolved)
1699
+ // A binding's snapshot is one change's (#840): spent here, measured or not.
1700
+ ahead.clear()
1671
1701
  // A child back under the key of one leaving takes over from it (#286): found before the mode
1672
1702
  // decides what goes on the page, so `wait` doesn't hold it. Nothing is looked up with no key.
1673
1703
  if (keyed) findTakeovers(resolved)
@@ -1694,6 +1724,7 @@ export const makePresence = (options: PresenceOptions): PresenceController => {
1694
1724
  return {
1695
1725
  context: ctx,
1696
1726
  shown,
1727
+ snapshot,
1697
1728
  leaveAll,
1698
1729
  anchor: () => anchor,
1699
1730
  mounted: () => {