@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.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __defProps = Object.defineProperties;
3
4
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
@@ -18,136 +19,24 @@ var __spreadValues = (a, b) => {
18
19
  };
19
20
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
21
 
21
- // ../../../node_modules/compute-scroll-into-view/dist/index.module.js
22
- function t(t3) {
23
- return typeof t3 == "object" && t3 != null && t3.nodeType === 1;
24
- }
25
- function e(t3, e2) {
26
- return (!e2 || t3 !== "hidden") && t3 !== "visible" && t3 !== "clip";
27
- }
28
- function n(t3, n2) {
29
- if (t3.clientHeight < t3.scrollHeight || t3.clientWidth < t3.scrollWidth) {
30
- var r2 = getComputedStyle(t3, null);
31
- return e(r2.overflowY, n2) || e(r2.overflowX, n2) || function(t4) {
32
- var e2 = function(t5) {
33
- if (!t5.ownerDocument || !t5.ownerDocument.defaultView)
34
- return null;
35
- try {
36
- return t5.ownerDocument.defaultView.frameElement;
37
- } catch (t6) {
38
- return null;
39
- }
40
- }(t4);
41
- return !!e2 && (e2.clientHeight < t4.scrollHeight || e2.clientWidth < t4.scrollWidth);
42
- }(t3);
43
- }
44
- return false;
45
- }
46
- function r(t3, e2, n2, r2, i, o, l, d) {
47
- 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;
48
- }
49
- function index_module_default(e2, i) {
50
- 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) {
51
- return t3 !== h;
52
- };
53
- if (!t(e2))
54
- throw new TypeError("Invalid target");
55
- for (var f = document.scrollingElement || document.documentElement, s = [], p = e2; t(p) && c(p); ) {
56
- if ((p = p.parentElement) === f) {
57
- s.push(p);
58
- break;
59
- }
60
- p != null && p === document.body && n(p) && !n(document.documentElement) || p != null && n(p, a) && s.push(p);
61
- }
62
- 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++) {
63
- 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;
64
- if (l === "if-needed" && y >= 0 && V >= 0 && M <= g && E <= m && y >= R && M <= Y && V >= L && E <= X)
65
- return C;
66
- 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;
67
- if (f === k)
68
- 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);
69
- else {
70
- 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);
71
- var N = k.scrollLeft, P = k.scrollTop;
72
- 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)));
73
- }
74
- C.push({ el: k, top: F, left: G });
75
- }
76
- return C;
77
- }
78
-
79
- // ../../utilities/dom/node_modules/scroll-into-view-if-needed/es/index.js
80
- function isOptionsObject(options) {
81
- return options === Object(options) && Object.keys(options).length !== 0;
82
- }
83
- function defaultBehavior(actions, behavior) {
84
- if (behavior === void 0) {
85
- behavior = "auto";
86
- }
87
- var canSmoothScroll = "scrollBehavior" in document.body.style;
88
- actions.forEach(function(_ref) {
89
- var el = _ref.el, top = _ref.top, left = _ref.left;
90
- if (el.scroll && canSmoothScroll) {
91
- el.scroll({
92
- top,
93
- left,
94
- behavior
95
- });
96
- } else {
97
- el.scrollTop = top;
98
- el.scrollLeft = left;
99
- }
100
- });
101
- }
102
- function getOptions(options) {
103
- if (options === false) {
104
- return {
105
- block: "end",
106
- inline: "nearest"
107
- };
108
- }
109
- if (isOptionsObject(options)) {
110
- return options;
111
- }
112
- return {
113
- block: "start",
114
- inline: "nearest"
115
- };
116
- }
117
- function scrollIntoView(target, options) {
118
- var isTargetAttached = target.isConnected || target.ownerDocument.documentElement.contains(target);
119
- if (isOptionsObject(options) && typeof options.behavior === "function") {
120
- return options.behavior(isTargetAttached ? index_module_default(target, options) : []);
121
- }
122
- if (!isTargetAttached) {
123
- return;
124
- }
125
- var computeOptions = getOptions(options);
126
- return defaultBehavior(index_module_default(target, computeOptions), computeOptions.behavior);
127
- }
128
- var es_default = scrollIntoView;
129
-
130
22
  // ../../utilities/dom/dist/index.mjs
