@zag-js/popover 0.1.5 → 0.1.8

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.
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __defProps = Object.defineProperties;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -41,136 +42,24 @@ __export(src_exports, {
41
42
  });
42
43
  module.exports = __toCommonJS(src_exports);
43
44
 
44
- // ../../../node_modules/compute-scroll-into-view/dist/index.module.js
45
- function t(t3) {
46
- return typeof t3 == "object" && t3 != null && t3.nodeType === 1;
47
- }
48
- function e(t3, e2) {
49
- return (!e2 || t3 !== "hidden") && t3 !== "visible" && t3 !== "clip";
50
- }
51
- function n(t3, n2) {
52
- if (t3.clientHeight < t3.scrollHeight || t3.clientWidth < t3.scrollWidth) {
53
- var r2 = getComputedStyle(t3, null);
54
- return e(r2.overflowY, n2) || e(r2.overflowX, n2) || function(t4) {
55
- var e2 = function(t5) {
56
- if (!t5.ownerDocument || !t5.ownerDocument.defaultView)
57
- return null;
58
- try {
59
- return t5.ownerDocument.defaultView.frameElement;
60
- } catch (t6) {
61
- return null;
62
- }
63
- }(t4);
64
- return !!e2 && (e2.clientHeight < t4.scrollHeight || e2.clientWidth < t4.scrollWidth);
65
- }(t3);
66
- }
67
- return false;
68
- }
69
- function r(t3, e2, n2, r2, i, o, l, d) {
70
- return o < t3 && l > e2 || o > t3 && l < e2 ? 0 : o <= t3 && d <= n2 || l >= e2 && d >= n2 ? o - t3 - r2 : l > e2 && d < n2 || o < t3 && d > n2 ? l - e2 + i : 0;
71
- }
72
- function index_module_default(e2, i) {
73
- var o = window, l = i.scrollMode, d = i.block, u = i.inline, h = i.boundary, a = i.skipOverflowHiddenElements, c = typeof h == "function" ? h : function(t3) {
74
- return t3 !== h;
75
- };
76
- if (!t(e2))
77
- throw new TypeError("Invalid target");
78
- for (var f = document.scrollingElement || document.documentElement, s = [], p = e2; t(p) && c(p); ) {
79
- if ((p = p.parentElement) === f) {
80
- s.push(p);
81
- break;
82
- }
83
- p != null && p === document.body && n(p) && !n(document.documentElement) || p != null && n(p, a) && s.push(p);
84
- }
85
- for (var m = o.visualViewport ? o.visualViewport.width : innerWidth, g = o.visualViewport ? o.visualViewport.height : innerHeight, w = window.scrollX || pageXOffset, v = window.scrollY || pageYOffset, W = e2.getBoundingClientRect(), b = W.height, H = W.width, y = W.top, E = W.right, M = W.bottom, V = W.left, x = d === "start" || d === "nearest" ? y : d === "end" ? M : y + b / 2, I = u === "center" ? V + H / 2 : u === "end" ? E : V, C = [], T = 0; T < s.length; T++) {
86
- var k = s[T], B = k.getBoundingClientRect(), D = B.height, O = B.width, R = B.top, X = B.right, Y = B.bottom, L = B.left;
87
- if (l === "if-needed" && y >= 0 && V >= 0 && M <= g && E <= m && y >= R && M <= Y && V >= L && E <= X)
88
- return C;
89
- var S = getComputedStyle(k), j = parseInt(S.borderLeftWidth, 10), q = parseInt(S.borderTopWidth, 10), z = parseInt(S.borderRightWidth, 10), A = parseInt(S.borderBottomWidth, 10), F = 0, G = 0, J = "offsetWidth" in k ? k.offsetWidth - k.clientWidth - j - z : 0, K = "offsetHeight" in k ? k.offsetHeight - k.clientHeight - q - A : 0;
90
- if (f === k)
91
- F = d === "start" ? x : d === "end" ? x - g : d === "nearest" ? r(v, v + g, g, q, A, v + x, v + x + b, b) : x - g / 2, G = u === "start" ? I : u === "center" ? I - m / 2 : u === "end" ? I - m : r(w, w + m, m, j, z, w + I, w + I + H, H), F = Math.max(0, F + v), G = Math.max(0, G + w);
92
- else {
93
- F = d === "start" ? x - R - q : d === "end" ? x - Y + A + K : d === "nearest" ? r(R, Y, D, q, A + K, x, x + b, b) : x - (R + D / 2) + K / 2, G = u === "start" ? I - L - j : u === "center" ? I - (L + O / 2) + J / 2 : u === "end" ? I - X + z + J : r(L, X, O, j, z + J, I, I + H, H);
94
- var N = k.scrollLeft, P = k.scrollTop;
95
- x += P - (F = Math.max(0, Math.min(P + F, k.scrollHeight - D + K))), I += N - (G = Math.max(0, Math.min(N + G, k.scrollWidth - O + J)));
96
- }
97
- C.push({ el: k, top: F, left: G });
98
- }
99
- return C;
100
- }
101
-
102
- // ../../utilities/dom/node_modules/scroll-into-view-if-needed/es/index.js
103
- function isOptionsObject(options) {
104
- return options === Object(options) && Object.keys(options).length !== 0;
105
- }
106
- function defaultBehavior(actions, behavior) {
107
- if (behavior === void 0) {
108
- behavior = "auto";
109
- }
110
- var canSmoothScroll = "scrollBehavior" in document.body.style;
111
- actions.forEach(function(_ref) {
112
- var el = _ref.el, top = _ref.top, left = _ref.left;
113
- if (el.scroll && canSmoothScroll) {
114
- el.scroll({
115
- top,
116
- left,
117
- behavior
118
- });
119
- } else {
120
- el.scrollTop = top;
121
- el.scrollLeft = left;
122
- }
123
- });
124
- }
125
- function getOptions(options) {
126
- if (options === false) {
127
- return {
128
- block: "end",
129
- inline: "nearest"
130
- };
131
- }
132
- if (isOptionsObject(options)) {
133
- return options;
134
- }
135
- return {
136
- block: "start",
137
- inline: "nearest"
138
- };
139
- }
140
- function scrollIntoView(target, options) {
141
- var isTargetAttached = target.isConnected || target.ownerDocument.documentElement.contains(target);
142
- if (isOptionsObject(options) && typeof options.behavior === "function") {
143
- return options.behavior(isTargetAttached ? index_module_default(target, options) : []);
144
- }
145
- if (!isTargetAttached) {
146
- return;
147
- }
148
- var computeOptions = getOptions(options);
149
- return defaultBehavior(index_module_default(target, computeOptions), computeOptions.behavior);
150
- }
151
- var es_default = scrollIntoView;
152
-
153
45
  // ../../utilities/dom/dist/index.mjs
