@wardic/dom 1.1.2 → 1.1.4

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 (74) hide show
  1. package/dist/ReorderController.d.ts +12 -1
  2. package/dist/ReorderController.d.ts.map +1 -1
  3. package/dist/ReorderController.js +25 -2
  4. package/dist/ReorderController.js.map +1 -1
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.js +1 -1
  7. package/dist/internal/appear.d.ts +7 -2
  8. package/dist/internal/appear.d.ts.map +1 -1
  9. package/dist/internal/appear.js +2 -2
  10. package/dist/internal/appear.js.map +1 -1
  11. package/dist/internal/clock.d.ts +3 -0
  12. package/dist/internal/clock.d.ts.map +1 -1
  13. package/dist/internal/clock.js +3 -0
  14. package/dist/internal/clock.js.map +1 -1
  15. package/dist/internal/reorder.d.ts +3 -0
  16. package/dist/internal/reorder.d.ts.map +1 -1
  17. package/dist/internal/reorder.js +1 -1
  18. package/dist/internal/reorder.js.map +1 -1
  19. package/dist/unstable/AppearAnimations.d.ts +14 -1
  20. package/dist/unstable/AppearAnimations.d.ts.map +1 -1
  21. package/dist/unstable/AppearAnimations.js +16 -3
  22. package/dist/unstable/AppearAnimations.js.map +1 -1
  23. package/dist/unstable/DragDefaults.d.ts +6 -5
  24. package/dist/unstable/DragDefaults.d.ts.map +1 -1
  25. package/dist/unstable/DragDefaults.js +2 -6
  26. package/dist/unstable/DragDefaults.js.map +1 -1
  27. package/dist/unstable/DragTrack.d.ts +8 -0
  28. package/dist/unstable/DragTrack.d.ts.map +1 -1
  29. package/dist/unstable/DragTrack.js +17 -0
  30. package/dist/unstable/DragTrack.js.map +1 -1
  31. package/dist/unstable/ElementSetup.d.ts +8 -2
  32. package/dist/unstable/ElementSetup.d.ts.map +1 -1
  33. package/dist/unstable/ElementSetup.js +14 -2
  34. package/dist/unstable/ElementSetup.js.map +1 -1
  35. package/dist/unstable/Feature.d.ts +5 -4
  36. package/dist/unstable/Feature.d.ts.map +1 -1
  37. package/dist/unstable/Feature.js +7 -4
  38. package/dist/unstable/Feature.js.map +1 -1
  39. package/dist/unstable/GestureFeatures.d.ts +132 -3
  40. package/dist/unstable/GestureFeatures.d.ts.map +1 -1
  41. package/dist/unstable/GestureFeatures.js +189 -11
  42. package/dist/unstable/GestureFeatures.js.map +1 -1
  43. package/dist/unstable/Imperative.d.ts +11 -0
  44. package/dist/unstable/Imperative.d.ts.map +1 -1
  45. package/dist/unstable/Imperative.js +37 -7
  46. package/dist/unstable/Imperative.js.map +1 -1
  47. package/dist/unstable/InstanceFeatures.d.ts +7 -0
  48. package/dist/unstable/InstanceFeatures.d.ts.map +1 -0
  49. package/dist/unstable/InstanceFeatures.js +69 -0
  50. package/dist/unstable/InstanceFeatures.js.map +1 -0
  51. package/dist/unstable/PagePoint.d.ts +43 -0
  52. package/dist/unstable/PagePoint.d.ts.map +1 -0
  53. package/dist/unstable/PagePoint.js +119 -0
  54. package/dist/unstable/PagePoint.js.map +1 -0
  55. package/dist/unstable/ReorderInput.d.ts +45 -0
  56. package/dist/unstable/ReorderInput.d.ts.map +1 -0
  57. package/dist/unstable/ReorderInput.js +116 -0
  58. package/dist/unstable/ReorderInput.js.map +1 -0
  59. package/package.json +3 -3
  60. package/src/ReorderController.ts +34 -1
  61. package/src/index.ts +1 -1
  62. package/src/internal/appear.ts +9 -4
  63. package/src/internal/clock.ts +3 -0
  64. package/src/internal/reorder.ts +1 -1
  65. package/src/unstable/AppearAnimations.ts +21 -3
  66. package/src/unstable/DragDefaults.ts +9 -9
  67. package/src/unstable/DragTrack.ts +21 -0
  68. package/src/unstable/ElementSetup.ts +14 -1
  69. package/src/unstable/Feature.ts +9 -5
  70. package/src/unstable/GestureFeatures.ts +297 -3
  71. package/src/unstable/Imperative.ts +45 -13
  72. package/src/unstable/InstanceFeatures.ts +82 -0
  73. package/src/unstable/PagePoint.ts +129 -0
  74. package/src/unstable/ReorderInput.ts +143 -0