131
23
  var dataAttr = (guard) => {
132
24
  return guard ? "" : void 0;
133
25
  };
134
- function getStyleCache() {
135
- ;
136
- globalThis.__styleCache__ = globalThis.__styleCache__ || /* @__PURE__ */ new WeakMap();
137
- return globalThis.__styleCache__;
138
- }
139
- function getComputedStyle2(el) {
140
- var _a;
141
- if (!el)
142
- return {};
143
- const cache = getStyleCache();
144
- let style = cache.get(el);
145
- if (!style) {
146
- const win = (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
147
- style = win.getComputedStyle(el);
148
- cache.set(el, style);
149
- }
150
- return style;
26
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
27
+ var isLeftClick = (v) => v.button === 0;
28
+ var runIfFn = (v, ...a) => {
29
+ const res = typeof v === "function" ? v(...a) : v;
30
+ return res != null ? res : void 0;
31
+ };
32
+ var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
33
+ var isRef = (v) => hasProp(v, "current");
34
+ function addDomEvent(target, eventName, handler, options) {
35
+ const node = isRef(target) ? target.current : runIfFn(target);
36
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
37
+ return () => {
38
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
39
+ };
151
40
  }
152
41
  function nextTick(fn) {
153
42
  const set = /* @__PURE__ */ new Set();
@@ -168,85 +57,6 @@ function raf(fn) {
168
57
  globalThis.cancelAnimationFrame(id);
169
58
  };
170
59
  }
171
- var noop = () => {
172
- };
173
- var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
174
- var platform = (v) => isDom() && v.test(navigator.platform);
175
- var isDom = () => !!(typeof window !== "undefined");
176
- var isMac = () => platform(/^Mac/);
177
- var isIPhone = () => platform(/^iPhone/);
178
- var isIPad = () => platform(/^iPad/) || isMac() && navigator.maxTouchPoints > 1;
179
- var isIos = () => isIPhone() || isIPad();
180
- var isLeftClick = (v) => v.button === 0;
181
- function getListenerElements() {
182
- ;
183
- globalThis.__listenerElements__ = globalThis.__listenerElements__ || /* @__PURE__ */ new Map();
184
- return globalThis.__listenerElements__;
185
- }
186
- function getListenerCache() {
187
- ;
188
- globalThis.__listenerCache__ = globalThis.__listenerCache__ || /* @__PURE__ */ new Map();
189
- return globalThis.__listenerCache__;
190
- }
191
- function globalEventBus(node, type, handler, options) {
192
- var _a;
193
- if (!node)
194
- return noop;
195
- const hash = JSON.stringify({ type, options });
196
- const listenerElements = getListenerElements();
197
- const listenerCache = getListenerCache();
198
- const group = listenerElements.get(node);
199
- if (!listenerElements.has(node)) {
200
- const group2 = /* @__PURE__ */ new Map([[hash, /* @__PURE__ */ new Set([handler])]]);
201
- listenerElements.set(node, group2);
202
- } else if (group == null ? void 0 : group.has(hash)) {
203
- (_a = group == null ? void 0 : group.get(hash)) == null ? void 0 : _a.add(handler);
204
- } else {
205
- group == null ? void 0 : group.set(hash, /* @__PURE__ */ new Set([handler]));
206
- }
207
- function attach(node2) {
208
- var _a2, _b;
209
- function listener(event) {
210
- var _a3;
211
- const group2 = listenerElements.get(node2);
212
- (_a3 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a3.forEach((fn) => fn(event));
213
- }
214
- if (!(listenerCache == null ? void 0 : listenerCache.has(node2))) {
215
- listenerCache.set(node2, /* @__PURE__ */ new Map([[hash, listener]]));
216
- node2.addEventListener(type, listener, options);
217
- return;
218
- }
219
- if (!((_a2 = listenerCache == null ? void 0 : listenerCache.get(node2)) == null ? void 0 : _a2.has(hash))) {
220
- (_b = listenerCache.get(node2)) == null ? void 0 : _b.set(hash, listener);
221
- node2.addEventListener(type, listener, options);
222
- }
223
- }
224
- attach(node);
225
- return function remove() {
226
- var _a2, _b, _c, _d;
227
- if (!listenerElements.has(node))
228
- return;
229
- const group2 = listenerElements.get(node);
230
- (_a2 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a2.delete(handler);
231
- if (((_b = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _b.size) === 0) {
232
- const listener = (_c = listenerCache.get(node)) == null ? void 0 : _c.get(hash);
233
- node.removeEventListener(type, listener, options);
234
- group2 == null ? void 0 : group2.delete(hash);
235
- (_d = listenerCache.get(node)) == null ? void 0 : _d.delete(hash);
236
- if ((group2 == null ? void 0 : group2.size) === 0) {
237
- listenerElements.delete(node);
238
- listenerCache.delete(node);
239
- }
240
- }
241
- };
242
- }
243
- var t2 = (v) => Object.prototype.toString.call(v).slice(8, -1);
244
- var isRef = (v) => t2(v) === "Object" && "current" in v;
245
- var runIfFn = (fn) => t2(fn) === "Function" ? fn() : fn;
246
- function addDomEvent(target, event, listener, options) {
247
- const node = isRef(target) ? target.current : runIfFn(target);
248
- return globalEventBus(node, event, listener, options);
249
- }
250
60
  var changeCount = 0;
251
61
  var originalBodyPointerEvents;
252
62
  function preventBodyPointerEvents(el, opts = {}) {
@@ -305,20 +115,23 @@ function preventBodyPointerEvents(el, opts = {}) {
305
115
  cleanups.forEach((cleanup) => cleanup == null ? void 0 : cleanup());
306
116
  };
307
117
  }
308
- function getOwnerDocument(el) {
309
- var _a;
310
- if (isWindow(el))
311
- return el.document;
312
- return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
313
- }
314
- function getNodeName(node) {
315
- return isWindow(node) ? "" : node ? node.localName || "" : "";
118
+ function getStyleCache() {
119
+ ;
120
+ globalThis.__styleCache__ = globalThis.__styleCache__ || /* @__PURE__ */ new WeakMap();
121
+ return globalThis.__styleCache__;
316
122
  }
317
- function getParent(el) {
318
- const doc = getOwnerDocument(el);
319
- if (getNodeName(el) === "html")
320
- return el;
321
- return el.assignedSlot || el.parentElement || doc.documentElement;
123
+ function getComputedStyle(el) {
124
+ var _a;
125
+ if (!el)
126
+ return {};
127
+ const cache = getStyleCache();
128
+ let style = cache.get(el);
129
+ if (!style) {
130
+ const win = (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
131
+ style = win.getComputedStyle(el);
132
+ cache.set(el, style);
133
+ }
134
+ return style;
322
135
  }
323
136
  function contains(parent, child) {
324
137
  if (!parent)
@@ -328,124 +141,9 @@ function contains(parent, child) {
328
141
  function isHTMLElement(v) {
329
142
  return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
330
143
  }
331
- function isWindow(value) {
332
- return (value == null ? void 0 : value.toString()) === "[object Window]";
333
- }
334
144
  var isDisabled = (el) => {
335
145
  return (el == null ? void 0 : el.getAttribute("disabled")) != null || !!(el == null ? void 0 : el.getAttribute("aria-disabled")) === true;
336
146
  };
337
- function isScrollParent(el) {
338
- const { overflow, overflowX, overflowY } = getComputedStyle2(el);
339
- return /auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX);
340
- }
341
- function getScrollParent(el) {
342
- if (["html", "body", "#document"].includes(getNodeName(el))) {
343
- return getOwnerDocument(el).body;
344
- }
345
- if (isHTMLElement(el) && isScrollParent(el)) {
346
- return el;
347
- }
348
- return getScrollParent(getParent(el));
349
- }
350
- var nonTextInputTypes = /* @__PURE__ */ new Set(["checkbox", "radio", "range", "color", "file", "image", "button", "submit", "reset"]);
351
- function preventBodyScroll(opts) {
352
- var _a;
353
- const { document: docProp, disabled = false, allowPinchZoom } = opts != null ? opts : {};
354
- const doc = docProp != null ? docProp : document;
355
- const win = (_a = doc == null ? void 0 : doc.defaultView) != null ? _a : window;
356
- const viewport = isDom() ? win.visualViewport : null;
357
- const docEl = doc.documentElement;
358
- function preventScrollStandard() {
359
- if (docEl.hasAttribute("scroll-lock"))
360
- return;
361
- const fn = pipe(setStyle(docEl, "paddingRight", `${win.innerWidth - docEl.clientWidth}px`), setStyle(docEl, "overflow", "hidden"), () => docEl.setAttribute("scroll-lock", "true"));
362
- return pipe(fn, () => docEl.removeAttribute("scroll-lock"));
363
- }
364
- function preventScrollMobileSafari() {
365
- if (docEl.hasAttribute("scroll-lock"))
366
- return;
367
- let scrollable;
368
- let lastY = 0;
369
- let onTouchStart = (e2) => {
370
- scrollable = getScrollParent(e2.target);
371
- if (scrollable === docEl && scrollable === doc.body) {
372
- return;
373
- }
374
- lastY = e2.changedTouches[0].pageY;
375
- };
376
- let onTouchMove = (e2) => {
377
- if (e2.touches.length === 2 && allowPinchZoom)
378
- return;
379
- if (!scrollable || scrollable === docEl || scrollable === doc.body) {
380
- e2.preventDefault();
381
- return;
382
- }
383
- let y = e2.changedTouches[0].pageY;
384
- let scrollTop = scrollable.scrollTop;
385
- let bottom = scrollable.scrollHeight - scrollable.clientHeight;
386
- if (scrollTop <= 0 && y > lastY || scrollTop >= bottom && y < lastY) {
387
- e2.preventDefault();
388
- }
389
- lastY = y;
390
- };
391
- let onTouchEnd = (e2) => {
392
- let target = e2.target;
393
- if (target instanceof win.HTMLInputElement && !nonTextInputTypes.has(target.type)) {
394
- e2.preventDefault();
395
- target.style.transform = "translateY(-2000px)";
396
- target.focus();
397
- win.requestAnimationFrame(() => {
398
- target.style.transform = "";
399
- });
400
- }
401
- };
402
- let onFocus = (e2) => {
403
- let target = e2.target;
404
- if (target instanceof win.HTMLInputElement && !nonTextInputTypes.has(target.type)) {
405
- target.style.transform = "translateY(-2000px)";
406
- win.requestAnimationFrame(() => {
407
- target.style.transform = "";
408
- if (!viewport)
409
- return;
410
- if (viewport.height < win.innerHeight) {
411
- win.requestAnimationFrame(function() {
412
- es_default(target, { scrollMode: "if-needed" });
413
- });
414
- } else {
415
- const onResize = () => {
416
- es_default(target, { scrollMode: "if-needed" });
417
- };
418
- addDomEvent(viewport, "resize", onResize, { once: true });
419
- }
420
- });
421
- }
422
- };
423
- let onWindowScroll = () => {
424
- win.scrollTo(0, 0);
425
- };
426
- let scrollX = win.scrollX;
427
- let scrollY = win.scrollY;
428
- let restoreStyles = pipe(setStyle(docEl, "paddingRight", `${win.innerWidth - docEl.clientWidth}px`), setStyle(docEl, "overflow", "hidden"), setStyle(doc.body, "marginTop", `-${scrollY}px`));
429
- win.scrollTo(0, 0);
430
- 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"));
431
- return () => {
432
- restoreStyles();
433
- removeEvents();
434
- win.scrollTo(scrollX, scrollY);
435
- docEl.removeAttribute("scroll-lock");
436
- };
437
- }
438
- if (disabled)
439
- return noop;
440
- return isIos() ? preventScrollMobileSafari() : preventScrollStandard();
441
- }
442
- function setStyle(el, key, value) {
443
- let cur = el.style[key];
444
- el.style[key] = value;
445
- return () => {
446
- el.style[key] = cur;
447
- };
448
- }
449
147
  function validateBlur(event, opts) {
450
148
  var _a;
451
149
  const exclude = Array.isArray(opts.exclude) ? opts.exclude : [opts.exclude];
@@ -470,7 +168,7 @@ var focusableSelector = /* @__PURE__ */ [
470
168
  "details > summary:first-of-type"
471
169
  ].join(",");
472
170
  function isHidden(el, until) {
473
- const style = getComputedStyle2(el);
171
+ const style = getComputedStyle(el);
474
172
  if (!el || style.getPropertyValue("visibility") === "hidden")
475
173
  return true;
476
174
  while (el) {
@@ -486,7 +184,8 @@ var getFocusables = (el, includeContainer = false) => {
486
184
  if (!el)
487
185
  return [];
488
186
  let els = Array.from(el.querySelectorAll(focusableSelector));
489
- if (includeContainer && isHTMLElement(el)) {
187
+ const shouldAddContainer = includeContainer == true || includeContainer == "if-empty" && els.length === 0;
188
+ if (shouldAddContainer && isHTMLElement(el)) {
490
189
  els.unshift(el);
491
190
  }
492
191
  return els.filter((el2) => isFocusable(el2) && !isHidden(el2));
@@ -497,35 +196,13 @@ var isFocusable = (el) => {
497
196
  return el == null ? void 0 : el.matches(focusableSelector);
498
197
  };
499
198
  var getTabbables = (el, includeContainer = false) => {
199
+ if (!el)
200
+ return [];
500
201
  return getFocusables(el, includeContainer).filter(isTabbable);
501
202
  };
502
203
  var isTabbable = (el) => {
503
204
  return isFocusable(el) && !isDisabled(el) && !isHidden(el);
504
205
  };
505
- function itemById(v, id) {
506
- return v.find((node) => node.id === id);
507
- }
508
- function indexOfId(v, id) {
509
- const item = itemById(v, id);
510
- return item ? v.indexOf(item) : -1;
511
- }
512
- var getValueText = (item) => {
513
- var _a, _b;
514
- return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
515
- };
516
- var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
517
- var wrap = (v, idx) => {
518
- return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
519
- };
520
- function findByText(v, text, currentId) {
521
- const index = currentId ? indexOfId(v, currentId) : -1;
522
- let items = currentId ? wrap(v, index) : v;
523
- const isSingleKey = text.length === 1;
524
- if (isSingleKey) {
525
- items = items.filter((item) => item.id !== currentId);
526
- }
527
- return items.find((item) => match(getValueText(item), text));
528
- }
529
206
  function trackPointerDown(doc, onPointerDown) {
530
207
  var _a;
531
208
  const win = (_a = doc.defaultView) != null ? _a : window;
@@ -536,41 +213,17 @@ function trackPointerDown(doc, onPointerDown) {
536
213
  };
537
214
  return addDomEvent(doc, "pointerdown", fn);
538
215
  }
539
- function findByTypeahead(_items, options) {
540
- const { state: state2, activeId, key, timeout = 350 } = options;
541
- const search = state2.keysSoFar + key;
542
- const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
543
- const query2 = isRepeated ? search[0] : search;
544
- let items = _items.slice();
545
- const next2 = findByText(items, query2, activeId);
546
- function cleanup() {
547
- clearTimeout(state2.timer);
548
- state2.timer = -1;
549
- }
550
- function update(value) {
551
- state2.keysSoFar = value;
552
- cleanup();
553
- if (value !== "") {
554
- state2.timer = +setTimeout(() => {
555
- update("");
556
- cleanup();
557
- }, timeout);
558
- }
559
- }
560
- update(search);
561
- return next2;
562
- }
563
- findByTypeahead.defaultOptions = {
564
- keysSoFar: "",
565
- timer: -1
566
- };
567
216
 
568
217
  // src/popover.connect.ts
569
218
  import { getPlacementStyles } from "@zag-js/popper";
570
219
 
571
220
  // ../../types/dist/index.mjs
572
221
  function createNormalizer(fn) {
573
- return { button: fn, label: fn, input: fn, output: fn, element: fn };
222
+ return new Proxy({}, {
223
+ get() {
224
+ return fn;
225
+ }
226
+ });
574
227
  }
575
228
  var normalizeProp = createNormalizer((v) => v);
576
229
 
@@ -645,15 +298,16 @@ var dom = {
645
298
  getFocusableEls: (ctx) => getFocusables(dom.getContentEl(ctx)),
646
299
  getFirstFocusableEl: (ctx) => dom.getFocusableEls(ctx)[0],
647
300
  getDocTabbableEls: (ctx) => getTabbables(cast(dom.getDoc(ctx))),
648
- getTabbableEls: (ctx) => {
649
- const el = dom.getContentEl(ctx);
650
- return el ? getTabbables(el) : [];
651
- },
301
+ getTabbableEls: (ctx) => getTabbables(dom.getContentEl(ctx), "if-empty"),
652
302
  getFirstTabbableEl: (ctx) => first(dom.getTabbableEls(ctx)),
653
303
  getLastTabbableEl: (ctx) => last(dom.getTabbableEls(ctx)),
654
304
  getInitialFocusEl: (ctx) => {
655
- var _a;
656
- return (_a = runIfFn2(ctx.initialFocusEl)) != null ? _a : dom.getFirstFocusableEl(ctx);
305
+ let el = runIfFn2(ctx.initialFocusEl);
306
+ if (!el && ctx.autoFocus)
307
+ el = dom.getFirstFocusableEl(ctx);
308
+ if (!el)
309
+ el = dom.getContentEl(ctx);
310
+ return el;
657
311
  }
658
312
  };
659
313
 
@@ -661,9 +315,10 @@ var dom = {
661
315
  function connect(state, send, normalize = normalizeProp) {
662
316
  const isOpen = state.matches("open");
663
317
  const pointerdownNode = state.context.pointerdownNode;
318
+ const currentPlacement = state.context.currentPlacement;
664
319
  const popperStyles = getPlacementStyles({
665
320
  measured: !!state.context.isPlacementComplete,
666
- placement: state.context.currentPlacement
321
+ placement: currentPlacement
667
322
  });
668
323
  return {
669
324
  portalled: state.context.currentPortalled,
@@ -690,7 +345,7 @@ function connect(state, send, normalize = normalizeProp) {
690
345
  triggerProps: normalize.button({
691
346
  "data-part": "trigger",
692
347
  type: "button",
693
- "data-placement": state.context.currentPlacement,
348
+ "data-placement": currentPlacement,
694
349
  id: dom.getTriggerId(state.context),
695
350
  "aria-haspopup": "dialog",
696
351
  "aria-expanded": isOpen,
@@ -698,6 +353,11 @@ function connect(state, send, normalize = normalizeProp) {
698
353
  "aria-controls": dom.getContentId(state.context),
699
354
  onClick() {
700
355
  send("TRIGGER_CLICK");
356
+ },
357
+ onKeyDown(event) {
358
+ if (event.key === "Escape") {
359
+ send("ESCAPE");
360
+ }
701
361
  }
702
362
  }),
703
363
  positionerProps: normalize.element({
@@ -712,9 +372,9 @@ function connect(state, send, normalize = normalizeProp) {
712
372
  role: "dialog",
713
373
  hidden: !isOpen,
714
374
  "data-expanded": dataAttr(isOpen),
715
- "aria-labelledby": state.context.isTitleRendered ? dom.getTitleId(state.context) : void 0,
716
- "aria-describedby": state.context.isDescriptionRendered ? dom.getDescriptionId(state.context) : void 0,
717
- "data-placement": state.context.currentPlacement,
375
+ "aria-labelledby": state.context.renderedElements.title ? dom.getTitleId(state.context) : void 0,
376
+ "aria-describedby": state.context.renderedElements.description ? dom.getDescriptionId(state.context) : void 0,
377
+ "data-placement": currentPlacement,
718
378
  onKeyDown(event) {
719
379
  const keyMap = {
720
380
  Escape(event2) {
@@ -762,19 +422,17 @@ function connect(state, send, normalize = normalizeProp) {
762
422
  }
763
423
 
764
424
  // src/popover.machine.ts
765
- import { choose, createMachine, guards, ref } from "@zag-js/core";
766
- import { getPlacement } from "@zag-js/popper";
767
425
  import { ariaHidden } from "@zag-js/aria-hidden";
426
+ import { createMachine, guards, ref } from "@zag-js/core";
427
+ import { getPlacement } from "@zag-js/popper";
428
+ import { preventBodyScroll } from "@zag-js/remove-scroll";
768
429
  import { createFocusTrap } from "focus-trap";
769
430
  var { and, or } = guards;
770
431
  function machine(ctx = {}) {
771
432
  return createMachine({
772
433
  id: "popover",
773
434
  initial: "unknown",
774
- context: __spreadValues({
775
- isTitleRendered: true,
776
- isDescriptionRendered: true,
777
- isAnchorRendered: false,
435
+ context: __spreadProps(__spreadValues({
778
436
  uid: "",
779
437
  closeOnBlur: true,
780
438
  closeOnEsc: true,
@@ -784,7 +442,13 @@ function machine(ctx = {}) {
784
442
  placement: "bottom"
785
443
  }, ctx.positioning),
786
444
  currentPlacement: void 0
787
- }, ctx),
445
+ }, ctx), {
446
+ renderedElements: {
447
+ title: true,
448
+ description: true,
449
+ anchor: false
450
+ }
451
+ }),
788
452
  computed: {
789
453
  currentPortalled: (ctx2) => !!ctx2.modal || !!ctx2.portalled
790
454
  },
@@ -792,7 +456,7 @@ function machine(ctx = {}) {
792
456
  unknown: {
793
457
  on: {
794
458
  SETUP: {
795
- target: "closed",
459
+ target: ctx.open ? "open" : "closed",
796
460
  actions: ["setupDocument", "checkRenderedElements"]
797
461
  }
798
462
  }
@@ -813,13 +477,7 @@ function machine(ctx = {}) {
813
477
  "disableOutsidePointerEvents",
814
478
  "computePlacement"
815
479
  ],
816
- entry: choose([
817
- {
818
- guard: "autoFocus",
819
- actions: ["setInitialFocus", "invokeOnOpen"]
820
- },
821
- { actions: ["focusContent", "invokeOnOpen"] }
822
- ]),
480
+ entry: ["setInitialFocus", "invokeOnOpen"],
823
481
  on: {
824
482
  CLOSE: {
825
483
  target: "closed",
@@ -862,7 +520,7 @@ function machine(ctx = {}) {
862
520
  activities: {
863
521
  computePlacement(ctx2) {
864
522
  ctx2.currentPlacement = ctx2.positioning.placement;
865
- const anchorEl = ctx2.isAnchorRendered ? dom.getAnchorEl(ctx2) : dom.getTriggerEl(ctx2);
523
+ const anchorEl = ctx2.renderedElements.anchor ? dom.getAnchorEl(ctx2) : dom.getTriggerEl(ctx2);
866
524
  return getPlacement(anchorEl, dom.getPositionerEl(ctx2), __spreadProps(__spreadValues({}, ctx2.positioning), {
867
525
  onComplete(data) {
868
526
  ctx2.currentPlacement = data.placement;
@@ -897,7 +555,6 @@ function machine(ctx = {}) {
897
555
  },
898
556
  preventScroll(ctx2) {
899
557
  return preventBodyScroll({
900
- allowPinchZoom: true,
901
558
  disabled: !ctx2.modal,
902
559
  document: dom.getDoc(ctx2)
903
560
  });
@@ -920,7 +577,7 @@ function machine(ctx = {}) {
920
577
  });
921
578
  try {
922
579
  trap.activate();
923
- } catch (e2) {
580
+ } catch (e) {
924
581
  }
925
582
  });
926
583
  return () => trap == null ? void 0 : trap.deactivate();
@@ -940,9 +597,11 @@ function machine(ctx = {}) {
940
597
  actions: {
941
598
  checkRenderedElements(ctx2) {
942
599
  raf(() => {
943
- ctx2.isAnchorRendered = !!dom.getAnchorEl(ctx2);
944
- ctx2.isTitleRendered = !!dom.getTitleEl(ctx2);
945
- ctx2.isDescriptionRendered = !!dom.getDescriptionEl(ctx2);
600
+ Object.assign(ctx2.renderedElements, {
601
+ title: !!dom.getTitleEl(ctx2),
602
+ description: !!dom.getDescriptionEl(ctx2),
603
+ anchor: !!dom.getAnchorEl(ctx2)
604
+ });
946
605
  });
947
606
  },
948
607
  setupDocument(ctx2, evt) {
@@ -955,12 +614,6 @@ function machine(ctx = {}) {
955
614
  clearPointerDown(ctx2) {
956
615
  ctx2.pointerdownNode = null;
957
616
  },
958
- focusContent(ctx2) {
959
- raf(() => {
960
- var _a;
961
- (_a = dom.getContentEl(ctx2)) == null ? void 0 : _a.focus();
962
- });
963
- },
964
617
  setInitialFocus(ctx2) {
965
618
  raf(() => {
966
619
  var _a;
@@ -1012,4 +665,3 @@ export {
1012
665
  connect,
1013
666
  machine
1014
667
  };
1015
- //# sourceMappingURL=index.mjs.map