@wardic/dom 0.0.0-stage → 1.0.0-rc.10

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 (315) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +8 -2
  3. package/THIRD_PARTY_NOTICES.md +145 -0
  4. package/dist/Appear.d.ts +30 -0
  5. package/dist/Appear.d.ts.map +1 -0
  6. package/dist/Appear.js +31 -0
  7. package/dist/Appear.js.map +1 -0
  8. package/dist/Color.d.ts +14 -0
  9. package/dist/Color.d.ts.map +1 -0
  10. package/dist/Color.js +16 -0
  11. package/dist/Color.js.map +1 -0
  12. package/dist/DomAnnouncer.d.ts +13 -0
  13. package/dist/DomAnnouncer.d.ts.map +1 -0
  14. package/dist/DomAnnouncer.js +14 -0
  15. package/dist/DomAnnouncer.js.map +1 -0
  16. package/dist/DomBatch.d.ts +20 -0
  17. package/dist/DomBatch.d.ts.map +1 -0
  18. package/dist/DomBatch.js +23 -0
  19. package/dist/DomBatch.js.map +1 -0
  20. package/dist/DomClock.d.ts +11 -0
  21. package/dist/DomClock.d.ts.map +1 -0
  22. package/dist/DomClock.js +12 -0
  23. package/dist/DomClock.js.map +1 -0
  24. package/dist/DomDefaultTransition.d.ts +12 -0
  25. package/dist/DomDefaultTransition.d.ts.map +1 -0
  26. package/dist/DomDefaultTransition.js +13 -0
  27. package/dist/DomDefaultTransition.js.map +1 -0
  28. package/dist/DomInput.d.ts +14 -0
  29. package/dist/DomInput.d.ts.map +1 -0
  30. package/dist/DomInput.js +17 -0
  31. package/dist/DomInput.js.map +1 -0
  32. package/dist/DomInterpolators.d.ts +13 -0
  33. package/dist/DomInterpolators.d.ts.map +1 -0
  34. package/dist/DomInterpolators.js +14 -0
  35. package/dist/DomInterpolators.js.map +1 -0
  36. package/dist/DomKeyRules.d.ts +13 -0
  37. package/dist/DomKeyRules.d.ts.map +1 -0
  38. package/dist/DomKeyRules.js +14 -0
  39. package/dist/DomKeyRules.js.map +1 -0
  40. package/dist/DomPositionalKeys.d.ts +13 -0
  41. package/dist/DomPositionalKeys.d.ts.map +1 -0
  42. package/dist/DomPositionalKeys.js +14 -0
  43. package/dist/DomPositionalKeys.js.map +1 -0
  44. package/dist/DomProbe.d.ts +13 -0
  45. package/dist/DomProbe.d.ts.map +1 -0
  46. package/dist/DomProbe.js +14 -0
  47. package/dist/DomProbe.js.map +1 -0
  48. package/dist/DomProjectionSystem.d.ts +28 -0
  49. package/dist/DomProjectionSystem.d.ts.map +1 -0
  50. package/dist/DomProjectionSystem.js +41 -0
  51. package/dist/DomProjectionSystem.js.map +1 -0
  52. package/dist/DomRenderer.d.ts +13 -0
  53. package/dist/DomRenderer.d.ts.map +1 -0
  54. package/dist/DomRenderer.js +15 -0
  55. package/dist/DomRenderer.js.map +1 -0
  56. package/dist/DomScrollSource.d.ts +14 -0
  57. package/dist/DomScrollSource.d.ts.map +1 -0
  58. package/dist/DomScrollSource.js +15 -0
  59. package/dist/DomScrollSource.js.map +1 -0
  60. package/dist/FlushGate.d.ts +48 -0
  61. package/dist/FlushGate.d.ts.map +1 -0
  62. package/dist/FlushGate.js +62 -0
  63. package/dist/FlushGate.js.map +1 -0
  64. package/dist/KeyboardPress.d.ts +16 -0
  65. package/dist/KeyboardPress.d.ts.map +1 -0
  66. package/dist/KeyboardPress.js +17 -0
  67. package/dist/KeyboardPress.js.map +1 -0
  68. package/dist/MeasuredPair.d.ts +14 -0
  69. package/dist/MeasuredPair.d.ts.map +1 -0
  70. package/dist/MeasuredPair.js +16 -0
  71. package/dist/MeasuredPair.js.map +1 -0
  72. package/dist/OwnValues.d.ts +14 -0
  73. package/dist/OwnValues.d.ts.map +1 -0
  74. package/dist/OwnValues.js +15 -0
  75. package/dist/OwnValues.js.map +1 -0
  76. package/dist/ProjectionSystem.d.ts +34 -0
  77. package/dist/ProjectionSystem.d.ts.map +1 -0
  78. package/dist/ProjectionSystem.js +29 -0
  79. package/dist/ProjectionSystem.js.map +1 -0
  80. package/dist/ReorderController.d.ts +61 -0
  81. package/dist/ReorderController.d.ts.map +1 -0
  82. package/dist/ReorderController.js +78 -0
  83. package/dist/ReorderController.js.map +1 -0
  84. package/dist/Report.d.ts +12 -0
  85. package/dist/Report.d.ts.map +1 -0
  86. package/dist/Report.js +13 -0
  87. package/dist/Report.js.map +1 -0
  88. package/dist/ScrollOffset.d.ts +34 -0
  89. package/dist/ScrollOffset.d.ts.map +1 -0
  90. package/dist/ScrollOffset.js +42 -0
  91. package/dist/ScrollOffset.js.map +1 -0
  92. package/dist/Style.d.ts +15 -0
  93. package/dist/Style.d.ts.map +1 -0
  94. package/dist/Style.js +17 -0
  95. package/dist/Style.js.map +1 -0
  96. package/dist/Svg.d.ts +13 -0
  97. package/dist/Svg.d.ts.map +1 -0
  98. package/dist/Svg.js +14 -0
  99. package/dist/Svg.js.map +1 -0
  100. package/dist/Variables.d.ts +13 -0
  101. package/dist/Variables.d.ts.map +1 -0
  102. package/dist/Variables.js +14 -0
  103. package/dist/Variables.js.map +1 -0
  104. package/dist/index.d.ts +83 -0
  105. package/dist/index.d.ts.map +1 -0
  106. package/dist/index.js +71 -0
  107. package/dist/index.js.map +1 -0
  108. package/dist/internal/announcer.d.ts +50 -0
  109. package/dist/internal/announcer.d.ts.map +1 -0
  110. package/dist/internal/announcer.js +115 -0
  111. package/dist/internal/announcer.js.map +1 -0
  112. package/dist/internal/appear-slot.d.ts +23 -0
  113. package/dist/internal/appear-slot.d.ts.map +1 -0
  114. package/dist/internal/appear-slot.js +29 -0
  115. package/dist/internal/appear-slot.js.map +1 -0
  116. package/dist/internal/appear.d.ts +237 -0
  117. package/dist/internal/appear.d.ts.map +1 -0
  118. package/dist/internal/appear.js +740 -0
  119. package/dist/internal/appear.js.map +1 -0
  120. package/dist/internal/auto-scroll.d.ts +24 -0
  121. package/dist/internal/auto-scroll.d.ts.map +1 -0
  122. package/dist/internal/auto-scroll.js +124 -0
  123. package/dist/internal/auto-scroll.js.map +1 -0
  124. package/dist/internal/batch-writer.d.ts +16 -0
  125. package/dist/internal/batch-writer.d.ts.map +1 -0
  126. package/dist/internal/batch-writer.js +77 -0
  127. package/dist/internal/batch-writer.js.map +1 -0
  128. package/dist/internal/clock.d.ts +19 -0
  129. package/dist/internal/clock.d.ts.map +1 -0
  130. package/dist/internal/clock.js +39 -0
  131. package/dist/internal/clock.js.map +1 -0
  132. package/dist/internal/colors.d.ts +45 -0
  133. package/dist/internal/colors.d.ts.map +1 -0
  134. package/dist/internal/colors.js +193 -0
  135. package/dist/internal/colors.js.map +1 -0
  136. package/dist/internal/complex.d.ts +24 -0
  137. package/dist/internal/complex.d.ts.map +1 -0
  138. package/dist/internal/complex.js +28 -0
  139. package/dist/internal/complex.js.map +1 -0
  140. package/dist/internal/content-watch.d.ts +20 -0
  141. package/dist/internal/content-watch.d.ts.map +1 -0
  142. package/dist/internal/content-watch.js +76 -0
  143. package/dist/internal/content-watch.js.map +1 -0
  144. package/dist/internal/default-transition.d.ts +25 -0
  145. package/dist/internal/default-transition.d.ts.map +1 -0
  146. package/dist/internal/default-transition.js +41 -0
  147. package/dist/internal/default-transition.js.map +1 -0
  148. package/dist/internal/drag-defaults.d.ts +18 -0
  149. package/dist/internal/drag-defaults.d.ts.map +1 -0
  150. package/dist/internal/drag-defaults.js +38 -0
  151. package/dist/internal/drag-defaults.js.map +1 -0
  152. package/dist/internal/gate.d.ts +188 -0
  153. package/dist/internal/gate.d.ts.map +1 -0
  154. package/dist/internal/gate.js +458 -0
  155. package/dist/internal/gate.js.map +1 -0
  156. package/dist/internal/input.d.ts +35 -0
  157. package/dist/internal/input.d.ts.map +1 -0
  158. package/dist/internal/input.js +658 -0
  159. package/dist/internal/input.js.map +1 -0
  160. package/dist/internal/interpolators.d.ts +19 -0
  161. package/dist/internal/interpolators.d.ts.map +1 -0
  162. package/dist/internal/interpolators.js +30 -0
  163. package/dist/internal/interpolators.js.map +1 -0
  164. package/dist/internal/key-rules.d.ts +11 -0
  165. package/dist/internal/key-rules.d.ts.map +1 -0
  166. package/dist/internal/key-rules.js +244 -0
  167. package/dist/internal/key-rules.js.map +1 -0
  168. package/dist/internal/keyboard-press.d.ts +7 -0
  169. package/dist/internal/keyboard-press.d.ts.map +1 -0
  170. package/dist/internal/keyboard-press.js +45 -0
  171. package/dist/internal/keyboard-press.js.map +1 -0
  172. package/dist/internal/measure.d.ts +157 -0
  173. package/dist/internal/measure.d.ts.map +1 -0
  174. package/dist/internal/measure.js +296 -0
  175. package/dist/internal/measure.js.map +1 -0
  176. package/dist/internal/measured-pairs.d.ts +38 -0
  177. package/dist/internal/measured-pairs.d.ts.map +1 -0
  178. package/dist/internal/measured-pairs.js +251 -0
  179. package/dist/internal/measured-pairs.js.map +1 -0
  180. package/dist/internal/own-values.d.ts +35 -0
  181. package/dist/internal/own-values.d.ts.map +1 -0
  182. package/dist/internal/own-values.js +117 -0
  183. package/dist/internal/own-values.js.map +1 -0
  184. package/dist/internal/pass-ends.d.ts +21 -0
  185. package/dist/internal/pass-ends.d.ts.map +1 -0
  186. package/dist/internal/pass-ends.js +14 -0
  187. package/dist/internal/pass-ends.js.map +1 -0
  188. package/dist/internal/positional-keys.d.ts +16 -0
  189. package/dist/internal/positional-keys.d.ts.map +1 -0
  190. package/dist/internal/positional-keys.js +21 -0
  191. package/dist/internal/positional-keys.js.map +1 -0
  192. package/dist/internal/probe.d.ts +10 -0
  193. package/dist/internal/probe.d.ts.map +1 -0
  194. package/dist/internal/probe.js +116 -0
  195. package/dist/internal/probe.js.map +1 -0
  196. package/dist/internal/projection-defaults.d.ts +34 -0
  197. package/dist/internal/projection-defaults.d.ts.map +1 -0
  198. package/dist/internal/projection-defaults.js +21 -0
  199. package/dist/internal/projection-defaults.js.map +1 -0
  200. package/dist/internal/projection.d.ts +537 -0
  201. package/dist/internal/projection.d.ts.map +1 -0
  202. package/dist/internal/projection.js +5228 -0
  203. package/dist/internal/projection.js.map +1 -0
  204. package/dist/internal/renderer.d.ts +104 -0
  205. package/dist/internal/renderer.d.ts.map +1 -0
  206. package/dist/internal/renderer.js +618 -0
  207. package/dist/internal/renderer.js.map +1 -0
  208. package/dist/internal/reorder.d.ts +174 -0
  209. package/dist/internal/reorder.d.ts.map +1 -0
  210. package/dist/internal/reorder.js +466 -0
  211. package/dist/internal/reorder.js.map +1 -0
  212. package/dist/internal/report.d.ts +30 -0
  213. package/dist/internal/report.d.ts.map +1 -0
  214. package/dist/internal/report.js +60 -0
  215. package/dist/internal/report.js.map +1 -0
  216. package/dist/internal/require-global.d.ts +11 -0
  217. package/dist/internal/require-global.d.ts.map +1 -0
  218. package/dist/internal/require-global.js +31 -0
  219. package/dist/internal/require-global.js.map +1 -0
  220. package/dist/internal/reveal.d.ts +20 -0
  221. package/dist/internal/reveal.d.ts.map +1 -0
  222. package/dist/internal/reveal.js +71 -0
  223. package/dist/internal/reveal.js.map +1 -0
  224. package/dist/internal/runner.d.ts +21 -0
  225. package/dist/internal/runner.d.ts.map +1 -0
  226. package/dist/internal/runner.js +48 -0
  227. package/dist/internal/runner.js.map +1 -0
  228. package/dist/internal/scale-correction.d.ts +80 -0
  229. package/dist/internal/scale-correction.d.ts.map +1 -0
  230. package/dist/internal/scale-correction.js +227 -0
  231. package/dist/internal/scale-correction.js.map +1 -0
  232. package/dist/internal/scroll.d.ts +37 -0
  233. package/dist/internal/scroll.d.ts.map +1 -0
  234. package/dist/internal/scroll.js +279 -0
  235. package/dist/internal/scroll.js.map +1 -0
  236. package/dist/internal/scrollers.d.ts +35 -0
  237. package/dist/internal/scrollers.d.ts.map +1 -0
  238. package/dist/internal/scrollers.js +38 -0
  239. package/dist/internal/scrollers.js.map +1 -0
  240. package/dist/internal/starting-style.d.ts +141 -0
  241. package/dist/internal/starting-style.d.ts.map +1 -0
  242. package/dist/internal/starting-style.js +300 -0
  243. package/dist/internal/starting-style.js.map +1 -0
  244. package/dist/internal/svg.d.ts +60 -0
  245. package/dist/internal/svg.d.ts.map +1 -0
  246. package/dist/internal/svg.js +257 -0
  247. package/dist/internal/svg.js.map +1 -0
  248. package/dist/internal/variables.d.ts +45 -0
  249. package/dist/internal/variables.d.ts.map +1 -0
  250. package/dist/internal/variables.js +92 -0
  251. package/dist/internal/variables.js.map +1 -0
  252. package/package.json +65 -4
  253. package/src/Appear.ts +54 -0
  254. package/src/Color.ts +24 -0
  255. package/src/DomAnnouncer.ts +22 -0
  256. package/src/DomBatch.ts +22 -0
  257. package/src/DomClock.ts +13 -0
  258. package/src/DomDefaultTransition.ts +16 -0
  259. package/src/DomInput.ts +23 -0
  260. package/src/DomInterpolators.ts +16 -0
  261. package/src/DomKeyRules.ts +17 -0
  262. package/src/DomPositionalKeys.ts +16 -0
  263. package/src/DomProbe.ts +15 -0
  264. package/src/DomProjectionSystem.ts +44 -0
  265. package/src/DomRenderer.ts +26 -0
  266. package/src/DomScrollSource.ts +14 -0
  267. package/src/FlushGate.ts +81 -0
  268. package/src/KeyboardPress.ts +16 -0
  269. package/src/MeasuredPair.ts +15 -0
  270. package/src/OwnValues.ts +19 -0
  271. package/src/ProjectionSystem.ts +51 -0
  272. package/src/ReorderController.ts +122 -0
  273. package/src/Report.ts +17 -0
  274. package/src/ScrollOffset.ts +54 -0
  275. package/src/Style.ts +37 -0
  276. package/src/Svg.ts +21 -0
  277. package/src/Variables.ts +17 -0
  278. package/src/index.ts +120 -0
  279. package/src/internal/announcer.ts +132 -0
  280. package/src/internal/appear-slot.ts +32 -0
  281. package/src/internal/appear.ts +1134 -0
  282. package/src/internal/auto-scroll.ts +176 -0
  283. package/src/internal/batch-writer.ts +95 -0
  284. package/src/internal/clock.ts +45 -0
  285. package/src/internal/colors.ts +258 -0
  286. package/src/internal/complex.ts +29 -0
  287. package/src/internal/content-watch.ts +84 -0
  288. package/src/internal/culori.d.ts +58 -0
  289. package/src/internal/default-transition.ts +50 -0
  290. package/src/internal/drag-defaults.ts +54 -0
  291. package/src/internal/gate.ts +598 -0
  292. package/src/internal/input.ts +763 -0
  293. package/src/internal/interpolators.ts +33 -0
  294. package/src/internal/key-rules.ts +357 -0
  295. package/src/internal/keyboard-press.ts +45 -0
  296. package/src/internal/measure.ts +373 -0
  297. package/src/internal/measured-pairs.ts +297 -0
  298. package/src/internal/own-values.ts +166 -0
  299. package/src/internal/pass-ends.ts +23 -0
  300. package/src/internal/positional-keys.ts +24 -0
  301. package/src/internal/probe.ts +175 -0
  302. package/src/internal/projection-defaults.ts +45 -0
  303. package/src/internal/projection.ts +6233 -0
  304. package/src/internal/renderer.ts +648 -0
  305. package/src/internal/reorder.ts +650 -0
  306. package/src/internal/report.ts +69 -0
  307. package/src/internal/require-global.ts +46 -0
  308. package/src/internal/reveal.ts +75 -0
  309. package/src/internal/runner.ts +64 -0
  310. package/src/internal/scale-correction.ts +310 -0
  311. package/src/internal/scroll.ts +358 -0
  312. package/src/internal/scrollers.ts +51 -0
  313. package/src/internal/starting-style.ts +396 -0
  314. package/src/internal/svg.ts +300 -0
  315. package/src/internal/variables.ts +145 -0
