@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.mjs CHANGED
@@ -502,6 +502,30 @@ var getTabbables = (el, includeContainer = false) => {
502
502
  var isTabbable = (el) => {
503
503
  return isFocusable(el) && !isDisabled(el) && !isHidden(el);
504
504
  };
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
+ }
505
529
  function trackPointerDown(doc, onPointerDown) {
506
530
  var _a;
507
531
  const win = (_a = doc.defaultView) != null ? _a : window;
@@ -512,6 +536,34 @@ function trackPointerDown(doc, onPointerDown) {
512
536
  };
513
537
  return addDomEvent(doc, "pointerdown", fn);
514
538
  }
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
+ };
515
567
 
516
568
  // src/popover.connect.ts
517
569
  import { getPlacementStyles } from "@zag-js/popper";
@@ -554,6 +606,10 @@ var dom = {
554
606
  return (_a = ctx.doc) != null ? _a : document;
555
607
  },
556
608
  getActiveEl: (ctx) => dom.getDoc(ctx).activeElement,
609
+ getRootNode: (ctx) => {
610
+ var _a;
611
+ return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
612
+ },
557
613
  getAnchorId: (ctx) => {
558
614
  var _a, _b;
559
615
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.anchor) != null ? _b : `popover:${ctx.uid}:anchor`;
@@ -580,12 +636,12 @@ var dom = {
580
636
  var _a, _b;
581
637
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.closeBtn) != null ? _b : `popover:${ctx.uid}:close-button`;
582
638
  },
583
- getAnchorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getAnchorId(ctx)),
584
- getTriggerEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getTriggerId(ctx)),
585
- getContentEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getContentId(ctx)),
586
- getPositionerEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getPositionerId(ctx)),
587
- getTitleEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getTitleId(ctx)),
588
- getDescriptionEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getDescriptionId(ctx)),
639
+ getAnchorEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getAnchorId(ctx)),
640
+ getTriggerEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getTriggerId(ctx)),
641
+ getContentEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getContentId(ctx)),
642
+ getPositionerEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getPositionerId(ctx)),
643
+ getTitleEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getTitleId(ctx)),
644
+ getDescriptionEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getDescriptionId(ctx)),
589
645
  getFocusableEls: (ctx) => getFocusables(dom.getContentEl(ctx)),
590
646
  getFirstFocusableEl: (ctx) => dom.getFocusableEls(ctx)[0],
591
647
  getDocTabbableEls: (ctx) => getTabbables(cast(dom.getDoc(ctx))),
@@ -606,7 +662,8 @@ function connect(state, send, normalize = normalizeProp) {
606
662
  const isOpen = state.matches("open");
607
663
  const pointerdownNode = state.context.pointerdownNode;
608
664
  const popperStyles = getPlacementStyles({
609
- measured: !!state.context.isPlacementComplete
665
+ measured: !!state.context.isPlacementComplete,
666
+ placement: state.context.currentPlacement
610
667
  });
611
668
  return {
612
669
  portalled: state.context.currentPortalled,
@@ -654,6 +711,7 @@ function connect(state, send, normalize = normalizeProp) {
654
711
  tabIndex: -1,
655
712
  role: "dialog",
656
713
  hidden: !isOpen,
714
+ "data-expanded": dataAttr(isOpen),
657
715
  "aria-labelledby": state.context.isTitleRendered ? dom.getTitleId(state.context) : void 0,
658
716
  "aria-describedby": state.context.isDescriptionRendered ? dom.getDescriptionId(state.context) : void 0,
659
717
  "data-placement": state.context.currentPlacement,
@@ -890,6 +948,8 @@ function machine(ctx = {}) {
890
948
  setupDocument(ctx2, evt) {
891
949
  if (evt.doc)
892
950
  ctx2.doc = ref(evt.doc);
951
+ if (evt.root)
952
+ ctx2.rootNode = ref(evt.root);
893
953
  ctx2.uid = evt.id;
894
954
  },
895
955
  clearPointerDown(ctx2) {