@zag-js/pin-input 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
@@ -46,7 +46,7 @@ var dataAttr = (guard) => {
46
46
  return guard ? "" : void 0;
47
47
  };
48
48
  var ariaAttr = (guard) => {
49
- return guard ? true : void 0;
49
+ return guard ? "true" : void 0;
50
50
  };
51
51
  function nextTick(fn) {
52
52
  const set = /* @__PURE__ */ new Set();
@@ -61,6 +61,12 @@ function nextTick(fn) {
61
61
  });
62
62
  };
63
63
  }
64
+ function raf(fn) {
65
+ const id = globalThis.requestAnimationFrame(fn);
66
+ return function cleanup() {
67
+ globalThis.cancelAnimationFrame(id);
68
+ };
69
+ }
64
70
  function getNativeEvent(event) {
65
71
  var _a;
66
72
  return (_a = event.nativeEvent) != null ? _a : event;
@@ -95,6 +101,58 @@ function queryAll(root, selector) {
95
101
  var _a;
96
102
  return Array.from((_a = root == null ? void 0 : root.querySelectorAll(selector)) != null ? _a : []);
97
103
  }
104
+ function itemById(v, id) {
105
+ return v.find((node) => node.id === id);
106
+ }
107
+ function indexOfId(v, id) {
108
+ const item = itemById(v, id);
109
+ return item ? v.indexOf(item) : -1;
110
+ }
111
+ var getValueText = (item) => {
112
+ var _a, _b;
113
+ return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
114
+ };
115
+ var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
116
+ var wrap = (v, idx) => {
117
+ return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
118
+ };
119
+ function findByText(v, text, currentId) {
120
+ const index = currentId ? indexOfId(v, currentId) : -1;
121
+ let items = currentId ? wrap(v, index) : v;
122
+ const isSingleKey = text.length === 1;
123
+ if (isSingleKey) {
124
+ items = items.filter((item) => item.id !== currentId);
125
+ }
126
+ return items.find((item) => match(getValueText(item), text));
127
+ }
128
+ function findByTypeahead(_items, options) {
129
+ const { state: state2, activeId, key, timeout = 350 } = options;
130
+ const search = state2.keysSoFar + key;
131
+ const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
132
+ const query2 = isRepeated ? search[0] : search;
133
+ let items = _items.slice();
134
+ const next = findByText(items, query2, activeId);
135
+ function cleanup() {
136
+ clearTimeout(state2.timer);
137
+ state2.timer = -1;
138
+ }
139
+ function update(value) {
140
+ state2.keysSoFar = value;
141
+ cleanup();
142
+ if (value !== "") {
143
+ state2.timer = +setTimeout(() => {
144
+ update("");
145
+ cleanup();
146
+ }, timeout);
147
+ }
148
+ }
149
+ update(search);
150
+ return next;
151
+ }
152
+ findByTypeahead.defaultOptions = {
153
+ keysSoFar: "",
154
+ timer: -1
155
+ };
98
156
 
99
157
  // ../../types/dist/index.mjs
100
158
  function createNormalizer(fn) {
@@ -104,7 +162,7 @@ var normalizeProp = createNormalizer((v) => v);
104
162
 
105
163
  // ../../utilities/core/dist/index.mjs
106
164
  var fromLength = (length) => Array.from(Array(length).keys());
107
- var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey || v.shiftKey;
165
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
108
166
  function invariant(...a) {
109
167
  const m = a.length === 1 ? a[0] : a[1];
110
168
  const c = a.length === 2 ? a[0] : true;
@@ -119,6 +177,10 @@ var dom = {
119
177
  var _a;
120
178
  return (_a = ctx.doc) != null ? _a : document;
121
179
  },
180
+ getRootNode: (ctx) => {
181
+ var _a;
182
+ return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
183
+ },
122
184
  getRootId: (ctx) => {
123
185
  var _a, _b;
124
186
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `pin-input:${ctx.uid}`;
@@ -127,7 +189,7 @@ var dom = {
127
189
  var _a, _b, _c;
128
190
  return (_c = (_b = (_a = ctx.ids) == null ? void 0 : _a.input) == null ? void 0 : _b.call(_a, id)) != null ? _c : `pin-input:${ctx.uid}:${id}`;
129
191
  },
130
- getRootEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getRootId(ctx)),
192
+ getRootEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getRootId(ctx)),
131
193
  getElements: (ctx) => {
132
194
  const ownerId = CSS.escape(dom.getRootId(ctx));
133
195
  const selector = `input[data-ownedby=${ownerId}]`;
@@ -160,10 +222,8 @@ function connect(state, send, normalize = normalizeProp) {
160
222
  send({ type: "SET_VALUE", value, index });
161
223
  },
162
224
  focus() {
163
- nextTick(() => {
164
- var _a;
165
- (_a = dom.getFirstInputEl(state.context)) == null ? void 0 : _a.focus();
166
- });
225
+ var _a;
226
+ (_a = dom.getFirstInputEl(state.context)) == null ? void 0 : _a.focus();
167
227
  },
168
228
  rootProps: normalize.element({
169
229
  dir: state.context.dir,
@@ -195,7 +255,7 @@ function connect(state, send, normalize = normalizeProp) {
195
255
  const evt = getNativeEvent(event);
196
256
  if (evt.isComposing)
197
257
  return;
198
- const value = event.target.value;
258
+ const { value } = event.currentTarget;
199
259
  if (evt.inputType === "insertFromPaste" || value.length > 2) {
200
260
  send({ type: "PASTE", value });
201
261
  event.preventDefault();
@@ -322,10 +382,6 @@ function machine(ctx = {}) {
322
382
  guard: and("isFinalValue", "isValidValue"),
323
383
  actions: "setFocusedValue"
324
384
  },
325
- {
326
- guard: and("hasValue", "isValidValue"),
327
- actions: ["replaceFocusedValue", "setNextFocusedIndex"]
328
- },
329
385
  {
330
386
  guard: "isValidValue",
331
387
  actions: ["setFocusedValue", "setNextFocusedIndex"]
@@ -383,6 +439,8 @@ function machine(ctx = {}) {
383
439
  setupDocument: (ctx2, evt) => {
384
440
  if (evt.doc)
385
441
  ctx2.doc = (0, import_core.ref)(evt.doc);
442
+ if (evt.root)
443
+ ctx2.rootNode = (0, import_core.ref)(evt.root);
386
444
  ctx2.uid = evt.id;
387
445
  },
388
446
  setupValue: (ctx2) => {
@@ -393,7 +451,7 @@ function machine(ctx = {}) {
393
451
  });
394
452
  },
395
453
  focusInput: (ctx2) => {
396
- nextTick(() => {
454
+ raf(() => {
397
455
  var _a;
398
456
  if (ctx2.focusedIndex === -1)
399
457
  return;
@@ -419,27 +477,23 @@ function machine(ctx = {}) {
419
477
  clearFocusedIndex: (ctx2) => {
420
478
  ctx2.focusedIndex = -1;
421
479
  },
422
- setValue: (ctx2, event) => {
423
- assign(ctx2, event.value);
424
- },
425
- setFocusedIndex: (ctx2, event) => {
426
- ctx2.focusedIndex = event.index;
480
+ setValue: (ctx2, evt) => {
481
+ assign(ctx2, evt.value);
427
482
  },
428
- setFocusedValue: (ctx2, event) => {
429
- ctx2.value[ctx2.focusedIndex] = event.value;
483
+ setFocusedIndex: (ctx2, evt) => {
484
+ ctx2.focusedIndex = evt.index;
430
485
  },
431
- replaceFocusedValue: (ctx2, evt) => {
432
- const val = ctx2.value[ctx2.focusedIndex];
433
- ctx2.value[ctx2.focusedIndex] = evt.value.replace(val, "").charAt(0);
486
+ setFocusedValue: (ctx2, evt) => {
487
+ ctx2.value[ctx2.focusedIndex] = lastChar(evt.value);
434
488
  },
435
489
  setPastedValue(ctx2, evt) {
436
- nextTick(() => {
437
- const value = evt.value.substr(0, ctx2.valueLength);
490
+ raf(() => {
491
+ const value = evt.value.substring(0, ctx2.valueLength);
438
492
  assign(ctx2, value.split("").filter(Boolean));
439
493
  });
440
494
  },
441
495
  setValueAtIndex: (ctx2, evt) => {
442
- ctx2.value[evt.index] = evt.value;
496
+ ctx2.value[evt.index] = lastChar(evt.value);
443
497
  },
444
498
  clearValue: (ctx2) => {
445
499
  assign(ctx2, "");
@@ -457,7 +511,7 @@ function machine(ctx = {}) {
457
511
  ctx2.focusedIndex = Math.max(ctx2.focusedIndex - 1, 0);
458
512
  },
459
513
  setLastValueFocusIndex: (ctx2) => {
460
- nextTick(() => {
514
+ raf(() => {
461
515
  ctx2.focusedIndex = Math.min(ctx2.filledValueLength, ctx2.valueLength - 1);
462
516
  });
463
517
  },
@@ -467,7 +521,7 @@ function machine(ctx = {}) {
467
521
  blurFocusedInputIfNeeded(ctx2) {
468
522
  if (!ctx2.blurOnComplete)
469
523
  return;
470
- nextTick(() => {
524
+ raf(() => {
471
525
  var _a;
472
526
  (_a = dom.getFocusedEl(ctx2)) == null ? void 0 : _a.blur();
473
527
  });
@@ -487,7 +541,8 @@ function isValidType(value, type) {
487
541
  return !!((_a = REGEX[type]) == null ? void 0 : _a.test(value));
488
542
  }
489
543
  function assign(ctx, value) {
490
- for (let i = 0; i < ctx.value.length; i++) {
544
+ const len = ctx.value.length;
545
+ for (let i = 0; i < len; i++) {
491
546
  if (Array.isArray(value)) {
492
547
  if (!value[i])
493
548
  continue;
@@ -497,4 +552,7 @@ function assign(ctx, value) {
497
552
  }
498
553
  }
499
554
  }
555
+ function lastChar(value) {
556
+ return value.charAt(value.length - 1);
557
+ }
500
558
  //# sourceMappingURL=index.js.map