@rcarls/rc-common 0.5.0 → 0.7.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 (85) hide show
  1. package/CHANGELOG.md +315 -0
  2. package/README.md +167 -50
  3. package/dist/modules/ActiveDescendantController.js +89 -0
  4. package/dist/modules/AnchorController.js +317 -0
  5. package/dist/modules/ControllerEffects.js +60 -0
  6. package/dist/modules/DragController.js +128 -0
  7. package/dist/modules/DragGestureController.js +174 -0
  8. package/dist/modules/FlowController.js +35 -0
  9. package/dist/modules/ItemsCollectionController.js +233 -0
  10. package/dist/modules/KeyboardInteractionDirective.js +39 -0
  11. package/dist/modules/KeyboardNavigationDirective.js +88 -0
  12. package/dist/modules/LiveAnnouncer.js +36 -0
  13. package/dist/modules/MutationObserverController.js +30 -0
  14. package/dist/modules/NavigationIndicatorController.js +81 -0
  15. package/dist/modules/PopoverController.js +44 -0
  16. package/dist/modules/RafScheduler.js +83 -0
  17. package/dist/modules/ReorderController.js +361 -0
  18. package/dist/modules/ResizeController.js +425 -0
  19. package/dist/modules/RovingTabIndexMixin.js +87 -0
  20. package/dist/modules/ScrollObserverController.js +78 -0
  21. package/dist/modules/TypeaheadController.js +59 -0
  22. package/dist/modules/anchorPlacement.js +13 -0
  23. package/dist/modules/clickDelegate.js +31 -0
  24. package/dist/modules/cssText.js +6 -0
  25. package/dist/modules/cssTime.js +11 -0
  26. package/dist/modules/cssZoom.js +16 -0
  27. package/dist/modules/findNearestScrollAncestor.js +22 -0
  28. package/dist/modules/flow.js +94 -0
  29. package/dist/modules/isFocusable.js +6 -0
  30. package/dist/modules/localeFormat.js +38 -0
  31. package/dist/modules/messages.js +77 -0
  32. package/dist/modules/nativeChild.js +37 -0
  33. package/dist/modules/observeLocale.js +70 -0
  34. package/dist/modules/sliderUtils.js +14 -0
  35. package/dist/modules/snapUtils.js +25 -0
  36. package/dist/modules/softKeyboard.js +72 -0
  37. package/dist/modules/visualViewport.js +14 -0
  38. package/dist/rc-common.js +99 -1575
  39. package/dist/rc-common.umd.cjs +10 -26
  40. package/dist/types/AnchorController.d.ts +82 -12
  41. package/dist/types/ControllerEffects.d.ts +15 -0
  42. package/dist/types/DragController.d.ts +13 -0
  43. package/dist/types/DragGestureController.d.ts +31 -7
  44. package/dist/types/FlowController.d.ts +31 -0
  45. package/dist/types/ItemsCollectionController.d.ts +3 -2
  46. package/dist/types/KeyboardInteractionDirective.d.ts +1 -2
  47. package/dist/types/KeyboardNavigationDirective.d.ts +16 -7
  48. package/dist/types/LiveAnnouncer.d.ts +22 -0
  49. package/dist/types/MutationObserverController.d.ts +0 -1
  50. package/dist/types/NavigationIndicatorController.d.ts +1 -1
  51. package/dist/types/PopoverController.d.ts +21 -0
  52. package/dist/types/RafScheduler.d.ts +36 -1
  53. package/dist/types/ReorderController.d.ts +131 -0
  54. package/dist/types/ResizeController.d.ts +46 -0
  55. package/dist/types/RovingTabIndexMixin.d.ts +1 -1
  56. package/dist/types/ScrollObserverController.d.ts +13 -2
  57. package/dist/types/TypeaheadController.d.ts +8 -2
  58. package/dist/types/anchorPlacement.d.ts +24 -0
  59. package/dist/types/clickDelegate.d.ts +69 -0
  60. package/dist/types/cssText.d.ts +17 -0
  61. package/dist/types/cssTime.d.ts +14 -0
  62. package/dist/types/cssZoom.d.ts +12 -0
  63. package/dist/types/findNearestScrollAncestor.d.ts +3 -3
  64. package/dist/types/flow.d.ts +121 -0
  65. package/dist/types/index.d.ts +33 -20
  66. package/dist/types/localeFormat.d.ts +4 -0
  67. package/dist/types/messages.d.ts +105 -0
  68. package/dist/types/nativeChild.d.ts +0 -3
  69. package/dist/types/observeLocale.d.ts +23 -0
  70. package/dist/types/softKeyboard.d.ts +79 -0
  71. package/dist/types/visualViewport.d.ts +21 -0
  72. package/package.json +1 -4
  73. package/dist/css-anchor-positioning-fn-hFYJeBk4.js +0 -6371
  74. package/dist/types/DragGestureController.test.d.ts +0 -1
  75. package/dist/types/KeyboardInteractionDirective.test.d.ts +0 -1
  76. package/dist/types/KeyboardNavigationDirective.test.d.ts +0 -1
  77. package/dist/types/MouseMoveDirective.d.ts +0 -42
  78. package/dist/types/MouseMoveDirective.test.d.ts +0 -1
  79. package/dist/types/MutationObserverController.test.d.ts +0 -1
  80. package/dist/types/RafScheduler.test.d.ts +0 -1
  81. package/dist/types/ScrollObserverController.test.d.ts +0 -1
  82. package/dist/types/TypeaheadController.test.d.ts +0 -1
  83. package/dist/types/isFocusable.test.d.ts +0 -1
  84. package/dist/types/nativeChild.test.d.ts +0 -1
  85. package/dist/types/snapUtils.unit.test.d.ts +0 -1
