stimeo-ui 0.11.0 → 0.13.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 (153) hide show
  1. package/CHANGELOG.md +107 -0
  2. package/dist/cable/index.d.ts +67 -13
  3. package/dist/cable/index.js +461 -154
  4. package/dist/cable/index.js.map +1 -1
  5. package/dist/controllers/alert_dialog_controller.d.ts +1 -1
  6. package/dist/controllers/alert_dialog_controller.js.map +1 -1
  7. package/dist/controllers/announcer_controller.d.ts +1 -1
  8. package/dist/controllers/announcer_controller.js.map +1 -1
  9. package/dist/controllers/auto_submit_controller.js.map +1 -1
  10. package/dist/controllers/breadcrumb_controller.d.ts +1 -1
  11. package/dist/controllers/breadcrumb_controller.js.map +1 -1
  12. package/dist/controllers/calendar_controller.d.ts +2 -1
  13. package/dist/controllers/calendar_controller.js +10 -2
  14. package/dist/controllers/calendar_controller.js.map +1 -1
  15. package/dist/controllers/carousel_controller.js +1 -1
  16. package/dist/controllers/carousel_controller.js.map +1 -1
  17. package/dist/controllers/character_counter_controller.js.map +1 -1
  18. package/dist/controllers/clipboard_controller.d.ts +1 -1
  19. package/dist/controllers/clipboard_controller.js.map +1 -1
  20. package/dist/controllers/collapsible_controller.js.map +1 -1
  21. package/dist/controllers/color_picker_controller.d.ts +1 -1
  22. package/dist/controllers/color_picker_controller.js.map +1 -1
  23. package/dist/controllers/command_palette_controller.d.ts +3 -3
  24. package/dist/controllers/command_palette_controller.js +2 -2
  25. package/dist/controllers/command_palette_controller.js.map +1 -1
  26. package/dist/controllers/conditional_fields_controller.js.map +1 -1
  27. package/dist/controllers/confirm_controller.d.ts +2 -2
  28. package/dist/controllers/confirm_controller.js.map +1 -1
  29. package/dist/controllers/context_menu_controller.d.ts +1 -1
  30. package/dist/controllers/context_menu_controller.js.map +1 -1
  31. package/dist/controllers/count_up_controller.d.ts +12 -10
  32. package/dist/controllers/count_up_controller.js +74 -35
  33. package/dist/controllers/count_up_controller.js.map +1 -1
  34. package/dist/controllers/countdown_controller.d.ts +1 -1
  35. package/dist/controllers/countdown_controller.js.map +1 -1
  36. package/dist/controllers/data_grid_controller.d.ts +1 -1
  37. package/dist/controllers/data_grid_controller.js.map +1 -1
  38. package/dist/controllers/date_range_picker_controller.js +10 -10
  39. package/dist/controllers/date_range_picker_controller.js.map +1 -1
  40. package/dist/controllers/dialog_controller.d.ts +1 -1
  41. package/dist/controllers/dialog_controller.js.map +1 -1
  42. package/dist/controllers/direct_upload_controller.js.map +1 -1
  43. package/dist/controllers/dirty_form_controller.js.map +1 -1
  44. package/dist/controllers/dismissible_controller.js.map +1 -1
  45. package/dist/controllers/drawer_controller.d.ts +2 -2
  46. package/dist/controllers/drawer_controller.js +2 -2
  47. package/dist/controllers/drawer_controller.js.map +1 -1
  48. package/dist/controllers/dropdown_controller.d.ts +1 -1
  49. package/dist/controllers/dropdown_controller.js.map +1 -1
  50. package/dist/controllers/file_dropzone_controller.js.map +1 -1
  51. package/dist/controllers/flash_controller.js.map +1 -1
  52. package/dist/controllers/focus_controller.d.ts +2 -2
  53. package/dist/controllers/focus_controller.js.map +1 -1
  54. package/dist/controllers/form_validation_controller.js.map +1 -1
  55. package/dist/controllers/frame_loading_controller.d.ts +3 -3
  56. package/dist/controllers/frame_loading_controller.js.map +1 -1
  57. package/dist/controllers/highlight_controller.js.map +1 -1
  58. package/dist/controllers/hover_card_controller.d.ts +1 -1
  59. package/dist/controllers/hover_card_controller.js.map +1 -1
  60. package/dist/controllers/idle_controller.d.ts +1 -1
  61. package/dist/controllers/idle_controller.js.map +1 -1
  62. package/dist/controllers/input_mask_controller.js.map +1 -1
  63. package/dist/controllers/intersection_controller.d.ts +20 -7
  64. package/dist/controllers/intersection_controller.js +55 -8
  65. package/dist/controllers/intersection_controller.js.map +1 -1
  66. package/dist/controllers/lazy_frame_controller.d.ts +31 -9
  67. package/dist/controllers/lazy_frame_controller.js +81 -19
  68. package/dist/controllers/lazy_frame_controller.js.map +1 -1
  69. package/dist/controllers/listbox_controller.js.map +1 -1
  70. package/dist/controllers/local_time_controller.d.ts +1 -1
  71. package/dist/controllers/local_time_controller.js.map +1 -1
  72. package/dist/controllers/masonry_controller.d.ts +1 -1
  73. package/dist/controllers/masonry_controller.js.map +1 -1
  74. package/dist/controllers/menu_controller.d.ts +1 -1
  75. package/dist/controllers/menu_controller.js.map +1 -1
  76. package/dist/controllers/menubar_controller.d.ts +2 -2
  77. package/dist/controllers/menubar_controller.js.map +1 -1
  78. package/dist/controllers/navigation_menu_controller.d.ts +1 -1
  79. package/dist/controllers/navigation_menu_controller.js.map +1 -1
  80. package/dist/controllers/nested_form_controller.js.map +1 -1
  81. package/dist/controllers/network_status_controller.js.map +1 -1
  82. package/dist/controllers/number_input_controller.js.map +1 -1
  83. package/dist/controllers/optimistic_controller.d.ts +23 -10
  84. package/dist/controllers/optimistic_controller.js +141 -20
  85. package/dist/controllers/optimistic_controller.js.map +1 -1
  86. package/dist/controllers/overflow_indicator_controller.d.ts +1 -1
  87. package/dist/controllers/overflow_indicator_controller.js.map +1 -1
  88. package/dist/controllers/overflow_menu_controller.js.map +1 -1
  89. package/dist/controllers/pagination_controller.js.map +1 -1
  90. package/dist/controllers/password_reveal_controller.js.map +1 -1
  91. package/dist/controllers/password_strength_controller.d.ts +2 -1
  92. package/dist/controllers/password_strength_controller.js.map +1 -1
  93. package/dist/controllers/persist_controller.js.map +1 -1
  94. package/dist/controllers/pointer_drag_controller.js +4 -0
  95. package/dist/controllers/pointer_drag_controller.js.map +1 -1
  96. package/dist/controllers/popover_controller.d.ts +1 -1
  97. package/dist/controllers/popover_controller.js.map +1 -1
  98. package/dist/controllers/portal_controller.d.ts +2 -2
  99. package/dist/controllers/portal_controller.js.map +1 -1
  100. package/dist/controllers/radio_group_controller.d.ts +1 -1
  101. package/dist/controllers/radio_group_controller.js.map +1 -1
  102. package/dist/controllers/reading_progress_controller.d.ts +18 -9
  103. package/dist/controllers/reading_progress_controller.js +177 -6
  104. package/dist/controllers/reading_progress_controller.js.map +1 -1
  105. package/dist/controllers/relative_time_controller.d.ts +3 -3
  106. package/dist/controllers/relative_time_controller.js.map +1 -1
  107. package/dist/controllers/roving_controller.d.ts +1 -1
  108. package/dist/controllers/roving_controller.js.map +1 -1
  109. package/dist/controllers/scroll_area_controller.js.map +1 -1
  110. package/dist/controllers/scrollspy_controller.js +13 -2
  111. package/dist/controllers/scrollspy_controller.js.map +1 -1
  112. package/dist/controllers/sidebar_controller.d.ts +1 -1
  113. package/dist/controllers/sidebar_controller.js.map +1 -1
  114. package/dist/controllers/skeleton_controller.d.ts +2 -2
  115. package/dist/controllers/skeleton_controller.js.map +1 -1
  116. package/dist/controllers/smart_sticky_header_controller.d.ts +17 -8
  117. package/dist/controllers/smart_sticky_header_controller.js +52 -10
  118. package/dist/controllers/smart_sticky_header_controller.js.map +1 -1
  119. package/dist/controllers/sortable_controller.d.ts +38 -8
  120. package/dist/controllers/sortable_controller.js +241 -67
  121. package/dist/controllers/sortable_controller.js.map +1 -1
  122. package/dist/controllers/spinner_controller.d.ts +3 -3
  123. package/dist/controllers/spinner_controller.js.map +1 -1
  124. package/dist/controllers/sticky_observer_controller.js +13 -2
  125. package/dist/controllers/sticky_observer_controller.js.map +1 -1
  126. package/dist/controllers/submit_once_controller.d.ts +3 -3
  127. package/dist/controllers/submit_once_controller.js.map +1 -1
  128. package/dist/controllers/switch_controller.js.map +1 -1
  129. package/dist/controllers/textarea_autosize_controller.d.ts +1 -1
  130. package/dist/controllers/textarea_autosize_controller.js.map +1 -1
  131. package/dist/controllers/theme_controller.d.ts +3 -2
  132. package/dist/controllers/theme_controller.js.map +1 -1
  133. package/dist/controllers/toast_controller.js +1 -1
  134. package/dist/controllers/toast_controller.js.map +1 -1
  135. package/dist/controllers/toggle_group_controller.js.map +1 -1
  136. package/dist/controllers/toolbar_controller.d.ts +1 -1
  137. package/dist/controllers/toolbar_controller.js.map +1 -1
  138. package/dist/controllers/tooltip_controller.d.ts +1 -1
  139. package/dist/controllers/tooltip_controller.js.map +1 -1
  140. package/dist/controllers/transition_controller.d.ts +1 -1
  141. package/dist/controllers/transition_controller.js.map +1 -1
  142. package/dist/controllers/tree_view_controller.d.ts +4 -3
  143. package/dist/controllers/tree_view_controller.js.map +1 -1
  144. package/dist/index.js +587 -183
  145. package/dist/index.js.map +1 -1
  146. package/dist/inspector/cli.d.ts +1 -1
  147. package/dist/inspector/cli.js +2 -1
  148. package/dist/inspector/cli.js.map +1 -1
  149. package/dist/inspector/cli_bin.js +2 -1
  150. package/dist/inspector/cli_bin.js.map +1 -1
  151. package/dist/inspector/examples.json +2 -2
  152. package/dist/inspector/manifest.json +13 -30
  153. package/package.json +1 -1
