@zag-js/popover 0.1.0 → 0.1.3

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");
@@ -919,19 +971,19 @@ function machine(ctx = {}) {
919
971
  ctx2.pointerdownNode = null;
920
972
  },
921
973
  focusContent(ctx2) {
922
- nextTick(() => {
974
+ raf(() => {
923
975
  var _a;
924
976
  (_a = dom.getContentEl(ctx2)) == null ? void 0 : _a.focus();
925
977
  });
926
978
  },
927
979
  setInitialFocus(ctx2) {
928
- nextTick(() => {
980
+ raf(() => {
929
981
  var _a;
930
982
  (_a = dom.getInitialFocusEl(ctx2)) == null ? void 0 : _a.focus();
931
983
  });
932
984
  },
933
985
  focusTrigger(ctx2) {
934
- nextTick(() => {
986
+ raf(() => {
935
987
  var _a;
936
988
  (_a = dom.getTriggerEl(ctx2)) == null ? void 0 : _a.focus();
937
989
  });
@@ -966,7 +1018,7 @@ function machine(ctx = {}) {
966
1018
  if (!elementAfterTrigger || elementAfterTrigger === button)
967
1019
  return;
968
1020
  evt.preventDefault();
969
- nextTick(() => elementAfterTrigger == null ? void 0 : elementAfterTrigger.focus());
1021
+ raf(() => elementAfterTrigger == null ? void 0 : elementAfterTrigger.focus());
970
1022
  }
971
1023
  }
972
1024
  });