@@ -0,0 +1,81 @@
1
+ import { cssZoom as _ } from "./cssZoom.js";
2
+ import { resolveFlow as u, logicalRect as d, physicalOffset as v } from "./flow.js";
3
+ import { observeLocale as b } from "./observeLocale.js";
4
+ class k {
5
+ constructor(e, t) {
6
+ this._frame = 0, this._links = [], this._host = e, this._options = t, e.addController(this);
7
+ }
8
+ hostConnected() {
9
+ this._unobserveLocale ??= b(() => this.update(), this._host), this.sync();
10
+ }
11
+ hostDisconnected() {
12
+ this._mutationObserver?.disconnect(), this._resizeObserver?.disconnect(), this._unobserveLocale?.(), this._mutationObserver = void 0, this._resizeObserver = void 0, this._unobserveLocale = void 0, this._frame && cancelAnimationFrame(this._frame), this._frame = 0;
13
+ }
14
+ sync() {
15
+ this._collectLinks(), this._observeLinks(), this.update();
16
+ }
17
+ update() {
18
+ this._frame && cancelAnimationFrame(this._frame), this._frame = requestAnimationFrame(() => {
19
+ this._frame = 0, this._positionIndicator();
20
+ });
21
+ }
22
+ _collectLinks() {
23
+ const t = this._options.slot()?.assignedElements({ flatten: !0 }) ?? [], i = /* @__PURE__ */ new Set();
24
+ for (const s of t)
25
+ s instanceof HTMLAnchorElement && i.add(s), s.querySelectorAll("a").forEach((n) => i.add(n));
26
+ this._links = Array.from(i);
27
+ }
28
+ _observeLinks() {
29
+ this._mutationObserver?.disconnect(), this._resizeObserver?.disconnect(), this._mutationObserver = new MutationObserver(() => {
30
+ this._collectLinks(), this._observeLinks(), this.update();
31
+ }), this._resizeObserver = new ResizeObserver(() => this.update()), this._resizeObserver.observe(this._host);
32
+ for (const e of this._links) {
33
+ this._mutationObserver.observe(e, {
34
+ attributes: !0,
35
+ attributeFilter: ["aria-current", "class", "data-active"],
36
+ childList: !0,
37
+ subtree: !0
38
+ }), this._resizeObserver.observe(e);
39
+ const t = this._targetFor(e);
40
+ t && t !== e && this._resizeObserver.observe(t);
41
+ }
42
+ }
43
+ _activeLink() {
44
+ const e = this._options.activeSelector().trim();
45
+ if (e) {
46
+ const t = this._links.find((i) => i.matches(e));
47
+ if (t)
48
+ return t;
49
+ }
50
+ return this._links.find((t) => {
51
+ const i = t.getAttribute("aria-current");
52
+ return i !== null && i !== "" && i !== "false";
53
+ }) ?? null;
54
+ }
55
+ _targetFor(e) {
56
+ const t = this._options.indicatorTargetSelector().trim();
57
+ if (t) {
58
+ const i = e.matches(t) ? e : e.querySelector(t);
59
+ if (i)
60
+ return i;
61
+ }
62
+ return e.querySelector("[data-rc-navigation-indicator]") ?? e.querySelector("[data-rc-navigation-icon]") ?? e;
63
+ }
64
+ _positionIndicator() {
65
+ const e = this._options.container(), t = this._options.indicator(), i = this._activeLink();
66
+ if (this._host.toggleAttribute("has-active", !!i), !e || !t || !i) {
67
+ t?.toggleAttribute("hidden", !0);
68
+ return;
69
+ }
70
+ const s = this._targetFor(i), n = e.getBoundingClientRect(), c = s.getBoundingClientRect();
71
+ if (!c.width || !c.height) {
72
+ t.toggleAttribute("hidden", !0);
73
+ return;
74
+ }
75
+ const a = u(e), r = d(c, n, a), { x: h, y: l } = v({ inline: r.inlineStart, block: r.blockStart }, a), o = _(t);
76
+ t.toggleAttribute("hidden", !1), t.style.inlineSize = `${r.inlineSize / o}px`, t.style.blockSize = `${r.blockSize / o}px`, t.style.transform = `translate(${h / o}px, ${l / o}px)`;
77
+ }
78
+ }
79
+ export {
80
+ k as NavigationIndicatorController
81
+ };
@@ -0,0 +1,44 @@
1
+ const o = "data-rc-popover-fallback-open";
2
+ class n {
3
+ constructor(e, t) {
4
+ this._current = null, this._connected = !1, this._element = t.element, e.addController(this);
5
+ }
6
+ hostConnected() {
7
+ this._connected = !0;
8
+ }
9
+ hostDisconnected() {
10
+ this._connected = !1, this._close(this._current), this._current = null;
11
+ }
12
+ /** Synchronize visibility after the popup exists in the DOM. */
13
+ sync(e) {
14
+ const t = typeof this._element == "function" ? this._element() : this._element;
15
+ if (t !== this._current && (this._close(this._current), this._current = t), !(!this._connected || !t?.isConnected)) {
16
+ if (!e) {
17
+ this._close(t);
18
+ return;
19
+ }
20
+ if (!t.hasAttribute(o)) {
21
+ try {
22
+ if (typeof t.showPopover == "function") {
23
+ t.matches(":popover-open") || t.showPopover();
24
+ return;
25
+ }
26
+ } catch {
27
+ }
28
+ t.setAttribute(o, "");
29
+ }
30
+ }
31
+ }
32
+ _close(e) {
33
+ if (e) {
34
+ e.removeAttribute(o);
35
+ try {
36
+ typeof e.hidePopover == "function" && e.matches(":popover-open") && e.hidePopover();
37
+ } catch {
38
+ }
39
+ }
40
+ }
41
+ }
42
+ export {
43
+ n as PopoverController
44
+ };
@@ -0,0 +1,83 @@
1
+ const r = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new WeakMap();
2
+ let i = 0;
3
+ function h(s) {
4
+ queueMicrotask(() => {
5
+ throw s;
6
+ });
7
+ }
8
+ function f() {
9
+ i || (i = requestAnimationFrame((s) => {
10
+ i = 0;
11
+ const t = [...r.values()], c = /* @__PURE__ */ new Map();
12
+ r.clear();
13
+ for (const e of t)
14
+ if (!(!e.active || a.get(e.scheduler) !== e))
15
+ try {
16
+ c.set(e, e.sample(s));
17
+ } catch (n) {
18
+ l(e), h(n);
19
+ }
20
+ for (const e of t) {
21
+ if (!c.has(e) || !e.active || a.get(e.scheduler) !== e)
22
+ continue;
23
+ const n = c.get(e);
24
+ l(e);
25
+ try {
26
+ e.apply(n, s);
27
+ } catch (o) {
28
+ h(o);
29
+ }
30
+ }
31
+ }));
32
+ }
33
+ function l(s) {
34
+ a.get(s.scheduler) === s && (s.active = !1, a.delete(s.scheduler));
35
+ }
36
+ class u {
37
+ constructor(t) {
38
+ this._callback = null, this._frame = 0, t?.addController(this);
39
+ }
40
+ get pending() {
41
+ return this._frame !== 0 || a.has(this);
42
+ }
43
+ schedule(t) {
44
+ this._cancelPhased(), this._callback = t, !this._frame && (this._frame = requestAnimationFrame((c) => {
45
+ const e = this._callback;
46
+ this._frame = 0, this._callback = null, e?.(c);
47
+ }));
48
+ }
49
+ /**
50
+ * Coalesces measurement and mutation into a shared animation frame.
51
+ * Every queued sample runs before any queued apply, including work from
52
+ * other scheduler instances. Repeated calls keep only the latest task.
53
+ *
54
+ * @typeParam T - Snapshot returned by `sample` and passed to `apply`.
55
+ * @param sample - reads browser state and returns a compact snapshot
56
+ * @param apply - mutates state or DOM from the sampled snapshot
57
+ * @example
58
+ * ```ts
59
+ * scheduler.schedulePhased(
60
+ * () => element.getBoundingClientRect(),
61
+ * (rect) => element.style.setProperty('--measured-width', `${rect.width}px`),
62
+ * );
63
+ * ```
64
+ */
65
+ schedulePhased(t, c) {
66
+ this._frame && (cancelAnimationFrame(this._frame), this._frame = 0, this._callback = null), this._cancelPhased();
67
+ const e = { scheduler: this, sample: t, apply: c, active: !0 };
68
+ a.set(this, e), r.set(this, e), f();
69
+ }
70
+ cancel() {
71
+ this._frame && (cancelAnimationFrame(this._frame), this._frame = 0, this._callback = null), this._cancelPhased();
72
+ }
73
+ hostDisconnected() {
74
+ this.cancel();
75
+ }
76
+ _cancelPhased() {
77
+ const t = a.get(this);
78
+ t && (t.active = !1, a.delete(this), r.delete(this), i && r.size === 0 && (cancelAnimationFrame(i), i = 0));
79
+ }
80
+ }
81
+ export {
82
+ u as RafScheduler
83
+ };
@@ -0,0 +1,361 @@
1
+ import { DragGestureController as y } from "./DragGestureController.js";
2
+ import { cssZoom as w } from "./cssZoom.js";
3
+ import { findNearestScrollAncestor as b } from "./findNearestScrollAncestor.js";
4
+ import { deepActiveElement as p } from "./softKeyboard.js";
5
+ import { resolveFlow as g, logicalRect as S, logicalDelta as k, physicalOffset as v, arrowKeys as D, renderedOrientation as E, getScrollOffset as P, isReversed as x } from "./flow.js";
6
+ import { LiveAnnouncer as F } from "./LiveAnnouncer.js";
7
+ import { messagesLocale as _, resolveMessages as O } from "./messages.js";
8
+ import { numberFormat as A } from "./localeFormat.js";
9
+ function d(a, t) {
10
+ return A(t, { useGrouping: !1 }).format(a);
11
+ }
12
+ const C = Object.freeze({
13
+ instructions: "Press Space to reorder.",
14
+ grabbed: (a, t, e) => `Picked up, position ${d(a, e)} of ${d(t, e)}. Use the arrow keys to move, Space to drop, Escape to cancel.`,
15
+ moved: (a, t, e) => `Moved to position ${d(a, e)} of ${d(t, e)}.`,
16
+ dropped: (a, t, e) => `Dropped at position ${d(a, e)} of ${d(t, e)}.`,
17
+ canceled: (a, t, e) => `Reorder canceled. Back at position ${d(a, e)} of ${d(t, e)}.`
18
+ }), M = 16;
19
+ class K {
20
+ constructor(t, e) {
21
+ this._activeGesture = null, this._pending = null, this._session = null, this._translated = /* @__PURE__ */ new Map(), this._announcer = null, this._autoScrollFrame = 0, this._restoringFocus = !1, this._onKeyDown = (s) => {
22
+ const o = this._session;
23
+ if (o?.mode === "pointer") {
24
+ s.key === "Escape" && (s.preventDefault(), s.stopPropagation(), this.cancel());
25
+ return;
26
+ }
27
+ if (o?.mode === "keyboard") {
28
+ const c = this._resolve(s), h = this._items()[o.current];
29
+ if (!c || c.item !== h) {
30
+ this._drop();
31
+ return;
32
+ }
33
+ this._keyboardStep(s, o);
34
+ return;
35
+ }
36
+ if (s.key !== " " && s.key !== "Enter" || this._isDisabled())
37
+ return;
38
+ const n = this._resolve(s);
39
+ if (!n)
40
+ return;
41
+ s.preventDefault();
42
+ const i = this._items(), r = i.indexOf(n.item);
43
+ this._session = {
44
+ mode: "keyboard",
45
+ item: n.item,
46
+ from: r,
47
+ current: r,
48
+ flow: g(this._host)
49
+ }, this._host.ownerDocument.addEventListener("pointerdown", this._onDocumentPointerDown, !0), this._options.onActiveChange?.(n.item, "keyboard"), this._announce(this.message("grabbed")(r + 1, i.length, _(this._host)));
50
+ }, this._onFocusOut = (s) => {
51
+ const o = this._session;
52
+ if (o?.mode !== "keyboard" || this._restoringFocus)
53
+ return;
54
+ const n = s.relatedTarget;
55
+ if (n instanceof Node) {
56
+ const i = this._items()[o.current];
57
+ this._handleOf(i)?.contains(n) || this._drop();
58
+ return;
59
+ }
60
+ requestAnimationFrame(() => {
61
+ if (this._session !== o)
62
+ return;
63
+ const i = p(this._host.ownerDocument), r = this._items(), c = r.includes(o.item) ? o.item : r[o.current], h = this._handleOf(c);
64
+ i && i !== this._host.ownerDocument.body && !h?.contains(i) ? this._drop() : h && i !== h && h.focus();
65
+ });
66
+ }, this._onDocumentPointerDown = (s) => {
67
+ const o = this._session;
68
+ if (o?.mode !== "keyboard")
69
+ return;
70
+ const n = this._handleOf(this._items()[o.current]);
71
+ (!n || !s.composedPath().includes(n)) && this._drop();
72
+ }, this._host = t, this._options = e, this._gesture = new y(t, {
73
+ target: t,
74
+ activation: "axis",
75
+ axis: "both",
76
+ canStart: (s) => this._canStart(s),
77
+ onStart: (s) => this._pointerStart(s, this._gesture),
78
+ onMove: (s) => this._pointerMove(s),
79
+ onEnd: () => this._pointerEnd(),
80
+ onCancel: () => this._cancel()
81
+ }), this._pressGesture = new y(t, {
82
+ target: t,
83
+ activation: "press",
84
+ canStart: (s) => this._canStartPress(s),
85
+ onStart: (s) => this._pointerStart(s, this._pressGesture),
86
+ onMove: (s) => this._pointerMove(s),
87
+ onEnd: (s) => {
88
+ const o = this._session?.mode === "pointer" ? this._session.item : null;
89
+ this._pointerEnd(), o && this._swallowNextClick(o, s);
90
+ },
91
+ onCancel: (s) => {
92
+ const o = this._session?.mode === "pointer" ? this._session.item : null;
93
+ this._cancel(), o && this._swallowNextClick(o, s);
94
+ }
95
+ }), t.addController(this);
96
+ }
97
+ /** Whether an item is currently lifted. */
98
+ get active() {
99
+ return this._session !== null;
100
+ }
101
+ hostConnected() {
102
+ this._host.addEventListener("keydown", this._onKeyDown), this._host.addEventListener("focusout", this._onFocusOut);
103
+ }
104
+ hostDisconnected() {
105
+ this._host.removeEventListener("keydown", this._onKeyDown), this._host.removeEventListener("focusout", this._onFocusOut), this._end(), this._announcer?.dispose(), this._announcer = null;
106
+ }
107
+ /** Cancels any active reorder, restoring the starting position. */
108
+ cancel() {
109
+ if (this._session?.mode === "pointer") {
110
+ this._activeGesture?.setOptions({});
111
+ return;
112
+ }
113
+ this._cancel();
114
+ }
115
+ /**
116
+ * The text of a message in the host's locale: an override, else registered
117
+ * text, else the English default.
118
+ */
119
+ message(t) {
120
+ return O(
121
+ this._host,
122
+ "reorder",
123
+ C,
124
+ this._options.messages?.()
125
+ )[t];
126
+ }
127
+ _items() {
128
+ return this._options.items();
129
+ }
130
+ _isDisabled() {
131
+ return this._options.disabled?.() ?? !1;
132
+ }
133
+ /** The item and handle an event landed in, if it landed on a handle. */
134
+ _resolve(t) {
135
+ const e = this._items();
136
+ for (const s of t.composedPath()) {
137
+ if (!(s instanceof HTMLElement))
138
+ continue;
139
+ if (s === this._host)
140
+ return null;
141
+ if (!s.matches(this._options.handleSelector))
142
+ continue;
143
+ const o = e.find((n) => n.contains(s));
144
+ return o ? { item: o, handle: s } : null;
145
+ }
146
+ return null;
147
+ }
148
+ _handleOf(t) {
149
+ return t?.querySelector(this._options.handleSelector) ?? null;
150
+ }
151
+ _announce(t) {
152
+ const e = this._options.announcerRoot?.() ?? L(this._host);
153
+ this._announcer?.element.parentNode !== e && (this._announcer?.dispose(), this._announcer = new F(e)), this._announcer.announce(t);
154
+ }
155
+ // ---------------------------------------------------------------- pointer
156
+ _canStart(t) {
157
+ if (this._session || this._isDisabled())
158
+ return !1;
159
+ const e = this._resolve(t);
160
+ return this._pending = e ? { item: e.item } : null, e !== null;
161
+ }
162
+ /** Whether a touch or pen press on an item, away from its handle, may long-press it. */
163
+ _canStartPress(t) {
164
+ if (t.pointerType === "mouse" || this._session || this._isDisabled() || this._resolve(t))
165
+ return !1;
166
+ const e = t.composedPath(), s = this._items().find((o) => e.includes(o));
167
+ return this._pending = s ? { item: s } : null, s !== void 0;
168
+ }
169
+ /**
170
+ * Swallows the click that follows lifting a pointer after a long-press
171
+ * drag, which would otherwise activate whatever control the press began on.
172
+ */
173
+ _swallowNextClick(t, e) {
174
+ const s = this._host.ownerDocument, o = (r) => {
175
+ r.detail === 0 || !r.composedPath().includes(t) || r instanceof PointerEvent && r.pointerId > 0 && r.pointerId !== e.pointerId || (r.preventDefault(), r.stopPropagation(), n());
176
+ }, n = () => {
177
+ clearTimeout(i), s.removeEventListener("click", o, !0);
178
+ }, i = setTimeout(n, 400);
179
+ s.addEventListener("click", o, !0);
180
+ }
181
+ _pointerStart(t, e) {
182
+ const s = this._pending?.item;
183
+ this._pending = null, this._activeGesture = e;
184
+ const o = this._items(), n = s ? o.indexOf(s) : -1;
185
+ if (!s || n === -1)
186
+ return;
187
+ const i = g(this._host), r = this._host.getBoundingClientRect(), c = o.map((l) => {
188
+ const u = S(l.getBoundingClientRect(), r, i);
189
+ return { start: u.blockStart, size: u.blockSize };
190
+ }), h = b(this._host);
191
+ this._session = {
192
+ mode: "pointer",
193
+ item: s,
194
+ from: n,
195
+ to: n,
196
+ flow: i,
197
+ slots: c,
198
+ step: R(c, n),
199
+ scroller: h,
200
+ scrollStartCssPx: f(h, i),
201
+ pointerDeltaViewportPx: 0,
202
+ x: t.x,
203
+ y: t.y
204
+ }, this._options.onActiveChange?.(s, "pointer"), this._announce(this.message("grabbed")(n + 1, o.length, _(this._host))), this._pointerMove(t);
205
+ }
206
+ _pointerMove(t) {
207
+ const e = this._session;
208
+ e?.mode === "pointer" && (e.x = t.x, e.y = t.y, e.pointerDeltaViewportPx = k(
209
+ { dx: t.deltaX, dy: t.deltaY },
210
+ "block",
211
+ e.flow
212
+ ), this._layoutPointer(e), this._queueAutoScroll());
213
+ }
214
+ _layoutPointer(t) {
215
+ const e = f(t.scroller, t.flow) - t.scrollStartCssPx, s = t.scroller === this._host.ownerDocument.scrollingElement ? 1 : w(t.scroller), o = t.pointerDeltaViewportPx + e * s, { slots: n, from: i } = t, r = n[i].start + o + n[i].size / 2;
216
+ let c = i;
217
+ for (let l = i + 1; l < n.length; l += 1)
218
+ r > n[l].start + n[l].size / 2 && (c = l);
219
+ for (let l = i - 1; l >= 0; l -= 1)
220
+ r < n[l].start + n[l].size / 2 && (c = l);
221
+ c !== t.to && (t.to = c, this._announce(this.message("moved")(c + 1, n.length, _(this._host)))), this._items().forEach((l, u) => {
222
+ let m = 0;
223
+ l === t.item ? m = o : u > i && u <= c ? m = -t.step : u < i && u >= c && (m = t.step), this._translate(l, m, t.flow);
224
+ });
225
+ }
226
+ _translate(t, e, s) {
227
+ if (e === 0) {
228
+ this._restoreTranslation(t);
229
+ return;
230
+ }
231
+ const o = e / w(t), { x: n, y: i } = v({ inline: 0, block: o }, s);
232
+ this._translated.has(t) || this._translated.set(t, {
233
+ value: t.style.getPropertyValue("translate"),
234
+ priority: t.style.getPropertyPriority("translate")
235
+ }), t.style.setProperty("translate", `${n}px ${i}px`);
236
+ }
237
+ _clearTranslations() {
238
+ for (const t of this._translated.keys())
239
+ this._restoreTranslation(t);
240
+ }
241
+ _restoreTranslation(t) {
242
+ const e = this._translated.get(t);
243
+ e && (this._translated.delete(t), e.value ? t.style.setProperty("translate", e.value, e.priority) : t.style.removeProperty("translate"));
244
+ }
245
+ _queueAutoScroll() {
246
+ this._autoScrollFrame || (this._autoScrollFrame = requestAnimationFrame(() => {
247
+ this._autoScrollFrame = 0;
248
+ const t = this._session;
249
+ if (t?.mode !== "pointer")
250
+ return;
251
+ const e = this._autoScrollSpeed(t);
252
+ if (e === 0)
253
+ return;
254
+ const s = f(t.scroller, t.flow);
255
+ t.scroller.scrollBy(t.flow.block === "x" ? { left: e } : { top: e }), f(t.scroller, t.flow) !== s && (this._layoutPointer(t), this._queueAutoScroll());
256
+ }));
257
+ }
258
+ /** Physical scroll speed for the pointer's distance from the scroller's block edges. */
259
+ _autoScrollSpeed(t) {
260
+ const e = this._options.autoScrollEdge ?? 48, { scroller: s, flow: o } = t, n = this._host.ownerDocument, i = s === n.scrollingElement || s === n.documentElement ? new DOMRect(0, 0, window.innerWidth, window.innerHeight) : s.getBoundingClientRect(), r = o.block === "x", c = r ? t.x : t.y, h = r ? i.left : i.top, l = r ? i.right : i.bottom, u = (m) => Math.ceil((e - Math.max(0, m)) / e * M);
261
+ return c - h < e ? -u(c - h) : l - c < e ? u(l - c) : 0;
262
+ }
263
+ _pointerEnd() {
264
+ const t = this._session;
265
+ if (t?.mode !== "pointer")
266
+ return;
267
+ const { item: e, from: s, to: o } = t, n = t.slots.length;
268
+ this._end(), o !== s && this._commit(e, s, o, "pointer"), this._announce(this.message("dropped")(o + 1, n, _(this._host)));
269
+ }
270
+ _keyboardStep(t, e) {
271
+ const s = this._items().length, o = D(E("vertical", e.flow), e.flow);
272
+ let n;
273
+ switch (t.key) {
274
+ case o.next:
275
+ n = Math.min(s - 1, e.current + 1);
276
+ break;
277
+ case o.prev:
278
+ n = Math.max(0, e.current - 1);
279
+ break;
280
+ case "Home":
281
+ n = 0;
282
+ break;
283
+ case "End":
284
+ n = s - 1;
285
+ break;
286
+ case " ":
287
+ case "Enter":
288
+ t.preventDefault(), this._drop();
289
+ return;
290
+ case "Escape": {
291
+ t.preventDefault(), t.stopPropagation();
292
+ const { item: i, current: r, from: c } = e;
293
+ this._end(), r !== c && this._commit(i, r, c, "keyboard"), this._announce(this.message("canceled")(c + 1, s, _(this._host)));
294
+ return;
295
+ }
296
+ default:
297
+ return;
298
+ }
299
+ t.preventDefault(), n !== e.current && (this._commit(e.item, e.current, n, "keyboard"), e.current = n, this._announce(this.message("moved")(n + 1, s, _(this._host))));
300
+ }
301
+ /** Ends a keyboard reorder where the item now is. */
302
+ _drop() {
303
+ if (this._session?.mode !== "keyboard")
304
+ return;
305
+ const t = this._items().length, e = this._session.current + 1;
306
+ this._end(), this._announce(this.message("dropped")(e, t, _(this._host)));
307
+ }
308
+ // ----------------------------------------------------------------- shared
309
+ /**
310
+ * Applies a move and keeps focus on the moved item's handle. Moving a node
311
+ * drops its focus, and a framework may replace the node outright, so the
312
+ * handle is looked up again once the host has rendered.
313
+ */
314
+ _commit(t, e, s, o) {
315
+ const n = this._handleOf(t), i = n !== null && n === p(this._host.ownerDocument);
316
+ this._restoringFocus = i;
317
+ try {
318
+ this._options.onMove(t, e, s, o);
319
+ } finally {
320
+ i || (this._restoringFocus = !1);
321
+ }
322
+ i && requestAnimationFrame(() => {
323
+ const r = this._items(), c = r.includes(t) ? t : r[s], h = this._handleOf(c);
324
+ this._session?.mode === "keyboard" && c && c !== this._session.item && (this._session.item = c), this._session?.mode === "keyboard" && c && (this._session.current = r.indexOf(c)), h && p(this._host.ownerDocument) !== h && h.focus(), this._restoringFocus = !1;
325
+ });
326
+ }
327
+ _cancel() {
328
+ const t = this._session;
329
+ if (!t)
330
+ return;
331
+ const e = this._items().length;
332
+ this._end(), t.mode === "keyboard" && t.current !== t.from && this._commit(t.item, t.current, t.from, "keyboard"), this._announce(this.message("canceled")(t.from + 1, e, _(this._host)));
333
+ }
334
+ _end() {
335
+ this._autoScrollFrame && (cancelAnimationFrame(this._autoScrollFrame), this._autoScrollFrame = 0);
336
+ const t = this._session !== null;
337
+ this._host.ownerDocument.removeEventListener("pointerdown", this._onDocumentPointerDown, !0), this._session = null, this._pending = null, this._activeGesture = null, t && this._options.onActiveChange?.(null, null), this._clearTranslations();
338
+ }
339
+ }
340
+ function f(a, t) {
341
+ const e = g(a), s = e.block === t.block ? "block" : "inline", o = P(a, s, e);
342
+ return x(s, e) === t.blockReversed ? o : -o;
343
+ }
344
+ function R(a, t) {
345
+ const e = a[t], s = a[t + 1], o = a[t - 1];
346
+ return s ? s.start - e.start : o ? e.start + e.size - (o.start + o.size) : e.size;
347
+ }
348
+ function L(a) {
349
+ let t = a;
350
+ for (; t; ) {
351
+ if (t.localName === "dialog" && t.hasAttribute("open") || t.hasAttribute("popover") && t.matches(":popover-open"))
352
+ return t;
353
+ const e = t.getRootNode();
354
+ t = t.parentElement ?? (e instanceof ShadowRoot ? e.host : null);
355
+ }
356
+ return a.ownerDocument.body;
357
+ }
358
+ export {
359
+ C as DEFAULT_REORDER_MESSAGES,
360
+ K as ReorderController
361
+ };