@@ -1,17 +1,27 @@
1
1
  /**
2
2
  * What an element reads of its section's gesture Features (#511, ADR-0067 decision 3): each gesture's
3
- * service, `undefined` without its Feature, the gestures' input a section with one has, and the words
4
- * development's message on a missing one names. Every binding's; it reaches no Feature's code, only
5
- * their keys.
3
+ * service, `undefined` without its Feature, the gestures' input a section with one has, the words
4
+ * development's message on a missing one names, and which gestures a config wants (Motion's feature
5
+ * definitions). Every binding's; it reaches no Feature's code, only their keys.
6
6
  *
7
7
  * Unstable, for Wardic's own packages: public, with no semver promise.
8
8
  *
9
9
  * @module
10
10
  */
11
11
  // Unstable: ADR-0071, decision 5. #672: solid's `gesture-features.ts`, moved to dom with the Features.
12
+ import type * as Drag from "@wardic/core/Drag"
13
+ import type * as DragRecognizer from "@wardic/core/DragRecognizer"
12
14
  import * as InputSource from "@wardic/core/InputSource"
15
+ import type * as Recognizer from "@wardic/core/Recognizer"
16
+ import * as Bool from "effect/Boolean"
13
17
  import * as Context from "effect/Context"
18
+ import * as Match from "effect/Match"
14
19
  import * as Option from "effect/Option"
20
+ import * as Predicate from "effect/Predicate"
21
+ import { constUndefined, identity } from "effect/Function"
22
+ import * as MutableRef from "effect/MutableRef"
23
+ import * as UndefinedOr from "effect/UndefinedOr"
24
+ import * as GestureInfo from "./GestureInfo.ts"
15
25
 
16
26
  /** `key`'s service among `services`, or `undefined` when they hold none: its Feature isn't there. */
17
27
  export const serviceIn = <I, S>(services: Context.Context<never>, key: Context.Key<I, S>): S | undefined =>