@@ -2,21 +2,78 @@ import { Controller } from '@hotwired/stimulus';
2
2
 
3
3
  // src/controllers/sortable_controller.ts
4
4
 
5
+ // src/utils/announce.ts
6
+ function announce(message, options = {}) {
7
+ const text = message.trim();
8
+ if (text.length === 0) return;
9
+ window.dispatchEvent(
10
+ new CustomEvent("stimeo--announcer:announce", {
11
+ detail: { message: text, assertive: options.assertive === true }
12
+ })
13
+ );
14
+ }
15
+ function fillTemplate(template, values) {
16
+ return template.replace(/\{([a-zA-Z][a-zA-Z0-9]*)\}/g, (match, name) => {
17
+ const replacement = values[name];
18
+ return replacement === void 0 ? match : String(replacement);
19
+ });
20
+ }
21
+
5
22
  // src/utils/logical_scroll.ts
6
23
  function isRtl(element) {
7
24
  return window.getComputedStyle(element).direction === "rtl";
8
25
  }
9
26
 
27
+ // src/utils/microtask_coalescer.ts
28
+ var MicrotaskCoalescer = class {
29
+ #run;
30
+ #queued = false;
31
+ #active = false;
32
+ #generation = 0;
33
+ /** @param run - the single reconciliation pass, invoked at most once per batch. */
34
+ constructor(run) {
35
+ this.#run = run;
36
+ }
37
+ /** Opens the window in which {@link schedule} is honoured; call from `connect()`. */
38
+ activate() {
39
+ this.#active = true;
40
+ }
41
+ /** Closes the window and drops any pending pass; call from `disconnect()`. */
42
+ cancel() {
43
+ this.#active = false;
44
+ this.#queued = false;
45
+ this.#generation += 1;
46
+ }
47
+ /** Requests one pass after the batch settles. Idempotent; inert outside the window. */
48
+ schedule() {
49
+ if (!this.#active || this.#queued) return;
50
+ this.#queued = true;
51
+ const generation = this.#generation;
52
+ queueMicrotask(() => {
53
+ if (generation !== this.#generation || !this.#queued || !this.#active) return;
54
+ this.#queued = false;
55
+ this.#run();
56
+ });
57
+ }
58
+ };
59
+
10
60
  // src/controllers/sortable_controller.ts
11
61
  var SortableController = class extends Controller {
12
- static targets = ["list", "item", "status"];
62
+ static targets = ["list", "item"];
13
63
  static values = {
14
- orientation: { type: String, default: "vertical" }
64
+ orientation: { type: String, default: "vertical" },
65
+ announceGrabbedText: { type: String, default: "" },
66
+ announceMovedText: { type: String, default: "" },
67
+ announceDroppedText: { type: String, default: "" },
68
+ announceCanceledText: { type: String, default: "" }
15
69
  };
16
70
  static events = ["reorder"];
17
71
  #session = null;
72
+ /** Defers the lost-item check to after the mutation batch (see below). */
73
+ #settle = new MicrotaskCoalescer(() => this.#dropLostSession());
18
74
  connect() {
19
75
  this.element.removeAttribute("data-sortable-dragging");
76
+ this.#settle.activate();
20
77
  this.element.addEventListener("stimeo--pointer-drag:start", this.#onDragStart);
21
78
  this.element.addEventListener("stimeo--pointer-drag:move", this.#onDragMove);
22
79
  this.element.addEventListener("stimeo--pointer-drag:end", this.#onDragEnd);
@@ -27,49 +84,121 @@ var SortableController = class extends Controller {
27
84
  this.element.removeEventListener("stimeo--pointer-drag:move", this.#onDragMove);
28
85
  this.element.removeEventListener("stimeo--pointer-drag:end", this.#onDragEnd);
29
86
  this.element.removeEventListener("stimeo--pointer-drag:cancel", this.#onDragCancel);
87
+ this.#settle.cancel();
30
88
  this.#session = null;
31
89
  this.element.removeAttribute("data-sortable-dragging");
32
90
  }
33
- /** Picks the item up: remembers its origin and announces the grab. */
91
+ /**
92
+ * Ends a session whose item left the item set.
93
+ *
94
+ * The controller's own reorder detaches and reattaches the item inside one
95
+ * mutation batch, so the loss is only real once the batch has settled — the
96
+ * check therefore runs a microtask later and asks whether the item is a target
97
+ * again. Without it a deleted row (a broadcast that drops it from the board)
98
+ * or a morph that strips the item's target attribute would hold the
99
+ * one-at-a-time session for the rest of the page's life, with the root hook
100
+ * stuck on and every later grab refused with no way out.
101
+ */
102
+ itemTargetDisconnected(item) {
103
+ if (this.#session?.item !== item) return;
104
+ this.#settle.schedule();
105
+ }
106
+ #dropLostSession() {
107
+ const session = this.#session;
108
+ if (!session) return;
109
+ const items = this.#items();
110
+ if (items.includes(session.item)) return;
111
+ this.#session = null;
112
+ this.element.removeAttribute("data-sortable-dragging");
113
+ if (session.item.isConnected) this.#restore(session, items);
114
+ }
115
+ /** Picks the item up: remembers its neighbours and announces the grab. */
34
116
  #onDragStart = (event) => {
35
117
  if (this.#session) return;
36
- const item = this.#itemFor(event.target);
118
+ const items = this.#items();
119
+ const item = this.#itemFor(event.target, items);
37
120
  if (!item) return;
38
- this.#session = { item, from: this.#items().indexOf(item), lastPrimary: 0 };
121
+ const index = items.indexOf(item);
122
+ this.#session = {
123
+ item,
124
+ anchor: items[index + 1] ?? null,
125
+ predecessor: items[index - 1] ?? null,
126
+ lastPrimary: 0
127
+ };
39
128
  this.element.setAttribute("data-sortable-dragging", "true");
40
129
  this.#announce("grabbed", item);
41
130
  };
42
131
  #onDragMove = (event) => {
43
132
  const session = this.#session;
133
+ if (!session) return;
134
+ const items = this.#items();
135
+ if (this.#itemFor(event.target, items) !== session.item) return;
44
136
  const detail = event.detail;
45
- if (!session || this.#itemFor(event.target) !== session.item) return;
46
137
  if (detail.pointerType === "keyboard") {
47
- this.#stepFromKeyboard(session, detail);
138
+ this.#stepFromKeyboard(session, detail, items);
48
139
  } else {
49
- this.#followPointer(session, detail);
140
+ this.#followPointer(session, detail, items);
50
141
  }
51
142
  };
52
143
  /** Drops the item: announces, then reports `reorder` if the position changed. */
53
144
  #onDragEnd = (event) => {
54
145
  const session = this.#session;
55
- if (!session || this.#itemFor(event.target) !== session.item) return;
146
+ if (!session) return;
147
+ const items = this.#items();
148
+ if (this.#itemFor(event.target, items) !== session.item) return;
56
149
  this.#session = null;
57
150
  this.element.removeAttribute("data-sortable-dragging");
58
151
  this.#announce("dropped", session.item);
59
- const to = this.#items().indexOf(session.item);
60
- if (to !== session.from) {
61
- this.dispatch("reorder", { detail: { item: session.item, from: session.from, to } });
152
+ const to = items.indexOf(session.item);
153
+ const from = this.#pickupSlot(session, items) ?? to;
154
+ if (to !== from) {
155
+ this.dispatch("reorder", { detail: { item: session.item, from, to } });
62
156
  }
63
157
  };
64
158
  /** Restores the pickup position (Escape / OS `pointercancel`). */
65
159
  #onDragCancel = (event) => {
66
160
  const session = this.#session;
67
- if (!session || this.#itemFor(event.target) !== session.item) return;
161
+ if (!session) return;
162
+ const items = this.#items();
163
+ if (this.#itemFor(event.target, items) !== session.item) return;
68
164
  this.#session = null;
69
165
  this.element.removeAttribute("data-sortable-dragging");
70
- this.#moveTo(session.item, session.from);
166
+ this.#restore(session, items);
71
167
  this.#announce("canceled", session.item);
72
168
  };
169
+ /**
170
+ * The slot the item was picked up from, read back through the neighbours it
171
+ * had then — `null` when neither survives and the item itself is gone.
172
+ *
173
+ * The anchor is the item that followed it, so restoring means "before that one
174
+ * again". When the anchor was deleted mid-drag the predecessor answers the
175
+ * same question from the other side. With both gone the item's current slot is
176
+ * the honest answer: nothing is known to have moved, so no reorder is reported
177
+ * and a cancel leaves the item where it is.
178
+ */
179
+ #pickupSlot(session, items) {
180
+ const others = items.filter((candidate) => candidate !== session.item);
181
+ if (session.anchor) {
182
+ const at = others.indexOf(session.anchor);
183
+ if (at !== -1) return at;
184
+ }
185
+ if (session.predecessor) {
186
+ const at = others.indexOf(session.predecessor);
187
+ if (at !== -1) return at + 1;
188
+ }
189
+ const here = items.indexOf(session.item);
190
+ return here === -1 ? null : here;
191
+ }
192
+ /** Puts the item back where it was picked up from. */
193
+ #restore(session, items) {
194
+ const slot = this.#pickupSlot(session, items);
195
+ if (slot === null) return;
196
+ this.#insertAt(
197
+ session.item,
198
+ items.filter((candidate) => candidate !== session.item),
199
+ slot
200
+ );
201
+ }
73
202
  /**
74
203
  * Keyboard stepping: `pointer-drag` reports *cumulative* synthetic deltas, so
75
204
  * the difference from the last consumed value is one arrow press — its sign is
@@ -81,77 +210,116 @@ var SortableController = class extends Controller {
81
210
  * `roving` already moves focus logically, so the same arrow would send the
82
211
  * focus and the grabbed item opposite ways.
83
212
  */
84
- #stepFromKeyboard(session, detail) {
213
+ #stepFromKeyboard(session, detail, items) {
85
214
  const primary = Number(this.#isVertical ? detail.dy : detail.dx) || 0;
86
215
  const delta = primary - session.lastPrimary;
87
216
  session.lastPrimary = primary;
88
217
  if (delta === 0) return;
89
- const items = this.#items();
90
218
  const index = items.indexOf(session.item);
91
219
  const step = (delta > 0 ? 1 : -1) * (this.#isReversed ? -1 : 1);
92
220
  const next = Math.max(0, Math.min(index + step, items.length - 1));
93
221
  if (next === index) return;
94
- this.#moveTo(session.item, next);
222
+ this.#insertAt(
223
+ session.item,
224
+ items.filter((candidate) => candidate !== session.item),
225
+ next
226
+ );
95
227
  this.#announce("moved", session.item);
96
228
  }
97
229
  /**
98
230
  * Pointer following: the item moves to the slot whose siblings' midpoints the
99
- * pointer has passed (per `orientation`). Skipped when the list has no layout
100
- * geometry (every rect is zero — nothing meaningful to compare against).
231
+ * pointer has passed (per `orientation`).
232
+ *
233
+ * Only siblings that occupy space take part. A row with no layout box — a
234
+ * filtered-out item, a `display: none` ancestor, a collapsed `<details>` — is
235
+ * reported with an empty rect at the document origin, and its midpoint of `0`
236
+ * sits below every pointer position: counted, it would read as passed on the
237
+ * very first move and send the item across a slot the pointer never crossed.
238
+ * With no laid-out sibling at all there is nothing to compare against and the
239
+ * move is skipped entirely.
101
240
  */
102
- #followPointer(session, detail) {
241
+ #followPointer(session, detail, items) {
103
242
  const pointer = Number(this.#isVertical ? detail.y : detail.x) || 0;
104
- const others = this.#items().filter((item) => item !== session.item);
105
- if (others.length === 0) return;
106
- let laidOut = false;
107
- let target = 0;
243
+ const vertical = this.#isVertical;
108
244
  const reversed = this.#isReversed;
109
- for (const other of others) {
110
- const rect = other.getBoundingClientRect();
111
- if (rect.width > 0 || rect.height > 0) laidOut = true;
112
- const midpoint = this.#isVertical ? rect.top + rect.height / 2 : rect.left + rect.width / 2;
113
- const precedes = reversed ? pointer < midpoint : pointer > midpoint;
114
- if (precedes) target += 1;
115
- }
116
- if (!laidOut) return;
117
- const current = this.#items().indexOf(session.item);
118
- if (target !== current) {
119
- this.#moveTo(session.item, target);
120
- this.#announce("moved", session.item);
121
- }
245
+ const here = items.indexOf(session.item);
246
+ const laidOut = [];
247
+ let target = 0;
248
+ let current = 0;
249
+ items.forEach((item, index) => {
250
+ if (item === session.item) return;
251
+ const rect = item.getBoundingClientRect();
252
+ if (rect.width === 0 && rect.height === 0) return;
253
+ if (index < here) current += 1;
254
+ laidOut.push(item);
255
+ const midpoint = vertical ? rect.top + rect.height / 2 : rect.left + rect.width / 2;
256
+ if (reversed ? pointer < midpoint : pointer > midpoint) target += 1;
257
+ });
258
+ if (laidOut.length === 0 || target === current) return;
259
+ this.#insertAt(session.item, laidOut, target);
260
+ this.#announce("moved", session.item);
122
261
  }
123
- /** Reinserts `item` so it lands at `index` among the list's items. */
124
- #moveTo(item, index) {
125
- const others = this.#items().filter((candidate) => candidate !== item);
126
- const clamped = Math.max(0, Math.min(index, others.length));
127
- const reference = others[clamped] ?? null;
262
+ /**
263
+ * Reinserts `item` at `index` among `scope`, relative to the sibling already
264
+ * standing there.
265
+ *
266
+ * The reorder is defined against the items, not against a container: the
267
+ * neighbour's own parent is where the item belongs. A `list` target that is
268
+ * not the items' parent — or none at all, which the markup contract allows —
269
+ * therefore still lands the move in the right place instead of throwing, and
270
+ * the last slot is *after the last item* rather than after whatever else the
271
+ * container holds (a live region, a footer), which would put the row outside
272
+ * the reading order the list publishes.
273
+ */
274
+ #insertAt(item, scope, index) {
275
+ const clamped = Math.max(0, Math.min(index, scope.length));
276
+ const ahead = scope[clamped] ?? null;
277
+ const behind = ahead ? null : scope[scope.length - 1] ?? null;
278
+ const neighbour = ahead ?? behind;
279
+ if (!neighbour) return;
280
+ const parent = neighbour.parentNode;
281
+ if (!parent) return;
128
282
  const active = document.activeElement;
129
283
  const hadFocus = active instanceof HTMLElement && item.contains(active);
130
- this.#list.insertBefore(item, reference);
284
+ parent.insertBefore(item, ahead ?? neighbour.nextSibling);
131
285
  if (hadFocus) active.focus();
132
286
  }
133
287
  /**
134
- * Mirrors a step into the `status` live region. Copy is localizable through
135
- * `data-grabbed` / `data-moved` / `data-dropped` / `data-canceled` templates on
136
- * the status element (`%{name}` / `%{position}` / `%{total}` placeholders);
137
- * terse English is the fallback.
288
+ * Hands one step to the page's shared announcer.
289
+ *
290
+ * The library carries no live region and no English copy: the wording is the
291
+ * consumer's, written into `announceGrabbedText` / `announceMovedText` /
292
+ * `announceDroppedText` / `announceCanceledText` with `{name}` / `{position}` /
293
+ * `{total}` placeholders, and an unset one announces nothing.
294
+ *
295
+ * Only transitions reach here — the pickup, a step that actually changed the
296
+ * landing slot, and the single end of the session — so a pointer crossing the
297
+ * same slot twice or an arrow clamped at an end stays silent.
138
298
  */
139
299
  #announce(key, item) {
140
- if (!this.hasStatusTarget) return;
141
- const position = String(this.#items().indexOf(item) + 1);
142
- const total = String(this.#items().length);
143
- const name = this.#nameOf(item);
144
- const fallback = {
145
- grabbed: `Grabbed ${name}, position ${position} of ${total}`,
146
- moved: `${name}, position ${position} of ${total}`,
147
- dropped: `Dropped ${name} at position ${position} of ${total}`,
148
- canceled: `Reorder canceled, ${name} returned to position ${position} of ${total}`
149
- };
150
- const values = { name, position, total };
151
- const template = this.statusTarget.dataset[key];
152
- this.statusTarget.textContent = template ? template.replace(/%\{(name|position|total)\}/g, (match, token) => {
153
- return values[token] ?? match;
154
- }) : fallback[key];
300
+ const template = this.#announceTemplate(key);
301
+ if (template.length === 0) return;
302
+ const items = this.#items();
303
+ announce(
304
+ fillTemplate(template, {
305
+ name: this.#nameOf(item),
306
+ position: items.indexOf(item) + 1,
307
+ total: items.length
308
+ })
309
+ );
310
+ }
311
+ /** The consumer's wording for one step, or `""` when they authored none. */
312
+ #announceTemplate(key) {
313
+ switch (key) {
314
+ case "grabbed":
315
+ return this.announceGrabbedTextValue;
316
+ case "moved":
317
+ return this.announceMovedTextValue;
318
+ case "dropped":
319
+ return this.announceDroppedTextValue;
320
+ case "canceled":
321
+ return this.announceCanceledTextValue;
322
+ }
155
323
  }
156
324
  /** The announced item name: the authored override, else its collapsed text. */
157
325
  #nameOf(item) {
@@ -159,11 +327,17 @@ var SortableController = class extends Controller {
159
327
  if (authored) return authored;
160
328
  return (item.textContent ?? "").replace(/\s+/g, " ").trim();
161
329
  }
162
- /** Resolves the sortable item owning a bubbled `pointer-drag` event. */
163
- #itemFor(target) {
164
- const node = target;
165
- if (!node) return null;
166
- return this.#items().find((item) => item === node || item.contains(node)) ?? null;
330
+ /**
331
+ * Resolves the sortable item owning a bubbled `pointer-drag` event.
332
+ *
333
+ * `pointer-drag` dispatches on its own element, and the markup contract puts
334
+ * one on each item, so the owner is the item that **is** the target. Matching
335
+ * an ancestor instead would make a card's own inner draggable — a knob, a
336
+ * split pane, anything the primitive is composed into — drive the card.
337
+ */
338
+ #itemFor(target, items) {
339
+ if (!target) return null;
340
+ return items.find((item) => item === target) ?? null;
167
341
  }
168
342
  /** The items in live DOM order (targets re-query the DOM on every access). */
169
343
  #items() {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utils/logical_scroll.ts","../../src/controllers/sortable_controller.ts"],"names":[],"mappings":";;;;;AAiBO,SAAS,MAAM,OAAA,EAA2B;AAC/C,EAAA,OAAO,MAAA,CAAO,gBAAA,CAAiB,OAAO,CAAA,CAAE,SAAA,KAAc,KAAA;AACxD;;;ACoDO,IAAM,kBAAA,GAAN,cAAiC,UAAA,CAAwB;AAAA,EAC9D,OAAgB,OAAA,GAAU,CAAC,MAAA,EAAQ,QAAQ,QAAQ,CAAA;AAA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A;AAC/D,IAAA,IAAI,UAAU,OAAO,QAAA;AACrB,IAAA,OAAA,CAAQ,KAAK,WAAA,IAAe,EAAA,EAAI,QAAQ,MAAA,EAAQ,GAAG,EAAE,IAAA,EAAK;AAAA,EAC5D;AAAA;AAAA,EAGA,SAAS,MAAA,EAAgD;AACvD,IAAA,MAAM,IAAA,GAAO,MAAA;AACb,IAAA,IAAI,CAAC,MAAM,OAAO,IAAA;AAClB,IAAA,OAAO,IAAA,CAAK,MAAA,EAAO,CAAE,IAAA,CAAK,CAAC,IAAA,KAAS,IAAA,KAAS,IAAA,IAAQ,IAAA,CAAK,QAAA,CAAS,IAAI,CAAC,CAAA,IAAK,IAAA;AAAA,EAC/E;AAAA;AAAA,EAGA,MAAA,GAAwB;AACtB,IAAA,OAAO,IAAA,CAAK,WAAA;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,KAAA,GAAqB;AACvB,IAAA,OAAO,IAAA,CAAK,aAAA,GAAgB,IAAA,CAAK,UAAA,GAAa,IAAA,CAAK,OAAA;AAAA,EACrD;AAAA,EAEA,IAAI,WAAA,GAAuB;AACzB,IAAA,OAAO,KAAK,gBAAA,KAAqB,YAAA;AAAA,EACnC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcA,IAAI,WAAA,GAAuB;AACzB,IAAA,OAAO,CAAC,IAAA,CAAK,WAAA,IAAe,KAAA,CAAM,KAAK,KAAK,CAAA;AAAA,EAC9C;AACF","file":"sortable_controller.js","sourcesContent":["/** Normalized scroll position and maximum distance on one logical axis. */\nexport interface LogicalScrollMetrics {\n position: number;\n max: number;\n}\n\n/**\n * Whether horizontal scrolling on `element` follows right-to-left inline flow.\n *\n * Resolved from the **computed** `direction`, so the authoring contract is the\n * usual `dir=\"rtl\"` (or a stylesheet) on the element or any ancestor.\n *\n * Scope: horizontal writing modes. A vertical writing mode (`writing-mode:\n * vertical-rl`) also inverts the horizontal axis, which this check does not\n * model — vertical writing modes are out of scope for the scroll utilities\n * (their consumers describe axes as horizontal/vertical, not inline/block).\n */\nexport function isRtl(element: Element): boolean {\n return window.getComputedStyle(element).direction === \"rtl\";\n}\n\n/**\n * Returns scroll distance from the logical start edge.\n *\n * CSSOM View exposes standards-mode RTL horizontal offsets as `0` at the inline\n * start (right) and increasingly negative values toward the inline end (left).\n * The normalized position is always clamped to `[0, max]`, which also absorbs\n * Safari's elastic overscroll values.\n */\nexport function logicalScrollMetrics(\n element: HTMLElement,\n horizontal: boolean,\n): LogicalScrollMetrics {\n const max = Math.max(\n 0,\n horizontal\n ? element.scrollWidth - element.clientWidth\n : element.scrollHeight - element.clientHeight,\n );\n const raw = horizontal ? element.scrollLeft : element.scrollTop;\n const position = horizontal && isRtl(element) ? -raw : raw;\n return { position: Math.min(max, Math.max(0, position)), max };\n}\n\n/**\n * Converts a logical start/end delta to the physical value accepted by\n * `Element.scrollBy`.\n */\nexport function physicalScrollDelta(\n element: HTMLElement,\n horizontal: boolean,\n logicalDelta: number,\n): number {\n return horizontal && isRtl(element) ? -logicalDelta : logicalDelta;\n}\n","import { Controller } from \"@hotwired/stimulus\";\nimport { isRtl } from \"../utils/logical_scroll\";\n\n/** A reorder in flight: one item picked up by pointer or keyboard. */\ninterface SortSession {\n item: HTMLElement;\n /** Index at pickup, for the `reorder` detail and the cancel restore. */\n from: number;\n /** Last primary-axis cumulative delta consumed by keyboard stepping. */\n lastPrimary: number;\n}\n\n/**\n * Headless, accessible **sortable** (single-list reorder) — the a11y-first\n * drag-and-drop that is the flagship consumer of the\n * `stimeo--pointer-drag` primitive. The composition is markup-level and each\n * layer keeps its job: `pointer-drag` (on every item) emits the normalized drag\n * signal with its built-in keyboard alternative, `roving` (on the list) keeps\n * the handles a single Tab stop, and this controller interprets the signal —\n * live-reordering the DOM, announcing each step through a `status` live region,\n * and reporting the final `reorder`. No dedicated APG pattern exists for\n * drag-and-drop; the keyboard model (grab → arrows → drop / Escape) comes from\n * `pointer-drag` and the announcements make it non-visually trackable\n * (WCAG 2.1.1 / 2.5.7 / 4.1.3). Core (zero dependencies).\n *\n * Markup contract (identifier: `stimeo--sortable`):\n * <div data-controller=\"stimeo--sortable\">\n * <ul data-stimeo--sortable-target=\"list\" data-controller=\"stimeo--roving\"\n * data-stimeo--roving-orientation-value=\"vertical\">\n * <li data-stimeo--sortable-target=\"item\" data-stimeo--sortable-name=\"Card A\"\n * data-controller=\"stimeo--pointer-drag\"\n * data-stimeo--pointer-drag-axis-value=\"y\">\n * <span>Card A</span>\n * <button type=\"button\" aria-label=\"Reorder Card A\"\n * data-stimeo--pointer-drag-target=\"handle\"\n * data-stimeo--roving-target=\"item\">⠿</button>\n * </li>\n * …\n * </ul>\n * <span role=\"status\" data-stimeo--sortable-target=\"status\"></span>\n * </div>\n *\n * The composed values must follow the sort axis: roving's `orientation`\n * (which defaults to `horizontal`, so a vertical list has to author\n * `vertical`) and pointer-drag's `axis`. `stimeo check` enforces both\n * alignments as composition rules (`composition-mismatch`).\n *\n * Pointer flow: dragging an item live-moves it whenever the pointer crosses a\n * sibling's midpoint (per `orientation`). Keyboard flow: each synthetic\n * `pointer-drag` move steps the item one position. A horizontal list under\n * `dir=\"rtl\"` reverses both, since DOM order then runs right-to-left while\n * `pointer-drag` reports physical coordinates. Dropping dispatches\n * `reorder` (`{ item, from, to }`, zero-based) when the position changed;\n * Escape / `pointercancel` restores the pickup position. Every step is mirrored\n * into the `status` live region (localizable via `data-grabbed` / `data-moved`\n * / `data-dropped` / `data-canceled` templates with `%{name}` / `%{position}` /\n * `%{total}` placeholders; terse English is the fallback).\n *\n * @remarks\n * Behavior only — the ghost/placeholder/drop-hint visuals are the consumer's\n * CSS, keyed off `pointer-drag`'s `data-dragging`/`data-grabbed` on the item\n * and this controller's `data-sortable-dragging` on the root. Persistence is\n * the consumer's `reorder` listener (post the new position; a Turbo Stream\n * broadcast makes the board collaborative). The DOM order is the single source\n * of truth: items are read live on every event, so rows appended by Turbo need\n * no rewiring, and a Turbo cache restore reconnects idempotently (`connect()`\n * clears the transient root hook; a drag cannot survive a navigation). Only the\n * event listeners are held, and they are removed on `disconnect()`. Multi-list\n * / kanban, nested DnD, auto-scroll at the edges, and virtualized lists are\n * deliberately out of this single-list scope.\n */\nexport class SortableController extends Controller<HTMLElement> {\n static override targets = [\"list\", \"item\", \"status\"];\n static override values = {\n orientation: { type: String, default: \"vertical\" },\n };\n static events = [\"reorder\"] as const;\n\n declare readonly hasListTarget: boolean;\n declare readonly listTarget: HTMLElement;\n declare readonly itemTargets: HTMLElement[];\n declare readonly hasStatusTarget: boolean;\n declare readonly statusTarget: HTMLElement;\n declare orientationValue: string;\n\n #session: SortSession | null = null;\n\n override connect(): void {\n // A drag cannot survive a navigation: drop the hook a Turbo cache snapshot\n // may have preserved mid-drag (idempotent reconnect).\n this.element.removeAttribute(\"data-sortable-dragging\");\n this.element.addEventListener(\"stimeo--pointer-drag:start\", this.#onDragStart);\n this.element.addEventListener(\"stimeo--pointer-drag:move\", this.#onDragMove);\n this.element.addEventListener(\"stimeo--pointer-drag:end\", this.#onDragEnd);\n this.element.addEventListener(\"stimeo--pointer-drag:cancel\", this.#onDragCancel);\n }\n\n override disconnect(): void {\n this.element.removeEventListener(\"stimeo--pointer-drag:start\", this.#onDragStart);\n this.element.removeEventListener(\"stimeo--pointer-drag:move\", this.#onDragMove);\n this.element.removeEventListener(\"stimeo--pointer-drag:end\", this.#onDragEnd);\n this.element.removeEventListener(\"stimeo--pointer-drag:cancel\", this.#onDragCancel);\n this.#session = null;\n this.element.removeAttribute(\"data-sortable-dragging\");\n }\n\n /** Picks the item up: remembers its origin and announces the grab. */\n readonly #onDragStart = (event: Event): void => {\n // One reorder at a time: a start from another item (each item has its own\n // pointer-drag instance) must not clobber the live session — its from\n // index and cancel restore would be lost.\n if (this.#session) return;\n const item = this.#itemFor(event.target);\n if (!item) return;\n this.#session = { item, from: this.#items().indexOf(item), lastPrimary: 0 };\n this.element.setAttribute(\"data-sortable-dragging\", \"true\");\n this.#announce(\"grabbed\", item);\n };\n\n readonly #onDragMove = (event: Event): void => {\n const session = this.#session;\n const detail = (event as CustomEvent<Record<string, number | string>>).detail;\n if (!session || this.#itemFor(event.target) !== session.item) return;\n\n if (detail.pointerType === \"keyboard\") {\n this.#stepFromKeyboard(session, detail);\n } else {\n this.#followPointer(session, detail);\n }\n };\n\n /** Drops the item: announces, then reports `reorder` if the position changed. */\n readonly #onDragEnd = (event: Event): void => {\n const session = this.#session;\n if (!session || this.#itemFor(event.target) !== session.item) return;\n this.#session = null;\n this.element.removeAttribute(\"data-sortable-dragging\");\n this.#announce(\"dropped\", session.item);\n const to = this.#items().indexOf(session.item);\n if (to !== session.from) {\n this.dispatch(\"reorder\", { detail: { item: session.item, from: session.from, to } });\n }\n };\n\n /** Restores the pickup position (Escape / OS `pointercancel`). */\n readonly #onDragCancel = (event: Event): void => {\n const session = this.#session;\n if (!session || this.#itemFor(event.target) !== session.item) return;\n this.#session = null;\n this.element.removeAttribute(\"data-sortable-dragging\");\n this.#moveTo(session.item, session.from);\n this.#announce(\"canceled\", session.item);\n };\n\n /**\n * Keyboard stepping: `pointer-drag` reports *cumulative* synthetic deltas, so\n * the difference from the last consumed value is one arrow press — its sign is\n * the direction. Cross-axis arrows never change the primary delta (no move).\n *\n * The sign is physical (`ArrowRight` is always `+dx`), so a right-to-left row\n * has to invert it: there, moving the item rightward means moving it *earlier*\n * in the DOM. Skipping this would also split the two halves of one keypress —\n * `roving` already moves focus logically, so the same arrow would send the\n * focus and the grabbed item opposite ways.\n */\n #stepFromKeyboard(session: SortSession, detail: Record<string, number | string>): void {\n const primary = Number(this.#isVertical ? detail.dy : detail.dx) || 0;\n const delta = primary - session.lastPrimary;\n session.lastPrimary = primary;\n if (delta === 0) return;\n\n const items = this.#items();\n const index = items.indexOf(session.item);\n const step = (delta > 0 ? 1 : -1) * (this.#isReversed ? -1 : 1);\n const next = Math.max(0, Math.min(index + step, items.length - 1));\n if (next === index) return;\n this.#moveTo(session.item, next);\n this.#announce(\"moved\", session.item);\n }\n\n /**\n * Pointer following: the item moves to the slot whose siblings' midpoints the\n * pointer has passed (per `orientation`). Skipped when the list has no layout\n * geometry (every rect is zero — nothing meaningful to compare against).\n */\n #followPointer(session: SortSession, detail: Record<string, number | string>): void {\n const pointer = Number(this.#isVertical ? detail.y : detail.x) || 0;\n const others = this.#items().filter((item) => item !== session.item);\n if (others.length === 0) return;\n\n let laidOut = false;\n let target = 0;\n // `target` counts the siblings that precede the pointer *in DOM order*, and\n // the two agree only while DOM order runs the same way as the coordinate. In\n // a right-to-left row it runs the other way, so the comparison flips;\n // leaving it physical would mix two orderings into one number and drop the\n // item at a slot the pointer never crossed. Resolved once: it cannot change\n // mid-loop, and reading it per sibling would re-run `getComputedStyle` on\n // every pointermove.\n const reversed = this.#isReversed;\n for (const other of others) {\n const rect = other.getBoundingClientRect();\n if (rect.width > 0 || rect.height > 0) laidOut = true;\n const midpoint = this.#isVertical ? rect.top + rect.height / 2 : rect.left + rect.width / 2;\n const precedes = reversed ? pointer < midpoint : pointer > midpoint;\n if (precedes) target += 1;\n }\n if (!laidOut) return;\n\n // The item's index among all items equals the count of others before it,\n // so it doubles as the current insertion slot in others-space.\n const current = this.#items().indexOf(session.item);\n if (target !== current) {\n this.#moveTo(session.item, target);\n this.#announce(\"moved\", session.item);\n }\n }\n\n /** Reinserts `item` so it lands at `index` among the list's items. */\n #moveTo(item: HTMLElement, index: number): void {\n const others = this.#items().filter((candidate) => candidate !== item);\n const clamped = Math.max(0, Math.min(index, others.length));\n const reference = others[clamped] ?? null;\n // Real browsers drop focus when the focused node is re-inserted (the move\n // is a remove+insert), which would strand the keyboard grab after one\n // arrow press — restore it so focus rides the moved item.\n const active = document.activeElement;\n const hadFocus = active instanceof HTMLElement && item.contains(active);\n this.#list.insertBefore(item, reference);\n if (hadFocus) active.focus();\n }\n\n /**\n * Mirrors a step into the `status` live region. Copy is localizable through\n * `data-grabbed` / `data-moved` / `data-dropped` / `data-canceled` templates on\n * the status element (`%{name}` / `%{position}` / `%{total}` placeholders);\n * terse English is the fallback.\n */\n #announce(key: \"grabbed\" | \"moved\" | \"dropped\" | \"canceled\", item: HTMLElement): void {\n if (!this.hasStatusTarget) return;\n const position = String(this.#items().indexOf(item) + 1);\n const total = String(this.#items().length);\n const name = this.#nameOf(item);\n const fallback = {\n grabbed: `Grabbed ${name}, position ${position} of ${total}`,\n moved: `${name}, position ${position} of ${total}`,\n dropped: `Dropped ${name} at position ${position} of ${total}`,\n canceled: `Reorder canceled, ${name} returned to position ${position} of ${total}`,\n } as const;\n // Single-pass function replacement keeps the substitution literal-safe:\n // `$`-sequences in the (author/content-derived) name never expand, and a\n // name that happens to contain a placeholder token is not re-substituted.\n const values: Record<string, string> = { name, position, total };\n const template = this.statusTarget.dataset[key];\n this.statusTarget.textContent = template\n ? template.replace(/%\\{(name|position|total)\\}/g, (match, token: string) => {\n return values[token] ?? match;\n })\n : fallback[key];\n }\n\n /** The announced item name: the authored override, else its collapsed text. */\n #nameOf(item: HTMLElement): string {\n const authored = item.getAttribute(\"data-stimeo--sortable-name\");\n if (authored) return authored;\n return (item.textContent ?? \"\").replace(/\\s+/g, \" \").trim();\n }\n\n /** Resolves the sortable item owning a bubbled `pointer-drag` event. */\n #itemFor(target: EventTarget | null): HTMLElement | null {\n const node = target as Node | null;\n if (!node) return null;\n return this.#items().find((item) => item === node || item.contains(node)) ?? null;\n }\n\n /** The items in live DOM order (targets re-query the DOM on every access). */\n #items(): HTMLElement[] {\n return this.itemTargets;\n }\n\n /** The reorder container: the `list` target when present, else the element. */\n get #list(): HTMLElement {\n return this.hasListTarget ? this.listTarget : this.element;\n }\n\n get #isVertical(): boolean {\n return this.orientationValue !== \"horizontal\";\n }\n\n /**\n * Whether DOM order runs opposite to the primary coordinate — true only for a\n * horizontal row under `dir=\"rtl\"`, where the first item sits at the largest\n * `x`. Both drag paths reach this controller in physical terms (`pointer-drag`\n * documents its deltas as physical and hands RTL to its consumer, which is\n * this controller), so both have to be mapped back onto DOM order here.\n *\n * Read from the list, the element that lays the items out — never from the\n * dragged item, which may carry its own `dir`. The list inherits the computed\n * direction, so authoring `dir` on the root works too. A vertical list is\n * unaffected: writing direction does not mirror the block axis.\n */\n get #isReversed(): boolean {\n return !this.#isVertical && isRtl(this.#list);\n }\n}\n"]}
1
+ {"version":3,"sources":["../../src/utils/announce.ts","../../src/utils/logical_scroll.ts","../../src/utils/microtask_coalescer.ts","../../src/controllers/sortable_controller.ts"],"names":[],"mappings":";;;;;AAoBO,SAAS,QAAA,CAAS,OAAA,EAAiB,OAAA,GAAmC,EAAC,EAAS;AACrF,EAAA,MAAM,IAAA,GAAO,QAAQ,IAAA,EAAK;AAC1B,EAAA,IAAI,IAAA,CAAK,WAAW,CAAA,EAAG;AACvB,EAAA,MAAA,CAAO,aAAA;AAAA,IACL,IAAI,YAAY,4BAAA,EAA8B;AAAA,MAC5C,QAAQ,EAAE,OAAA,EAAS,MAAM,SAAA,EAAW,OAAA,CAAQ,cAAc,IAAA;AAAK,KAChE;AAAA,GACH;AACF;AAUO,SAAS,YAAA,CAAa,UAAkB,MAAA,EAAiD;AAC9F,EAAA,OAAO,QAAA,CAAS,OAAA,CAAQ,6BAAA,EAA+B,CAAC,OAAO,IAAA,KAAiB;AAC9E,IAAA,MAAM,WAAA,GAAc,OAAO,IAAI,CAAA;AAC/B,IAAA,OAAO,WAAA,KAAgB,MAAA,GAAY,KAAA,GAAQ,MAAA,CAAO,WAAW,CAAA;AAAA,EAC/D,CAAC,CAAA;AACH;;;AC1BO,SAAS,MAAM,OAAA,EAA2B;AAC/C,EAAA,OAAO,MAAA,CAAO,gBAAA,CAAiB,OAAO,CAAA,CAAE,SAAA,KAAc,KAAA;AACxD;;;ACkCO,IAAM,qBAAN,MAAyB;AAAA,EACrB,IAAA;AAAA,EACT,OAAA,GAAU,KAAA;AAAA,EACV,OAAA,GAAU,KAAA;AAAA,EACV,WAAA,GAAc,CAAA;AAAA;AAAA,EAGd,YAAY,GAAA,EAAiB;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WAAA,IAAe,EAAA,EAAI,QAAQ,MAAA,EAAQ,GAAG,EAAE,IAAA,EAAK;AAAA,EAC5D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,QAAA,CAAS,QAA4B,KAAA,EAA0C;AAC7E,IAAA,IAAI,CAAC,QAAQ,OAAO,IAAA;AACpB,IAAA,OAAO,MAAM,IAAA,CAAK,CAAC,IAAA,KAAS,IAAA,KAAS,MAAM,CAAA,IAAK,IAAA;AAAA,EAClD;AAAA;AAAA,EAGA,MAAA,GAAwB;AACtB,IAAA,OAAO,IAAA,CAAK,WAAA;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,KAAA,GAAqB;AACvB,IAAA,OAAO,IAAA,CAAK,aAAA,GAAgB,IAAA,CAAK,UAAA,GAAa,IAAA,CAAK,OAAA;AAAA,EACrD;AAAA,EAEA,IAAI,WAAA,GAAuB;AACzB,IAAA,OAAO,KAAK,gBAAA,KAAqB,YAAA;AAAA,EACnC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcA,IAAI,WAAA,GAAuB;AACzB,IAAA,OAAO,CAAC,IAAA,CAAK,WAAA,IAAe,KAAA,CAAM,KAAK,KAAK,CAAA;AAAA,EAC9C;AACF","file":"sortable_controller.js","sourcesContent":["/**\n * Sends one message to the page's shared `stimeo--announcer`.\n *\n * A component that has to reach assistive tech does not carry a live region of its\n * own: a region only announces what changes *after* assistive tech already knows\n * about it, which a region that appears (or is un-hidden) with its message cannot\n * satisfy. The one region that can is the announcer sitting in the page from the\n * start, so state changes are handed to it as an event and it does the reading.\n *\n * The event goes to `window` because the announcer is usually a sibling high in the\n * document rather than an ancestor of the component dispatching it.\n *\n * Wording comes from the consumer — the library ships no English strings — so an\n * empty message is silently dropped and nothing is announced.\n *\n * @example\n * ```ts\n * announce(this.announceTextValue, { assertive: false });\n * ```\n */\nexport function announce(message: string, options: { assertive?: boolean } = {}): void {\n const text = message.trim();\n if (text.length === 0) return;\n window.dispatchEvent(\n new CustomEvent(\"stimeo--announcer:announce\", {\n detail: { message: text, assertive: options.assertive === true },\n }),\n );\n}\n\n/**\n * Fills `{name}` placeholders in an announcement template from `values`.\n *\n * The same substitution the value-text templates use, so a consumer writes\n * `\"{percent}% complete\"` in one attribute and gets the same rules everywhere. A\n * placeholder with no matching entry is left as authored rather than blanked, which\n * keeps a typo visible instead of silently swallowing the word.\n */\nexport function fillTemplate(template: string, values: Record<string, string | number>): string {\n return template.replace(/\\{([a-zA-Z][a-zA-Z0-9]*)\\}/g, (match, name: string) => {\n const replacement = values[name];\n return replacement === undefined ? match : String(replacement);\n });\n}\n","/** Normalized scroll position and maximum distance on one logical axis. */\nexport interface LogicalScrollMetrics {\n position: number;\n max: number;\n}\n\n/**\n * Whether horizontal scrolling on `element` follows right-to-left inline flow.\n *\n * Resolved from the **computed** `direction`, so the authoring contract is the\n * usual `dir=\"rtl\"` (or a stylesheet) on the element or any ancestor.\n *\n * Scope: horizontal writing modes. A vertical writing mode (`writing-mode:\n * vertical-rl`) also inverts the horizontal axis, which this check does not\n * model — vertical writing modes are out of scope for the scroll utilities\n * (their consumers describe axes as horizontal/vertical, not inline/block).\n */\nexport function isRtl(element: Element): boolean {\n return window.getComputedStyle(element).direction === \"rtl\";\n}\n\n/**\n * Returns scroll distance from the logical start edge.\n *\n * CSSOM View exposes standards-mode RTL horizontal offsets as `0` at the inline\n * start (right) and increasingly negative values toward the inline end (left).\n * The normalized position is always clamped to `[0, max]`, which also absorbs\n * Safari's elastic overscroll values.\n */\nexport function logicalScrollMetrics(\n element: HTMLElement,\n horizontal: boolean,\n): LogicalScrollMetrics {\n const max = Math.max(\n 0,\n horizontal\n ? element.scrollWidth - element.clientWidth\n : element.scrollHeight - element.clientHeight,\n );\n const raw = horizontal ? element.scrollLeft : element.scrollTop;\n const position = horizontal && isRtl(element) ? -raw : raw;\n return { position: Math.min(max, Math.max(0, position)), max };\n}\n\n/**\n * Converts a logical start/end delta to the physical value accepted by\n * `Element.scrollBy`.\n */\nexport function physicalScrollDelta(\n element: HTMLElement,\n horizontal: boolean,\n logicalDelta: number,\n): number {\n return horizontal && isRtl(element) ? -logicalDelta : logicalDelta;\n}\n","/**\n * Collapses many Stimulus lifecycle callbacks from one DOM mutation into a\n * single pass.\n *\n * Stimulus fires `<name>TargetConnected` / `Disconnected` once per element and\n * `<name>ValueChanged` once per changed attribute. Replacing a list of N options\n * or morphing several render Values therefore delivers N callbacks — but the\n * useful unit of work is \"reconcile against the resulting declarative input\",\n * once, after the batch has settled. Every controller with reconcilable targets\n * or render Values needs the same shape: a `queued` flag plus `queueMicrotask`.\n *\n * **A microtask is the right horizon, and the reason is specific.** Stimulus\n * drives these callbacks from a `MutationObserver`, whose own callback already\n * runs as a microtask with the whole batch in hand; scheduling one more lands\n * after the last sibling callback of that batch and still before paint or any\n * event handler. A timer would be later than it needs to be, and reconciling\n * synchronously would run once per element against a half-applied DOM.\n *\n * **The two guards are not the same guard.** Scheduling is refused before the\n * controller connects, and running is refused after it disconnects:\n *\n * - **Before `connect()`** — Stimulus delivers initial target and Value callbacks\n * ahead of `connect()`. Reconciling there would compute output against a\n * controller whose own state has not been initialised, and `connect()` is\n * about to do a full pass anyway.\n * - **After `disconnect()`** — Stimulus fires a callback for **every** target\n * during teardown, and a microtask queued just before it would otherwise run\n * against a detached tree. {@link MicrotaskCoalescer.cancel} exists for the\n * teardown path to drop the pending pass outright.\n *\n * Both guards are part of one contract here rather than something each consumer\n * has to remember separately.\n *\n * Scope is the scheduling only. *What* to reconcile — keep the surviving active\n * option, fall back to the next / previous / first visible one, rebuild derived\n * chips or hidden fields — stays in the controller, because no two consumers\n * answer it the same way.\n *\n * This file's own doc block is dropped from `dist`, but every member comment is\n * inlined into each consumer entry (`tsup` builds with `splitting: false`), so\n * rationale belongs here and only the contract belongs on the members.\n *\n * @example\n * ```ts\n * readonly #reconcile = new MicrotaskCoalescer(() => this.#reconcileOptions());\n *\n * connect() { this.#reconcile.activate(); }\n * disconnect() { this.#reconcile.cancel(); }\n *\n * optionTargetConnected() { this.#reconcile.schedule(); }\n * optionTargetDisconnected() { this.#reconcile.schedule(); }\n * ```\n */\nexport class MicrotaskCoalescer {\n readonly #run: () => void;\n #queued = false;\n #active = false;\n #generation = 0;\n\n /** @param run - the single reconciliation pass, invoked at most once per batch. */\n constructor(run: () => void) {\n this.#run = run;\n }\n\n /** Opens the window in which {@link schedule} is honoured; call from `connect()`. */\n activate(): void {\n this.#active = true;\n }\n\n /** Closes the window and drops any pending pass; call from `disconnect()`. */\n cancel(): void {\n this.#active = false;\n this.#queued = false;\n this.#generation += 1;\n }\n\n /** Requests one pass after the batch settles. Idempotent; inert outside the window. */\n schedule(): void {\n if (!this.#active || this.#queued) return;\n this.#queued = true;\n const generation = this.#generation;\n queueMicrotask(() => {\n // A cancelled callback must not consume a pass queued after reconnect.\n if (generation !== this.#generation || !this.#queued || !this.#active) return;\n this.#queued = false;\n this.#run();\n });\n }\n}\n","import { Controller } from \"@hotwired/stimulus\";\nimport { announce, fillTemplate } from \"../utils/announce\";\nimport { isRtl } from \"../utils/logical_scroll\";\nimport { MicrotaskCoalescer } from \"../utils/microtask_coalescer\";\n\n/** The four steps a consumer can give wording to. */\ntype AnnounceKey = \"grabbed\" | \"moved\" | \"dropped\" | \"canceled\";\n\n/** A reorder in flight: one item picked up by pointer or keyboard. */\ninterface SortSession {\n item: HTMLElement;\n /**\n * The item that followed it at pickup, and the one that preceded it.\n *\n * The pickup slot is remembered as nodes rather than as an index because the\n * list can change underneath a live drag — a broadcast board inserts and\n * deletes rows while someone is holding one — and an index taken at pickup\n * stops pointing at that place the moment a row appears above it.\n */\n anchor: HTMLElement | null;\n predecessor: HTMLElement | null;\n /** Last primary-axis cumulative delta consumed by keyboard stepping. */\n lastPrimary: number;\n}\n\n/**\n * Headless, accessible **sortable** (single-list reorder) — the a11y-first\n * drag-and-drop that is the flagship consumer of the\n * `stimeo--pointer-drag` primitive. The composition is markup-level and each\n * layer keeps its job: `pointer-drag` (on every item) emits the normalized drag\n * signal with its built-in keyboard alternative, `roving` (on the list) keeps\n * the handles a single Tab stop, and this controller interprets the signal —\n * live-reordering the DOM, handing each step to the page's shared announcer,\n * and reporting the final `reorder`. No dedicated APG pattern exists for\n * drag-and-drop; the keyboard model (grab → arrows → drop / Escape) comes from\n * `pointer-drag` and the announcements make it non-visually trackable\n * (WCAG 2.1.1 / 2.5.7 / 4.1.3). Core (zero dependencies).\n *\n * Markup contract (identifier: `stimeo--sortable`):\n * <div data-controller=\"stimeo--sortable\">\n * <ul data-stimeo--sortable-target=\"list\" data-controller=\"stimeo--roving\"\n * data-stimeo--roving-orientation-value=\"vertical\">\n * <li data-stimeo--sortable-target=\"item\" data-stimeo--sortable-name=\"Card A\"\n * data-controller=\"stimeo--pointer-drag\"\n * data-stimeo--pointer-drag-axis-value=\"y\">\n * <span>Card A</span>\n * <button type=\"button\" aria-label=\"Reorder Card A\"\n * data-stimeo--pointer-drag-target=\"handle\"\n * data-stimeo--roving-target=\"item\">⠿</button>\n * </li>\n * …\n * </ul>\n * </div>\n *\n * The composed values must follow the sort axis: roving's `orientation`\n * (which defaults to `horizontal`, so a vertical list has to author\n * `vertical`) and pointer-drag's `axis`. `stimeo check` enforces both\n * alignments as composition rules (`composition-mismatch`).\n *\n * Pointer flow: dragging an item live-moves it whenever the pointer crosses a\n * sibling's midpoint (per `orientation`). Keyboard flow: each synthetic\n * `pointer-drag` move steps the item one position. A horizontal list under\n * `dir=\"rtl\"` reverses both, since DOM order then runs right-to-left while\n * `pointer-drag` reports physical coordinates. Dropping dispatches\n * `reorder` (`{ item, from, to }`, zero-based) when the position changed;\n * Escape / `pointercancel` restores the pickup position. Every step is handed to\n * the page's shared `stimeo--announcer` as the consumer's own wording —\n * `announceGrabbedText` / `announceMovedText` / `announceDroppedText` /\n * `announceCanceledText`, with `{name}` / `{position}` / `{total}` placeholders —\n * and an unset one announces nothing.\n *\n * @remarks\n * Behavior only — the ghost/placeholder/drop-hint visuals are the consumer's\n * CSS, keyed off `pointer-drag`'s `data-dragging`/`data-grabbed` on the item\n * and this controller's `data-sortable-dragging` on the root. Persistence is\n * the consumer's `reorder` listener (post the new position; a Turbo Stream\n * broadcast makes the board collaborative). The DOM order is the single source\n * of truth: items are read live on every event, so rows appended by Turbo need\n * no rewiring, and a Turbo cache restore reconnects idempotently (`connect()`\n * clears the transient root hook; a drag cannot survive a navigation). Only the\n * event listeners are held, and they are removed on `disconnect()`. Multi-list\n * / kanban, nested DnD, auto-scroll at the edges, and virtualized lists are\n * deliberately out of this single-list scope.\n */\nexport class SortableController extends Controller<HTMLElement> {\n static override targets = [\"list\", \"item\"];\n static override values = {\n orientation: { type: String, default: \"vertical\" },\n announceGrabbedText: { type: String, default: \"\" },\n announceMovedText: { type: String, default: \"\" },\n announceDroppedText: { type: String, default: \"\" },\n announceCanceledText: { type: String, default: \"\" },\n };\n static events = [\"reorder\"] as const;\n\n declare readonly hasListTarget: boolean;\n declare readonly listTarget: HTMLElement;\n declare readonly itemTargets: HTMLElement[];\n declare orientationValue: string;\n declare announceGrabbedTextValue: string;\n declare announceMovedTextValue: string;\n declare announceDroppedTextValue: string;\n declare announceCanceledTextValue: string;\n\n #session: SortSession | null = null;\n /** Defers the lost-item check to after the mutation batch (see below). */\n readonly #settle = new MicrotaskCoalescer(() => this.#dropLostSession());\n\n override connect(): void {\n // A drag cannot survive a navigation: drop the hook a Turbo cache snapshot\n // may have preserved mid-drag (idempotent reconnect).\n this.element.removeAttribute(\"data-sortable-dragging\");\n this.#settle.activate();\n this.element.addEventListener(\"stimeo--pointer-drag:start\", this.#onDragStart);\n this.element.addEventListener(\"stimeo--pointer-drag:move\", this.#onDragMove);\n this.element.addEventListener(\"stimeo--pointer-drag:end\", this.#onDragEnd);\n this.element.addEventListener(\"stimeo--pointer-drag:cancel\", this.#onDragCancel);\n }\n\n override disconnect(): void {\n this.element.removeEventListener(\"stimeo--pointer-drag:start\", this.#onDragStart);\n this.element.removeEventListener(\"stimeo--pointer-drag:move\", this.#onDragMove);\n this.element.removeEventListener(\"stimeo--pointer-drag:end\", this.#onDragEnd);\n this.element.removeEventListener(\"stimeo--pointer-drag:cancel\", this.#onDragCancel);\n this.#settle.cancel();\n this.#session = null;\n this.element.removeAttribute(\"data-sortable-dragging\");\n }\n\n /**\n * Ends a session whose item left the item set.\n *\n * The controller's own reorder detaches and reattaches the item inside one\n * mutation batch, so the loss is only real once the batch has settled — the\n * check therefore runs a microtask later and asks whether the item is a target\n * again. Without it a deleted row (a broadcast that drops it from the board)\n * or a morph that strips the item's target attribute would hold the\n * one-at-a-time session for the rest of the page's life, with the root hook\n * stuck on and every later grab refused with no way out.\n */\n itemTargetDisconnected(item: HTMLElement): void {\n if (this.#session?.item !== item) return;\n this.#settle.schedule();\n }\n\n #dropLostSession(): void {\n const session = this.#session;\n if (!session) return;\n const items = this.#items();\n if (items.includes(session.item)) return;\n this.#session = null;\n this.element.removeAttribute(\"data-sortable-dragging\");\n // An element that is still in the document (a morph took only its target\n // attribute) goes back where it was picked up. Nothing is announced either\n // way: the row is no longer one of the items, so there is no position to\n // read out — what matters is that the next grab is not refused.\n if (session.item.isConnected) this.#restore(session, items);\n }\n\n /** Picks the item up: remembers its neighbours and announces the grab. */\n readonly #onDragStart = (event: Event): void => {\n // One reorder at a time: a start from another item (each item has its own\n // pointer-drag instance) must not clobber the live session — its pickup\n // slot and cancel restore would be lost.\n if (this.#session) return;\n const items = this.#items();\n const item = this.#itemFor(event.target, items);\n if (!item) return;\n const index = items.indexOf(item);\n this.#session = {\n item,\n anchor: items[index + 1] ?? null,\n predecessor: items[index - 1] ?? null,\n lastPrimary: 0,\n };\n this.element.setAttribute(\"data-sortable-dragging\", \"true\");\n this.#announce(\"grabbed\", item);\n };\n\n readonly #onDragMove = (event: Event): void => {\n const session = this.#session;\n if (!session) return;\n const items = this.#items();\n if (this.#itemFor(event.target, items) !== session.item) return;\n\n const detail = (event as CustomEvent<Record<string, number | string>>).detail;\n if (detail.pointerType === \"keyboard\") {\n this.#stepFromKeyboard(session, detail, items);\n } else {\n this.#followPointer(session, detail, items);\n }\n };\n\n /** Drops the item: announces, then reports `reorder` if the position changed. */\n readonly #onDragEnd = (event: Event): void => {\n const session = this.#session;\n if (!session) return;\n const items = this.#items();\n if (this.#itemFor(event.target, items) !== session.item) return;\n this.#session = null;\n this.element.removeAttribute(\"data-sortable-dragging\");\n this.#announce(\"dropped\", session.item);\n const to = items.indexOf(session.item);\n const from = this.#pickupSlot(session, items) ?? to;\n if (to !== from) {\n this.dispatch(\"reorder\", { detail: { item: session.item, from, to } });\n }\n };\n\n /** Restores the pickup position (Escape / OS `pointercancel`). */\n readonly #onDragCancel = (event: Event): void => {\n const session = this.#session;\n if (!session) return;\n const items = this.#items();\n if (this.#itemFor(event.target, items) !== session.item) return;\n this.#session = null;\n this.element.removeAttribute(\"data-sortable-dragging\");\n this.#restore(session, items);\n this.#announce(\"canceled\", session.item);\n };\n\n /**\n * The slot the item was picked up from, read back through the neighbours it\n * had then — `null` when neither survives and the item itself is gone.\n *\n * The anchor is the item that followed it, so restoring means \"before that one\n * again\". When the anchor was deleted mid-drag the predecessor answers the\n * same question from the other side. With both gone the item's current slot is\n * the honest answer: nothing is known to have moved, so no reorder is reported\n * and a cancel leaves the item where it is.\n */\n #pickupSlot(session: SortSession, items: HTMLElement[]): number | null {\n const others = items.filter((candidate) => candidate !== session.item);\n if (session.anchor) {\n const at = others.indexOf(session.anchor);\n if (at !== -1) return at;\n }\n if (session.predecessor) {\n const at = others.indexOf(session.predecessor);\n if (at !== -1) return at + 1;\n }\n const here = items.indexOf(session.item);\n return here === -1 ? null : here;\n }\n\n /** Puts the item back where it was picked up from. */\n #restore(session: SortSession, items: HTMLElement[]): void {\n const slot = this.#pickupSlot(session, items);\n if (slot === null) return;\n this.#insertAt(\n session.item,\n items.filter((candidate) => candidate !== session.item),\n slot,\n );\n }\n\n /**\n * Keyboard stepping: `pointer-drag` reports *cumulative* synthetic deltas, so\n * the difference from the last consumed value is one arrow press — its sign is\n * the direction. Cross-axis arrows never change the primary delta (no move).\n *\n * The sign is physical (`ArrowRight` is always `+dx`), so a right-to-left row\n * has to invert it: there, moving the item rightward means moving it *earlier*\n * in the DOM. Skipping this would also split the two halves of one keypress —\n * `roving` already moves focus logically, so the same arrow would send the\n * focus and the grabbed item opposite ways.\n */\n #stepFromKeyboard(\n session: SortSession,\n detail: Record<string, number | string>,\n items: HTMLElement[],\n ): void {\n const primary = Number(this.#isVertical ? detail.dy : detail.dx) || 0;\n const delta = primary - session.lastPrimary;\n session.lastPrimary = primary;\n if (delta === 0) return;\n\n const index = items.indexOf(session.item);\n const step = (delta > 0 ? 1 : -1) * (this.#isReversed ? -1 : 1);\n const next = Math.max(0, Math.min(index + step, items.length - 1));\n if (next === index) return;\n this.#insertAt(\n session.item,\n items.filter((candidate) => candidate !== session.item),\n next,\n );\n this.#announce(\"moved\", session.item);\n }\n\n /**\n * Pointer following: the item moves to the slot whose siblings' midpoints the\n * pointer has passed (per `orientation`).\n *\n * Only siblings that occupy space take part. A row with no layout box — a\n * filtered-out item, a `display: none` ancestor, a collapsed `<details>` — is\n * reported with an empty rect at the document origin, and its midpoint of `0`\n * sits below every pointer position: counted, it would read as passed on the\n * very first move and send the item across a slot the pointer never crossed.\n * With no laid-out sibling at all there is nothing to compare against and the\n * move is skipped entirely.\n */\n #followPointer(\n session: SortSession,\n detail: Record<string, number | string>,\n items: HTMLElement[],\n ): void {\n const pointer = Number(this.#isVertical ? detail.y : detail.x) || 0;\n const vertical = this.#isVertical;\n // `target` counts the siblings that precede the pointer *in DOM order*, and\n // the two agree only while DOM order runs the same way as the coordinate. In\n // a right-to-left row it runs the other way, so the comparison flips;\n // leaving it physical would mix two orderings into one number and drop the\n // item at a slot the pointer never crossed. Resolved once: it cannot change\n // mid-loop, and reading it per sibling would re-run `getComputedStyle` on\n // every pointermove.\n const reversed = this.#isReversed;\n const here = items.indexOf(session.item);\n const laidOut: HTMLElement[] = [];\n let target = 0;\n // `current` is the item's own slot in that same laid-out space: the number\n // of laid-out siblings that come before it in DOM order.\n let current = 0;\n items.forEach((item, index) => {\n if (item === session.item) return;\n const rect = item.getBoundingClientRect();\n if (rect.width === 0 && rect.height === 0) return;\n if (index < here) current += 1;\n laidOut.push(item);\n const midpoint = vertical ? rect.top + rect.height / 2 : rect.left + rect.width / 2;\n if (reversed ? pointer < midpoint : pointer > midpoint) target += 1;\n });\n if (laidOut.length === 0 || target === current) return;\n\n this.#insertAt(session.item, laidOut, target);\n this.#announce(\"moved\", session.item);\n }\n\n /**\n * Reinserts `item` at `index` among `scope`, relative to the sibling already\n * standing there.\n *\n * The reorder is defined against the items, not against a container: the\n * neighbour's own parent is where the item belongs. A `list` target that is\n * not the items' parent — or none at all, which the markup contract allows —\n * therefore still lands the move in the right place instead of throwing, and\n * the last slot is *after the last item* rather than after whatever else the\n * container holds (a live region, a footer), which would put the row outside\n * the reading order the list publishes.\n */\n #insertAt(item: HTMLElement, scope: HTMLElement[], index: number): void {\n const clamped = Math.max(0, Math.min(index, scope.length));\n const ahead = scope[clamped] ?? null;\n const behind = ahead ? null : (scope[scope.length - 1] ?? null);\n const neighbour = ahead ?? behind;\n if (!neighbour) return;\n const parent = neighbour.parentNode;\n if (!parent) return;\n // Real browsers drop focus when the focused node is re-inserted (the move\n // is a remove+insert), which would strand the keyboard grab after one\n // arrow press — restore it so focus rides the moved item.\n const active = document.activeElement;\n const hadFocus = active instanceof HTMLElement && item.contains(active);\n parent.insertBefore(item, ahead ?? neighbour.nextSibling);\n if (hadFocus) active.focus();\n }\n\n /**\n * Hands one step to the page's shared announcer.\n *\n * The library carries no live region and no English copy: the wording is the\n * consumer's, written into `announceGrabbedText` / `announceMovedText` /\n * `announceDroppedText` / `announceCanceledText` with `{name}` / `{position}` /\n * `{total}` placeholders, and an unset one announces nothing.\n *\n * Only transitions reach here — the pickup, a step that actually changed the\n * landing slot, and the single end of the session — so a pointer crossing the\n * same slot twice or an arrow clamped at an end stays silent.\n */\n #announce(key: AnnounceKey, item: HTMLElement): void {\n const template = this.#announceTemplate(key);\n if (template.length === 0) return;\n const items = this.#items();\n announce(\n fillTemplate(template, {\n name: this.#nameOf(item),\n position: items.indexOf(item) + 1,\n total: items.length,\n }),\n );\n }\n\n /** The consumer's wording for one step, or `\"\"` when they authored none. */\n #announceTemplate(key: AnnounceKey): string {\n switch (key) {\n case \"grabbed\":\n return this.announceGrabbedTextValue;\n case \"moved\":\n return this.announceMovedTextValue;\n case \"dropped\":\n return this.announceDroppedTextValue;\n case \"canceled\":\n return this.announceCanceledTextValue;\n }\n }\n\n /** The announced item name: the authored override, else its collapsed text. */\n #nameOf(item: HTMLElement): string {\n const authored = item.getAttribute(\"data-stimeo--sortable-name\");\n if (authored) return authored;\n return (item.textContent ?? \"\").replace(/\\s+/g, \" \").trim();\n }\n\n /**\n * Resolves the sortable item owning a bubbled `pointer-drag` event.\n *\n * `pointer-drag` dispatches on its own element, and the markup contract puts\n * one on each item, so the owner is the item that **is** the target. Matching\n * an ancestor instead would make a card's own inner draggable — a knob, a\n * split pane, anything the primitive is composed into — drive the card.\n */\n #itemFor(target: EventTarget | null, items: HTMLElement[]): HTMLElement | null {\n if (!target) return null;\n return items.find((item) => item === target) ?? null;\n }\n\n /** The items in live DOM order (targets re-query the DOM on every access). */\n #items(): HTMLElement[] {\n return this.itemTargets;\n }\n\n /** The reorder container: the `list` target when present, else the element. */\n get #list(): HTMLElement {\n return this.hasListTarget ? this.listTarget : this.element;\n }\n\n get #isVertical(): boolean {\n return this.orientationValue !== \"horizontal\";\n }\n\n /**\n * Whether DOM order runs opposite to the primary coordinate — true only for a\n * horizontal row under `dir=\"rtl\"`, where the first item sits at the largest\n * `x`. Both drag paths reach this controller in physical terms (`pointer-drag`\n * documents its deltas as physical and hands RTL to its consumer, which is\n * this controller), so both have to be mapped back onto DOM order here.\n *\n * Read from the list, the element that lays the items out — never from the\n * dragged item, which may carry its own `dir`. The list inherits the computed\n * direction, so authoring `dir` on the root works too. A vertical list is\n * unaffected: writing direction does not mirror the block axis.\n */\n get #isReversed(): boolean {\n return !this.#isVertical && isRtl(this.#list);\n }\n}\n"]}
@@ -41,9 +41,9 @@ import { Controller } from '@hotwired/stimulus';
41
41
  *
42
42
  * @remarks
43
43
  * Behavior only — the visual spinner is the consumer's, alongside the text and
44
- * `aria-hidden="true"`. Both timers are owned by {@link SafeTimeout}, kept across
45
- * an in-page move and dropped on a real detach via {@link DetachGate}, while the
46
- * loading state a cached page would freeze is rewound by {@link BeforeCacheReset}.
44
+ * `aria-hidden="true"`. Both timers are owned by `SafeTimeout`, kept across
45
+ * an in-page move and dropped on a real detach via `DetachGate`, while the
46
+ * loading state a cached page would freeze is rewound by `BeforeCacheReset`.
47
47
  */
48
48
  declare class SpinnerController extends Controller<HTMLElement> {
49
49
  #private;