@zag-js/popover 0.0.0-dev-20220704163406 → 0.0.0-dev-20220708193601

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,6 +67,9 @@ 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
+ }
54
73
  function isWindow(value) {
55
74
  return (value == null ? void 0 : value.toString()) === "[object Window]";
56
75
  }
@@ -61,12 +80,28 @@ function getDocument(el) {
61
80
  var _a;
62
81
  if (isWindow(el))
63
82
  return el.document;
83
+ if (isDocument(el))
84
+ return el;
64
85
  return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
65
86
  }
66
87
  function getEventTarget(event) {
67
88
  var _a, _b;
68
89
  return (_b = (_a = event.composedPath) == null ? void 0 : _a.call(event)[0]) != null ? _b : event.target;
69
90
  }
91
+ function withRootHelpers(domUtils) {
92
+ const roots = {
93
+ getRootNode: (ctx) => {
94
+ var _a, _b;
95
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
96
+ },
97
+ getDoc: (ctx) => getDocument(roots.getRootNode(ctx)),
98
+ getWin: (ctx) => {
99
+ var _a;
100
+ return (_a = roots.getDoc(ctx).defaultView) != null ? _a : window;
101
+ }
102
+ };
103
+ return __spreadValues2(__spreadValues2({}, roots), domUtils);
104
+ }
70
105
  function contains(parent, child) {
71
106
  if (!parent)
72
107
  return false;
@@ -193,45 +228,33 @@ var runIfFn2 = (v, ...a) => {
193
228
  };
194
229
 
195
230
  // src/popover.dom.ts
196
- var dom = {
197
- getDoc: (ctx) => {
198
- var _a;
199
- return (_a = ctx.doc) != null ? _a : document;
200
- },
201
- getWin: (ctx) => {
202
- var _a;
203
- return (_a = dom.getDoc(ctx).defaultView) != null ? _a : window;
204
- },
231
+ var dom = withRootHelpers({
205
232
  getActiveEl: (ctx) => dom.getDoc(ctx).activeElement,
206
- getRootNode: (ctx) => {
207
- var _a;
208
- return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
209
- },
210
233
  getAnchorId: (ctx) => {
211
234
  var _a, _b;
212
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.anchor) != null ? _b : `popover:${ctx.uid}:anchor`;
235
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.anchor) != null ? _b : `popover:${ctx.id}:anchor`;
213
236
  },
214
237
  getTriggerId: (ctx) => {
215
238
  var _a, _b;
216
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `popover:${ctx.uid}:trigger`;
239
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `popover:${ctx.id}:trigger`;
217
240
  },
218
241
  getContentId: (ctx) => {
219
242
  var _a, _b;
220
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `popover:${ctx.uid}:content`;
243
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `popover:${ctx.id}:content`;
221
244
  },
222
- getPositionerId: (ctx) => `popover:${ctx.uid}:popper`,
223
- getArrowId: (ctx) => `popover:${ctx.uid}:arrow`,
245
+ getPositionerId: (ctx) => `popover:${ctx.id}:popper`,
246
+ getArrowId: (ctx) => `popover:${ctx.id}:arrow`,
224
247
  getTitleId: (ctx) => {
225
248
  var _a, _b;
226
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.title) != null ? _b : `popover:${ctx.uid}:title`;
249
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.title) != null ? _b : `popover:${ctx.id}:title`;
227
250
  },
228
251
  getDescriptionId: (ctx) => {
229
252
  var _a, _b;
230
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.description) != null ? _b : `popover:${ctx.uid}:desc`;
253
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.description) != null ? _b : `popover:${ctx.id}:desc`;
231
254
  },
232
255
  getCloseButtonId: (ctx) => {
233
256
  var _a, _b;
234
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.closeBtn) != null ? _b : `popover:${ctx.uid}:close-button`;
257
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.closeBtn) != null ? _b : `popover:${ctx.id}:close-button`;
235
258
  },
236
259
  getAnchorEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getAnchorId(ctx)),
