@zag-js/popover 0.0.0-dev-20220526121406 → 0.0.0-dev-20220604091726

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
@@ -42,36 +42,36 @@ __export(src_exports, {
42
42
  module.exports = __toCommonJS(src_exports);
43
43
 
44
44
  // ../../../node_modules/compute-scroll-into-view/dist/index.module.js
45
- function t(t3) {
46
- return typeof t3 == "object" && t3 != null && t3.nodeType === 1;
45
+ function t(t2) {
46
+ return typeof t2 == "object" && t2 != null && t2.nodeType === 1;
47
47
  }
48
- function e(t3, e2) {
49
- return (!e2 || t3 !== "hidden") && t3 !== "visible" && t3 !== "clip";
48
+ function e(t2, e2) {
49
+ return (!e2 || t2 !== "hidden") && t2 !== "visible" && t2 !== "clip";
50
50
  }
51
- function n(t3, n2) {
52
- if (t3.clientHeight < t3.scrollHeight || t3.clientWidth < t3.scrollWidth) {
53
- var r2 = getComputedStyle(t3, null);
54
- return e(r2.overflowY, n2) || e(r2.overflowX, n2) || function(t4) {
55
- var e2 = function(t5) {
56
- if (!t5.ownerDocument || !t5.ownerDocument.defaultView)
51
+ function n(t2, n2) {
52
+ if (t2.clientHeight < t2.scrollHeight || t2.clientWidth < t2.scrollWidth) {
53
+ var r2 = getComputedStyle(t2, null);
54
+ return e(r2.overflowY, n2) || e(r2.overflowX, n2) || function(t3) {
55
+ var e2 = function(t4) {
56
+ if (!t4.ownerDocument || !t4.ownerDocument.defaultView)
57
57
  return null;
58
58
  try {
59
- return t5.ownerDocument.defaultView.frameElement;
60
- } catch (t6) {
59
+ return t4.ownerDocument.defaultView.frameElement;
60
+ } catch (t5) {
61
61
  return null;
62
62
  }
63
- }(t4);
64
- return !!e2 && (e2.clientHeight < t4.scrollHeight || e2.clientWidth < t4.scrollWidth);
65
- }(t3);
63
+ }(t3);
64
+ return !!e2 && (e2.clientHeight < t3.scrollHeight || e2.clientWidth < t3.scrollWidth);
65
+ }(t2);
66
66
  }
67
67
  return false;
68
68
  }
69
- function r(t3, e2, n2, r2, i, o, l, d) {
70
- return o < t3 && l > e2 || o > t3 && l < e2 ? 0 : o <= t3 && d <= n2 || l >= e2 && d >= n2 ? o - t3 - r2 : l > e2 && d < n2 || o < t3 && d > n2 ? l - e2 + i : 0;
69
+ function r(t2, e2, n2, r2, i, o, l, d) {
70
+ return o < t2 && l > e2 || o > t2 && l < e2 ? 0 : o <= t2 && d <= n2 || l >= e2 && d >= n2 ? o - t2 - r2 : l > e2 && d < n2 || o < t2 && d > n2 ? l - e2 + i : 0;
71
71
  }
72
72
  function index_module_default(e2, i) {
73
- var o = window, l = i.scrollMode, d = i.block, u = i.inline, h = i.boundary, a = i.skipOverflowHiddenElements, c = typeof h == "function" ? h : function(t3) {
74
- return t3 !== h;
73
+ var o = window, l = i.scrollMode, d = i.block, u = i.inline, h = i.boundary, a = i.skipOverflowHiddenElements, c = typeof h == "function" ? h : function(t2) {
74
+ return t2 !== h;
75
75
  };
76
76
  if (!t(e2))
77
77
  throw new TypeError("Invalid target");
@@ -191,15 +191,20 @@ function raf(fn) {
191
191
  globalThis.cancelAnimationFrame(id);
192
192
  };
193
193
  }
194
+ var runIfFn = (v, ...a) => {
195
+ const res = typeof v === "function" ? v(...a) : v;
196
+ return res != null ? res : void 0;
197
+ };
194
198
  var noop = () => {
195
199
  };
196
200
  var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
197
201
  var platform = (v) => isDom() && v.test(navigator.platform);
198
- var isDom = () => !!(typeof window !== "undefined");
202
+ var isDom = () => typeof window !== "undefined";
199
203
  var isMac = () => platform(/^Mac/);
200
204
  var isIPhone = () => platform(/^iPhone/);
201
205
  var isIPad = () => platform(/^iPad/) || isMac() && navigator.maxTouchPoints > 1;
202
206
  var isIos = () => isIPhone() || isIPad();
207
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
203
208
  var isLeftClick = (v) => v.button === 0;
204
209
  function getListenerElements() {
205
210
  ;
@@ -211,7 +216,7 @@ function getListenerCache() {
211
216
  globalThis.__listenerCache__ = globalThis.__listenerCache__ || /* @__PURE__ */ new Map();
212
217
  return globalThis.__listenerCache__;
213
218
  }
214
- function globalEventBus(node, type, handler, options) {
219
+ function addGlobalEventListener(node, type, handler, options) {
215
220
  var _a;
216
221
  if (!node)
217
222
  return noop;
@@ -263,12 +268,10 @@ function globalEventBus(node, type, handler, options) {
263
268
  }
264
269
  };
265
270
  }
266
- var t2 = (v) => Object.prototype.toString.call(v).slice(8, -1);
267
- var isRef = (v) => t2(v) === "Object" && "current" in v;
268
- var runIfFn = (fn) => t2(fn) === "Function" ? fn() : fn;
271
+ var isRef = (v) => hasProp(v, "current");
269
272
  function addDomEvent(target, event, listener, options) {
270
273
  const node = isRef(target) ? target.current : runIfFn(target);
271
- return globalEventBus(node, event, listener, options);
274
+ return addGlobalEventListener(node, event, listener, options);
272
275
  }
273
276
  var changeCount = 0;
274
277
  var originalBodyPointerEvents;
@@ -509,7 +512,8 @@ var getFocusables = (el, includeContainer = false) => {
509
512
  if (!el)
510
513
  return [];
511
514
  let els = Array.from(el.querySelectorAll(focusableSelector));
512
- if (includeContainer && isHTMLElement(el)) {
515
+ const shouldAddContainer = includeContainer == true || includeContainer == "if-empty" && els.length === 0;
516
+ if (shouldAddContainer && isHTMLElement(el)) {
513
517
  els.unshift(el);
514
518
  }
515
519
  return els.filter((el2) => isFocusable(el2) && !isHidden(el2));
@@ -520,6 +524,8 @@ var isFocusable = (el) => {
520
524
  return el == null ? void 0 : el.matches(focusableSelector);
521
525
  };
522
526
  var getTabbables = (el, includeContainer = false) => {
527
+ if (!el)
528
+ return [];
523
529
  return getFocusables(el, includeContainer).filter(isTabbable);
524
530
  };
525
531
  var isTabbable = (el) => {
@@ -672,15 +678,16 @@ var dom = {
672
678
  getFocusableEls: (ctx) => getFocusables(dom.getContentEl(ctx)),
673
679
  getFirstFocusableEl: (ctx) => dom.getFocusableEls(ctx)[0],
674
680
  getDocTabbableEls: (ctx) => getTabbables(cast(dom.getDoc(ctx))),
675
- getTabbableEls: (ctx) => {
676
- const el = dom.getContentEl(ctx);
677
- return el ? getTabbables(el) : [];
678
- },
681
+ getTabbableEls: (ctx) => getTabbables(dom.getContentEl(ctx), "if-empty"),
679
682
  getFirstTabbableEl: (ctx) => first(dom.getTabbableEls(ctx)),
680
683
  getLastTabbableEl: (ctx) => last(dom.getTabbableEls(ctx)),
681
684
  getInitialFocusEl: (ctx) => {
682
- var _a;
683
- return (_a = runIfFn2(ctx.initialFocusEl)) != null ? _a : dom.getFirstFocusableEl(ctx);
685
+ let el = runIfFn2(ctx.initialFocusEl);
686
+ if (!el && ctx.autoFocus)
687
+ el = dom.getFirstFocusableEl(ctx);
688
+ if (!el)
689
+ el = dom.getContentEl(ctx);
690
+ return el;
684
691
  }
685
692
  };
686
693
 
@@ -688,9 +695,10 @@ var dom = {
688
695
  function connect(state, send, normalize = normalizeProp) {
689
696
  const isOpen = state.matches("open");
690
697
  const pointerdownNode = state.context.pointerdownNode;
698
+ const currentPlacement = state.context.currentPlacement;
691
699
  const popperStyles = (0, import_popper.getPlacementStyles)({
692
700
  measured: !!state.context.isPlacementComplete,
693
- placement: state.context.currentPlacement
701
+ placement: currentPlacement
694
702
  });
695
703
  return {
696
704
  portalled: state.context.currentPortalled,
@@ -717,7 +725,7 @@ function connect(state, send, normalize = normalizeProp) {
717
725
  triggerProps: normalize.button({
718
726
  "data-part": "trigger",
719
727
  type: "button",
720
- "data-placement": state.context.currentPlacement,
728
+ "data-placement": currentPlacement,
721
729
  id: dom.getTriggerId(state.context),
722
730
  "aria-haspopup": "dialog",
723
731
  "aria-expanded": isOpen,
@@ -725,6 +733,11 @@ function connect(state, send, normalize = normalizeProp) {
725
733
  "aria-controls": dom.getContentId(state.context),
726
734
  onClick() {
727
735
  send("TRIGGER_CLICK");
736
+ },
737
+ onKeyDown(event) {
738
+ if (event.key === "Escape") {
739
+ send("ESCAPE");
740
+ }
728
741
  }
729
742
  }),
730
743
  positionerProps: normalize.element({
@@ -739,9 +752,9 @@ function connect(state, send, normalize = normalizeProp) {
739
752
  role: "dialog",
740
753
  hidden: !isOpen,
741
754
  "data-expanded": dataAttr(isOpen),
742
- "aria-labelledby": state.context.isTitleRendered ? dom.getTitleId(state.context) : void 0,
743
- "aria-describedby": state.context.isDescriptionRendered ? dom.getDescriptionId(state.context) : void 0,
744
- "data-placement": state.context.currentPlacement,
755
+ "aria-labelledby": state.context.renderedElements.title ? dom.getTitleId(state.context) : void 0,
756
+ "aria-describedby": state.context.renderedElements.description ? dom.getDescriptionId(state.context) : void 0,
757
+ "data-placement": currentPlacement,
745
758
  onKeyDown(event) {
746
759
  const keyMap = {
747
760
  Escape(event2) {
@@ -789,19 +802,16 @@ function connect(state, send, normalize = normalizeProp) {
789
802
  }
790
803
 
791
804
  // src/popover.machine.ts
805
+ var import_aria_hidden = require("@zag-js/aria-hidden");
792
806
  var import_core = require("@zag-js/core");
793
807
  var import_popper2 = require("@zag-js/popper");
794
- var import_aria_hidden = require("@zag-js/aria-hidden");
795
808
  var import_focus_trap = require("focus-trap");
796
809
  var { and, or } = import_core.guards;
797
810
  function machine(ctx = {}) {
798
811
  return (0, import_core.createMachine)({
799
812
  id: "popover",
800
813
  initial: "unknown",
801
- context: __spreadValues({
802
- isTitleRendered: true,
803
- isDescriptionRendered: true,
804
- isAnchorRendered: false,
814
+ context: __spreadProps(__spreadValues({
805
815
  uid: "",
806
816
  closeOnBlur: true,
807
817
  closeOnEsc: true,
@@ -811,7 +821,13 @@ function machine(ctx = {}) {
811
821
  placement: "bottom"
812
822
  }, ctx.positioning),
813
823
  currentPlacement: void 0
814
- }, ctx),
824
+ }, ctx), {
825
+ renderedElements: {
826
+ title: true,
827
+ description: true,
828
+ anchor: false
829
+ }
830
+ }),
815
831
  computed: {
816
832
  currentPortalled: (ctx2) => !!ctx2.modal || !!ctx2.portalled
817
833
  },
@@ -840,13 +856,7 @@ function machine(ctx = {}) {
840
856
  "disableOutsidePointerEvents",
841
857
  "computePlacement"
842
858
  ],
843
- entry: (0, import_core.choose)([
844
- {
845
- guard: "autoFocus",
846
- actions: ["setInitialFocus", "invokeOnOpen"]
847
- },
848
- { actions: ["focusContent", "invokeOnOpen"] }
849
- ]),
859
+ entry: ["setInitialFocus", "invokeOnOpen"],
850
860
  on: {
851
861
  CLOSE: {
852
862
  target: "closed",
@@ -889,7 +899,7 @@ function machine(ctx = {}) {
889
899
  activities: {
890
900
  computePlacement(ctx2) {
891
901
  ctx2.currentPlacement = ctx2.positioning.placement;
892
- const anchorEl = ctx2.isAnchorRendered ? dom.getAnchorEl(ctx2) : dom.getTriggerEl(ctx2);
902
+ const anchorEl = ctx2.renderedElements.anchor ? dom.getAnchorEl(ctx2) : dom.getTriggerEl(ctx2);
893
903
  return (0, import_popper2.getPlacement)(anchorEl, dom.getPositionerEl(ctx2), __spreadProps(__spreadValues({}, ctx2.positioning), {
894
904
  onComplete(data) {
895
905
  ctx2.currentPlacement = data.placement;
@@ -967,9 +977,11 @@ function machine(ctx = {}) {
967
977
  actions: {
968
978
  checkRenderedElements(ctx2) {
969
979
  raf(() => {
970
- ctx2.isAnchorRendered = !!dom.getAnchorEl(ctx2);
971
- ctx2.isTitleRendered = !!dom.getTitleEl(ctx2);
972
- ctx2.isDescriptionRendered = !!dom.getDescriptionEl(ctx2);
980
+ Object.assign(ctx2.renderedElements, {
981
+ title: !!dom.getTitleEl(ctx2),
982
+ description: !!dom.getDescriptionEl(ctx2),
983
+ anchor: !!dom.getAnchorEl(ctx2)
984
+ });
973
985
  });
974
986
  },
975
987
  setupDocument(ctx2, evt) {
@@ -982,12 +994,6 @@ function machine(ctx = {}) {
982
994
  clearPointerDown(ctx2) {
983
995
  ctx2.pointerdownNode = null;
984
996
  },
985
- focusContent(ctx2) {
986
- raf(() => {
987
- var _a;
988
- (_a = dom.getContentEl(ctx2)) == null ? void 0 : _a.focus();
989
- });
990
- },
991
997
  setInitialFocus(ctx2) {
992
998
  raf(() => {
993
999
  var _a;