154
46
  var dataAttr = (guard) => {
155
47
  return guard ? "" : void 0;
156
48
  };
157
- function getStyleCache() {
158
- ;
159
- globalThis.__styleCache__ = globalThis.__styleCache__ || /* @__PURE__ */ new WeakMap();
160
- return globalThis.__styleCache__;
161
- }
162
- function getComputedStyle2(el) {
163
- var _a;
164
- if (!el)
165
- return {};
166
- const cache = getStyleCache();
167
- let style = cache.get(el);
168
- if (!style) {
169
- const win = (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
170
- style = win.getComputedStyle(el);
171
- cache.set(el, style);
172
- }
173
- return style;
49
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
50
+ var isLeftClick = (v) => v.button === 0;
51
+ var runIfFn = (v, ...a) => {
52
+ const res = typeof v === "function" ? v(...a) : v;
53
+ return res != null ? res : void 0;
54
+ };
55
+ var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
56
+ var isRef = (v) => hasProp(v, "current");
57
+ function addDomEvent(target, eventName, handler, options) {
58
+ const node = isRef(target) ? target.current : runIfFn(target);
59
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
60
+ return () => {
61
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
62
+ };
174
63
  }
175
64
  function nextTick(fn) {
176
65
  const set = /* @__PURE__ */ new Set();
@@ -191,85 +80,6 @@ function raf(fn) {
191
80
  globalThis.cancelAnimationFrame(id);
192
81
  };
193
82
  }
194
- var noop = () => {
195
- };
196
- var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
197
- var platform = (v) => isDom() && v.test(navigator.platform);
198
- var isDom = () => !!(typeof window !== "undefined");
199
- var isMac = () => platform(/^Mac/);
200
- var isIPhone = () => platform(/^iPhone/);
201
- var isIPad = () => platform(/^iPad/) || isMac() && navigator.maxTouchPoints > 1;
202
- var isIos = () => isIPhone() || isIPad();
203
- var isLeftClick = (v) => v.button === 0;
204
- function getListenerElements() {
205
- ;
206
- globalThis.__listenerElements__ = globalThis.__listenerElements__ || /* @__PURE__ */ new Map();
207
- return globalThis.__listenerElements__;
208
- }
209
- function getListenerCache() {
210
- ;
211
- globalThis.__listenerCache__ = globalThis.__listenerCache__ || /* @__PURE__ */ new Map();
212
- return globalThis.__listenerCache__;
213
- }
214
- function globalEventBus(node, type, handler, options) {
215
- var _a;
216
- if (!node)
217
- return noop;
218
- const hash = JSON.stringify({ type, options });
219
- const listenerElements = getListenerElements();
220
- const listenerCache = getListenerCache();
221
- const group = listenerElements.get(node);
222
- if (!listenerElements.has(node)) {
223
- const group2 = /* @__PURE__ */ new Map([[hash, /* @__PURE__ */ new Set([handler])]]);
224
- listenerElements.set(node, group2);
225
- } else if (group == null ? void 0 : group.has(hash)) {
226
- (_a = group == null ? void 0 : group.get(hash)) == null ? void 0 : _a.add(handler);
227
- } else {
228
- group == null ? void 0 : group.set(hash, /* @__PURE__ */ new Set([handler]));
229
- }
230
- function attach(node2) {
231
- var _a2, _b;
232
- function listener(event) {
233
- var _a3;
234
- const group2 = listenerElements.get(node2);
235
- (_a3 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a3.forEach((fn) => fn(event));
236
- }
237
- if (!(listenerCache == null ? void 0 : listenerCache.has(node2))) {
238
- listenerCache.set(node2, /* @__PURE__ */ new Map([[hash, listener]]));
239
- node2.addEventListener(type, listener, options);
240
- return;
241
- }
242
- if (!((_a2 = listenerCache == null ? void 0 : listenerCache.get(node2)) == null ? void 0 : _a2.has(hash))) {
243
- (_b = listenerCache.get(node2)) == null ? void 0 : _b.set(hash, listener);
244
- node2.addEventListener(type, listener, options);
245
- }
246
- }
247
- attach(node);
248
- return function remove() {
249
- var _a2, _b, _c, _d;
250
- if (!listenerElements.has(node))
251
- return;
252
- const group2 = listenerElements.get(node);
253
- (_a2 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a2.delete(handler);
254
- if (((_b = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _b.size) === 0) {
255
- const listener = (_c = listenerCache.get(node)) == null ? void 0 : _c.get(hash);
256
- node.removeEventListener(type, listener, options);
257
- group2 == null ? void 0 : group2.delete(hash);
258
- (_d = listenerCache.get(node)) == null ? void 0 : _d.delete(hash);
259
- if ((group2 == null ? void 0 : group2.size) === 0) {
260
- listenerElements.delete(node);
261
- listenerCache.delete(node);
262
- }
263
- }
264
- };
265
- }
266
- var t2 = (v) => Object.prototype.toString.call(v).slice(8, -1);
267
- var isRef = (v) => t2(v) === "Object" && "current" in v;
268
- var runIfFn = (fn) => t2(fn) === "Function" ? fn() : fn;
269
- function addDomEvent(target, event, listener, options) {
270
- const node = isRef(target) ? target.current : runIfFn(target);
271
- return globalEventBus(node, event, listener, options);
272
- }
273
83
  var changeCount = 0;
274
84
  var originalBodyPointerEvents;
275
85
  function preventBodyPointerEvents(el, opts = {}) {
@@ -328,20 +138,23 @@ function preventBodyPointerEvents(el, opts = {}) {
328
138
  cleanups.forEach((cleanup) => cleanup == null ? void 0 : cleanup());
329
139
  };
330
140
  }
331
- function getOwnerDocument(el) {
332
- var _a;
333
- if (isWindow(el))
334
- return el.document;
335
- return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
336
- }
337
- function getNodeName(node) {
338
- return isWindow(node) ? "" : node ? node.localName || "" : "";
141
+ function getStyleCache() {
142
+ ;
143
+ globalThis.__styleCache__ = globalThis.__styleCache__ || /* @__PURE__ */ new WeakMap();
144
+ return globalThis.__styleCache__;
339
145
  }
340
- function getParent(el) {
341
- const doc = getOwnerDocument(el);
342
- if (getNodeName(el) === "html")
343
- return el;
344
- return el.assignedSlot || el.parentElement || doc.documentElement;
146
+ function getComputedStyle(el) {
147
+ var _a;
148
+ if (!el)
149
+ return {};
150
+ const cache = getStyleCache();
151
+ let style = cache.get(el);
152
+ if (!style) {
153
+ const win = (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
154
+ style = win.getComputedStyle(el);
155
+ cache.set(el, style);
156
+ }
157
+ return style;
345
158
  }
346
159
  function contains(parent, child) {
347
160
  if (!parent)
@@ -351,124 +164,9 @@ function contains(parent, child) {
351
164
  function isHTMLElement(v) {
352
165
  return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
353
166
  }
354
- function isWindow(value) {
355
- return (value == null ? void 0 : value.toString()) === "[object Window]";
356
- }
357
167
  var isDisabled = (el) => {
358
168
  return (el == null ? void 0 : el.getAttribute("disabled")) != null || !!(el == null ? void 0 : el.getAttribute("aria-disabled")) === true;
359
169
  };
360
- function isScrollParent(el) {
361
- const { overflow, overflowX, overflowY } = getComputedStyle2(el);
362
- return /auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX);
363
- }
364
- function getScrollParent(el) {
365
- if (["html", "body", "#document"].includes(getNodeName(el))) {
366
- return getOwnerDocument(el).body;
367
- }
368
- if (isHTMLElement(el) && isScrollParent(el)) {
369
- return el;
370
- }
371
- return getScrollParent(getParent(el));
372
- }
373
- var nonTextInputTypes = /* @__PURE__ */ new Set(["checkbox", "radio", "range", "color", "file", "image", "button", "submit", "reset"]);
374
- function preventBodyScroll(opts) {
375
- var _a;
376
- const { document: docProp, disabled = false, allowPinchZoom } = opts != null ? opts : {};
377
- const doc = docProp != null ? docProp : document;
378
- const win = (_a = doc == null ? void 0 : doc.defaultView) != null ? _a : window;
379
- const viewport = isDom() ? win.visualViewport : null;
380
- const docEl = doc.documentElement;
381
- function preventScrollStandard() {
382
- if (docEl.hasAttribute("scroll-lock"))
383
- return;
384
- const fn = pipe(setStyle(docEl, "paddingRight", `${win.innerWidth - docEl.clientWidth}px`), setStyle(docEl, "overflow", "hidden"), () => docEl.setAttribute("scroll-lock", "true"));
385
- return pipe(fn, () => docEl.removeAttribute("scroll-lock"));
386
- }
387
- function preventScrollMobileSafari() {
388
- if (docEl.hasAttribute("scroll-lock"))
389
- return;
390
- let scrollable;
391
- let lastY = 0;
392
- let onTouchStart = (e2) => {
393
- scrollable = getScrollParent(e2.target);
394
- if (scrollable === docEl && scrollable === doc.body) {
395
- return;
396
- }
397
- lastY = e2.changedTouches[0].pageY;
398
- };
399
- let onTouchMove = (e2) => {
400
- if (e2.touches.length === 2 && allowPinchZoom)
401
- return;
402
- if (!scrollable || scrollable === docEl || scrollable === doc.body) {
403
- e2.preventDefault();
404
- return;
405
- }
406
- let y = e2.changedTouches[0].pageY;
407
- let scrollTop = scrollable.scrollTop;
408
- let bottom = scrollable.scrollHeight - scrollable.clientHeight;
409
- if (scrollTop <= 0 && y > lastY || scrollTop >= bottom && y < lastY) {
410
- e2.preventDefault();
411
- }
412
- lastY = y;
413
- };
414
- let onTouchEnd = (e2) => {
415
- let target = e2.target;
416
- if (target instanceof win.HTMLInputElement && !nonTextInputTypes.has(target.type)) {
417
- e2.preventDefault();
418
- target.style.transform = "translateY(-2000px)";
419
- target.focus();
420
- win.requestAnimationFrame(() => {
421
- target.style.transform = "";
422
- });
423
- }
424
- };
425
- let onFocus = (e2) => {
426
- let target = e2.target;
427
- if (target instanceof win.HTMLInputElement && !nonTextInputTypes.has(target.type)) {
428
- target.style.transform = "translateY(-2000px)";
429
- win.requestAnimationFrame(() => {
430
- target.style.transform = "";
431
- if (!viewport)
432
- return;
433
- if (viewport.height < win.innerHeight) {
434
- win.requestAnimationFrame(function() {
435
- es_default(target, { scrollMode: "if-needed" });
436
- });
437
- } else {
438
- const onResize = () => {
439
- es_default(target, { scrollMode: "if-needed" });
440
- };
441
- addDomEvent(viewport, "resize", onResize, { once: true });
442
- }
443
- });
444
- }
445
- };
446
- let onWindowScroll = () => {
447
- win.scrollTo(0, 0);
448
- };
449
- let scrollX = win.scrollX;
450
- let scrollY = win.scrollY;
451
- let restoreStyles = pipe(setStyle(docEl, "paddingRight", `${win.innerWidth - docEl.clientWidth}px`), setStyle(docEl, "overflow", "hidden"), setStyle(doc.body, "marginTop", `-${scrollY}px`));
452
- win.scrollTo(0, 0);
453
- let removeEvents = pipe(addDomEvent(doc, "touchstart", onTouchStart, { passive: false, capture: true }), addDomEvent(doc, "touchmove", onTouchMove, { passive: false, capture: true }), addDomEvent(doc, "touchend", onTouchEnd, { passive: false, capture: true }), addDomEvent(doc, "focus", onFocus, true), addDomEvent(win, "scroll", onWindowScroll), () => docEl.setAttribute("scroll-lock", "true"));
454
- return () => {
455
- restoreStyles();
456
- removeEvents();
457
- win.scrollTo(scrollX, scrollY);
458
- docEl.removeAttribute("scroll-lock");
459
- };
460
- }
461
- if (disabled)
462
- return noop;
463
- return isIos() ? preventScrollMobileSafari() : preventScrollStandard();
464
- }
465
- function setStyle(el, key, value) {
466
- let cur = el.style[key];
467
- el.style[key] = value;
468
- return () => {
469
- el.style[key] = cur;
470
- };
471
- }
472
170
  function validateBlur(event, opts) {
473
171
  var _a;
474
172
  const exclude = Array.isArray(opts.exclude) ? opts.exclude : [opts.exclude];
@@ -493,7 +191,7 @@ var focusableSelector = /* @__PURE__ */ [
493
191
  "details > summary:first-of-type"
494
192
  ].join(",");
495
193
  function isHidden(el, until) {
496
- const style = getComputedStyle2(el);
194
+ const style = getComputedStyle(el);
497
195
  if (!el || style.getPropertyValue("visibility") === "hidden")
498
196
  return true;
499
197
  while (el) {
@@ -509,7 +207,8 @@ var getFocusables = (el, includeContainer = false) => {
509
207
  if (!el)
510
208
  return [];
511
209
  let els = Array.from(el.querySelectorAll(focusableSelector));
512
- if (includeContainer && isHTMLElement(el)) {
210
+ const shouldAddContainer = includeContainer == true || includeContainer == "if-empty" && els.length === 0;
211
+ if (shouldAddContainer && isHTMLElement(el)) {
513
212
  els.unshift(el);
514
213
  }
515
214
  return els.filter((el2) => isFocusable(el2) && !isHidden(el2));
@@ -520,35 +219,13 @@ var isFocusable = (el) => {
520
219
  return el == null ? void 0 : el.matches(focusableSelector);
521
220
  };
522
221
  var getTabbables = (el, includeContainer = false) => {
222
+ if (!el)
223
+ return [];
523
224
  return getFocusables(el, includeContainer).filter(isTabbable);
524
225
  };
525
226
  var isTabbable = (el) => {
526
227
  return isFocusable(el) && !isDisabled(el) && !isHidden(el);
527
228
  };
528
- function itemById(v, id) {
529
- return v.find((node) => node.id === id);
530
- }
531
- function indexOfId(v, id) {
532
- const item = itemById(v, id);
533
- return item ? v.indexOf(item) : -1;
534
- }
535
- var getValueText = (item) => {
536
- var _a, _b;
537
- return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
538
- };
539
- var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
540
- var wrap = (v, idx) => {
541
- return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
542
- };
543
- function findByText(v, text, currentId) {
544
- const index = currentId ? indexOfId(v, currentId) : -1;
545
- let items = currentId ? wrap(v, index) : v;
546
- const isSingleKey = text.length === 1;
547
- if (isSingleKey) {
548
- items = items.filter((item) => item.id !== currentId);
549
- }
550
- return items.find((item) => match(getValueText(item), text));
551
- }
552
229
  function trackPointerDown(doc, onPointerDown) {
553
230
  var _a;
554
231
  const win = (_a = doc.defaultView) != null ? _a : window;
@@ -559,41 +236,17 @@ function trackPointerDown(doc, onPointerDown) {
559
236
  };
560
237
  return addDomEvent(doc, "pointerdown", fn);
561
238
  }
562
- function findByTypeahead(_items, options) {
563
- const { state: state2, activeId, key, timeout = 350 } = options;
564
- const search = state2.keysSoFar + key;
565
- const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
566
- const query2 = isRepeated ? search[0] : search;
567
- let items = _items.slice();
568
- const next2 = findByText(items, query2, activeId);
569
- function cleanup() {
570
- clearTimeout(state2.timer);
571
- state2.timer = -1;
572
- }
573
- function update(value) {
574
- state2.keysSoFar = value;
575
- cleanup();
576
- if (value !== "") {
577
- state2.timer = +setTimeout(() => {
578
- update("");
579
- cleanup();
580
- }, timeout);
581
- }
582
- }
583
- update(search);
584
- return next2;
585
- }
586
- findByTypeahead.defaultOptions = {
587
- keysSoFar: "",
588
- timer: -1
589
- };
590
239
 
591
240
  // src/popover.connect.ts
592
241
  var import_popper = require("@zag-js/popper");
593
242
 
594
243
  // ../../types/dist/index.mjs
595
244
  function createNormalizer(fn) {
596
- return { button: fn, label: fn, input: fn, output: fn, element: fn };
245
+ return new Proxy({}, {
246
+ get() {
247
+ return fn;
248
+ }
249
+ });
597
250
  }
598
251
  var normalizeProp = createNormalizer((v) => v);
599
252
 
@@ -668,15 +321,16 @@ var dom = {
668
321
  getFocusableEls: (ctx) => getFocusables(dom.getContentEl(ctx)),
669
322
  getFirstFocusableEl: (ctx) => dom.getFocusableEls(ctx)[0],
670
323
  getDocTabbableEls: (ctx) => getTabbables(cast(dom.getDoc(ctx))),
671
- getTabbableEls: (ctx) => {
672
- const el = dom.getContentEl(ctx);
673
- return el ? getTabbables(el) : [];
674
- },
324
+ getTabbableEls: (ctx) => getTabbables(dom.getContentEl(ctx), "if-empty"),
675
325
  getFirstTabbableEl: (ctx) => first(dom.getTabbableEls(ctx)),
676
326
  getLastTabbableEl: (ctx) => last(dom.getTabbableEls(ctx)),
677
327
  getInitialFocusEl: (ctx) => {
678
- var _a;
679
- return (_a = runIfFn2(ctx.initialFocusEl)) != null ? _a : dom.getFirstFocusableEl(ctx);
328
+ let el = runIfFn2(ctx.initialFocusEl);
329
+ if (!el && ctx.autoFocus)
330
+ el = dom.getFirstFocusableEl(ctx);
331
+ if (!el)
332
+ el = dom.getContentEl(ctx);
333
+ return el;
680
334
  }
681
335
  };
682
336
 
@@ -684,9 +338,10 @@ var dom = {
684
338
  function connect(state, send, normalize = normalizeProp) {
685
339
  const isOpen = state.matches("open");
686
340
  const pointerdownNode = state.context.pointerdownNode;
341
+ const currentPlacement = state.context.currentPlacement;
687
342
  const popperStyles = (0, import_popper.getPlacementStyles)({
688
343
  measured: !!state.context.isPlacementComplete,
689
- placement: state.context.currentPlacement
344
+ placement: currentPlacement
690
345
  });
691
346
  return {
692
347
  portalled: state.context.currentPortalled,
@@ -713,7 +368,7 @@ function connect(state, send, normalize = normalizeProp) {
713
368
  triggerProps: normalize.button({
714
369
  "data-part": "trigger",
715
370
  type: "button",
716
- "data-placement": state.context.currentPlacement,
371
+ "data-placement": currentPlacement,
717
372
  id: dom.getTriggerId(state.context),
718
373
  "aria-haspopup": "dialog",
719
374
  "aria-expanded": isOpen,
@@ -721,6 +376,11 @@ function connect(state, send, normalize = normalizeProp) {
721
376
  "aria-controls": dom.getContentId(state.context),
722
377
  onClick() {
723
378
  send("TRIGGER_CLICK");
379
+ },
380
+ onKeyDown(event) {
381
+ if (event.key === "Escape") {
382
+ send("ESCAPE");
383
+ }
724
384
  }
725
385
  }),
726
386
  positionerProps: normalize.element({
@@ -735,9 +395,9 @@ function connect(state, send, normalize = normalizeProp) {
735
395
  role: "dialog",
736
396
  hidden: !isOpen,
737
397
  "data-expanded": dataAttr(isOpen),
738
- "aria-labelledby": state.context.isTitleRendered ? dom.getTitleId(state.context) : void 0,
739
- "aria-describedby": state.context.isDescriptionRendered ? dom.getDescriptionId(state.context) : void 0,
740
- "data-placement": state.context.currentPlacement,
398
+ "aria-labelledby": state.context.renderedElements.title ? dom.getTitleId(state.context) : void 0,
399
+ "aria-describedby": state.context.renderedElements.description ? dom.getDescriptionId(state.context) : void 0,
400
+ "data-placement": currentPlacement,
741
401
  onKeyDown(event) {
742
402
  const keyMap = {
743
403
  Escape(event2) {
@@ -785,19 +445,17 @@ function connect(state, send, normalize = normalizeProp) {
785
445
  }
786
446
 
787
447
  // src/popover.machine.ts
448
+ var import_aria_hidden = require("@zag-js/aria-hidden");
788
449
  var import_core = require("@zag-js/core");
789
450
  var import_popper2 = require("@zag-js/popper");
790
- var import_aria_hidden = require("@zag-js/aria-hidden");
451
+ var import_remove_scroll = require("@zag-js/remove-scroll");
791
452
  var import_focus_trap = require("focus-trap");
792
453
  var { and, or } = import_core.guards;
793
454
  function machine(ctx = {}) {
794
455
  return (0, import_core.createMachine)({
795
456
  id: "popover",
796
457
  initial: "unknown",
797
- context: __spreadValues({
798
- isTitleRendered: true,
799
- isDescriptionRendered: true,
800
- isAnchorRendered: false,
458
+ context: __spreadProps(__spreadValues({
801
459
  uid: "",
802
460
  closeOnBlur: true,
803
461
  closeOnEsc: true,
@@ -807,7 +465,13 @@ function machine(ctx = {}) {
807
465
  placement: "bottom"
808
466
  }, ctx.positioning),
809
467
  currentPlacement: void 0
810
- }, ctx),
468
+ }, ctx), {
469
+ renderedElements: {
470
+ title: true,
471
+ description: true,
472
+ anchor: false
473
+ }
474
+ }),
811
475
  computed: {
812
476
  currentPortalled: (ctx2) => !!ctx2.modal || !!ctx2.portalled
813
477
  },
@@ -815,7 +479,7 @@ function machine(ctx = {}) {
815
479
  unknown: {
816
480
  on: {
817
481
  SETUP: {
818
- target: "closed",
482
+ target: ctx.open ? "open" : "closed",
819
483
  actions: ["setupDocument", "checkRenderedElements"]
820
484
  }
821
485
  }
@@ -836,13 +500,7 @@ function machine(ctx = {}) {
836
500
  "disableOutsidePointerEvents",
837
501
  "computePlacement"
838
502
  ],
839
- entry: (0, import_core.choose)([
840
- {
841
- guard: "autoFocus",
842
- actions: ["setInitialFocus", "invokeOnOpen"]
843
- },
844
- { actions: ["focusContent", "invokeOnOpen"] }
845
- ]),
503
+ entry: ["setInitialFocus", "invokeOnOpen"],
846
504
  on: {
847
505
  CLOSE: {
848
506
  target: "closed",
@@ -885,7 +543,7 @@ function machine(ctx = {}) {
885
543
  activities: {
886
544
  computePlacement(ctx2) {
887
545
  ctx2.currentPlacement = ctx2.positioning.placement;
888
- const anchorEl = ctx2.isAnchorRendered ? dom.getAnchorEl(ctx2) : dom.getTriggerEl(ctx2);
546
+ const anchorEl = ctx2.renderedElements.anchor ? dom.getAnchorEl(ctx2) : dom.getTriggerEl(ctx2);
889
547
  return (0, import_popper2.getPlacement)(anchorEl, dom.getPositionerEl(ctx2), __spreadProps(__spreadValues({}, ctx2.positioning), {
890
548
  onComplete(data) {
891
549
  ctx2.currentPlacement = data.placement;
@@ -919,8 +577,7 @@ function machine(ctx = {}) {
919
577
  return () => unhide == null ? void 0 : unhide();
920
578
  },
921
579
  preventScroll(ctx2) {
922
- return preventBodyScroll({
923
- allowPinchZoom: true,
580
+ return (0, import_remove_scroll.preventBodyScroll)({
924
581
  disabled: !ctx2.modal,
925
582
  document: dom.getDoc(ctx2)
926
583
  });
@@ -943,7 +600,7 @@ function machine(ctx = {}) {
943
600
  });
944
601
  try {
945
602
  trap.activate();
946
- } catch (e2) {
603
+ } catch (e) {
947
604
  }
948
605
  });
949
606
  return () => trap == null ? void 0 : trap.deactivate();
@@ -963,9 +620,11 @@ function machine(ctx = {}) {
963
620
  actions: {
964
621
  checkRenderedElements(ctx2) {
965
622
  raf(() => {
966
- ctx2.isAnchorRendered = !!dom.getAnchorEl(ctx2);
967
- ctx2.isTitleRendered = !!dom.getTitleEl(ctx2);
968
- ctx2.isDescriptionRendered = !!dom.getDescriptionEl(ctx2);
623
+ Object.assign(ctx2.renderedElements, {
624
+ title: !!dom.getTitleEl(ctx2),
625
+ description: !!dom.getDescriptionEl(ctx2),
626
+ anchor: !!dom.getAnchorEl(ctx2)
627
+ });
969
628
  });
970
629
  },
971
630
  setupDocument(ctx2, evt) {
@@ -978,12 +637,6 @@ function machine(ctx = {}) {
978
637
  clearPointerDown(ctx2) {
979
638
  ctx2.pointerdownNode = null;
980
639
  },
981
- focusContent(ctx2) {
982
- raf(() => {
983
- var _a;
984
- (_a = dom.getContentEl(ctx2)) == null ? void 0 : _a.focus();
985
- });
986
- },
987
640
  setInitialFocus(ctx2) {
988
641
  raf(() => {
989
642
  var _a;
@@ -1031,4 +684,3 @@ function machine(ctx = {}) {
1031
684
  }
1032
685
  });
1033
686
  }
1034
- //# sourceMappingURL=index.js.map