@@ -36,3 +46,287 @@ export const hasService = <I, S>(services: Context.Context<never>, key: Context.
36
46
 
37
47
  /** `key`'s service among `services`, which hold it: a Feature a hook or component brought. */
38
48
  export const serviceOf = <I, S>(services: Context.Context<never>, key: Context.Key<I, S>): S => Context.get(services as Context.Context<I>, key)
49
+
50
+ /**
51
+ * The keys a gesture is wanted by (Motion's `features/definitions.ts`): every binding's config has
52
+ * them, its callbacks in its own shape (Solid 1's functions, Foldkit's Messages), so only their presence
53
+ * is read.
54
+ */
55
+ export interface GestureWants {
56
+ readonly whileHover?: unknown
57
+ readonly onHoverStart?: unknown
58
+ readonly onHoverEnd?: unknown
59
+ readonly whileTap?: unknown
60
+ readonly onTap?: unknown
61
+ readonly onTapStart?: unknown
62
+ readonly onTapCancel?: unknown
63
+ readonly onPan?: unknown
64
+ readonly onPanStart?: unknown
65
+ readonly onPanSessionStart?: unknown
66
+ readonly onPanEnd?: unknown
67
+ readonly whileInView?: unknown
68
+ readonly onViewportEnter?: unknown
69
+ readonly onViewportLeave?: unknown
70
+ readonly whileFocus?: unknown
71
+ }
72
+
73
+ /**
74
+ * Every key the `wants*` below read (#525): an element given none of them, its keys fixed, can never
75
+ * want a gesture, so it starts no computation to watch for one. Solid's `gesture-keys.ssr.test.tsx`
76
+ * checks each of them turns its gesture on.
77
+ */
78
+ // #793 (PR 1's review): solid's `gesture-callbacks.ts`'s, moved to dom for every binding's gesture start.
79
+ export const GESTURE_KEYS = [
80
+ "whileHover",
81
+ "onHoverStart",
82
+ "onHoverEnd",
83
+ "whileTap",
84
+ "onTap",
85
+ "onTapStart",
86
+ "onTapCancel",
87
+ "onPan",
88
+ "onPanStart",
89
+ "onPanSessionStart",
90
+ "onPanEnd",
91
+ "whileInView",
92
+ "onViewportEnter",
93
+ "onViewportLeave",
94
+ "whileFocus",
95
+ ] as const satisfies ReadonlyArray<keyof GestureWants>
96
+
97
+ /**
98
+ * Whether `config` runs the hover recognizer (Motion's hover feature). Read at every config change of
99
+ * an element that doesn't run it yet: statement `if`s, no array (ADR-0025).
100
+ */
101
+ export const wantsHover = (config: GestureWants): boolean => {
102
+ if (Predicate.isNotUndefined(config.whileHover)) return true
103
+ if (Predicate.isNotUndefined(config.onHoverStart)) return true
104
+ return Predicate.isNotUndefined(config.onHoverEnd)
105
+ }
106
+
107
+ /** Whether `config` runs the press recognizer (Motion's tap feature): as {@link wantsHover}. */
108
+ export const wantsPress = (config: GestureWants): boolean => {
109
+ if (Predicate.isNotUndefined(config.whileTap)) return true
110
+ if (Predicate.isNotUndefined(config.onTap)) return true
111
+ if (Predicate.isNotUndefined(config.onTapStart)) return true
112
+ return Predicate.isNotUndefined(config.onTapCancel)
113
+ }
114
+
115
+ /** Whether `config` runs the pan recognizer (Motion's pan feature, #273): as {@link wantsHover}. */
116
+ export const wantsPan = (config: GestureWants): boolean => {
117
+ if (Predicate.isNotUndefined(config.onPan)) return true
118
+ if (Predicate.isNotUndefined(config.onPanStart)) return true
119
+ if (Predicate.isNotUndefined(config.onPanSessionStart)) return true
120
+ return Predicate.isNotUndefined(config.onPanEnd)
121
+ }
122
+
123
+ /** Whether `config` runs the in-view recognizer (Motion's inView feature, #273): `whileInView` or a viewport callback. */
124
+ export const wantsInView = (config: GestureWants): boolean => {
125
+ if (Predicate.isNotUndefined(config.whileInView)) return true
126
+ if (Predicate.isNotUndefined(config.onViewportEnter)) return true
127
+ return Predicate.isNotUndefined(config.onViewportLeave)
128
+ }
129
+
130
+ /** Whether `config` runs the focus recognizer (Motion's focus feature, #273): `whileFocus`. */
131
+ export const wantsFocus = (config: GestureWants): boolean => Predicate.isNotUndefined(config.whileFocus)
132
+
133
+ /** Each press phase's callback (Motion's `onTapStart`, `onTap`, `onTapCancel`): a press a drag took over has none, as Motion tells neither. */
134
+ export const PRESS_CALLBACK = { start: "onTapStart", tap: "onTap", cancel: "onTapCancel", dragged: undefined } as const
135
+
136
+ /** Each drag phase's callback (Motion's `onDragStart`, `onDragEnd`), `onDrag` aside, which hears the start and each move. */
137
+ // #793 (PR 2): solid's `gesture-callbacks.ts`'s, moved to dom for every binding's drag callbacks.
138
+ export const DRAG_CALLBACK = { start: "onDragStart", move: undefined, end: "onDragEnd" } as const
139
+
140
+ /** Each pan phase's callback past its session's start (Motion's `onPanStart`, `onPanEnd`), `onPan` aside. */
141
+ export const PAN_CALLBACK = { start: "onPanStart", move: undefined, end: "onPanEnd" } as const
142
+
143
+ /**
144
+ * A config's gesture callbacks (Motion's `onHoverStart`, `onTap`, `onDragEnd` and the rest), each in the
145
+ * binding's own shape (Solid 1's functions, Foldkit's Messages): read by name at the event, their
146
+ * presence alone typed here; and `dragDirectionLock`, which says whether a drag's start tells its lock.
147
+ */
148
+ export interface GestureCallbacks {
149
+ readonly onHoverStart?: unknown
150
+ readonly onHoverEnd?: unknown
151
+ readonly onTapStart?: unknown
152
+ readonly onTap?: unknown
153
+ readonly onTapCancel?: unknown
154
+ readonly onDragStart?: unknown
155
+ readonly onDrag?: unknown
156
+ readonly onDragEnd?: unknown
157
+ readonly onDragTransitionEnd?: unknown
158
+ readonly onDirectionLock?: unknown
159
+ readonly onPanSessionStart?: unknown
160
+ readonly onPanStart?: unknown
161
+ readonly onPan?: unknown
162
+ readonly onPanEnd?: unknown
163
+ readonly onViewportEnter?: unknown
164
+ readonly onViewportLeave?: unknown
165
+ readonly dragDirectionLock?: boolean | undefined
166
+ }
167
+
168
+ /** A gesture callback's name, as a report says it. */
169
+ export type GestureCallbackName = Exclude<keyof GestureCallbacks, "dragDirectionLock">
170
+
171
+ /** What a binding tells its element's gesture callbacks with. */
172
+ export interface Telling {
173
+ /** The element's config now, read at each event. */
174
+ readonly config: () => GestureCallbacks
175
+ /** Run `handler`, a callback of the config's, with the event's `args`: Solid 1 calls it, Foldkit sends its Message. */
176
+ readonly invoke: (handler: unknown, args: ReadonlyArray<unknown>) => void
177
+ /** Report that the callback `name` threw `defect`. */
178
+ readonly report: (name: GestureCallbackName, defect: unknown) => void
179
+ }
180
+
181
+ /**
182
+ * What tells an element's gestures to its config's callbacks, for every binding (#273, ADR-0047):
183
+ * Motion's `(event, info)`, the platform's own event carried through (`raw`), then the info
184
+ * (`GestureInfo`), in Motion's order: a locked drag's `onDirectionLock` before its `onDragStart`, a
185
+ * pan's `onPanSessionStart` at the down with the point alone, `onDrag` and `onPan` at the start and
186
+ * each move after. A drag its controls cancelled ends untold (#431). A callback that throws is reported
187
+ * by name and the gesture goes on; `onDrag`'s and `onPan`'s run of throws at its first (#257 item 4).
188
+ */
189
+ export interface GestureTeller {
190
+ readonly hovered: (edge: Recognizer.Edge) => void
191
+ readonly pressed: (press: Recognizer.PressEvent, element: Element) => void
192
+ readonly dragged: (sample: Drag.Sample) => void
193
+ /** The drag's release came to rest on its own. */
194
+ readonly released: () => void
195
+ readonly panned: (sample: DragRecognizer.PanSample) => void
196
+ /** An in-view edge: `onViewportEnter` or `onViewportLeave`, with the platform's entry. */
197
+ readonly viewed: (edge: Recognizer.ViewEdge) => void
198
+ }
199
+
200
+ /** A drag's or a pan's start or end callback. */
201
+ type MovementCallback = "onDragStart" | "onDragEnd" | "onPanStart" | "onPanEnd"
202
+
203
+ /** A {@link GestureTeller} over `telling`'s config, invoking and reporting as the binding says. */
204
+ // #793 (PR 2): solid's `gestureCallbacksOf`, moved to dom for both bindings: each gives how a handler is invoked.
205
+ export const gestureTellerOf = ({ config, invoke, report }: Telling): GestureTeller => {
206
+ const guard = callbackGuard(report)
207
+ /** Tell the config's `name`, if it has one, `args`: reported if it throws. */
208
+ const tell = (name: GestureCallbackName, ...args: ReadonlyArray<unknown>): void => {
209
+ const handler = config()[name]
210
+ if (Predicate.isUndefined(handler)) return
211
+ guard.once(name, () => invoke(handler, args))
212
+ }
213
+ /**
214
+ * A drag's or a pan's sample, past a pan's session's start: its start's or its end's callback
215
+ * (`named`), then `every`, which hears its start and each move (Motion's pan session runs `onMove` at
216
+ * the start too). The info is built once, for whichever is set.
217
+ */
218
+ const moved = (named: MovementCallback | undefined, every: "onDrag" | "onPan", sample: Drag.Sample): void => {
219
+ const c = config()
220
+ const own = UndefinedOr.match(named, { onUndefined: constUndefined, onDefined: (key) => c[key] })
221
+ const each = Bool.match(sample.phase !== "end", { onTrue: () => c[every], onFalse: constUndefined })
222
+ if (Bool.and(Predicate.isUndefined(own), Predicate.isUndefined(each))) return
223
+ const raw = sample.raw as PointerEvent
224
+ const info = GestureInfo.dragInfoOf(sample)
225
+ UndefinedOr.map(named, (key) => UndefinedOr.map(own, (handler) => guard.once(key, () => invoke(handler, [raw, info]))))
226
+ UndefinedOr.map(each, (handler) => guard.perMove(every, () => invoke(handler, [raw, info])))
227
+ }
228
+ return {
229
+ hovered: (edge) => {
230
+ const name = Bool.match(edge.active, { onTrue: () => "onHoverStart" as const, onFalse: () => "onHoverEnd" as const })
231
+ tell(name, edge.event.raw as PointerEvent, GestureInfo.pointInfoOf(edge.event))
232
+ },
233
+ pressed: (press, element) => {
234
+ const name = PRESS_CALLBACK[press.phase]
235
+ if (Predicate.isUndefined(name)) return
236
+ tell(name, press.event.raw as GestureInfo.WardicPressEvent, GestureInfo.pressInfoOf(press, element))
237
+ },
238
+ dragged: (sample) => {
239
+ // A drag its controls cancelled (#431) ends untold, as Motion's `cancel` tells no `onDragEnd`.
240
+ if (sample.cancelled === true) return
241
+ // A direction-locked drag's lock, at its start, before `onDragStart` (Motion's order).
242
+ if (Bool.and(sample.phase === "start", config().dragDirectionLock === true)) tell("onDirectionLock", sample.axis as "x" | "y")
243
+ moved(DRAG_CALLBACK[sample.phase], "onDrag", sample)
244
+ },
245
+ released: () => tell("onDragTransitionEnd"),
246
+ panned: (sample) => {
247
+ // Its session's start, at the down: Motion's `onPanSessionStart`, with the point only.
248
+ if (sample.phase === "session") {
249
+ tell("onPanSessionStart", sample.raw as PointerEvent, GestureInfo.pointOfSample(sample))
250
+ return
251
+ }
252
+ moved(PAN_CALLBACK[sample.phase], "onPan", sample as Drag.Sample)
253
+ },
254
+ viewed: (edge) => {
255
+ const name = Bool.match(edge.active, { onTrue: () => "onViewportEnter" as const, onFalse: () => "onViewportLeave" as const })
256
+ tell(name, edge.raw as IntersectionObserverEntry)
257
+ },
258
+ }
259
+ }
260
+
261
+ /**
262
+ * A guard around an element's gesture callbacks (#257 item 4): `once` runs `call` and reports what it
263
+ * throws; `perMove`, for `onDrag` and `onPan`, reports a run of throws at its first, so one that throws
264
+ * at every move logs once, and a call that returns ends the run. The gesture goes on either way.
265
+ */
266
+ interface CallbackGuard {
267
+ readonly once: (name: GestureCallbackName, call: () => void) => void
268
+ readonly perMove: (name: GestureCallbackName, call: () => void) => void
269
+ }
270
+
271
+ /** A {@link CallbackGuard} reporting through `report`, told the callback's name and what it threw. */
272
+ // #793 (PR 2): solid's `gesture-callbacks.ts`'s `call` and `callPerMove`, moved to dom with the teller.
273
+ const callbackGuard = (report: Telling["report"]): CallbackGuard => {
274
+ // The per-move callbacks whose last call threw, made at the first throw: most elements never see one.
275
+ const failing = MutableRef.make<Set<string> | undefined>(undefined)
276
+ const failingNow = (): Set<string> =>
277
+ UndefinedOr.match(MutableRef.get(failing), {
278
+ onUndefined: () => {
279
+ const made = new Set<string>()
280
+ MutableRef.set(failing, made)
281
+ return made
282
+ },
283
+ onDefined: identity,
284
+ })
285
+ return {
286
+ once: (name, call) => {
287
+ try {
288
+ call()
289
+ } catch (defect) {
290
+ report(name, defect)
291
+ }
292
+ },
293
+ perMove: (name, call) => {
294
+ try {
295
+ call()
296
+ UndefinedOr.map(MutableRef.get(failing), (names) => names.delete(name))
297
+ } catch (defect) {
298
+ const names = failingNow()
299
+ if (names.has(name)) return
300
+ names.add(name)
301
+ report(name, defect)
302
+ }
303
+ },
304
+ }
305
+ }
306
+
307
+ /** Motion's `amount` as a share of the element: `"some"` any of it, `"all"` all of it; a number as it is. Built once. */
308
+ export const amountOf: (amount: "some" | "all" | number | undefined) => number | undefined = Match.type<"some" | "all" | number | undefined>().pipe(
309
+ Match.when("some", () => 0),
310
+ Match.when("all", () => 1),
311
+ Match.orElse((share) => share),
312
+ )
313
+
314
+ /** Motion's `viewport` as a binding holds it: dom's `WardicViewport`, or Foldkit's with its root resolved into an accessor. */
315
+ export interface ViewportLike {
316
+ readonly once?: boolean | undefined
317
+ readonly margin?: string | undefined
318
+ readonly amount?: "some" | "all" | number | undefined
319
+ readonly root?: (() => Element | undefined) | undefined
320
+ }
321
+
322
+ /** Motion's `viewport` as core's in-view options (#273): its root read now, when the recognizer starts. */
323
+ export const inViewOptionsOf = (viewport: ViewportLike | undefined): Recognizer.InViewOptions =>
324
+ UndefinedOr.match(viewport, {
325
+ onUndefined: () => ({}),
326
+ onDefined: (v) => ({
327
+ once: v.once,
328
+ margin: v.margin,
329
+ amount: amountOf(v.amount),
330
+ ...UndefinedOr.match(v.root, { onUndefined: () => ({}), onDefined: (root) => ({ root: root() }) }),
331
+ }),
332
+ })
@@ -761,8 +761,8 @@ export const serverAnimate: WardicAnimate = () => finishedControls()
761
761
 
762
762
  // --- Sequences (#433's PR 2) ------------------------------------------------------------------
763
763
 
764
- /** Whether `animate` was given a sequence (Motion's `isSequence`): a list with a Segment in it. */
765
- const isSequence = (given: unknown): given is WardicAnimationSequence =>
764
+ /** Whether `given` is a sequence rather than a subject (#433, Motion's `isSequence`): a list with a Segment, itself a list, in it; a list of elements has none. A binding whose `animate` is its own tells the forms apart by it (#798). */
765
+ export const isSequence = (given: unknown): given is WardicAnimationSequence =>
766
766
  Bool.match(Arr.isArray(given), { onTrue: () => Arr.some(given as ReadonlyArray<unknown>, Arr.isArray), onFalse: () => false })
767
767
 
768
768
  /** A function Segment's value (Motion's motion value): its own subject, its callback told each frame. */
@@ -905,6 +905,21 @@ const stepsOf = (sequence: WardicAnimationSequence, scope: Element | undefined,
905
905
  })
