@zag-js/popover 0.0.0-dev-20220706194607 → 0.0.0-dev-20220709094240

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
@@ -43,6 +43,22 @@ __export(src_exports, {
43
43
  module.exports = __toCommonJS(src_exports);
44
44
 
45
45
  // ../../utilities/dom/dist/index.mjs
46
+ var __defProp2 = Object.defineProperty;
47
+ var __getOwnPropSymbols2 = Object.getOwnPropertySymbols;
48
+ var __hasOwnProp2 = Object.prototype.hasOwnProperty;
49
+ var __propIsEnum2 = Object.prototype.propertyIsEnumerable;
50
+ var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
51
+ var __spreadValues2 = (a, b) => {
52
+ for (var prop in b || (b = {}))
53
+ if (__hasOwnProp2.call(b, prop))
54
+ __defNormalProp2(a, prop, b[prop]);
55
+ if (__getOwnPropSymbols2)
56
+ for (var prop of __getOwnPropSymbols2(b)) {
57
+ if (__propIsEnum2.call(b, prop))
58
+ __defNormalProp2(a, prop, b[prop]);
59
+ }
60
+ return a;
61
+ };
46
62
  var dataAttr = (guard) => {
47
63
  return guard ? "" : void 0;
48
64
  };
@@ -51,9 +67,37 @@ var runIfFn = (v, ...a) => {
51
67
  return res != null ? res : void 0;
52
68
  };
53
69
  var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
70
+ function isDocument(el) {
71
+ return el.nodeType === Node.DOCUMENT_NODE;
72
+ }
73
+ function isWindow(value) {
74
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
75
+ }
54
76
  function isFrame(element) {
55
77
  return element.localName === "iframe";
56
78
  }
79
+ function getDocument(el) {
80
+ var _a;
81
+ if (isWindow(el))
82
+ return el.document;
83
+ if (isDocument(el))
84
+ return el;
85
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
86
+ }
87
+ function withRootHelpers(domUtils) {
88
+ const roots = {
89
+ getRootNode: (ctx) => {
90
+ var _a, _b;
91
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
92
+ },
93
+ getDoc: (ctx) => getDocument(roots.getRootNode(ctx)),
94
+ getWin: (ctx) => {
95
+ var _a;
96
+ return (_a = roots.getDoc(ctx).defaultView) != null ? _a : window;
97
+ }
98
+ };
99
+ return __spreadValues2(__spreadValues2({}, roots), domUtils);
100
+ }
57
101
  function contains(parent, child) {
58
102
  if (!parent)
59
103
  return false;
@@ -180,45 +224,33 @@ var runIfFn2 = (v, ...a) => {
180
224
  };
181
225
 
182
226
  // src/popover.dom.ts
183
- var dom = {
184
- getDoc: (ctx) => {
185
- var _a;
186
- return (_a = ctx.doc) != null ? _a : document;
187
- },
188
- getWin: (ctx) => {
189
- var _a;
190
- return (_a = dom.getDoc(ctx).defaultView) != null ? _a : window;
191
- },
227
+ var dom = withRootHelpers({
192
228
  getActiveEl: (ctx) => dom.getDoc(ctx).activeElement,
193
- getRootNode: (ctx) => {
194
- var _a;
195
- return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
196
- },
197
229
  getAnchorId: (ctx) => {
198
230
  var _a, _b;
199
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.anchor) != null ? _b : `popover:${ctx.uid}:anchor`;
231
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.anchor) != null ? _b : `popover:${ctx.id}:anchor`;
200
232
  },
201
233
  getTriggerId: (ctx) => {
202
234
  var _a, _b;
203
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `popover:${ctx.uid}:trigger`;
235
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `popover:${ctx.id}:trigger`;
204
236
  },
205
237
  getContentId: (ctx) => {
206
238
  var _a, _b;
207
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `popover:${ctx.uid}:content`;
239
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `popover:${ctx.id}:content`;
208
240
  },
209
- getPositionerId: (ctx) => `popover:${ctx.uid}:popper`,
210
- getArrowId: (ctx) => `popover:${ctx.uid}:arrow`,
241
+ getPositionerId: (ctx) => `popover:${ctx.id}:popper`,
242
+ getArrowId: (ctx) => `popover:${ctx.id}:arrow`,
211
243
  getTitleId: (ctx) => {
212
244
  var _a, _b;
213
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.title) != null ? _b : `popover:${ctx.uid}:title`;
245
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.title) != null ? _b : `popover:${ctx.id}:title`;
214
246
  },
215
247
  getDescriptionId: (ctx) => {
216
248
  var _a, _b;
217
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.description) != null ? _b : `popover:${ctx.uid}:desc`;
249
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.description) != null ? _b : `popover:${ctx.id}:desc`;
218
250
  },
