@rcarls/rc-common 0.6.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 (73) hide show
  1. package/CHANGELOG.md +295 -0
  2. package/README.md +148 -72
  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 -1711
  39. package/dist/rc-common.umd.cjs +10 -26
  40. package/dist/types/AnchorController.d.ts +45 -22
  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/ScrollObserverController.d.ts +13 -2
  56. package/dist/types/TypeaheadController.d.ts +8 -2
  57. package/dist/types/anchorPlacement.d.ts +24 -0
  58. package/dist/types/clickDelegate.d.ts +9 -56
  59. package/dist/types/cssText.d.ts +17 -0
  60. package/dist/types/cssTime.d.ts +14 -0
  61. package/dist/types/cssZoom.d.ts +12 -0
  62. package/dist/types/findNearestScrollAncestor.d.ts +3 -3
  63. package/dist/types/flow.d.ts +121 -0
  64. package/dist/types/index.d.ts +13 -1
  65. package/dist/types/localeFormat.d.ts +4 -0
  66. package/dist/types/messages.d.ts +105 -0
  67. package/dist/types/nativeChild.d.ts +0 -3
  68. package/dist/types/observeLocale.d.ts +23 -0
  69. package/dist/types/softKeyboard.d.ts +79 -0
  70. package/dist/types/visualViewport.d.ts +21 -0
  71. package/package.json +1 -4
  72. package/dist/css-anchor-positioning-fn-hFYJeBk4.js +0 -6371
  73. package/dist/types/MouseMoveDirective.d.ts +0 -42
