@zag-js/popover 0.1.2 → 0.1.5

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
@@ -525,6 +525,30 @@ var getTabbables = (el, includeContainer = false) => {
525
525
  var isTabbable = (el) => {
526
526
  return isFocusable(el) && !isDisabled(el) && !isHidden(el);
527
527
  };
528
+ function itemById(v, id) {
529
+ return v.find((node) => node.id === id);
530
+ }
531
+ function indexOfId(v, id) {
532
+ const item = itemById(v, id);
533
+ return item ? v.indexOf(item) : -1;
534
+ }
535
+ var getValueText = (item) => {
536
+ var _a, _b;
537
+ return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
538
+ };
539
+ var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
540
+ var wrap = (v, idx) => {
541
+ return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
542
+ };
543
+ function findByText(v, text, currentId) {
544
+ const index = currentId ? indexOfId(v, currentId) : -1;
545
+ let items = currentId ? wrap(v, index) : v;
546
+ const isSingleKey = text.length === 1;
547
+ if (isSingleKey) {
548
+ items = items.filter((item) => item.id !== currentId);
549
+ }
550
+ return items.find((item) => match(getValueText(item), text));
551
+ }
528
552
  function trackPointerDown(doc, onPointerDown) {
529
553
  var _a;
530
554
  const win = (_a = doc.defaultView) != null ? _a : window;
@@ -535,6 +559,34 @@ function trackPointerDown(doc, onPointerDown) {
535
559
  };
536
560
  return addDomEvent(doc, "pointerdown", fn);
537
561
  }
562
+ function findByTypeahead(_items, options) {
563
+ const { state: state2, activeId, key, timeout = 350 } = options;
564
+ const search = state2.keysSoFar + key;
565
+ const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
566
+ const query2 = isRepeated ? search[0] : search;
567
+ let items = _items.slice();
568
+ const next2 = findByText(items, query2, activeId);
569
+ function cleanup() {
570
+ clearTimeout(state2.timer);
571
+ state2.timer = -1;
572
+ }
573
+ function update(value) {
574
+ state2.keysSoFar = value;
575
+ cleanup();
576
+ if (value !== "") {
577
+ state2.timer = +setTimeout(() => {
578
+ update("");
579
+ cleanup();
580
+ }, timeout);
581
+ }
582
+ }
583
+ update(search);
584
+ return next2;
585
+ }
586
+ findByTypeahead.defaultOptions = {
587
+ keysSoFar: "",
588
+ timer: -1
589
+ };
538
590
 
539
591
  // src/popover.connect.ts
540
592
  var import_popper = require("@zag-js/popper");
@@ -577,6 +629,10 @@ var dom = {
577
629
  return (_a = ctx.doc) != null ? _a : document;
578
630
  },
579
631
  getActiveEl: (ctx) => dom.getDoc(ctx).activeElement,
632
+ getRootNode: (ctx) => {
633
+ var _a;
634
+ return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
635
+ },
580
636
  getAnchorId: (ctx) => {
581
637
  var _a, _b;
582
638
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.anchor) != null ? _b : `popover:${ctx.uid}:anchor`;
@@ -603,12 +659,12 @@ var dom = {
603
659
  var _a, _b;
604
660
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.closeBtn) != null ? _b : `popover:${ctx.uid}:close-button`;
605
661
  },
606
- getAnchorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getAnchorId(ctx)),
607
- getTriggerEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getTriggerId(ctx)),
608
- getContentEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getContentId(ctx)),
609
- getPositionerEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getPositionerId(ctx)),
610
- getTitleEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getTitleId(ctx)),
611
- getDescriptionEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getDescriptionId(ctx)),
662
+ getAnchorEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getAnchorId(ctx)),
663
+ getTriggerEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getTriggerId(ctx)),
664
+ getContentEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getContentId(ctx)),
665
+ getPositionerEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getPositionerId(ctx)),
666
+ getTitleEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getTitleId(ctx)),
667
+ getDescriptionEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getDescriptionId(ctx)),
612
668
  getFocusableEls: (ctx) => getFocusables(dom.getContentEl(ctx)),
613
669
  getFirstFocusableEl: (ctx) => dom.getFocusableEls(ctx)[0],
614
670
  getDocTabbableEls: (ctx) => getTabbables(cast(dom.getDoc(ctx))),
@@ -629,7 +685,8 @@ function connect(state, send, normalize = normalizeProp) {
629
685
  const isOpen = state.matches("open");
630
686
  const pointerdownNode = state.context.pointerdownNode;
631
687
  const popperStyles = (0, import_popper.getPlacementStyles)({
632
- measured: !!state.context.isPlacementComplete
688
+ measured: !!state.context.isPlacementComplete,
689
+ placement: state.context.currentPlacement
633
690
  });
634
691
  return {
635
692
  portalled: state.context.currentPortalled,
@@ -677,6 +734,7 @@ function connect(state, send, normalize = normalizeProp) {
677
734
  tabIndex: -1,
678
735
  role: "dialog",
679
736
  hidden: !isOpen,
737
+ "data-expanded": dataAttr(isOpen),
680
738
  "aria-labelledby": state.context.isTitleRendered ? dom.getTitleId(state.context) : void 0,
681
739
  "aria-describedby": state.context.isDescriptionRendered ? dom.getDescriptionId(state.context) : void 0,
682
740
  "data-placement": state.context.currentPlacement,
@@ -913,6 +971,8 @@ function machine(ctx = {}) {
913
971
  setupDocument(ctx2, evt) {
914
972
  if (evt.doc)
915
973
  ctx2.doc = (0, import_core.ref)(evt.doc);
974
+ if (evt.root)
975
+ ctx2.rootNode = (0, import_core.ref)(evt.root);
916
976
  ctx2.uid = evt.id;
917
977
  },
918
978
  clearPointerDown(ctx2) {