219
251
  getCloseButtonId: (ctx) => {
220
252
  var _a, _b;
221
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.closeBtn) != null ? _b : `popover:${ctx.uid}:close-button`;
253
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.closeBtn) != null ? _b : `popover:${ctx.id}:close-button`;
222
254
  },
223
255
  getAnchorEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getAnchorId(ctx)),
224
256
  getTriggerEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getTriggerId(ctx)),
@@ -240,7 +272,7 @@ var dom = {
240
272
  el = dom.getContentEl(ctx);
241
273
  return el;
242
274
  }
243
- };
275
+ });
244
276
 
245
277
  // src/popover.connect.ts
246
278
  function connect(state, send, normalize) {
@@ -345,13 +377,18 @@ function getPlatform() {
345
377
  var pt = (v) => isDom() && v.test(getPlatform());
346
378
  var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
347
379
  var isMac = () => pt(/^Mac/) && !isTouchDevice;
348
- function isWindow(value) {
380
+ function isDocument2(el) {
381
+ return el.nodeType === Node.DOCUMENT_NODE;
382
+ }
383
+ function isWindow2(value) {
349
384
  return (value == null ? void 0 : value.toString()) === "[object Window]";
350
385
  }
351
- function getDocument(el) {
386
+ function getDocument2(el) {
352
387
  var _a;
353
- if (isWindow(el))
388
+ if (isWindow2(el))
354
389
  return el.document;
390
+ if (isDocument2(el))
391
+ return el;
355
392
  return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
356
393
  }
357
394
  function getWindow(el) {
@@ -382,7 +419,8 @@ var isCtrlKey = (v) => isMac() ? v.metaKey && !v.ctrlKey : v.ctrlKey && !v.metaK
382
419
  function fireCustomEvent(el, type, init) {
383
420
  if (!el)
384
421
  return;
385
- const event = new CustomEvent(type, init);
422
+ const win = getWindow(el);
423
+ const event = new win.CustomEvent(type, init);
386
424
  return el.dispatchEvent(event);
387
425
  }
388
426
  var focusableSelector2 = "input:not([type='hidden']):not([disabled]), select:not([disabled]), textarea:not([disabled]), a[href], button:not([disabled]), [tabindex], iframe, object, embed, area[href], audio[controls], video[controls], [contenteditable]:not([contenteditable='false']), details > summary:first-of-type";
@@ -405,7 +443,7 @@ function trackInteractOutside(node, options) {
405
443
  const { exclude, onFocusOutside, onPointerDownOutside } = options;
406
444
  if (!node)
407
445
  return;
408
- const doc = getDocument(node);
446
+ const doc = getDocument2(node);
409
447
  const win = getWindow(node);
410
448
  function isEventOutside(event) {
411
449
  const target = getEventTarget(event);
@@ -481,13 +519,18 @@ var runIfFn4 = (v, ...a) => {
481
519
  return res != null ? res : void 0;
482
520
  };
483
521
  var hasProp3 = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
484
- function isWindow2(value) {
522
+ function isDocument3(el) {
523
+ return el.nodeType === Node.DOCUMENT_NODE;
524
+ }
525
+ function isWindow3(value) {
485
526
  return (value == null ? void 0 : value.toString()) === "[object Window]";
486
527
  }
487
- function getDocument2(el) {
528
+ function getDocument3(el) {
488
529
  var _a;
489
- if (isWindow2(el))
530
+ if (isWindow3(el))
490
531
  return el.document;
532
+ if (isDocument3(el))
533
+ return el;
491
534
  return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
492
535
  }
493
536
  function getEventTarget2(event) {
@@ -542,14 +585,14 @@ var layerStack = {
542
585
  const layer = this.layers[this.count() - 1];
543
586
  return (layer == null ? void 0 : layer.node) === node;
544
587
  },
545
- getChildLayers(node) {
546
- return this.layers.slice(this.indexOf(node));
588
+ getNestedLayers(node) {
589
+ return Array.from(this.layers).slice(this.indexOf(node) + 1);
547
590
  },
548
- isInChildLayer(node, target) {
549
- return this.getChildLayers(node).some((layer) => contains3(layer.node, target));
591
+ isInNestedLayer(node, target) {
592
+ return this.getNestedLayers(node).some((layer) => contains3(layer.node, target));
550
593
  },
551
594
  isInBranch(target) {
552
- return [...this.branches].some((branch) => contains3(branch, target));
595
+ return Array.from(this.branches).some((branch) => contains3(branch, target));
553
596
  },
554
597
  add(layer) {
555
598
  this.layers.push(layer);
@@ -559,10 +602,13 @@ var layerStack = {
559
602
  },
560
603
  remove(node) {
561
604
  const index = this.indexOf(node);
562
- if (index + 1 < this.count() - 1) {
563
- this.layers.splice(index, this.count()).forEach((item) => item.dismiss());
605
+ if (index < 0)
606
+ return;
607
+ if (index < this.count() - 1) {
608
+ const _layers = this.getNestedLayers(node);
609
+ _layers.forEach((layer) => layer.dismiss());
564
610
  }
565
- this.layers = this.layers.filter((item) => item.node !== node);
611
+ this.layers.splice(index, 1);
566
612
  },
567
613
  removeBranch(node) {
568
614
  const index = this.branches.indexOf(node);
@@ -591,7 +637,7 @@ function clearPointerEvent(node) {
591
637
  }
592
638
  var DATA_ATTR = "data-inert";
593
639
  function disablePointerEventsOutside(node) {
594
- const doc = getDocument2(node);
640
+ const doc = getDocument3(node);
595
641
  if (layerStack.hasPointerBlockingLayer() && !doc.body.hasAttribute(DATA_ATTR)) {
596
642
  originalBodyPointerEvents = document.body.style.pointerEvents;
597
643
  doc.body.style.pointerEvents = "none";
@@ -609,7 +655,7 @@ function disablePointerEventsOutside(node) {
609
655
  function trackDismissableElement(node, options) {
610
656
  if (!node)
611
657
  return;
612
- const { onDismiss, pointerBlocking, exclude: excludeContainers } = options;
658
+ const { onDismiss, pointerBlocking, exclude: excludeContainers, debug } = options;
613
659
  const layer = { dismiss: onDismiss, node, pointerBlocking };
614
660
  layerStack.add(layer);
615
661
  assignPointerEventToLayers();
@@ -622,6 +668,9 @@ function trackDismissableElement(node, options) {
622
668
  (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
623
669
  if (event.defaultPrevented)
624
670
  return;
671
+ if (debug) {
672
+ console.log("onPointerDownOutside:", event.detail.originalEvent);
673
+ }
625
674
  onDismiss == null ? void 0 : onDismiss();
626
675
  }
627
676
  function onFocusOutside(event) {
@@ -633,6 +682,9 @@ function trackDismissableElement(node, options) {
633
682
  (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
634
683
  if (event.defaultPrevented)
635
684
  return;
685
+ if (debug) {
686
+ console.log("onFocusOutside:", event.detail.originalEvent);
687
+ }
636
688
  onDismiss == null ? void 0 : onDismiss();
637
689
  }
638
690
  function onEscapeKeyDown(event) {
@@ -646,9 +698,11 @@ function trackDismissableElement(node, options) {
646
698
  }
647
699
  }
648
700
  function exclude(target) {
701
+ if (!node)
702
+ return false;
649
703
  const containers = typeof excludeContainers === "function" ? excludeContainers() : excludeContainers;
650
- const inContainers = Array.isArray(containers) ? containers : [containers];
651
- return inContainers.some((node2) => contains3(node2, target)) || layerStack.isInChildLayer(node, target);
704
+ const _containers = Array.isArray(containers) ? containers : [containers];
705
+ return _containers.some((node2) => contains3(node2, target)) || layerStack.isInNestedLayer(node, target);
652
706
  }
653
707
  const cleanups = [
654
708
  pointerBlocking ? disablePointerEventsOutside(node) : void 0,
@@ -668,12 +722,11 @@ var import_popper2 = require("@zag-js/popper");
668
722
  var import_remove_scroll = require("@zag-js/remove-scroll");
669
723
  var import_focus_trap = require("focus-trap");
670
724
  var { and, or, not } = import_core.guards;
671
- function machine(ctx = {}) {
725
+ function machine(ctx) {
672
726
  return (0, import_core.createMachine)({
673
727
  id: "popover",
674
728
  initial: "unknown",
675
729
  context: __spreadProps(__spreadValues({
676
- uid: "",
677
730
  closeOnInteractOutside: true,
678
731
  closeOnEsc: true,
679
732
  autoFocus: true,
@@ -683,7 +736,7 @@ function machine(ctx = {}) {
683
736
  }, ctx.positioning),
684
737
  currentPlacement: void 0
685
738
  }, ctx), {
686
- preventReturnFocus: false,
739
+ focusTriggerOnClose: true,
687
740
  renderedElements: {
688
741
  title: true,
689
742
  description: true,
@@ -698,7 +751,7 @@ function machine(ctx = {}) {
698
751
  on: {
699
752
  SETUP: {
700
753
  target: ctx.defaultOpen ? "open" : "closed",
701
- actions: ["setupDocument", "checkRenderedElements"]
754
+ actions: "checkRenderedElements"
702
755
  }
703
756
  }
704
757
  },
@@ -773,7 +826,7 @@ function machine(ctx = {}) {
773
826
  (_a = ctx2.onEscapeKeyDown) == null ? void 0 : _a.call(ctx2, event);
774
827
  if (ctx2.closeOnEsc)
775
828
  return;
776
- ctx2.preventReturnFocus = false;
829
+ ctx2.focusTriggerOnClose = true;
777
830
  event.preventDefault();
778
831
  },
779
832
  onInteractOutside(event) {
@@ -781,7 +834,7 @@ function machine(ctx = {}) {
781
834
  (_a = ctx2.onInteractOutside) == null ? void 0 : _a.call(ctx2, event);
782
835
  if (event.defaultPrevented)
783
836
  return;
784
- ctx2.preventReturnFocus = event.detail.focusable || event.detail.contextmenu;
837
+ ctx2.focusTriggerOnClose = !(event.detail.focusable || event.detail.contextmenu);
785
838
  if (!ctx2.closeOnInteractOutside) {
786
839
  event.preventDefault();
787
840
  }
@@ -872,13 +925,6 @@ function machine(ctx = {}) {
872
925
  });
873
926
  });
874
927
  },
875
- setupDocument(ctx2, evt) {
876
- if (evt.doc)
877
- ctx2.doc = (0, import_core.ref)(evt.doc);
878
- if (evt.root)
879
- ctx2.rootNode = (0, import_core.ref)(evt.root);
880
- ctx2.uid = evt.id;
881
- },
882
928
  clearPointerDown(ctx2) {
883
929
  ctx2.pointerdownNode = null;
884
930
  },
@@ -889,15 +935,11 @@ function machine(ctx = {}) {
889
935
  });
890
936
  },
891
937
  focusTriggerIfNeeded(ctx2) {
892
- const focus = () => {
938
+ if (!ctx2.focusTriggerOnClose)
939
+ return;
940
+ raf(() => {
893
941
  var _a;
894
942
  return (_a = dom.getTriggerEl(ctx2)) == null ? void 0 : _a.focus();
895
- };
896
- raf(() => {
897
- if (!ctx2.preventReturnFocus) {
898
- focus();
899
- ctx2.preventReturnFocus = false;
900
- }
901
943
  });
902
944
  },
903
945
  focusFirstTabbableElement(ctx2, evt) {
package/dist/index.mjs CHANGED
@@ -19,6 +19,22 @@ var __spreadValues = (a, b) => {
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
20
 
21
21
  // ../../utilities/dom/dist/index.mjs
22
+ var __defProp2 = Object.defineProperty;
23
+ var __getOwnPropSymbols2 = Object.getOwnPropertySymbols;
24
+ var __hasOwnProp2 = Object.prototype.hasOwnProperty;
25
+ var __propIsEnum2 = Object.prototype.propertyIsEnumerable;
26
+ var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
27
+ var __spreadValues2 = (a, b) => {
28
+ for (var prop in b || (b = {}))
29
+ if (__hasOwnProp2.call(b, prop))
30
+ __defNormalProp2(a, prop, b[prop]);
31
+ if (__getOwnPropSymbols2)
32
+ for (var prop of __getOwnPropSymbols2(b)) {
33
+ if (__propIsEnum2.call(b, prop))
34
+ __defNormalProp2(a, prop, b[prop]);
35
+ }
36
+ return a;
37
+ };
22
38
  var dataAttr = (guard) => {
23
39
  return guard ? "" : void 0;
24
40
  };
@@ -27,9 +43,37 @@ var runIfFn = (v, ...a) => {
27
43
  return res != null ? res : void 0;
28
44
  };
29
45
  var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
46
+ function isDocument(el) {
47
+ return el.nodeType === Node.DOCUMENT_NODE;
48
+ }
49
+ function isWindow(value) {
50
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
51
+ }
30
52
  function isFrame(element) {
31
53
  return element.localName === "iframe";
32
54
  }
55
+ function getDocument(el) {
56
+ var _a;
57
+ if (isWindow(el))
58
+ return el.document;
59
+ if (isDocument(el))
60
+ return el;
61
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
62
+ }
63
+ function withRootHelpers(domUtils) {
64
+ const roots = {
65
+ getRootNode: (ctx) => {
66
+ var _a, _b;
67
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
68
+ },
69
+ getDoc: (ctx) => getDocument(roots.getRootNode(ctx)),
70
+ getWin: (ctx) => {
71
+ var _a;
72
+ return (_a = roots.getDoc(ctx).defaultView) != null ? _a : window;
73
+ }
74
+ };
75
+ return __spreadValues2(__spreadValues2({}, roots), domUtils);
76
+ }
33
77
  function contains(parent, child) {
34
78
  if (!parent)
35
79
  return false;
@@ -156,45 +200,33 @@ var runIfFn2 = (v, ...a) => {
156
200
  };
157
201
 
158
202
  // src/popover.dom.ts
159
- var dom = {
160
- getDoc: (ctx) => {
161
- var _a;
162
- return (_a = ctx.doc) != null ? _a : document;
163
- },
164
- getWin: (ctx) => {
165
- var _a;
166
- return (_a = dom.getDoc(ctx).defaultView) != null ? _a : window;
167
- },
203
+ var dom = withRootHelpers({
168
204
  getActiveEl: (ctx) => dom.getDoc(ctx).activeElement,
169
- getRootNode: (ctx) => {
170
- var _a;
171
- return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
172
- },
173
205
  getAnchorId: (ctx) => {
174
206
  var _a, _b;
175
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.anchor) != null ? _b : `popover:${ctx.uid}:anchor`;
207
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.anchor) != null ? _b : `popover:${ctx.id}:anchor`;
176
208
  },
177
209
  getTriggerId: (ctx) => {
178
210
  var _a, _b;
179
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `popover:${ctx.uid}:trigger`;
211
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `popover:${ctx.id}:trigger`;
180
212
  },
181
213
  getContentId: (ctx) => {
182
214
  var _a, _b;
183
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `popover:${ctx.uid}:content`;
215
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `popover:${ctx.id}:content`;
184
216
  },
185
- getPositionerId: (ctx) => `popover:${ctx.uid}:popper`,
186
- getArrowId: (ctx) => `popover:${ctx.uid}:arrow`,
217
+ getPositionerId: (ctx) => `popover:${ctx.id}:popper`,
218
+ getArrowId: (ctx) => `popover:${ctx.id}:arrow`,
187
219
  getTitleId: (ctx) => {
188
220
  var _a, _b;
189
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.title) != null ? _b : `popover:${ctx.uid}:title`;
221
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.title) != null ? _b : `popover:${ctx.id}:title`;
190
222
  },
191
223
  getDescriptionId: (ctx) => {
192
224
  var _a, _b;
193
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.description) != null ? _b : `popover:${ctx.uid}:desc`;
225
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.description) != null ? _b : `popover:${ctx.id}:desc`;
194
226
  },
195
227
  getCloseButtonId: (ctx) => {
196
228
  var _a, _b;
197
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.closeBtn) != null ? _b : `popover:${ctx.uid}:close-button`;
229
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.closeBtn) != null ? _b : `popover:${ctx.id}:close-button`;
198
230
  },
199
231
  getAnchorEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getAnchorId(ctx)),
200
232
  getTriggerEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getTriggerId(ctx)),
@@ -216,7 +248,7 @@ var dom = {
216
248
  el = dom.getContentEl(ctx);
217
249
  return el;
218
250
  }
219
- };
251
+ });
220
252
 
221
253
  // src/popover.connect.ts
222
254
  function connect(state, send, normalize) {
@@ -304,7 +336,7 @@ function connect(state, send, normalize) {
304
336
 
305
337
  // src/popover.machine.ts
306
338
  import { ariaHidden } from "@zag-js/aria-hidden";
307
- import { createMachine, guards, ref } from "@zag-js/core";
339
+ import { createMachine, guards } from "@zag-js/core";
308
340
 
309
341
  // ../../utilities/interact-outside/dist/index.mjs
310
342
  var runIfFn3 = (v, ...a) => {
@@ -321,13 +353,18 @@ function getPlatform() {
321
353
  var pt = (v) => isDom() && v.test(getPlatform());
322
354
  var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
323
355
  var isMac = () => pt(/^Mac/) && !isTouchDevice;
324
- function isWindow(value) {
356
+ function isDocument2(el) {
357
+ return el.nodeType === Node.DOCUMENT_NODE;
358
+ }
359
+ function isWindow2(value) {
325
360
  return (value == null ? void 0 : value.toString()) === "[object Window]";
326
361
  }
327
- function getDocument(el) {
362
+ function getDocument2(el) {
328
363
  var _a;
329
- if (isWindow(el))
364
+ if (isWindow2(el))
330
365
  return el.document;
366
+ if (isDocument2(el))
367
+ return el;
331
368
  return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
332
369
  }
333
370
  function getWindow(el) {
@@ -358,7 +395,8 @@ var isCtrlKey = (v) => isMac() ? v.metaKey && !v.ctrlKey : v.ctrlKey && !v.metaK
358
395
  function fireCustomEvent(el, type, init) {
359
396
  if (!el)
360
397
  return;
361
- const event = new CustomEvent(type, init);
398
+ const win = getWindow(el);
399
+ const event = new win.CustomEvent(type, init);
362
400
  return el.dispatchEvent(event);
363
401
  }
364
402
  var focusableSelector2 = "input:not([type='hidden']):not([disabled]), select:not([disabled]), textarea:not([disabled]), a[href], button:not([disabled]), [tabindex], iframe, object, embed, area[href], audio[controls], video[controls], [contenteditable]:not([contenteditable='false']), details > summary:first-of-type";
@@ -381,7 +419,7 @@ function trackInteractOutside(node, options) {
381
419
  const { exclude, onFocusOutside, onPointerDownOutside } = options;
382
420
  if (!node)
383
421
  return;
384
- const doc = getDocument(node);
422
+ const doc = getDocument2(node);
385
423
  const win = getWindow(node);
386
424
  function isEventOutside(event) {
387
425
  const target = getEventTarget(event);
@@ -457,13 +495,18 @@ var runIfFn4 = (v, ...a) => {
457
495
  return res != null ? res : void 0;
458
496
  };
459
497
  var hasProp3 = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
460
- function isWindow2(value) {
498
+ function isDocument3(el) {
499
+ return el.nodeType === Node.DOCUMENT_NODE;
500
+ }
501
+ function isWindow3(value) {
461
502
  return (value == null ? void 0 : value.toString()) === "[object Window]";
462
503
  }
463
- function getDocument2(el) {
504
+ function getDocument3(el) {
464
505
  var _a;
465
- if (isWindow2(el))
506
+ if (isWindow3(el))
466
507
  return el.document;
508
+ if (isDocument3(el))
509
+ return el;
467
510
  return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
468
511
  }
469
512
  function getEventTarget2(event) {
@@ -518,14 +561,14 @@ var layerStack = {
518
561
  const layer = this.layers[this.count() - 1];
519
562
  return (layer == null ? void 0 : layer.node) === node;
520
563
  },
521
- getChildLayers(node) {
522
- return this.layers.slice(this.indexOf(node));
564
+ getNestedLayers(node) {
565
+ return Array.from(this.layers).slice(this.indexOf(node) + 1);
523
566
  },
524
- isInChildLayer(node, target) {
525
- return this.getChildLayers(node).some((layer) => contains3(layer.node, target));
567
+ isInNestedLayer(node, target) {
568
+ return this.getNestedLayers(node).some((layer) => contains3(layer.node, target));
526
569
  },
527
570
  isInBranch(target) {
528
- return [...this.branches].some((branch) => contains3(branch, target));
571
+ return Array.from(this.branches).some((branch) => contains3(branch, target));
529
572
  },
530
573
  add(layer) {
531
574
  this.layers.push(layer);
@@ -535,10 +578,13 @@ var layerStack = {
535
578
  },
536
579
  remove(node) {
537
580
  const index = this.indexOf(node);
538
- if (index + 1 < this.count() - 1) {
539
- this.layers.splice(index, this.count()).forEach((item) => item.dismiss());
581
+ if (index < 0)
582
+ return;
583
+ if (index < this.count() - 1) {
584
+ const _layers = this.getNestedLayers(node);
585
+ _layers.forEach((layer) => layer.dismiss());
540
586
  }
541
- this.layers = this.layers.filter((item) => item.node !== node);
587
+ this.layers.splice(index, 1);
542
588
  },
543
589
  removeBranch(node) {
544
590
  const index = this.branches.indexOf(node);
@@ -567,7 +613,7 @@ function clearPointerEvent(node) {
567
613
  }
568
614
  var DATA_ATTR = "data-inert";
569
615
  function disablePointerEventsOutside(node) {
570
- const doc = getDocument2(node);
616
+ const doc = getDocument3(node);
571
617
  if (layerStack.hasPointerBlockingLayer() && !doc.body.hasAttribute(DATA_ATTR)) {
572
618
  originalBodyPointerEvents = document.body.style.pointerEvents;
573
619
  doc.body.style.pointerEvents = "none";
@@ -585,7 +631,7 @@ function disablePointerEventsOutside(node) {
585
631
  function trackDismissableElement(node, options) {
586
632
  if (!node)
587
633
  return;
588
- const { onDismiss, pointerBlocking, exclude: excludeContainers } = options;
634
+ const { onDismiss, pointerBlocking, exclude: excludeContainers, debug } = options;
589
635
  const layer = { dismiss: onDismiss, node, pointerBlocking };
590
636
  layerStack.add(layer);
591
637
  assignPointerEventToLayers();
@@ -598,6 +644,9 @@ function trackDismissableElement(node, options) {
598
644
  (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
599
645
  if (event.defaultPrevented)
600
646
  return;
647
+ if (debug) {
648
+ console.log("onPointerDownOutside:", event.detail.originalEvent);
649
+ }
601
650
  onDismiss == null ? void 0 : onDismiss();
602
651
  }
603
652
  function onFocusOutside(event) {
@@ -609,6 +658,9 @@ function trackDismissableElement(node, options) {
609
658
  (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
610
659
  if (event.defaultPrevented)
611
660
  return;
661
+ if (debug) {
662
+ console.log("onFocusOutside:", event.detail.originalEvent);
663
+ }
612
664
  onDismiss == null ? void 0 : onDismiss();
613
665
  }
614
666
  function onEscapeKeyDown(event) {
@@ -622,9 +674,11 @@ function trackDismissableElement(node, options) {
622
674
  }
623
675
  }
624
676
  function exclude(target) {
677
+ if (!node)
678
+ return false;
625
679
  const containers = typeof excludeContainers === "function" ? excludeContainers() : excludeContainers;
626
- const inContainers = Array.isArray(containers) ? containers : [containers];
627
- return inContainers.some((node2) => contains3(node2, target)) || layerStack.isInChildLayer(node, target);
680
+ const _containers = Array.isArray(containers) ? containers : [containers];
681
+ return _containers.some((node2) => contains3(node2, target)) || layerStack.isInNestedLayer(node, target);
628
682
  }
629
683
  const cleanups = [
630
684
  pointerBlocking ? disablePointerEventsOutside(node) : void 0,
@@ -644,12 +698,11 @@ import { getPlacement } from "@zag-js/popper";
644
698
  import { preventBodyScroll } from "@zag-js/remove-scroll";
645
699
  import { createFocusTrap } from "focus-trap";
646
700
  var { and, or, not } = guards;
647
- function machine(ctx = {}) {
701
+ function machine(ctx) {
648
702
  return createMachine({
649
703
  id: "popover",
650
704
  initial: "unknown",
651
705
  context: __spreadProps(__spreadValues({
652
- uid: "",
653
706
  closeOnInteractOutside: true,
654
707
  closeOnEsc: true,
655
708
  autoFocus: true,
@@ -659,7 +712,7 @@ function machine(ctx = {}) {
659
712
  }, ctx.positioning),
660
713
  currentPlacement: void 0
661
714
  }, ctx), {
662
- preventReturnFocus: false,
715
+ focusTriggerOnClose: true,
663
716
  renderedElements: {
664
717
  title: true,
665
718
  description: true,
@@ -674,7 +727,7 @@ function machine(ctx = {}) {
674
727
  on: {
675
728
  SETUP: {
676
729
  target: ctx.defaultOpen ? "open" : "closed",
677
- actions: ["setupDocument", "checkRenderedElements"]
730
+ actions: "checkRenderedElements"
678
731
  }
679
732
  }
680
733
  },
@@ -749,7 +802,7 @@ function machine(ctx = {}) {
749
802
  (_a = ctx2.onEscapeKeyDown) == null ? void 0 : _a.call(ctx2, event);
750
803
  if (ctx2.closeOnEsc)
751
804
  return;
752
- ctx2.preventReturnFocus = false;
805
+ ctx2.focusTriggerOnClose = true;
753
806
  event.preventDefault();
754
807
  },
755
808
  onInteractOutside(event) {
@@ -757,7 +810,7 @@ function machine(ctx = {}) {
757
810
  (_a = ctx2.onInteractOutside) == null ? void 0 : _a.call(ctx2, event);
758
811
  if (event.defaultPrevented)
759
812
  return;
760
- ctx2.preventReturnFocus = event.detail.focusable || event.detail.contextmenu;
813
+ ctx2.focusTriggerOnClose = !(event.detail.focusable || event.detail.contextmenu);
761
814
  if (!ctx2.closeOnInteractOutside) {
762
815
  event.preventDefault();
763
816
  }
@@ -848,13 +901,6 @@ function machine(ctx = {}) {
848
901
  });
849
902
  });
850
903
  },
851
- setupDocument(ctx2, evt) {
852
- if (evt.doc)
853
- ctx2.doc = ref(evt.doc);
854
- if (evt.root)
855
- ctx2.rootNode = ref(evt.root);
856
- ctx2.uid = evt.id;
857
- },
858
904
  clearPointerDown(ctx2) {
859
905
  ctx2.pointerdownNode = null;
860
906
  },
@@ -865,15 +911,11 @@ function machine(ctx = {}) {
865
911
  });
866
912
  },
867
913
  focusTriggerIfNeeded(ctx2) {
868
- const focus = () => {
914
+ if (!ctx2.focusTriggerOnClose)
915
+ return;
916
+ raf(() => {
869
917
  var _a;
870
918
  return (_a = dom.getTriggerEl(ctx2)) == null ? void 0 : _a.focus();
871
- };
872
- raf(() => {
873
- if (!ctx2.preventReturnFocus) {
874
- focus();
875
- ctx2.preventReturnFocus = false;
876
- }
877
919
  });
878
920
  },
879
921
  focusFirstTabbableElement(ctx2, evt) {
@@ -1,9 +1,16 @@
1
1
  import type { MachineContext as Ctx } from "./popover.types";
2
2
  export declare const dom: {
3
- getDoc: (ctx: Ctx) => Document;
4
- getWin: (ctx: Ctx) => Window & typeof globalThis;
3
+ getRootNode: (ctx: {
4
+ getRootNode?: () => Node | Document | ShadowRoot;
5
+ }) => Document | ShadowRoot;
6
+ getDoc: (ctx: {
7
+ getRootNode?: () => Node | Document | ShadowRoot;
8
+ }) => Document;
9
+ getWin: (ctx: {
10
+ getRootNode?: () => Node | Document | ShadowRoot;
11
+ }) => Window & typeof globalThis;
12
+ } & {
5
13
  getActiveEl: (ctx: Ctx) => Element;
6
- getRootNode: (ctx: Ctx) => Document | ShadowRoot;
7
14
  getAnchorId: (ctx: Ctx) => string;
8
15
  getTriggerId: (ctx: Ctx) => string;
9
16
  getContentId: (ctx: Ctx) => string;
@@ -1,2 +1,2 @@
1
1
  import type { MachineContext, MachineState, UserDefinedContext } from "./popover.types";
2
- export declare function machine(ctx?: UserDefinedContext): import("@zag-js/core").Machine<MachineContext, MachineState, import("@zag-js/core").StateMachine.AnyEventObject>;
2
+ export declare function machine(ctx: UserDefinedContext): import("@zag-js/core").Machine<MachineContext, MachineState, import("@zag-js/core").StateMachine.AnyEventObject>;
@@ -1,7 +1,7 @@
1
1
  import type { StateMachine as S } from "@zag-js/core";
2
2
  import type { DismissableElementHandlers } from "@zag-js/dimissable";
3
3
  import type { PositioningOptions, Placement } from "@zag-js/popper";
4
- import type { Context, MaybeElement } from "@zag-js/types";
4
+ import type { CommonProperties, Context, MaybeElement, RequiredBy } from "@zag-js/types";
5
5
  declare type ElementIds = Partial<{
6
6
  anchor: string;
7
7
  trigger: string;
@@ -10,7 +10,7 @@ declare type ElementIds = Partial<{
10
10
  description: string;
11
11
  closeBtn: string;
12
12
  }>;
13
- declare type PublicContext = DismissableElementHandlers & {
13
+ declare type PublicContext = DismissableElementHandlers & CommonProperties & {
14
14
  /**
15
15
  * The ids of the elements in the popover. Useful for composition.
16
16
  */
@@ -59,7 +59,7 @@ declare type PublicContext = DismissableElementHandlers & {
59
59
  */
60
60
  defaultOpen?: boolean;
61
61
  };
62
- export declare type UserDefinedContext = Partial<PublicContext>;
62
+ export declare type UserDefinedContext = RequiredBy<PublicContext, "id">;
63
63
  declare type ComputedContext = Readonly<{
64
64
  /**
65
65
  * @computed
@@ -90,7 +90,7 @@ declare type PrivateContext = Context<{
90
90
  /**
91
91
  * Whether to prevent returning focus to the trigger
92
92
  */
93
- preventReturnFocus?: boolean;
93
+ focusTriggerOnClose?: boolean;
94
94
  }>;
95
95
  export declare type MachineContext = PublicContext & ComputedContext & PrivateContext;
96
96
  export declare type MachineState = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/popover",
3
- "version": "0.0.0-dev-20220706194607",
3
+ "version": "0.0.0-dev-20220709094240",
4
4
  "description": "Core logic for the popover widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -30,11 +30,11 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "@zag-js/aria-hidden": "0.1.0",
33
- "@zag-js/core": "0.0.0-dev-20220706194607",
34
- "@zag-js/dom-utils": "0.0.0-dev-20220706194607",
35
- "@zag-js/popper": "0.0.0-dev-20220706194607",
36
- "@zag-js/remove-scroll": "0.0.0-dev-20220706194607",
37
- "@zag-js/types": "0.0.0-dev-20220706194607",
33
+ "@zag-js/core": "0.1.7",
34
+ "@zag-js/dom-utils": "0.0.0-dev-20220709094240",
35
+ "@zag-js/popper": "0.0.0-dev-20220709094240",
36
+ "@zag-js/remove-scroll": "0.0.0-dev-20220709094240",
37
+ "@zag-js/types": "0.0.0-dev-20220709094240",
38
38
  "@zag-js/utils": "0.1.2",
39
39
  "focus-trap": "6.9.4"
40
40
  },