906
906
  }
907
907
 
908
+ /**
909
+ * Whether a Segment animates anything under `scope` (#798): a function Segment does, and a Batch's; a
910
+ * Segment of elements when its subject finds one, a selector inside `scope`, else the document; a
911
+ * label (a string, or `{ name, at }`) never. What a binding asks of a sequence before it plays it,
912
+ * since one that animates nothing lands at once.
913
+ */
914
+ export const segmentAnimates = (segment: WardicSegment, scope: Element | undefined): boolean => {
915
+ if (Predicate.isString(segment)) return false
916
+ if (Bool.not(Arr.isArray(segment))) return false
917
+ const parts = segment as ReadonlyArray<unknown>
918
+ if (Predicate.isNotUndefined(BatchMembers.batchSubjectOf(parts[0]))) return true
919
+ if (Predicate.isFunction(parts[0])) return true
920
+ return subjectsOf(parts[0] as WardicAnimationSubject, scope).length > 0
921
+ }
922
+
908
923
  /**
909
924
  * A function Segment's value animated (#433): its stops, a first `null` 0, through the
910
925
  * Interpolators on its tween over the sequence, each frame told to its callback. Cancelled, the
@@ -995,6 +1010,22 @@ export interface WardicAnimationScope<T extends Element = HTMLElement> {
995
1010
  readonly animations: ReadonlyArray<WardicPlaybackControls>
996
1011
  }
997
1012
 
1013
+ /** `watch`, with `also` told each start and end after it, when given: a scope's own watch beside its owner's (#433), a binding's beside a scope's (#798). */
1014
+ export const alsoTold = (watch: AnimationWatch, also: AnimationWatch | undefined): AnimationWatch =>
1015
+ UndefinedOr.match(also, {
1016
+ onUndefined: () => watch,
1017
+ onDefined: (more) => ({
1018
+ started: (controls) => {
1019
+ watch.started(controls)
1020
+ more.started(controls)
1021
+ },
1022
+ ended: (controls, landed) => {
1023
+ watch.ended(controls, landed)
1024
+ more.ended(controls, landed)
1025
+ },
1026
+ }),
1027
+ })
1028
+
998
1029
  /**
999
1030
  * An Animation scope's ref and what it hears of its animations (#433): each in `animations` while in
1000
1031
  * flight, and `also` told too (Solid 2's `settled()`). `stopAll` stops them, at its owner's cleanup.
@@ -1020,18 +1051,19 @@ export const scopeOf = <T extends Element>(also?: AnimationWatch): {
1020
1051
  current: { get: () => current },
1021
1052
  animations: { get: animationsNow },
1022
1053
  }) as WardicAnimationScope<T>
1023
- const watch: AnimationWatch = {
1024
- started: (controls) => {
1025
- live.add(controls)
1026
- listed = undefined
1027
- UndefinedOr.map(also, (more) => more.started(controls))
1054
+ const watch = alsoTold(
1055
+ {
1056
+ started: (controls) => {
1057
+ live.add(controls)
1058
+ listed = undefined
1059
+ },
1060
+ ended: (controls) => {
1061
+ live.delete(controls)
1062
+ listed = undefined
1063
+ },
1028
1064
  },
1029
- ended: (controls, landed) => {
1030
- live.delete(controls)
1031
- listed = undefined
1032
- UndefinedOr.map(also, (more) => more.ended(controls, landed))
1033
- },
1034
- }
1065
+ also,
1066
+ )
1035
1067
  // Stopping one ends it, which deletes it from the set: safe in the set's own walk.
1036
1068
  return { scope, watch, stopAll: () => Iter.forEach(live, (controls) => controls.stop()) }
1037
1069
  }
@@ -0,0 +1,82 @@
1
+ /**
2
+ * A **Wardic instance**'s Features (#516, ADR-0067 *Amendment (#516)*), for every binding: what
3
+ * Solid's `createWardic` and Foldkit's `Wardic.make` split their list into, and what their elements take
4
+ * in each provider: those given at once, flattened, each once, and each load (`() => import(…)`), started
5
+ * once per instance the first time any provider asks, so one module's instance loads each once. In a
6
+ * module only the makers import, apart from the spine every page shares, so an app with no Wardic
7
+ * instance ships none of it (ADR-0067, *Amendment (#548)*).
8
+ *
9
+ * Unstable, for Wardic's own packages: public, with no semver promise.
10
+ *
11
+ * @module
12
+ */
13
+ // Unstable: ADR-0071, decision 5. #793 (PR 1's review): solid's `instance-features.ts`, moved to dom for
14
+ // the Foldkit binding's loads (Q15).
15
+ import * as Arr from "effect/Array"
16
+ import * as Bool from "effect/Boolean"
17
+ import type * as Cause from "effect/Cause"
18
+ import { constVoid, identity } from "effect/Function"
19
+ import * as Predicate from "effect/Predicate"
20
+ import * as UndefinedOr from "effect/UndefinedOr"
21
+ import * as Feature from "./Feature.ts"
22
+
23
+ /**
24
+ * `prop`'s Features (#516): those given at once, flattened, each once, and its loads, each started once,
25
+ * the first time any provider asks, so one module's Wardic instance loads each once.
26
+ */
27
+ export const instanceFeaturesOf = (prop: Feature.InstanceFeaturesProp): Feature.InstanceFeatures => {
28
+ const leaves = leavesOf(prop)
29
+ const given = Arr.dedupeWith(Arr.filter(leaves, isFeature), (a, b) => a === b)
30
+ const loads = Arr.map(Arr.filter(leaves, isLoad), once)
31
+ const owns = new WeakMap<Feature.FeatureRegistry, Feature.Own>()
32
+ return {
33
+ own: (registry, host) =>
34
+ UndefinedOr.match(owns.get(registry), {
35
+ onDefined: identity,
36
+ onUndefined: () => {
37
+ const own = ownIn(registry.build, given, loads, host.report, { reactivity: host.reactivity, server: host.server, onCleanup: registry.onEnd })
38
+ owns.set(registry, own)
39
+ return own
40
+ },
41
+ }),
42
+ }
43
+ }
44
+
45
+ /** `prop`'s Features and loads, every list flattened. */
46
+ const leavesOf = (prop: Feature.InstanceFeaturesProp): ReadonlyArray<Feature.Feature | Feature.FeatureLoad> =>
47
+ Bool.match(Array.isArray(prop), {
48
+ onTrue: () => Arr.flatMap(prop as ReadonlyArray<Feature.InstanceFeaturesProp>, leavesOf),
49
+ onFalse: () => [prop as Feature.Feature | Feature.FeatureLoad],
50
+ })
51
+
52
+ const isFeature = (leaf: Feature.Feature | Feature.FeatureLoad): leaf is Feature.Feature => Predicate.hasProperty(leaf, "layer")
53
+
54
+ const isLoad = (leaf: Feature.Feature | Feature.FeatureLoad): leaf is Feature.FeatureLoad => Predicate.isFunction(leaf)
55
+
56
+ /**
57
+ * `load`, started at its first call, its promise kept for every call after. A failed load is forgotten,
58
+ * so the next provider to ask loads again, as a new `LazyWardic` does.
59
+ */
60
+ const once = (load: Feature.FeatureLoad): Feature.FeatureLoad => {
61
+ let started: Promise<Feature.Features> | undefined
62
+ return () =>
63
+ UndefinedOr.match(started, {
64
+ onDefined: identity,
65
+ onUndefined: () => {
66
+ const loading = load()
67
+ started = loading
68
+ loading.then(constVoid, () => {
69
+ if (started === loading) started = undefined
70
+ })
71
+ return loading
72
+ },
73
+ })
74
+ }
75
+
76
+ /** A Wardic instance's Features in one provider: those given at once, and each load's, joined. */
77
+ const ownIn = (build: Feature.Build, given: ReadonlyArray<Feature.Feature>, loads: ReadonlyArray<Feature.FeatureLoad>, report: (cause: Cause.Cause<unknown>) => void, host: Feature.SectionHost): Feature.Own =>
78
+ Arr.reduce(
79
+ Arr.map(loads, (load) => Feature.ownOf(build, load, report, host)),
80
+ Feature.ownOf(build, given, report, host),
81
+ Feature.joinOwns,
82
+ )
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Point mappings for a provider's `transformPagePoint` (#443, Motion's `correctParentTransform` and
3
+ * `transformViewBoxPoint`), for every DOM binding. Each takes its element as it is, an accessor of one
4
+ * (a ref's, set at mount), or a selector, for a binding with no refs (Foldkit's), read at each point.
5
+ * Wardic's points are in the viewport's coordinates, where Motion's are the page's, so neither adds the
6
+ * page's scroll.
7
+ *
8
+ * Unstable, for Wardic's own packages: public, with no semver promise.
9
+ *
10
+ * @module
11
+ */
12
+ // Unstable: ADR-0071, decision 5. #795: solid's `page-point.ts`, moved to dom for the Foldkit binding (its storefront's room example).
13
+ import * as Predicate from "effect/Predicate"
14
+ import type * as WardConfig from "@wardic/dom/unstable/WardConfig"
15
+
16
+ /** An element given as it is, an accessor of one, or a selector (#795), looked up in the document at each point. */
17
+ export type ElementGiven<E extends Element> = E | (() => E | undefined) | string
18
+
19
+ /** The element now: an accessor's, the first its selector finds, or the one given. */
20
+ const elementOf = <E extends Element>(given: ElementGiven<E>): E | undefined => {
21
+ if (Predicate.isFunction(given)) return given()
22
+ if (Predicate.isString(given)) return foundBy<E>(given)
23
+ return given
24
+ }
25
+
26
+ /** The first element `selector` finds in the document, if any. */
27
+ const foundBy = <E extends Element>(selector: string): E | undefined => {
28
+ const found = document.querySelector<E>(selector)
29
+ if (Predicate.isNull(found)) return undefined
30
+ return found
31
+ }
32
+
33
+ /** Whether an element is an SVG root: a selector may find any element. */
34
+ const isSvg = (element: Element): element is SVGSVGElement => Predicate.hasProperty(element, "viewBox")
35
+
36
+ /** The SVG `given` names now, if it names one. */
37
+ const svgOf = (given: ElementGiven<SVGSVGElement>): SVGSVGElement | undefined => {
38
+ const element = elementOf(given)
39
+ if (Predicate.isUndefined(element)) return undefined
40
+ if (isSvg(element)) return element
41
+ return undefined
42
+ }
43
+
44
+ /** A 2D linear map's coefficients: the inverse of an element's transform, as Motion's `getInverseMatrix`. */
45
+ interface Linear {
46
+ readonly a: number
47
+ readonly b: number
48
+ readonly c: number
49
+ readonly d: number
50
+ }
51
+
52
+ /** The inverse of `element`'s computed transform's linear part, or `undefined` for none, or one that can't be inverted. */
53
+ const inverseOf = (element: Element): Linear | undefined => {
54
+ const transform = getComputedStyle(element).transform
55
+ if (Predicate.isUndefined(transform) || transform === "" || transform === "none") return undefined
56
+ const { a, b, c, d } = new DOMMatrixReadOnly(transform)
57
+ const det = a * d - b * c
58
+ if (Math.abs(det) < 1e-10) return undefined
59
+ return { a: d / det, b: -b / det, c: -c / det, d: a / det }
60
+ }
61
+
62
+ /**
63
+ * A `transformPagePoint` for a drag or a pan inside a parent with a CSS transform, rotated, scaled or
64
+ * skewed: each pointer point is turned through the inverse of the parent's transform, read at each
65
+ * point, so the element stays under the pointer at any zoom, and a parent that animates is followed.
66
+ * The parent is an element, an accessor of a ref, or a selector looked up in the document at each
67
+ * point. Motion's `correctParentTransform`.
68
+ *
69
+ * @example
70
+ * // On Solid: a ref's accessor.
71
+ * let plan!: HTMLDivElement
72
+ * <div ref={plan} style={{ transform: `scale(${zoom()})` }}>
73
+ * <WardicConfig transformPagePoint={correctParentTransform(() => plan)}>
74
+ * <w.div drag />
75
+ * </WardicConfig>
76
+ * </div>
77
+ * // On Foldkit, which has no refs: a selector.
78
+ * w.div([w.Drag(), w.Config({ transformPagePoint: correctParentTransform("#plan") })])
79
+ * @see https://wardic.dev/reference/solid/utilities/page-points
80
+ */
81
+ // #443. Points pass as they are while the parent is gone or has no transform.
82
+ export const correctParentTransform =
83
+ (parent: ElementGiven<Element>): WardConfig.WardicTransformPoint =>
84
+ (point) => {
85
+ const element = elementOf(parent)
86
+ if (Predicate.isUndefined(element)) return point
87
+ const inverse = inverseOf(element)
88
+ if (Predicate.isUndefined(inverse)) return point
89
+ const box = element.getBoundingClientRect()
90
+ const cx = box.left + box.width / 2
91
+ const cy = box.top + box.height / 2
92
+ const dx = point.x - cx
93
+ const dy = point.y - cy
94
+ return { x: cx + inverse.a * dx + inverse.c * dy, y: cy + inverse.b * dx + inverse.d * dy }
95
+ }
96
+
97
+ /**
98
+ * A `transformPagePoint` for a drag or a pan inside an SVG whose `viewBox` differs from the size it
99
+ * renders at: each pointer point is scaled into the viewBox's units, so a dragged shape stays under
100
+ * the pointer. The SVG is an element, an accessor of a ref, or a selector looked up in the document at
101
+ * each point; a selector that finds no SVG maps nothing. Motion's `transformViewBoxPoint`.
102
+ *
103
+ * @example
104
+ * // On Solid: a ref's accessor.
105
+ * let svg!: SVGSVGElement
106
+ * <WardicConfig transformPagePoint={transformViewBoxPoint(() => svg)}>
107
+ * <svg ref={svg} viewBox="0 0 100 100" width="500" height="500">
108
+ * <w.rect drag width="10" height="10" />
109
+ * </svg>
110
+ * </WardicConfig>
111
+ * // On Foldkit: a selector.
112
+ * w.rect([w.Drag(), w.Config({ transformPagePoint: transformViewBoxPoint("#plan") })])
113
+ * @see https://wardic.dev/reference/solid/utilities/page-points
114
+ */
115
+ // #443. Points pass as they are while the SVG is gone, has no viewBox, or renders at no size.
116
+ export const transformViewBoxPoint =
117
+ (svg: ElementGiven<SVGSVGElement>): WardConfig.WardicTransformPoint =>
118
+ (point) => {
119
+ const element = svgOf(svg)
120
+ if (Predicate.isUndefined(element)) return point
121
+ const viewBox = element.viewBox.baseVal
122
+ if (Predicate.isNullish(viewBox) || (viewBox.width === 0 && viewBox.height === 0)) return point
123
+ const box = element.getBoundingClientRect()
124
+ if (box.width === 0 || box.height === 0) return point
125
+ return {
126
+ x: (point.x - box.left) * (viewBox.width / box.width) + box.left,
127
+ y: (point.y - box.top) * (viewBox.height / box.height) + box.top,
128
+ }
129
+ }