@@ -0,0 +1,650 @@
1
+ /**
2
+ * The DOM's **Reorder group** controller (ADR-0020): the framework-free half of
3
+ * `<Reorder>`, shared by every DOM binding. A binding adds only its reactivity, its
4
+ * lifecycle and its elements.
5
+ *
6
+ * It owns core's `Reorder.Group` (the **Crossover** and the new order), reads the
7
+ * group element's `direction` at each drag start, builds the group's **Auto-scroll**,
8
+ * and routes each item's drag samples: the start opens the group's gesture, each move
9
+ * runs the Crossover and Auto-scroll, the end (or the item's disposal mid-gesture)
10
+ * closes both. The items' `touch-action` needs the axis before a touch starts, so after
11
+ * the items are laid out (a Measure pass, or a placed group's placing) the controller detects the axis from the items' boxes and reports
12
+ * a change through `onAxis`; while the app sets the axis it detects nothing. Motion
13
+ * detects on every item's every measurement (Group.tsx:121-133 at 16c6122). When the app
14
+ * clears its axis, no Measure pass comes with that: a binding reads `detect()`, the axis
15
+ * from the boxes the items have now, in the computation that derives its axis, so whatever
16
+ * reads the axis follows in the same update (#257's audit: an effect that detected and
17
+ * reported it updated them a step late).
18
+ *
19
+ * It also runs each item's **Keyboard reorder** (ADR-0037), on by default: a key from the item
20
+ * itself that the page didn't take (Space or Enter lifts and drops, Escape cancels, the arrows,
21
+ * Home and End move the lifted item) goes to core's `Reorder` model, its default prevented; each
22
+ * step is an **Announcement** (a held arrow's thinned to the last), scrolls the item's new place
23
+ * into view once the items are laid out again, and keeps focus on the item; the lifted node is
24
+ * raised as a dragged one is, and the binding turns its `whileDrag` on. Focus leaving the item, a
25
+ * press on it, or the app changing `values` drops it where it is; its unmount ends the lift unsaid.
26
+ *
27
+ * Each **Reorder hold** (#348, ADR-0020 *Amendment (#348)*), an item in a user's hand from its drag's start or
28
+ * its lift, is told at its start and, once, at its end, whatever ends it: after `onReorderEnd`, when
29
+ * it moved. One at a time: a hold's start ends the open one first, and that item's later release
30
+ * tells nothing more.
31
+ * `move` announces too. Nothing is announced without an `announce`.
32
+ *
33
+ * Observability: `wardic.reorder:` debug logs through the runtime, annotated: the
34
+ * group's and Auto-scroll's events, and `axis` when the detected axis changes (cold
35
+ * path: never per sample), plus one warning per controller for a remount (below).
36
+ *
37
+ * Tier 2 (ADR-0008): for DOM binding authors.
38
+ */
39
+ import * as AutoScroll from "@wardic/core/AutoScroll"
40
+ import type * as Drag from "@wardic/core/Drag"
41
+ import type * as InputSource from "@wardic/core/InputSource"
42
+ import * as Reorder from "@wardic/core/Reorder"
43
+ import * as Arr from "effect/Array"
44
+ import * as Bool from "effect/Boolean"
45
+ import * as Duration from "effect/Duration"
46
+ import * as Effect from "effect/Effect"
47
+ import * as Equivalence from "effect/Equivalence"
48
+ import * as FiberHandle from "effect/FiberHandle"
49
+ import { constFalse, constTrue, constVoid, identity } from "effect/Function"
50
+ import * as Match from "effect/Match"
51
+ import * as MutableRef from "effect/MutableRef"
52
+ import * as Option from "effect/Option"
53
+ import * as Predicate from "effect/Predicate"
54
+ import * as Scope from "effect/Scope"
55
+ import * as Str from "effect/String"
56
+ import * as UndefinedOr from "effect/UndefinedOr"
57
+ import { documentOf, prepareAnnouncer } from "./announcer.ts"
58
+ import { type AutoScroller, makeAutoScroller } from "./auto-scroll.ts"
59
+ import { debugOf } from "./report.ts"
60
+ import { type Rect, revealLayoutOf, revealRectOf } from "./reveal.ts"
61
+ import type { Runner } from "./runner.ts"
62
+
63
+ /**
64
+ * The group element's style: no scroll anchoring, so the browser doesn't shift the
65
+ * page as items move (Motion's Group.tsx:187-195 at 16c6122). The user's style goes
66
+ * over it.
67
+ */
68
+ export const REORDER_GROUP_STYLE = Object.freeze({ "overflow-anchor": "none" } as const)
69
+
70
+ /**
71
+ * What {@link makeReorderController} runs on: the runner it logs through, how its items are laid out
72
+ * again and when they have been, and the Environment's `Announcer`. `ReorderController.make` wires
73
+ * them to the projection system its items are nodes of (#132's PR 3, ADR-0041); `makePlaced` to a
74
+ * binding that places its items itself, a Batch Reorder group's (#712, ADR-0072).
75
+ */
76
+ export interface ReorderControllerWiring {
77
+ /** The runner the controller logs through and waits in. */
78
+ readonly runtime: Runner<never>
79
+ /** The app changed `values`: have the items laid out in it, a Measure pass, or nothing for items the binding places itself. */
80
+ readonly layOut: () => void
81
+ /** Speak an **Announcement**: the Environment's `Announcer`. Left out, nothing is announced. */
82
+ readonly announce?: ((message: string) => void) | undefined
83
+ /** Hears each time the items have been laid out: a Measure pass's end (the engine's `PassEnds`), or the binding's placing. */
84
+ readonly onLaidOut: (listener: () => void) => () => void
85
+ }
86
+
87
+ /**
88
+ * What an item registers with its group: core's `ItemSource` (its box, its drag offset, its box's
89
+ * version), how to raise it over the rest while it's lifted, and, for an item placed rather than
90
+ * measured, its place on screen, for a keyboard step's reveal. A projection handle is one.
91
+ */
92
+ // #712: structural, so a Batch Reorder group's member registers as a layout node does.
93
+ export interface ReorderItemHandle extends Reorder.ItemSource {
94
+ /** Raise the item over the rest, or let it down: a keyboard lift's. */
95
+ readonly setLifted: (lifted: boolean) => void
96
+ /**
97
+ * Its place on screen, in client coordinates, which a keyboard step reveals once the items are laid
98
+ * out. Left out, its painted box with its own transform undone, a layout node's.
99
+ */
100
+ readonly placeOnScreen?: (() => Rect) | undefined
101
+ }
102
+
103
+ /**
104
+ * A user's reorder that changed the order, at its end: a pointer drag released, or a keyboard lift
105
+ * dropped, with the item at another index than at its start. What an app saves: the whole order, or the
106
+ * one item that moved and where.
107
+ */
108
+ // #257 item 3.
109
+ export interface ReorderEnd<V> {
110
+ /** The order at the end. */
111
+ readonly values: Array<V>
112
+ /** The item the user moved: the one dragged, or lifted. */
113
+ readonly value: V
114
+ /** Its index at the gesture's start. */
115
+ readonly from: number
116
+ /** Its index at the end. */
117
+ readonly to: number
118
+ }
119
+
120
+ /** A **Reorder hold**'s start: the item taken in hand, by its drag's start or a keyboard lift, and its index then. */
121
+ // #348.
122
+ export interface ReorderHoldStart<V> {
123
+ /** The item taken in hand. */
124
+ readonly value: V
125
+ /** Its index at the start. */
126
+ readonly from: number
127
+ }
128
+
129
+ /**
130
+ * How a **Reorder hold** ended: `"drop"`, released or dropped where it is (a pointer's release or
131
+ * cancel, a drop key, focus leaving, a press, the app's `values`, another hold); `"cancel"`, Escape, the
132
+ * order put back; `"unmount"`, its item went away.
133
+ */
134
+ // #348.
135
+ export type ReorderHoldHow = "drop" | "cancel" | "unmount"
136
+
137
+ /**
138
+ * A **Reorder hold**'s end, whatever ended it, once: the order then, the item, its index at the start
139
+ * and now (`-1` once it's out of the values), and how. After `onReorderEnd`, in the same task, when the
140
+ * order changed.
141
+ */
142
+ // #348.
143
+ export interface ReorderHoldEnd<V> extends ReorderEnd<V> {
144
+ /** How it ended. */
145
+ readonly how: ReorderHoldHow
146
+ }
147
+
148
+ /** What a binding hands its group's controller. */
149
+ export interface ReorderControllerOptions<V> {
150
+ /** The app's values, in order. */
151
+ readonly values: () => ReadonlyArray<V>
152
+ /** Receives a new order; the app sets `values` from it. */
153
+ readonly onReorder: (values: Array<V>) => void
154
+ /**
155
+ * Hears a user's reorder that changed the order, at its end: once a gesture, not at each step, as
156
+ * `onReorder` is. Not a keyboard cancel, which puts the order back, nor `move`.
157
+ */
158
+ // #257 item 3.
159
+ readonly onReorderEnd?: ((end: ReorderEnd<V>) => void) | undefined
160
+ /** Hears each **Reorder hold**'s start: a drag of an item past its threshold, or a keyboard lift. */
161
+ // #348.
162
+ readonly onHoldStart?: ((start: ReorderHoldStart<V>) => void) | undefined
163
+ /** Hears each **Reorder hold**'s end, once, whatever ended it; after `onReorderEnd` when it moved. */
164
+ // #348.
165
+ readonly onHoldEnd?: ((end: ReorderHoldEnd<V>) => void) | undefined
166
+ /** The app's axis, overriding detection; `None` to detect. */
167
+ readonly axis?: (() => Option.Option<Reorder.Axis>) | undefined
168
+ /** Called when the detected axis changes (once the items are laid out, or {@link ReorderController.resolve}). */
169
+ readonly onAxis?: ((axis: Reorder.Axis) => void) | undefined
170
+ /**
171
+ * How the binding keys its items, for the warning logged once per controller when an
172
+ * item registers with a value an item of this group unregistered in the same task
173
+ * (the signature of a list keyed by index): "key items with `keyedWith`". Solid's is
174
+ * `"<For>"`; by default "their value".
175
+ */
176
+ readonly keyedWith?: string | undefined
177
+ /** Whether the items reorder by keyboard, read at each key; `false` turns it off. On when left out. */
178
+ // ADR-0037.
179
+ readonly keyboard?: (() => boolean) | undefined
180
+ /** The group's announcements over the defaults, read at each announcement. */
181
+ // ADR-0037.
182
+ readonly announcements?: (() => Option.Option<Partial<Reorder.Announcements<V>>>) | undefined
183
+ }
184
+
185
+ /** One item of a {@link ReorderController}: its binding feeds it the item's handle, drag samples, keys, focus and presses. */
186
+ export interface ReorderItemDriver {
187
+ /** Register the item's handle, a projection node's or a placed item's, with the group; returns the unregister. */
188
+ readonly register: (handle: ReorderItemHandle) => () => void
189
+ /** The axis the item's drag takes, read at each gesture's start. */
190
+ readonly dragAxis: () => Reorder.Axis
191
+ /** The item's drag samples, as the drag delivers them. */
192
+ readonly onSample: (sample: Drag.Sample) => void
193
+ /** A key at the item or inside it, inside its dispatch: a keyboard reorder's. */
194
+ // ADR-0037.
195
+ readonly onKey: (event: InputSource.KeyboardLikeEvent) => void
196
+ /** Focus left the item: a lift drops where it is, unless focus is back on the item. */
197
+ // ADR-0037.
198
+ readonly onBlur: () => void
199
+ /** A press on the item: a lift drops where it is, before any drag. */
200
+ // ADR-0037.
201
+ readonly onPress: () => void
202
+ /** The item is going away: end a gesture or a lift still in progress. */
203
+ readonly dispose: () => void
204
+ }
205
+
206
+ /** How long a held arrow's announcements wait for the next repeat: only the last is spoken (ADR-0037, Q4). */
207
+ export const REPEAT_ANNOUNCE: Duration.Duration = Duration.millis(150)
208
+
209
+ /**
210
+ * A Reorder item's default instructions on the web: core's keys, and focus mode. A
211
+ * screen reader in browse mode (NVDA's, JAWS's) keeps Space and the arrows for itself until its user
212
+ * switches to focus mode, and it doesn't switch by itself on a list item.
213
+ */
214
+ // ADR-0037: the NVDA pass.
215
+ export const DEFAULT_REORDER_INSTRUCTIONS = `${Reorder.DEFAULT_INSTRUCTIONS} Some screen readers need focus mode for these keys.`
216
+
217
+ /** How an item is spoken: its `aria-label`, else its text with its whitespace collapsed, else its value. */
218
+ const labelOf = (element: Option.Option<Element>, value: unknown): string =>
219
+ Option.getOrElse(
220
+ Option.orElse(
221
+ Option.flatMap(element, (el) => nonEmpty(Option.map(Option.fromNullOr(el.getAttribute("aria-label")), Str.trim))),
222
+ () => Option.flatMap(element, (el) => nonEmpty(Option.map(Option.fromNullOr(el.textContent), (text) => text.replace(/\s+/g, " ").trim()))),
223
+ ),
224
+ () => String(value),
225
+ )
226
+
227
+ /** A text, `None` when it's empty. */
228
+ const nonEmpty = (text: Option.Option<string>): Option.Option<string> => Option.filter(text, Str.isNonEmpty)
229
+
230
+ /** A **Reorder group** on the DOM. */
231
+ // ADR-0020.
232
+ export interface ReorderController<V> {
233
+ /** Core's Reorder group (`Reorder.move(group, value, toIndex)` reorders in code). */
234
+ readonly group: Reorder.Group<V>
235
+ /** The app's axis, else the last detected one (`"y"` before the first). */
236
+ readonly axis: () => Reorder.Axis
237
+ /** The group's element: its `direction`, and the scroller Auto-scroll looks for from it. */
238
+ readonly attach: (element: Element) => void
239
+ /**
240
+ * The driver for the item with `value`; `element` is its element (for Auto-scroll, its label, its
241
+ * focus and its reveal), and `onLift` hears its keyboard lift start and end (the binding's `whileDrag`).
242
+ */
243
+ readonly item: (value: V, element: () => Option.Option<Element>, onLift?: (lifted: boolean) => void) => ReorderItemDriver
244
+ /** Move `value` to `toIndex` in code (`Reorder.move`), and announce it. */
245
+ // ADR-0037.
246
+ readonly move: (value: V, toIndex: number) => void
247
+ /**
248
+ * The axis now: the app's, else detected from the items' boxes as they are, reporting nothing. A
249
+ * binding derives its axis with it when the app's is cleared.
250
+ */
251
+ // #257's audit.
252
+ readonly detect: () => Reorder.Axis
253
+ /** The app's values changed: its items laid out in them (a Measure pass), so they animate to their new places. */
254
+ readonly valuesChanged: () => void
255
+ /** Stops listening for the items' layouts and ends the auto-scroll. The Scope calls it. */
256
+ readonly dispose: () => void
257
+ }
258
+
259
+ /** Build a {@link ReorderController}: a **Reorder group** over `wiring.projection`'s nodes. */
260
+ export const makeReorderController = <V>(wiring: ReorderControllerWiring, options: ReorderControllerOptions<V>): ReorderController<V> => {
261
+ const { runtime, announce } = wiring
262
+ const debug = debugOf(runtime, "wardic.reorder")
263
+ // Speak an Announcement, or nothing without an `announce`.
264
+ const speak = UndefinedOr.match(announce, { onUndefined: () => constVoid, onDefined: identity })
265
+ const keyboardOn = (): boolean => UndefinedOr.match(options.keyboard, { onUndefined: constTrue, onDefined: (on) => on() })
266
+ // The group's element and its Auto-scroll, once it's attached.
267
+ const element = MutableRef.make(Option.none<Element>())
268
+ const autoScroller = MutableRef.make(Option.none<AutoScroller>())
269
+ const resetAutoScroll = (): void => {
270
+ const scroller = MutableRef.get(autoScroller)
271
+ if (Option.isSome(scroller)) scroller.value.reset()
272
+ }
273
+ // The order the group last asked for: a `values` change to anything else is the app's own (ADR-0037).
274
+ let asked: ReadonlyArray<V> | undefined
275
+ /** The app's axis, if it sets one. */
276
+ const appAxis = (): Option.Option<Reorder.Axis> => Option.flatMap(Option.fromUndefinedOr(options.axis), (axis) => axis())
277
+ const group = Reorder.make<V>({
278
+ values: options.values,
279
+ onReorder: (values) => {
280
+ // A copy of what it hands on: an app that keeps that array, a store's own, may change it in place (#721).
281
+ asked = Arr.copy(values)
282
+ options.onReorder(values)
283
+ },
284
+ axis: () => appAxis(),
285
+ // Read once per drag start (Motion reads it on every sample, Group.tsx:142-147 at 16c6122).
286
+ direction: () =>
287
+ Bool.match(Option.exists(MutableRef.get(element), (el) => getComputedStyle(el).direction === "rtl"), {
288
+ onTrue: () => "rtl" as const,
289
+ onFalse: () => "ltr" as const,
290
+ }),
291
+ debug,
292
+ })
293
+
294
+ let detected: Reorder.Axis = "y"
295
+ let reported: Reorder.Axis | undefined
296
+ const resolve = (): void => {
297
+ if (Option.isSome(appAxis())) return
298
+ detected = Reorder.resolveAxis(group)
299
+ if (detected === reported) return
300
+ reported = detected
301
+ debug("axis", { axis: detected })
302
+ if (Predicate.isNotUndefined(options.onAxis)) options.onAxis(detected)
303
+ }
304
+ // --- The keyboard reorder (ADR-0037) ---------------------------------------
305
+ /** The lifted item's element, its handle and its `onLift`, by value, while it's registered. */
306
+ interface Keyed {
307
+ readonly element: () => Option.Option<Element>
308
+ handle: Option.Option<ReorderItemHandle>
309
+ readonly onLift: (lifted: boolean) => void
310
+ }
311
+ const keyed = new Map<V, Keyed>()
312
+ const keyedOf = (value: V): Option.Option<Keyed> => Option.fromUndefinedOr(keyed.get(value))
313
+ const elementOf = (value: V): Option.Option<Element> => Option.flatMap(keyedOf(value), (k) => k.element())
314
+ // The item whose new place the next layout lays out, to scroll into view then.
315
+ const reveal = MutableRef.make(Option.none<V>())
316
+ // A held arrow's latest announcement, waiting for the next repeat (ADR-0025, *Amendment (#69)*): a
317
+ // fiber in the Scope the controller was built in, which closes it after `dispose`.
318
+ const pending = runtime.runSync(FiberHandle.make<void, never>().pipe(Scope.provide(runtime.scope)))
319
+
320
+ const announceNow = (message: string): void => {
321
+ runtime.runFork(FiberHandle.clear(pending))
322
+ speak(message)
323
+ }
324
+ /** A held arrow's step: spoken once the repeats stop for {@link REPEAT_ANNOUNCE}, only the last. */
325
+ const announceLater = (message: string): void => {
326
+ FiberHandle.setUnsafe(pending, runtime.runFork(Effect.sleep(REPEAT_ANNOUNCE).pipe(Effect.andThen(Effect.sync(() => speak(message))))))
327
+ }
328
+ const wording = (): Reorder.Announcements<V> => ({
329
+ ...(Reorder.defaultAnnouncements as Reorder.Announcements<V>),
330
+ ...Option.getOrUndefined(Option.flatMap(Option.fromUndefinedOr(options.announcements), (announcements) => announcements())),
331
+ })
332
+ const say = (kind: keyof Reorder.Announcements<V>, value: V, placement: Reorder.Placement): string =>
333
+ wording()[kind]({ value, label: labelOf(elementOf(value), value), position: placement.position, count: placement.count })
334
+
335
+ /** Raise the lifted item (or lower it) and tell its binding (its `whileDrag`). */
336
+ const setLifted = (value: V, lifted: boolean): void => {
337
+ const k = keyed.get(value)
338
+ if (Predicate.isUndefined(k)) return
339
+ if (Option.isSome(k.handle)) k.handle.value.setLifted(lifted)
340
+ k.onLift(lifted)
341
+ }
342
+ /** `value`'s index in the app's values now. */
343
+ const indexOf = (value: V): number => options.values().indexOf(value)
344
+ /** The end of a user's reorder of `value`, begun at `from`: told when it moved (#257 item 3). */
345
+ const ended = (value: V, from: number): void => {
346
+ const to = indexOf(value)
347
+ if (to === from) return
348
+ UndefinedOr.map(options.onReorderEnd, (tell) => tell({ values: [...options.values()], value, from, to }))
349
+ }
350
+ // --- Holds (#348) ------------------------------------------------------------
351
+ /** The open **Reorder hold**: the item in hand, how it was taken, and its index then. One at a time. */
352
+ let held: Option.Option<{ readonly value: V; readonly from: number; readonly by: "drag" | "lift" }> = Option.none()
353
+ /** Whether `value`'s drag is the open hold: a drag a later hold took over tells nothing more. */
354
+ const holdsDrag = (value: V): boolean => Option.exists(held, (h) => h.by === "drag" && h.value === value)
355
+ const holdStart = (value: V, from: number, by: "drag" | "lift"): void => {
356
+ held = Option.some({ value, from, by })
357
+ UndefinedOr.map(options.onHoldStart, (tell) => tell({ value, from }))
358
+ }
359
+ /** End `value`'s hold, `how`: told once, and only for the open one. */
360
+ const holdEnd = (value: V, how: ReorderHoldHow): void =>
361
+ Option.match(Option.filter(held, (h) => h.value === value), {
362
+ onNone: constVoid,
363
+ onSome: (h) => {
364
+ held = Option.none()
365
+ UndefinedOr.map(options.onHoldEnd, (tell) => tell({ values: [...options.values()], value, from: h.from, to: indexOf(value), how }))
366
+ },
367
+ })
368
+
369
+ /** The lifted item's index at its lift. */
370
+ let liftedFrom = -1
371
+ /** Drop the lifted item where it is and announce it: a drop key, or an interruption. */
372
+ const dropLifted = (): void =>
373
+ Option.match(Reorder.liftedValue(group), {
374
+ onNone: constVoid,
375
+ onSome: (value) => {
376
+ Option.match(Reorder.drop(group, value), { onNone: constVoid, onSome: (placement) => announceNow(say("drop", value, placement)) })
377
+ setLifted(value, false)
378
+ ended(value, liftedFrom)
379
+ holdEnd(value, "drop")
380
+ },
381
+ })
382
+ /**
383
+ * A drag's start ends the open hold first (#348, one at a time): a lift is dropped where it is,
384
+ * and another item's drag ends, told as if released, its own release telling nothing more.
385
+ */
386
+ const endHeld = (): void =>
387
+ Option.match(held, {
388
+ onNone: constVoid,
389
+ onSome: (h) => {
390
+ if (h.by === "lift") return dropLifted()
391
+ ended(h.value, h.from)
392
+ holdEnd(h.value, "drop")
393
+ },
394
+ })
395
+ /** Focus back on the lifted item, if a reorder took it (moving a focused node can blur it), without scrolling. */
396
+ const keepFocus = (value: V): void =>
397
+ Option.match(Option.filter(elementOf(value), (el) => el.ownerDocument.activeElement !== el), {
398
+ onNone: constVoid,
399
+ onSome: (el) => (el as HTMLElement).focus({ preventScroll: true }),
400
+ })
401
+ /** Whether focus is on `value`'s element. */
402
+ const focused = (value: V): boolean => Option.exists(elementOf(value), (el) => el.ownerDocument.activeElement === el)
403
+
404
+ const onKeyOf = (value: V) => (event: InputSource.KeyboardLikeEvent): void => {
405
+ if (event.type !== "down") return
406
+ if (Bool.not(keyboardOn())) return
407
+ // A key the page took, or one from inside the item (a text field), isn't the reorder's.
408
+ if (event.handled) return
409
+ if (Bool.not(event.fromTarget)) return
410
+ // Keys during a pointer drag are left alone.
411
+ if (Reorder.isDragging(group)) return
412
+ const lifted = Option.contains(Reorder.liftedValue(group), value)
413
+ Option.match(Reorder.keyIntent(event.key), {
414
+ onNone: constVoid,
415
+ onSome: (intent) =>
416
+ Match.value(intent).pipe(
417
+ Match.tag("Toggle", () => {
418
+ event.handle()
419
+ if (event.repeat) return
420
+ if (lifted) return dropLifted()
421
+ // Lifting another item drops the one lifted first.
422
+ dropLifted()
423
+ Option.match(Reorder.lift(group, value), {
424
+ onNone: constVoid,
425
+ onSome: (placement) => {
426
+ liftedFrom = indexOf(value)
427
+ // The values the lift began with are the group's own: a binding re-reading them,
428
+ // the same, mid-lift (a memo whose sources changed) isn't the app changing them (#348).
429
+ asked = options.values()
430
+ setLifted(value, true)
431
+ announceNow(say("lift", value, placement))
432
+ holdStart(value, liftedFrom, "lift")
433
+ },
434
+ })
435
+ }),
436
+ Match.tag("Cancel", () => {
437
+ if (Bool.not(lifted)) return
438
+ event.handle()
439
+ const placement = withReveal(value, () => Reorder.cancel(group, value))
440
+ Option.match(placement, {
441
+ onNone: constVoid,
442
+ onSome: (placement) => {
443
+ setLifted(value, false)
444
+ keepFocus(value)
445
+ announceNow(say("cancel", value, placement))
446
+ holdEnd(value, "cancel")
447
+ },
448
+ })
449
+ }),
450
+ Match.tagsExhaustive({
451
+ Step: (step) => stepLifted(event, value, lifted, step),
452
+ End: (end) => stepLifted(event, value, lifted, end),
453
+ }),
454
+ ),
455
+ })
456
+ }
457
+ /**
458
+ * `reorder`'s placement, `value`'s new place to be revealed once its items are laid out when it moved.
459
+ * Asked before the reorder: a binding that places its items as `onReorder` sets them (`makePlaced`'s, a
460
+ * Batch Reorder group's, #712) says they're laid out before the reorder returns. Moving nothing, the
461
+ * ask is taken back.
462
+ */
463
+ const withReveal = (value: V, reorder: () => Option.Option<Reorder.Placement>): Option.Option<Reorder.Placement> => {
464
+ const before = MutableRef.get(reveal)
465
+ MutableRef.set(reveal, Option.some(value))
466
+ const placement = reorder()
467
+ const moved = Option.exists(placement, (p) => p.moved)
468
+ // Laid out within it, its reveal was taken: nothing to take back.
469
+ if (Bool.and(Bool.not(moved), Option.contains(MutableRef.get(reveal), value))) MutableRef.set(reveal, before)
470
+ return placement
471
+ }
472
+ /** An arrow, Home or End: the lifted item's step, its announcement (a held arrow's thinned), its reveal. */
473
+ const stepLifted = (event: InputSource.KeyboardLikeEvent, value: V, lifted: boolean, intent: Extract<Reorder.KeyIntent, { readonly _tag: "Step" | "End" }>): void => {
474
+ // Not lifted, the arrows, Home and End keep their own work (scrolling the page).
475
+ if (Bool.not(lifted)) return
476
+ event.handle()
477
+ Option.match(withReveal(value, () => Reorder.moveLifted(group, value, intent)), {
478
+ onNone: constVoid,
479
+ onSome: (placement) => {
480
+ // A repeat before the last step was laid out moved nothing: nothing to say.
481
+ if (placement.waiting) return
482
+ if (placement.moved) keepFocus(value)
483
+ const message = say("step", value, placement)
484
+ Bool.match(event.repeat, { onTrue: () => announceLater(message), onFalse: () => announceNow(message) })
485
+ },
486
+ })
487
+ }
488
+
489
+ /** `value`'s element in view, connected: by its place on screen when its handle gives one, else by its layout box. */
490
+ const revealOf = (value: V): void => {
491
+ const el = Option.filter(elementOf(value), (e) => e.isConnected)
492
+ if (Option.isNone(el)) return
493
+ const place = Option.flatMap(Option.flatMap(keyedOf(value), (k) => k.handle), (handle) => Option.fromUndefinedOr(handle.placeOnScreen))
494
+ Option.match(place, { onNone: () => revealLayoutOf(el.value), onSome: (placeOf) => revealRectOf(el.value, placeOf()) })
495
+ }
496
+ // Every Measure pass measures every node, and a binding that places its items says when it has: either
497
+ // way the items are laid out, and a stepped item's new place with them.
498
+ const stopLaidOut = wiring.onLaidOut(() => {
499
+ resolve()
500
+ const stepped = MutableRef.get(reveal)
501
+ MutableRef.set(reveal, Option.none())
502
+ if (Option.isSome(stepped)) revealOf(stepped.value)
503
+ })
504
+
505
+ const keyedWith = UndefinedOr.match(options.keyedWith, { onUndefined: () => "their value", onDefined: identity })
506
+ // Values whose item unregistered in this task, and whether a remount was warned of.
507
+ const unregisteredThisTask = new Set<V>()
508
+ let remountReported = false
509
+
510
+ const item = (value: V, itemElement: () => Option.Option<Element>, onLift: (lifted: boolean) => void = constVoid): ReorderItemDriver => {
511
+ // The gesture's axis (core's, fixed at its start), for Auto-scroll's axis.
512
+ let gestureAxis: Reorder.Axis = "y"
513
+ let inGesture = false
514
+ // Its index at the drag's start, for its `onReorderEnd` (#257 item 3).
515
+ let draggedFrom = -1
516
+ const k: Keyed = { element: itemElement, handle: Option.none(), onLift }
517
+ keyed.set(value, k)
518
+ const end = (): void => {
519
+ inGesture = false
520
+ Reorder.dragEnd(group, value)
521
+ resetAutoScroll()
522
+ }
523
+ return {
524
+ register: (handle) => {
525
+ if (!remountReported && unregisteredThisTask.has(value)) {
526
+ remountReported = true
527
+ runtime.runFork(
528
+ Effect.logWarning(
529
+ `wardic.reorder: an item remounted with the same value in one task (key items with ${keyedWith})`,
530
+ ).pipe(Effect.annotateLogs({ value: String(value) })),
531
+ )
532
+ }
533
+ const unregister = Reorder.register(group, value, {
534
+ layoutBox: handle.layoutBox,
535
+ dragOffset: handle.dragOffset,
536
+ layoutVersion: handle.layoutVersion,
537
+ })
538
+ k.handle = Option.some(handle)
539
+ keyed.set(value, k)
540
+ return () => {
541
+ if (Option.contains(k.handle, handle)) k.handle = Option.none()
542
+ unregister()
543
+ if (unregisteredThisTask.size === 0) queueMicrotask(() => unregisteredThisTask.clear())
544
+ unregisteredThisTask.add(value)
545
+ }
546
+ },
547
+ dragAxis: () => Reorder.resolveAxis(group),
548
+ onSample: (sample) => {
549
+ if (sample.phase === "start") {
550
+ endHeld()
551
+ inGesture = true
552
+ draggedFrom = indexOf(value)
553
+ gestureAxis = Reorder.dragStart(group, value)
554
+ holdStart(value, draggedFrom, "drag")
555
+ return
556
+ }
557
+ if (sample.phase === "move") {
558
+ const velocity = Reorder.dragMove(group, value, sample.velocity)
559
+ const scroller = MutableRef.get(autoScroller)
560
+ if (Option.isSome(scroller)) {
561
+ const on = AutoScroll.axis(gestureAxis, velocity)
562
+ scroller.value.update(sample.point, on, velocity[on], Option.getOrUndefined(k.element()))
563
+ }
564
+ return
565
+ }
566
+ end()
567
+ // A drag a later hold took over ended then.
568
+ if (Bool.not(holdsDrag(value))) return
569
+ ended(value, draggedFrom)
570
+ holdEnd(value, "drop")
571
+ },
572
+ onKey: onKeyOf(value),
573
+ onBlur: () => {
574
+ if (Bool.not(Option.contains(Reorder.liftedValue(group), value))) return
575
+ // A reorder can blur the node it moves; focus was put back on it, so it's no interruption.
576
+ if (focused(value)) return
577
+ dropLifted()
578
+ },
579
+ onPress: () => {
580
+ if (Option.contains(Reorder.liftedValue(group), value)) dropLifted()
581
+ },
582
+ dispose: () => {
583
+ if (inGesture) end()
584
+ // Unmounted while lifted: the lift ends, unsaid.
585
+ if (Option.contains(Reorder.liftedValue(group), value)) Reorder.drop(group, value)
586
+ holdEnd(value, "unmount")
587
+ if (keyed.get(value) === k) keyed.delete(value)
588
+ },
589
+ }
590
+ }
591
+
592
+ return {
593
+ group,
594
+ axis: () => Option.getOrElse(appAxis(), () => detected),
595
+ attach: (el) => {
596
+ MutableRef.set(element, Option.some(el))
597
+ MutableRef.set(autoScroller, Option.some(makeAutoScroller(el, debug)))
598
+ // The live region has to be in the page before the first lift (ADR-0037, Q7).
599
+ if (keyboardOn() && Predicate.isNotUndefined(announce)) prepareAnnouncer(documentOf(el))
600
+ },
601
+ item,
602
+ move: (value, toIndex) =>
603
+ Option.match(Reorder.move(group, value, toIndex), {
604
+ onNone: constVoid,
605
+ onSome: (placement) => {
606
+ if (placement.moved) announceNow(say("move", value, placement))
607
+ },
608
+ }),
609
+ detect: () => Reorder.resolveAxis(group),
610
+ valuesChanged: () => {
611
+ // The app changed `values` itself while an item is lifted: it's dropped at its new place. The group's
612
+ // own order is told by its items, not its array: a binding reads a store's order as a new one (#721).
613
+ if (Option.isSome(Reorder.liftedValue(group)) && Bool.not(isAsked(asked, options.values()))) dropLifted()
614
+ wiring.layOut()
615
+ },
616
+ dispose: () => {
617
+ stopLaidOut()
618
+ resetAutoScroll()
619
+ },
620
+ }
621
+ }
622
+
623
+ const noop = (): void => {}
624
+
625
+ /** Whether two orders hold the same items in the same order. */
626
+ const sameOrder: Equivalence.Equivalence<ReadonlyArray<unknown>> = Arr.makeEquivalence(Equivalence.strictEqual())
627
+
628
+ /** Whether `values` is the order the group last asked for (ADR-0037, *Amendment (#721)*): its items, in its order. */
629
+ const isAsked = (asked: ReadonlyArray<unknown> | undefined, values: ReadonlyArray<unknown>): boolean =>
630
+ UndefinedOr.match(asked, { onUndefined: constFalse, onDefined: (order) => sameOrder(order, values) })
631
+
632
+ /**
633
+ * A {@link ReorderController} for a server render (ADR-0021), where there's no projection
634
+ * system to drive and nothing to reorder: its group has no values, so every operation on it
635
+ * does nothing, the axis is `"y"`, and an item's driver is inert too. A binding's server path hands it to its group, so code
636
+ * that reads `group`, `move` or `controller` at setup works there. Tier 2, for binding
637
+ * authors.
638
+ */
639
+ export const INERT_REORDER_CONTROLLER: ReorderController<never> = Object.freeze({
640
+ // A group with no values: every operation on it finds nothing to do.
641
+ group: Reorder.make<never>({ values: () => [], onReorder: noop }),
642
+ axis: () => "y" as const,
643
+ attach: noop,
644
+ item: () => ({ register: () => noop, dragAxis: () => "y" as const, onSample: noop, onKey: noop, onBlur: noop, onPress: noop, dispose: noop }),
645
+ move: noop,
646
+ detect: () => "y" as const,
647
+ valuesChanged: noop,
648
+ dispose: noop,
649
+ })
650
+