@@ -0,0 +1,89 @@
1
+ let r = 0;
2
+ class h {
3
+ constructor(t, e) {
4
+ this._activeItem = null, this._activeIndex = -1, this._itemCounter = 0, this._opts = e, this._instanceId = `rc-adc-${++r}`, t.addController(this);
5
+ }
6
+ hostConnected() {
7
+ }
8
+ hostDisconnected() {
9
+ this.clear();
10
+ }
11
+ /** The currently active item, or `null` when cleared. */
12
+ get activeItem() {
13
+ return this._activeItem;
14
+ }
15
+ /** Index into the current `items()` array. `-1` when cleared. */
16
+ get activeIndex() {
17
+ return this._activeIndex;
18
+ }
19
+ /**
20
+ * Move the virtual cursor by `delta` steps (+1 forward, -1 backward),
21
+ * skipping any items for which `isSkipped` returns true.
22
+ */
23
+ navigate(t) {
24
+ const e = this._opts.items();
25
+ if (e.length === 0)
26
+ return;
27
+ const i = this._opts.wrap ?? !0;
28
+ let s = this._activeIndex < 0 ? t > 0 ? -1 : e.length : this._activeIndex;
29
+ const n = e.length;
30
+ let a = 0;
31
+ do
32
+ if (s += t, i ? s = (s % n + n) % n : s = Math.max(0, Math.min(n - 1, s)), a++, !this._isSkipped(e[s]))
33
+ break;
34
+ while (a < n);
35
+ this._setActive(e[s] ?? null, s);
36
+ }
37
+ /** Move the virtual cursor to the first non-skipped item. */
38
+ navigateToFirst() {
39
+ const t = this._opts.items(), e = t.findIndex((i) => !this._isSkipped(i));
40
+ e >= 0 && this._setActive(t[e], e);
41
+ }
42
+ /** Move the virtual cursor to the last non-skipped item. */
43
+ navigateToLast() {
44
+ const t = this._opts.items();
45
+ let e = -1;
46
+ for (let i = t.length - 1; i >= 0; i--)
47
+ if (!this._isSkipped(t[i])) {
48
+ e = i;
49
+ break;
50
+ }
51
+ e >= 0 && this._setActive(t[e], e);
52
+ }
53
+ /** Move the virtual cursor to the item at `index`. */
54
+ navigateTo(t) {
55
+ const i = this._opts.items()[t];
56
+ i && !this._isSkipped(i) && this._setActive(i, t);
57
+ }
58
+ /** Move the virtual cursor to a specific item reference. */
59
+ navigateToItem(t) {
60
+ const i = this._opts.items().indexOf(t);
61
+ i >= 0 && !this._isSkipped(t) && this._setActive(t, i);
62
+ }
63
+ /**
64
+ * Clear the virtual cursor: removes `aria-activedescendant` from the host,
65
+ * removes `data-active` from the previously active item, and resets internal state.
66
+ */
67
+ clear() {
68
+ this._activeItem?.removeAttribute("data-active"), this._activeItem = null, this._activeIndex = -1;
69
+ const t = this._opts.host();
70
+ t && t.removeAttribute("aria-activedescendant");
71
+ }
72
+ _isSkipped(t) {
73
+ return !t || t.hidden || t.getAttribute("aria-disabled") === "true" ? !0 : this._opts.isSkipped?.(t) ?? !1;
74
+ }
75
+ _setActive(t, e) {
76
+ this._activeItem?.removeAttribute("data-active"), this._activeItem = t, this._activeIndex = e;
77
+ const i = this._opts.host();
78
+ if (i) {
79
+ if (!t) {
80
+ i.removeAttribute("aria-activedescendant");
81
+ return;
82
+ }
83
+ t.id || (t.id = `${this._instanceId}-${++this._itemCounter}`), i.setAttribute("aria-activedescendant", t.id), t.setAttribute("data-active", ""), t.scrollIntoView({ block: "nearest" });
84
+ }
85
+ }
86
+ }
87
+ export {
88
+ h as ActiveDescendantController
89
+ };
@@ -0,0 +1,317 @@
1
+ import { cssZoom as $ } from "./cssZoom.js";
2
+ import { resolveAnchorPlacement as S } from "./anchorPlacement.js";
3
+ import { resolveFlow as A } from "./flow.js";
4
+ import { observeLocale as L } from "./observeLocale.js";
5
+ import { RafScheduler as F } from "./RafScheduler.js";
6
+ import { getVisualViewportBounds as P } from "./visualViewport.js";
7
+ function w(C, e) {
8
+ const t = `${e}px`;
9
+ switch (C) {
10
+ case "top":
11
+ return `position-area: top; margin-bottom: ${t};`;
12
+ case "top-start":
13
+ return `bottom: anchor(top); top: auto; left: anchor(left); right: auto; margin-bottom: ${t};`;
14
+ case "top-end":
15
+ return `bottom: anchor(top); top: auto; left: auto; right: anchor(right); margin-bottom: ${t};`;
16
+ case "bottom":
17
+ return `position-area: bottom; margin-top: ${t};`;
18
+ case "bottom-start":
19
+ return `top: anchor(bottom); bottom: auto; left: anchor(left); right: auto; margin-top: ${t};`;
20
+ case "bottom-end":
21
+ return `top: anchor(bottom); bottom: auto; left: auto; right: anchor(right); margin-top: ${t};`;
22
+ case "left":
23
+ return `position-area: left; margin-right: ${t};`;
24
+ case "left-start":
25
+ return `top: anchor(top); bottom: auto; right: anchor(left); left: auto; margin-right: ${t};`;
26
+ case "left-end":
27
+ return `top: auto; bottom: anchor(bottom); right: anchor(left); left: auto; margin-right: ${t};`;
28
+ case "right":
29
+ return `position-area: right; margin-left: ${t};`;
30
+ case "right-start":
31
+ return `top: anchor(top); bottom: auto; left: anchor(right); right: auto; margin-left: ${t};`;
32
+ case "right-end":
33
+ return `top: auto; bottom: anchor(bottom); left: anchor(right); right: auto; margin-left: ${t};`;
34
+ }
35
+ }
36
+ const x = {
37
+ top: "bottom",
38
+ "top-start": "bottom-start",
39
+ "top-end": "bottom-end",
40
+ bottom: "top",
41
+ "bottom-start": "top-start",
42
+ "bottom-end": "top-end",
43
+ left: "right",
44
+ "left-start": "right-start",
45
+ "left-end": "right-end",
46
+ right: "left",
47
+ "right-start": "left-start",
48
+ "right-end": "left-end"
49
+ }, O = {
50
+ top: "top",
51
+ "top-start": "top-end",
52
+ "top-end": "top-start",
53
+ bottom: "bottom",
54
+ "bottom-start": "bottom-end",
55
+ "bottom-end": "bottom-start",
56
+ left: "left",
57
+ "left-start": "left-end",
58
+ "left-end": "left-start",
59
+ right: "right",
60
+ "right-start": "right-end",
61
+ "right-end": "right-start"
62
+ }, R = typeof CSS < "u" && CSS.supports("position-try-fallbacks: flip-block") && CSS.supports("min-width: anchor-size(width)");
63
+ let z = 0;
64
+ class M {
65
+ constructor(e, t) {
66
+ this._styleEl = null, this._adoptedSheet = null, this._connected = !1, this._clampScheduler = new F(), this._clampLoopActive = !1, this._settleFramesRemaining = 0, this._stableFrames = 0, this._lastNaturalRect = "", this._appliedTranslateXCssPx = 0, this._appliedTranslateYCssPx = 0, this._geometryAbort = null, this._resizeObserver = null, this._intersectionObserver = null, this._mutationObserver = null, this._unobserveLocale = null, this._$observedAnchor = null, this._$observedFloating = null, this._handleGeometryChange = () => {
67
+ this._scheduleClamp();
68
+ }, this._handlePositionChange = () => {
69
+ !this._usesNativeAnchor() && this._connected && !this._disabled() && this._positionFallback(), this._scheduleClamp();
70
+ }, this._handleDirectionChange = () => {
71
+ this._applyAndPosition();
72
+ }, this._host = e, this._opts = t, this._uid = `rc-anchor-${++z}`, e.addController(this);
73
+ }
74
+ setOptions(e) {
75
+ Object.assign(this._opts, e), this._applyAndPosition();
76
+ }
77
+ /** Re-apply positioning after the popup becomes visible or the anchor moves. */
78
+ update() {
79
+ this._applyAndPosition();
80
+ }
81
+ hostConnected() {
82
+ this._connected = !0;
83
+ const e = this._host, t = typeof e.getRootNode == "function" ? e : this._anchor();
84
+ this._unobserveLocale ??= L(
85
+ this._handleDirectionChange,
86
+ t ?? void 0,
87
+ () => [t, this._anchor()]
88
+ ), this._applyAndPosition();
89
+ }
90
+ hostDisconnected() {
91
+ this._connected = !1, this._unobserveLocale?.(), this._unobserveLocale = null, this._cleanup();
92
+ }
93
+ _anchor() {
94
+ const { anchor: e } = this._opts;
95
+ return typeof e == "function" ? e() : e;
96
+ }
97
+ _floating() {
98
+ const { floating: e } = this._opts;
99
+ return typeof e == "function" ? e() : e;
100
+ }
101
+ _shadowHost() {
102
+ const { shadowHost: e } = this._opts;
103
+ return typeof e == "function" ? e() : e ?? null;
104
+ }
105
+ _disabled() {
106
+ const { disabled: e } = this._opts;
107
+ return typeof e == "function" ? e() : e ?? !1;
108
+ }
109
+ // A method rather than a direct read of the module constant, so tests can
110
+ // run the script fallback in engines with native support.
111
+ _usesNativeAnchor() {
112
+ return R;
113
+ }
114
+ _applyAndPosition() {
115
+ !this._connected || !this._apply() || (this._usesNativeAnchor() || this._positionFallback(), this._startWatchingGeometry(), this._scheduleClamp());
116
+ }
117
+ /**
118
+ * Checks viewport overflow during a bounded post-open settling window.
119
+ * Observers and viewport events restart the window when geometry changes,
120
+ * avoiding permanent layout polling while a popup is idle.
121
+ */
122
+ _scheduleClamp() {
123
+ !this._connected || this._disabled() || (this._clampLoopActive || (this._clampLoopActive = !0), this._settleFramesRemaining = 10, this._stableFrames = 0, this._lastNaturalRect = "", this._clampScheduler.schedule(() => this._runClampTick()));
124
+ }
125
+ _runClampTick() {
126
+ if (!this._connected || this._disabled()) {
127
+ this._stopClampLoop();
128
+ return;
129
+ }
130
+ const e = this._floating();
131
+ if (!e) {
132
+ this._stopClampLoop();
133
+ return;
134
+ }
135
+ const t = e.getBoundingClientRect(), s = $(e);
136
+ if (!(t.width > 0 || t.height > 0)) {
137
+ this._stopClampLoop(), this._stopWatchingGeometry();
138
+ return;
139
+ }
140
+ const l = [
141
+ t.left - this._appliedTranslateXCssPx * s,
142
+ t.top - this._appliedTranslateYCssPx * s,
143
+ t.width,
144
+ t.height
145
+ ].map((n) => Math.round(n * 4) / 4).join(":");
146
+ if (this._stableFrames = l === this._lastNaturalRect ? this._stableFrames + 1 : 0, this._lastNaturalRect = l, this._settleFramesRemaining -= 1, this._clampToViewport(t), this._stableFrames >= 3 || this._settleFramesRemaining <= 0) {
147
+ this._stopClampLoop();
148
+ return;
149
+ }
150
+ this._clampScheduler.schedule(() => this._runClampTick());
151
+ }
152
+ _stopClampLoop() {
153
+ this._clampScheduler.cancel(), this._clampLoopActive = !1, this._settleFramesRemaining = 0, this._stableFrames = 0, this._lastNaturalRect = "";
154
+ }
155
+ _startWatchingGeometry() {
156
+ const e = this._anchor(), t = this._floating();
157
+ if (!e || !t) {
158
+ this._stopWatchingGeometry();
159
+ return;
160
+ }
161
+ if (this._geometryAbort && this._$observedAnchor === e && this._$observedFloating === t)
162
+ return;
163
+ this._stopWatchingGeometry(), this._$observedAnchor = e, this._$observedFloating = t, this._geometryAbort = new AbortController();
164
+ const s = t.ownerDocument.defaultView, i = { passive: !0, signal: this._geometryAbort.signal };
165
+ t.ownerDocument.addEventListener("scroll", this._handlePositionChange, {
166
+ ...i,
167
+ capture: !0
168
+ }), s?.addEventListener("resize", this._handlePositionChange, i), s?.visualViewport?.addEventListener("resize", this._handlePositionChange, i), s?.visualViewport?.addEventListener("scroll", this._handlePositionChange, i), typeof ResizeObserver == "function" && (this._resizeObserver = new ResizeObserver(this._handlePositionChange), this._resizeObserver.observe(e), this._resizeObserver.observe(t)), typeof IntersectionObserver == "function" && (this._intersectionObserver = new IntersectionObserver(this._handlePositionChange, {
169
+ threshold: [0, 1]
170
+ }), this._intersectionObserver.observe(t)), typeof MutationObserver == "function" && (this._mutationObserver = new MutationObserver(this._handleGeometryChange), this._mutationObserver.observe(e, {
171
+ attributes: !0,
172
+ attributeFilter: ["class", "style", "hidden", "open"]
173
+ }), this._mutationObserver.observe(t, {
174
+ attributes: !0,
175
+ attributeFilter: ["class", "style", "hidden", "open"]
176
+ }));
177
+ }
178
+ _stopWatchingGeometry() {
179
+ this._geometryAbort?.abort(), this._geometryAbort = null, this._resizeObserver?.disconnect(), this._resizeObserver = null, this._intersectionObserver?.disconnect(), this._intersectionObserver = null, this._mutationObserver?.disconnect(), this._mutationObserver = null, this._$observedAnchor = null, this._$observedFloating = null;
180
+ }
181
+ /**
182
+ * Nudges the floating element back on-screen via `translate` if whatever
183
+ * positioned it (native or otherwise) still left it overflowing the
184
+ * viewport. `translate` is used rather than adjusting `left`/`top`/etc.
185
+ * directly because it's a pure visual offset on top of the existing
186
+ * positioning, independent of which mechanism produced it — no need to
187
+ * know or reconstruct the underlying `left`/`right`/`position-anchor`
188
+ * values to correct them. It's safe to apply even to a popover-promoted
189
+ * element with its own further-nested anchor-positioned descendants (e.g.
190
+ * a submenu): top-layer promotion means a nested popover isn't contained
191
+ * by this element's box for `position: fixed` purposes, so translating
192
+ * this one doesn't drag a nested popup along or break its own anchoring.
193
+ *
194
+ * `getBoundingClientRect()` reflects whatever `translate` the previous
195
+ * update applied, and computing the new correction directly
196
+ * off that rect would see the already-corrected (on-screen) position,
197
+ * clear the correction as unnecessary, then reapply it next frame once
198
+ * the underlying overflow reappears — an every-frame flicker confirmed
199
+ * by a real click landing on a mid-flicker frame in the browser test
200
+ * suite. `_appliedTranslateXCssPx`/`_appliedTranslateYCssPx` track the offset this method itself
201
+ * last applied so it can subtract it back out and compute against the
202
+ * element's natural, uncorrected position each time.
203
+ */
204
+ _clampToViewport(e) {
205
+ const t = this._floating();
206
+ if (!t)
207
+ return;
208
+ const s = 4, i = e ?? t.getBoundingClientRect();
209
+ if (i.width === 0 && i.height === 0)
210
+ return;
211
+ const l = t.ownerDocument.defaultView;
212
+ if (!l)
213
+ return;
214
+ const n = P(l), o = $(t), _ = this._appliedTranslateXCssPx * o, a = this._appliedTranslateYCssPx * o, y = i.left - _, v = i.right - _, r = i.top - a, h = i.bottom - a;
215
+ let c = 0, p = 0;
216
+ v > n.right - s && (c = n.right - s - v), y + c < n.left + s && (c = n.left + s - y), h > n.bottom - s && (p = n.bottom - s - h), r + p < n.top + s && (p = n.top + s - r), this._appliedTranslateXCssPx = c / o, this._appliedTranslateYCssPx = p / o;
217
+ const d = c || p ? `${this._appliedTranslateXCssPx}px ${this._appliedTranslateYCssPx}px` : "", m = `${Math.max(0, n.width - s * 2) / o}px`, f = `${Math.max(0, n.height - s * 2) / o}px`;
218
+ t.style.translate !== d && (t.style.translate = d), t.style.getPropertyValue("--rc-anchor-viewport-inline-size") !== m && t.style.setProperty("--rc-anchor-viewport-inline-size", m), t.style.getPropertyValue("--rc-anchor-viewport-block-size") !== f && t.style.setProperty("--rc-anchor-viewport-block-size", f);
219
+ }
220
+ /**
221
+ * The configured placement resolved against the anchor's writing mode and
222
+ * direction. Read at every apply, since a `dir` change fires no event.
223
+ */
224
+ _physicalPlacement(e) {
225
+ return S(this._opts.placement ?? "block-end-start", A(e));
226
+ }
227
+ _apply() {
228
+ if (this._disabled())
229
+ return this._cleanup(), !1;
230
+ const e = this._anchor(), t = this._floating();
231
+ if (!e || !t)
232
+ return this._stopClampLoop(), !1;
233
+ const s = this._physicalPlacement(e), i = this._opts.offset ?? 4, l = this._opts.flip ?? !0, n = `--${this._uid}`, o = this._uid;
234
+ e.style.setProperty("anchor-name", n), t.setAttribute("data-rc-anchor", o);
235
+ const _ = x[s], a = O[s], y = x[a], v = l ? `
236
+ @position-try --${o}-end { ${w(a, i)} }
237
+ @position-try --${o}-flip { ${w(_, i)} }
238
+ @position-try --${o}-both { ${w(y, i)} }
239
+ ` : "", r = l ? `position-try-fallbacks: --${o}-end, --${o}-flip, --${o}-both;` : "", h = `
240
+ ${v}
241
+ [data-rc-anchor="${o}"] {
242
+ position: fixed;
243
+ position-anchor: ${n};
244
+ box-sizing: border-box;
245
+ min-width: anchor-size(width);
246
+ ${w(s, i)}
247
+ ${r}
248
+ }
249
+ `;
250
+ return this._injectStyles(h), !0;
251
+ }
252
+ _injectStyles(e) {
253
+ const t = this._shadowHost();
254
+ if (t?.shadowRoot)
255
+ this._adoptedSheet || (this._adoptedSheet = new CSSStyleSheet(), t.shadowRoot.adoptedStyleSheets = [
256
+ ...t.shadowRoot.adoptedStyleSheets,
257
+ this._adoptedSheet
258
+ ]), this._adoptedSheet.replaceSync(e);
259
+ else {
260
+ this._styleEl?.remove();
261
+ const s = this._floating()?.ownerDocument ?? this._anchor()?.ownerDocument;
262
+ if (!s)
263
+ return;
264
+ this._styleEl = s.createElement("style"), this._styleEl.setAttribute("data-rc-anchor-style", this._uid), this._styleEl.textContent = e, s.head.appendChild(this._styleEl);
265
+ }
266
+ }
267
+ /**
268
+ * Hand-rolled positioning for browsers without full native anchor
269
+ * positioning. Handles all four placement
270
+ * sides (not just top/bottom — a side flyout like a cascading submenu
271
+ * needs its primary axis to be horizontal, offsetting by the anchor's
272
+ * own width/height rather than always stacking below it), with the
273
+ * `-start`/`-end` suffix controlling cross-axis alignment and an
274
+ * unsuffixed placement centering on the cross axis.
275
+ */
276
+ _positionFallback() {
277
+ const e = this._anchor(), t = this._floating();
278
+ if (!e || !t)
279
+ return;
280
+ const s = this._physicalPlacement(e), i = this._opts.offset ?? 4, l = this._opts.flip ?? !0, n = 4, o = e.getBoundingClientRect(), _ = e.ownerDocument.defaultView;
281
+ if (!_)
282
+ return;
283
+ const a = P(_), y = _.innerWidth, v = _.innerHeight, r = $(t), h = i * r;
284
+ t.style.position = "fixed", t.style.boxSizing = "border-box", t.style.minWidth = `${o.width / r}px`, t.style.top = "", t.style.bottom = "", t.style.left = "", t.style.right = "";
285
+ const c = (t.offsetWidth || o.width / r) * r, p = t.offsetHeight * r, d = s.split("-")[0], m = s.endsWith("-start") ? "start" : s.endsWith("-end") ? "end" : "center";
286
+ let f = d;
287
+ if (d === "top" || d === "bottom") {
288
+ const u = a.bottom - o.bottom, b = o.top - a.top;
289
+ l && (d === "bottom" && u < p + h && b > u ? f = "top" : d === "top" && b < p + h && u > b && (f = "bottom")), f === "top" ? t.style.bottom = `${(v - o.top + h) / r}px` : t.style.top = `${(o.bottom + h) / r}px`;
290
+ let g = m === "end" ? o.right - c : m === "start" ? o.left : o.left + o.width / 2 - c / 2;
291
+ g = Math.max(
292
+ a.left + n,
293
+ Math.min(g, a.right - c - n)
294
+ ), t.style.left = `${g / r}px`;
295
+ } else {
296
+ const u = a.right - o.right, b = o.left - a.left;
297
+ l && (d === "right" && u < c + h && b > u ? f = "left" : d === "left" && b < c + h && u > b && (f = "right")), f === "left" ? t.style.right = `${(y - o.left + h) / r}px` : t.style.left = `${(o.right + h) / r}px`;
298
+ let g = m === "end" ? o.bottom - p : m === "start" ? o.top : o.top + o.height / 2 - p / 2;
299
+ g = Math.max(
300
+ a.top + n,
301
+ Math.min(g, a.bottom - p - n)
302
+ ), t.style.top = `${g / r}px`;
303
+ }
304
+ }
305
+ _cleanup() {
306
+ this._stopClampLoop(), this._stopWatchingGeometry(), this._appliedTranslateXCssPx = 0, this._appliedTranslateYCssPx = 0;
307
+ const e = this._anchor(), t = this._floating();
308
+ e && e.style.removeProperty("anchor-name"), t && (t.removeAttribute("data-rc-anchor"), t.style.translate = "", t.style.removeProperty("--rc-anchor-viewport-inline-size"), t.style.removeProperty("--rc-anchor-viewport-block-size")), this._styleEl?.remove(), this._styleEl = null;
309
+ const s = this._shadowHost();
310
+ s?.shadowRoot && this._adoptedSheet && (s.shadowRoot.adoptedStyleSheets = s.shadowRoot.adoptedStyleSheets.filter(
311
+ (i) => i !== this._adoptedSheet
312
+ ), this._adoptedSheet = null);
313
+ }
314
+ }
315
+ export {
316
+ M as AnchorController
317
+ };
@@ -0,0 +1,60 @@
1
+ class p {
2
+ constructor() {
3
+ this._cleanups = [], this._attributes = /* @__PURE__ */ new WeakMap(), this._styles = /* @__PURE__ */ new WeakMap(), this._disposed = !1;
4
+ }
5
+ add(t) {
6
+ if (this._disposed) {
7
+ t();
8
+ return;
9
+ }
10
+ this._cleanups.push(t);
11
+ }
12
+ listen(t, e, r, i) {
13
+ this._disposed || (t.addEventListener(e, r, i), this.add(() => t.removeEventListener(e, r, i)));
14
+ }
15
+ append(t, e) {
16
+ this._disposed || (t.appendChild(e), this.add(() => {
17
+ e.parentNode === t && t.removeChild(e);
18
+ }));
19
+ }
20
+ setAttribute(t, e, r) {
21
+ if (this._disposed)
22
+ return;
23
+ let i = this._attributes.get(t);
24
+ i || (i = /* @__PURE__ */ new Map(), this._attributes.set(t, i));
25
+ let s = i.get(e);
26
+ s || (s = { written: r, previous: t.getAttribute(e) }, i.set(e, s), this.add(() => {
27
+ t.getAttribute(e) === s?.written && (s.previous === null ? t.removeAttribute(e) : t.setAttribute(e, s.previous));
28
+ })), s.written = r, t.setAttribute(e, r);
29
+ }
30
+ setStyle(t, e, r, i = "") {
31
+ if (this._disposed)
32
+ return;
33
+ let s = this._styles.get(t);
34
+ s || (s = /* @__PURE__ */ new Map(), this._styles.set(t, s));
35
+ let o = s.get(e);
36
+ o || (o = {
37
+ written: r,
38
+ writtenPriority: i,
39
+ previous: t.style.getPropertyValue(e),
40
+ previousPriority: t.style.getPropertyPriority(e)
41
+ }, s.set(e, o), this.add(() => {
42
+ t.style.getPropertyValue(e) !== o?.written || t.style.getPropertyPriority(e) !== o.writtenPriority || (o.previous ? t.style.setProperty(e, o.previous, o.previousPriority) : t.style.removeProperty(e));
43
+ })), o.written = r, o.writtenPriority = i, t.style.setProperty(e, r, i);
44
+ }
45
+ dispose() {
46
+ if (!this._disposed) {
47
+ this._disposed = !0;
48
+ for (const t of this._cleanups.reverse())
49
+ t();
50
+ this._cleanups.length = 0;
51
+ }
52
+ }
53
+ /** Keep current effects when another owner takes over their state. */
54
+ abandon() {
55
+ this._disposed = !0, this._cleanups.length = 0;
56
+ }
57
+ }
58
+ export {
59
+ p as ControllerEffects
60
+ };
@@ -0,0 +1,128 @@
1
+ import { ControllerEffects as x } from "./ControllerEffects.js";
2
+ import { cssZoom as p } from "./cssZoom.js";
3
+ import { DragGestureController as y } from "./DragGestureController.js";
4
+ class T {
5
+ constructor(e, t) {
6
+ this._dragging = !1, this._startLeft = 0, this._startTop = 0, this._zoom = 1, this._handleEffects = /* @__PURE__ */ new Map(), this._connected = !1, this._disposed = !1, this._host = e, this._opts = {
7
+ axis: "both",
8
+ step: 4,
9
+ disabled: !1,
10
+ handle: null,
11
+ bounds: "viewport",
12
+ ...t
13
+ }, this._onKeyDown = this._handleKeyDown.bind(this), this._gesture = new y(e, {
14
+ target: () => this._handle(),
15
+ axis: this._opts.axis,
16
+ canStart: (s) => this._canStartGesture(s),
17
+ onStart: (s) => this._startDrag(s),
18
+ onMove: (s) => this._moveDrag(s),
19
+ onEnd: () => this._finishDrag(),
20
+ onCancel: () => this._finishDrag()
21
+ }), e.addController(this);
22
+ }
23
+ /** Reconfigures drag behavior and rebinds the handle when it changes. */
24
+ setOptions(e) {
25
+ if (this._disposed)
26
+ return;
27
+ const t = this._handle();
28
+ Object.assign(this._opts, e), this._gesture.setOptions({ axis: this._opts.axis, target: () => this._handle() });
29
+ const s = this._handle();
30
+ this._connected && t !== s && (this._detachFrom(t), this._attachTo(s));
31
+ }
32
+ hostConnected() {
33
+ this._disposed || this._connected || (this._connected = !0, this._attachTo(this._handle()));
34
+ }
35
+ hostDisconnected() {
36
+ this._connected = !1, this._detachFrom(this._handle());
37
+ }
38
+ /**
39
+ * Detaches the handle (undoing the `tabindex` it added) and removes the
40
+ * controller and its gesture from the host for good. Use it to replace a
41
+ * controller when move options change after setup.
42
+ */
43
+ dispose() {
44
+ this._disposed || (this._disposed = !0, this._gesture.dispose(), this.hostDisconnected(), this._host.removeController(this));
45
+ }
46
+ _handle() {
47
+ return this._opts.handle ?? this._opts.target;
48
+ }
49
+ _target() {
50
+ return this._opts.target;
51
+ }
52
+ _attachTo(e) {
53
+ if (this._handleEffects.has(e))
54
+ return;
55
+ const t = new x();
56
+ t.listen(e, "keydown", this._onKeyDown), e.hasAttribute("tabindex") || t.setAttribute(e, "tabindex", "0"), this._handleEffects.set(e, t);
57
+ }
58
+ _detachFrom(e) {
59
+ this._handleEffects.get(e)?.dispose(), this._handleEffects.delete(e);
60
+ }
61
+ _canStartGesture(e) {
62
+ return this._opts.disabled ? !1 : !e.target.closest('button, [role="button"], a');
63
+ }
64
+ _startDrag(e) {
65
+ const t = this._target(), s = t.getBoundingClientRect();
66
+ this._zoom = p(t), getComputedStyle(t).position === "static" && (t.style.position = "fixed"), t.style.translate = "none", t.style.inset = "auto", t.style.margin = "0", this._startLeft = s.left / this._zoom, this._startTop = s.top / this._zoom, t.style.left = `${this._startLeft}px`, t.style.top = `${this._startTop}px`, this._dragging = !0;
67
+ }
68
+ _moveDrag(e) {
69
+ if (!this._dragging)
70
+ return;
71
+ const t = this._target(), { axis: s } = this._opts, i = this._zoom, [a, o, n, r] = this._boundsRect().map((d) => d / i), { width: h, height: l } = t.getBoundingClientRect(), _ = s !== "y" ? Math.min(Math.max(this._startLeft + e.deltaX / i, a), n - h / i) : parseFloat(t.style.left) || 0, c = s !== "x" ? Math.min(Math.max(this._startTop + e.deltaY / i, o), r - l / i) : parseFloat(t.style.top) || 0;
72
+ t.style.left = `${_}px`, t.style.top = `${c}px`;
73
+ }
74
+ _finishDrag() {
75
+ this._dragging && (this._dragging = !1);
76
+ }
77
+ _handleKeyDown(e) {
78
+ if (this._opts.disabled || e.currentTarget !== e.target)
79
+ return;
80
+ const { axis: t } = this._opts, s = e.shiftKey ? this._opts.step * 10 : this._opts.step;
81
+ let i = 0, a = 0;
82
+ switch (e.key) {
83
+ case "ArrowLeft":
84
+ if (t === "y")
85
+ return;
86
+ i = -s;
87
+ break;
88
+ case "ArrowRight":
89
+ if (t === "y")
90
+ return;
91
+ i = s;
92
+ break;
93
+ case "ArrowUp":
94
+ if (t === "x")
95
+ return;
96
+ a = -s;
97
+ break;
98
+ case "ArrowDown":
99
+ if (t === "x")
100
+ return;
101
+ a = s;
102
+ break;
103
+ default:
104
+ return;
105
+ }
106
+ const o = this._target(), n = p(o), r = o.getBoundingClientRect();
107
+ getComputedStyle(o).position === "static" && (o.style.position = "fixed", o.style.translate = "none", o.style.left = `${r.left / n}px`, o.style.top = `${r.top / n}px`);
108
+ const [h, l, _, c] = this._boundsRect().map((m) => m / n), d = parseFloat(o.style.left) || 0, f = parseFloat(o.style.top) || 0, g = _ - r.width / n, u = c - r.height / n;
109
+ o.style.left = `${Math.min(Math.max(d + i, h), g)}px`, o.style.top = `${Math.min(Math.max(f + a, l), u)}px`, e.preventDefault(), e.stopPropagation();
110
+ }
111
+ _boundsRect() {
112
+ const { bounds: e } = this._opts;
113
+ if (!e || e === "viewport")
114
+ return [0, 0, window.innerWidth, window.innerHeight];
115
+ if (e === "parent") {
116
+ const s = this._opts.target.parentElement;
117
+ if (!s)
118
+ return [0, 0, window.innerWidth, window.innerHeight];
119
+ const i = s.getBoundingClientRect();
120
+ return [i.left, i.top, i.right, i.bottom];
121
+ }
122
+ const t = e.getBoundingClientRect();
123
+ return [t.left, t.top, t.right, t.bottom];
124
+ }
125
+ }
126
+ export {
127
+ T as DragController
128
+ };