237
260
  getTriggerEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getTriggerId(ctx)),
@@ -253,7 +276,7 @@ var dom = {
253
276
  el = dom.getContentEl(ctx);
254
277
  return el;
255
278
  }
256
- };
279
+ });
257
280
 
258
281
  // src/popover.connect.ts
259
282
  function connect(state, send, normalize) {
@@ -358,6 +381,9 @@ function getPlatform() {
358
381
  var pt = (v) => isDom() && v.test(getPlatform());
359
382
  var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
360
383
  var isMac = () => pt(/^Mac/) && !isTouchDevice;
384
+ function isDocument2(el) {
385
+ return el.nodeType === Node.DOCUMENT_NODE;
386
+ }
361
387
  function isWindow2(value) {
362
388
  return (value == null ? void 0 : value.toString()) === "[object Window]";
363
389
  }
@@ -365,6 +391,8 @@ function getDocument2(el) {
365
391
  var _a;
366
392
  if (isWindow2(el))
367
393
  return el.document;
394
+ if (isDocument2(el))
395
+ return el;
368
396
  return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
369
397
  }
370
398
  function getWindow(el) {
@@ -395,7 +423,8 @@ var isCtrlKey = (v) => isMac() ? v.metaKey && !v.ctrlKey : v.ctrlKey && !v.metaK
395
423
  function fireCustomEvent(el, type, init) {
396
424
  if (!el)
397
425
  return;
398
- const event = new CustomEvent(type, init);
426
+ const win = getWindow(el);
427
+ const event = new win.CustomEvent(type, init);
399
428
  return el.dispatchEvent(event);
400
429
  }
401
430
  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";
@@ -523,14 +552,14 @@ var layerStack = {
523
552
  const layer = this.layers[this.count() - 1];
524
553
  return (layer == null ? void 0 : layer.node) === node;
525
554
  },
526
- getChildLayers(node) {
527
- return this.layers.slice(this.indexOf(node));
555
+ getNestedLayers(node) {
556
+ return Array.from(this.layers).slice(this.indexOf(node) + 1);
528
557
  },
529
- isInChildLayer(node, target) {
530
- return this.getChildLayers(node).some((layer) => contains(layer.node, target));
558
+ isInNestedLayer(node, target) {
559
+ return this.getNestedLayers(node).some((layer) => contains(layer.node, target));
531
560
  },
532
561
  isInBranch(target) {
533
- return [...this.branches].some((branch) => contains(branch, target));
562
+ return Array.from(this.branches).some((branch) => contains(branch, target));
534
563
  },
535
564
  add(layer) {
536
565
  this.layers.push(layer);
@@ -540,10 +569,13 @@ var layerStack = {
540
569
  },
541
570
  remove(node) {
542
571
  const index = this.indexOf(node);
543
- if (index + 1 < this.count() - 1) {
544
- this.layers.splice(index, this.count()).forEach((item) => item.dismiss());
572
+ if (index < 0)
573
+ return;
574
+ if (index < this.count() - 1) {
575
+ const _layers = this.getNestedLayers(node);
576
+ _layers.forEach((layer) => layer.dismiss());
545
577
  }
546
- this.layers = this.layers.filter((item) => item.node !== node);
578
+ this.layers.splice(index, 1);
547
579
  },
548
580
  removeBranch(node) {
549
581
  const index = this.branches.indexOf(node);
@@ -594,7 +626,7 @@ function disablePointerEventsOutside(node) {
594
626
  function trackDismissableElement(node, options) {
595
627
  if (!node)
596
628
  return;
597
- const { onDismiss, pointerBlocking, exclude: excludeContainers } = options;
629
+ const { onDismiss, pointerBlocking, exclude: excludeContainers, debug } = options;
598
630
  const layer = { dismiss: onDismiss, node, pointerBlocking };
599
631
  layerStack.add(layer);
600
632
  assignPointerEventToLayers();
@@ -607,6 +639,9 @@ function trackDismissableElement(node, options) {
607
639
  (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
608
640
  if (event.defaultPrevented)
609
641
  return;
642
+ if (debug) {
643
+ console.log("onPointerDownOutside:", event.detail.originalEvent);
644
+ }
610
645
  onDismiss == null ? void 0 : onDismiss();
611
646
  }
612
647
  function onFocusOutside(event) {
@@ -618,6 +653,9 @@ function trackDismissableElement(node, options) {
618
653
  (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
619
654
  if (event.defaultPrevented)
620
655
  return;
656
+ if (debug) {
657
+ console.log("onFocusOutside:", event.detail.originalEvent);
658
+ }
621
659
  onDismiss == null ? void 0 : onDismiss();
622
660
  }
623
661
  function onEscapeKeyDown(event) {
@@ -631,9 +669,11 @@ function trackDismissableElement(node, options) {
631
669
  }
632
670
  }
633
671
  function exclude(target) {
672
+ if (!node)
673
+ return false;
634
674
  const containers = typeof excludeContainers === "function" ? excludeContainers() : excludeContainers;
635
- const inContainers = Array.isArray(containers) ? containers : [containers];
636
- return inContainers.some((node2) => contains(node2, target)) || layerStack.isInChildLayer(node, target);
675
+ const _containers = Array.isArray(containers) ? containers : [containers];
676
+ return _containers.some((node2) => contains(node2, target)) || layerStack.isInNestedLayer(node, target);
637
677
  }
638
678
  const cleanups = [
639
679
  pointerBlocking ? disablePointerEventsOutside(node) : void 0,
@@ -653,12 +693,11 @@ var import_popper2 = require("@zag-js/popper");
653
693
  var import_remove_scroll = require("@zag-js/remove-scroll");
654
694
  var import_focus_trap = require("focus-trap");
655
695
  var { and, or, not } = import_core.guards;
656
- function machine(ctx = {}) {
696
+ function machine(ctx) {
657
697
  return (0, import_core.createMachine)({
658
698
  id: "popover",
659
699
  initial: "unknown",
660
700
  context: __spreadProps(__spreadValues({
661
- uid: "",
662
701
  closeOnInteractOutside: true,
663
702
  closeOnEsc: true,
664
703
  autoFocus: true,
@@ -668,7 +707,7 @@ function machine(ctx = {}) {
668
707
  }, ctx.positioning),
669
708
  currentPlacement: void 0
670
709
  }, ctx), {
671
- preventReturnFocus: false,
710
+ focusTriggerOnClose: true,
672
711
  renderedElements: {
673
712
  title: true,
674
713
  description: true,
@@ -683,7 +722,7 @@ function machine(ctx = {}) {
683
722
  on: {
684
723
  SETUP: {
685
724
  target: ctx.defaultOpen ? "open" : "closed",
686
- actions: ["setupDocument", "checkRenderedElements"]
725
+ actions: "checkRenderedElements"
687
726
  }
688
727
  }
689
728
  },
@@ -758,7 +797,7 @@ function machine(ctx = {}) {
758
797
  (_a = ctx2.onEscapeKeyDown) == null ? void 0 : _a.call(ctx2, event);
759
798
  if (ctx2.closeOnEsc)
760
799
  return;
761
- ctx2.preventReturnFocus = false;
800
+ ctx2.focusTriggerOnClose = true;
762
801
  event.preventDefault();
763
802
  },
764
803
  onInteractOutside(event) {
@@ -766,7 +805,7 @@ function machine(ctx = {}) {
766
805
  (_a = ctx2.onInteractOutside) == null ? void 0 : _a.call(ctx2, event);
767
806
  if (event.defaultPrevented)
768
807
  return;
769
- ctx2.preventReturnFocus = event.detail.focusable || event.detail.contextmenu;
808
+ ctx2.focusTriggerOnClose = !(event.detail.focusable || event.detail.contextmenu);
770
809
  if (!ctx2.closeOnInteractOutside) {
771
810
  event.preventDefault();
772
811
  }
@@ -857,13 +896,6 @@ function machine(ctx = {}) {
857
896
  });
858
897
  });
859
898
  },
860
- setupDocument(ctx2, evt) {
861
- if (evt.doc)
862
- ctx2.doc = (0, import_core.ref)(evt.doc);
863
- if (evt.root)
864
- ctx2.rootNode = (0, import_core.ref)(evt.root);
865
- ctx2.uid = evt.id;
866
- },
867
899
  clearPointerDown(ctx2) {
868
900
  ctx2.pointerdownNode = null;
869
901
  },
@@ -874,15 +906,11 @@ function machine(ctx = {}) {
874
906
  });
875
907
  },
876
908
  focusTriggerIfNeeded(ctx2) {
877
- const focus = () => {
909
+ if (!ctx2.focusTriggerOnClose)
910
+ return;
911
+ raf(() => {
878
912
  var _a;
879
913
  return (_a = dom.getTriggerEl(ctx2)) == null ? void 0 : _a.focus();
880
- };
881
- raf(() => {
882
- if (!ctx2.preventReturnFocus) {
883
- focus();
884
- ctx2.preventReturnFocus = false;
885
- }
886
914
  });
887
915
  },
888
916
  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,6 +43,9 @@ 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
+ }
30
49
  function isWindow(value) {
31
50
  return (value == null ? void 0 : value.toString()) === "[object Window]";
32
51
  }
@@ -37,12 +56,28 @@ function getDocument(el) {
37
56
  var _a;
38
57
  if (isWindow(el))
39
58
  return el.document;
59
+ if (isDocument(el))
60
+ return el;
40
61
  return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
41
62
  }
42
63
  function getEventTarget(event) {
43
64
  var _a, _b;
44
65
  return (_b = (_a = event.composedPath) == null ? void 0 : _a.call(event)[0]) != null ? _b : event.target;
45
66
  }
67
+ function withRootHelpers(domUtils) {
68
+ const roots = {
69
+ getRootNode: (ctx) => {
70
+ var _a, _b;
71
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
72
+ },
73
+ getDoc: (ctx) => getDocument(roots.getRootNode(ctx)),
74
+ getWin: (ctx) => {
75
+ var _a;
76
+ return (_a = roots.getDoc(ctx).defaultView) != null ? _a : window;
77
+ }
78
+ };
79
+ return __spreadValues2(__spreadValues2({}, roots), domUtils);
80
+ }
46
81
  function contains(parent, child) {
47
82
  if (!parent)
48
83
  return false;
@@ -169,45 +204,33 @@ var runIfFn2 = (v, ...a) => {
169
204
  };
170
205
 
171
206
  // src/popover.dom.ts
172
- var dom = {
173
- getDoc: (ctx) => {
174
- var _a;
175
- return (_a = ctx.doc) != null ? _a : document;
176
- },
177
- getWin: (ctx) => {
178
- var _a;
179
- return (_a = dom.getDoc(ctx).defaultView) != null ? _a : window;
180
- },
207
+ var dom = withRootHelpers({
181
208
  getActiveEl: (ctx) => dom.getDoc(ctx).activeElement,
182
- getRootNode: (ctx) => {
183
- var _a;
184
- return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
185
- },
186
209
  getAnchorId: (ctx) => {
187
210
  var _a, _b;
188
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.anchor) != null ? _b : `popover:${ctx.uid}:anchor`;
211
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.anchor) != null ? _b : `popover:${ctx.id}:anchor`;
189
212
  },
190
213
  getTriggerId: (ctx) => {
191
214
  var _a, _b;
192
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `popover:${ctx.uid}:trigger`;
215
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.trigger) != null ? _b : `popover:${ctx.id}:trigger`;
193
216
  },
194
217
  getContentId: (ctx) => {
195
218
  var _a, _b;
196
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `popover:${ctx.uid}:content`;
219
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.content) != null ? _b : `popover:${ctx.id}:content`;
197
220
  },
198
- getPositionerId: (ctx) => `popover:${ctx.uid}:popper`,
199
- getArrowId: (ctx) => `popover:${ctx.uid}:arrow`,
221
+ getPositionerId: (ctx) => `popover:${ctx.id}:popper`,
222
+ getArrowId: (ctx) => `popover:${ctx.id}:arrow`,
200
223
  getTitleId: (ctx) => {
201
224
  var _a, _b;
202
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.title) != null ? _b : `popover:${ctx.uid}:title`;
225
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.title) != null ? _b : `popover:${ctx.id}:title`;
203
226
  },
204
227
  getDescriptionId: (ctx) => {
205
228
  var _a, _b;
206
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.description) != null ? _b : `popover:${ctx.uid}:desc`;
229
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.description) != null ? _b : `popover:${ctx.id}:desc`;
207
230
  },
208
231
  getCloseButtonId: (ctx) => {
209
232
  var _a, _b;
210
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.closeBtn) != null ? _b : `popover:${ctx.uid}:close-button`;
233
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.closeBtn) != null ? _b : `popover:${ctx.id}:close-button`;
211
234
  },
212
235
  getAnchorEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getAnchorId(ctx)),
213
236
  getTriggerEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getTriggerId(ctx)),
@@ -229,7 +252,7 @@ var dom = {
229
252
  el = dom.getContentEl(ctx);
230
253
  return el;
231
254
  }
232
- };
255
+ });
233
256
 
234
257
  // src/popover.connect.ts
235
258
  function connect(state, send, normalize) {
@@ -317,7 +340,7 @@ function connect(state, send, normalize) {
317
340
 
318
341
  // src/popover.machine.ts
319
342
  import { ariaHidden } from "@zag-js/aria-hidden";
320
- import { createMachine, guards, ref } from "@zag-js/core";
343
+ import { createMachine, guards } from "@zag-js/core";
321
344
 
322
345
  // ../../utilities/interact-outside/dist/index.mjs
323
346
  var runIfFn3 = (v, ...a) => {
@@ -334,6 +357,9 @@ function getPlatform() {
334
357
  var pt = (v) => isDom() && v.test(getPlatform());
335
358
  var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
336
359
  var isMac = () => pt(/^Mac/) && !isTouchDevice;
360
+ function isDocument2(el) {
361
+ return el.nodeType === Node.DOCUMENT_NODE;
362
+ }
337
363
  function isWindow2(value) {
338
364
  return (value == null ? void 0 : value.toString()) === "[object Window]";
339
365
  }
@@ -341,6 +367,8 @@ function getDocument2(el) {
341
367
  var _a;
342
368
  if (isWindow2(el))
343
369
  return el.document;
370
+ if (isDocument2(el))
371
+ return el;
344
372
  return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
345
373
  }
346
374
  function getWindow(el) {
@@ -371,7 +399,8 @@ var isCtrlKey = (v) => isMac() ? v.metaKey && !v.ctrlKey : v.ctrlKey && !v.metaK
371
399
  function fireCustomEvent(el, type, init) {
372
400
  if (!el)
373
401
  return;
374
- const event = new CustomEvent(type, init);
402
+ const win = getWindow(el);
403
+ const event = new win.CustomEvent(type, init);
375
404
  return el.dispatchEvent(event);
376
405
  }
377
406
  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";
@@ -499,14 +528,14 @@ var layerStack = {
499
528
  const layer = this.layers[this.count() - 1];
500
529
  return (layer == null ? void 0 : layer.node) === node;
501
530
  },
502
- getChildLayers(node) {
503
- return this.layers.slice(this.indexOf(node));
531
+ getNestedLayers(node) {
532
+ return Array.from(this.layers).slice(this.indexOf(node) + 1);
504
533
  },
505
- isInChildLayer(node, target) {
506
- return this.getChildLayers(node).some((layer) => contains(layer.node, target));
534
+ isInNestedLayer(node, target) {
535
+ return this.getNestedLayers(node).some((layer) => contains(layer.node, target));
507
536
  },
508
537
  isInBranch(target) {
509
- return [...this.branches].some((branch) => contains(branch, target));
538
+ return Array.from(this.branches).some((branch) => contains(branch, target));
510
539
  },
511
540
  add(layer) {
512
541
  this.layers.push(layer);
@@ -516,10 +545,13 @@ var layerStack = {
516
545
  },
517
546
  remove(node) {
518
547
  const index = this.indexOf(node);
519
- if (index + 1 < this.count() - 1) {
520
- this.layers.splice(index, this.count()).forEach((item) => item.dismiss());
548
+ if (index < 0)
549
+ return;
550
+ if (index < this.count() - 1) {
551
+ const _layers = this.getNestedLayers(node);
552
+ _layers.forEach((layer) => layer.dismiss());
521
553
  }
522
- this.layers = this.layers.filter((item) => item.node !== node);
554
+ this.layers.splice(index, 1);
523
555
  },
524
556
  removeBranch(node) {
525
557
  const index = this.branches.indexOf(node);
@@ -570,7 +602,7 @@ function disablePointerEventsOutside(node) {
570
602
  function trackDismissableElement(node, options) {
571
603
  if (!node)
572
604
  return;
573
- const { onDismiss, pointerBlocking, exclude: excludeContainers } = options;
605
+ const { onDismiss, pointerBlocking, exclude: excludeContainers, debug } = options;
574
606
  const layer = { dismiss: onDismiss, node, pointerBlocking };
575
607
  layerStack.add(layer);
576
608
  assignPointerEventToLayers();
@@ -583,6 +615,9 @@ function trackDismissableElement(node, options) {
583
615
  (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
584
616
  if (event.defaultPrevented)
585
617
  return;
618
+ if (debug) {
619
+ console.log("onPointerDownOutside:", event.detail.originalEvent);
620
+ }
586
621
  onDismiss == null ? void 0 : onDismiss();
587
622
  }
588
623
  function onFocusOutside(event) {
@@ -594,6 +629,9 @@ function trackDismissableElement(node, options) {
594
629
  (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
595
630
  if (event.defaultPrevented)
596
631
  return;
632
+ if (debug) {
633
+ console.log("onFocusOutside:", event.detail.originalEvent);
634
+ }
597
635
  onDismiss == null ? void 0 : onDismiss();
598
636
  }
599
637
  function onEscapeKeyDown(event) {
@@ -607,9 +645,11 @@ function trackDismissableElement(node, options) {
607
645
  }
608
646
  }
609
647
  function exclude(target) {
648
+ if (!node)
649
+ return false;
610
650
  const containers = typeof excludeContainers === "function" ? excludeContainers() : excludeContainers;
611
- const inContainers = Array.isArray(containers) ? containers : [containers];
612
- return inContainers.some((node2) => contains(node2, target)) || layerStack.isInChildLayer(node, target);
651
+ const _containers = Array.isArray(containers) ? containers : [containers];
652
+ return _containers.some((node2) => contains(node2, target)) || layerStack.isInNestedLayer(node, target);
613
653
  }
614
654
  const cleanups = [
615
655
  pointerBlocking ? disablePointerEventsOutside(node) : void 0,
@@ -629,12 +669,11 @@ import { getPlacement } from "@zag-js/popper";
629
669
  import { preventBodyScroll } from "@zag-js/remove-scroll";
630
670
  import { createFocusTrap } from "focus-trap";
631
671
  var { and, or, not } = guards;
632
- function machine(ctx = {}) {
672
+ function machine(ctx) {
633
673
  return createMachine({
634
674
  id: "popover",
635
675
  initial: "unknown",
636
676
  context: __spreadProps(__spreadValues({
637
- uid: "",
638
677
  closeOnInteractOutside: true,
639
678
  closeOnEsc: true,
640
679
  autoFocus: true,
@@ -644,7 +683,7 @@ function machine(ctx = {}) {
644
683
  }, ctx.positioning),
645
684
  currentPlacement: void 0
646
685
  }, ctx), {
647
- preventReturnFocus: false,
686
+ focusTriggerOnClose: true,
648
687
  renderedElements: {
649
688
  title: true,
650
689
  description: true,
@@ -659,7 +698,7 @@ function machine(ctx = {}) {
659
698
  on: {
660
699
  SETUP: {
661
700
  target: ctx.defaultOpen ? "open" : "closed",
662
- actions: ["setupDocument", "checkRenderedElements"]
701
+ actions: "checkRenderedElements"
663
702
  }
664
703
  }
665
704
  },
@@ -734,7 +773,7 @@ function machine(ctx = {}) {
734
773
  (_a = ctx2.onEscapeKeyDown) == null ? void 0 : _a.call(ctx2, event);
735
774
  if (ctx2.closeOnEsc)
736
775
  return;
737
- ctx2.preventReturnFocus = false;
776
+ ctx2.focusTriggerOnClose = true;
738
777
  event.preventDefault();
739
778
  },
740
779
  onInteractOutside(event) {
@@ -742,7 +781,7 @@ function machine(ctx = {}) {
742
781
  (_a = ctx2.onInteractOutside) == null ? void 0 : _a.call(ctx2, event);
743
782
  if (event.defaultPrevented)
744
783
  return;
745
- ctx2.preventReturnFocus = event.detail.focusable || event.detail.contextmenu;
784
+ ctx2.focusTriggerOnClose = !(event.detail.focusable || event.detail.contextmenu);
746
785
  if (!ctx2.closeOnInteractOutside) {
747
786
  event.preventDefault();
748
787
  }
@@ -833,13 +872,6 @@ function machine(ctx = {}) {
833
872
  });
834
873
  });
835
874
  },
836
- setupDocument(ctx2, evt) {
837
- if (evt.doc)
838
- ctx2.doc = ref(evt.doc);
839
- if (evt.root)
840
- ctx2.rootNode = ref(evt.root);
841
- ctx2.uid = evt.id;
842
- },
843
875
  clearPointerDown(ctx2) {
844
876
  ctx2.pointerdownNode = null;
845
877
  },
@@ -850,15 +882,11 @@ function machine(ctx = {}) {
850
882
  });
851
883
  },
852
884
  focusTriggerIfNeeded(ctx2) {
853
- const focus = () => {
885
+ if (!ctx2.focusTriggerOnClose)
886
+ return;
887
+ raf(() => {
854
888
  var _a;
855
889
  return (_a = dom.getTriggerEl(ctx2)) == null ? void 0 : _a.focus();
856
- };
857
- raf(() => {
858
- if (!ctx2.preventReturnFocus) {
859
- focus();
860
- ctx2.preventReturnFocus = false;
861
- }
862
890
  });
863
891
  },
864
892
  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?: () => Document | Node | ShadowRoot;
5
+ }) => Document | ShadowRoot;
6
+ getDoc: (ctx: {
7
+ getRootNode?: () => Document | Node | ShadowRoot;
8
+ }) => Document;
9
+ getWin: (ctx: {
10
+ getRootNode?: () => Document | Node | 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-20220704163406",
3
+ "version": "0.0.0-dev-20220708193601",
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-20220704163406",
34
- "@zag-js/dom-utils": "0.0.0-dev-20220704163406",
35
- "@zag-js/popper": "0.0.0-dev-20220704163406",
36
- "@zag-js/remove-scroll": "0.0.0-dev-20220704163406",
37
- "@zag-js/types": "0.0.0-dev-20220704163406",
33
+ "@zag-js/core": "0.1.7",
34
+ "@zag-js/dom-utils": "0.0.0-dev-20220708193601",
35
+ "@zag-js/popper": "0.0.0-dev-20220708193601",
36
+ "@zag-js/remove-scroll": "0.0.0-dev-20220708193601",
37
+ "@zag-js/types": "0.0.0-dev-20220708193601",
38
38
  "@zag-js/utils": "0.1.2",
39
39
  "focus-trap": "6.9.4"
40
40
  },