@zag-js/pin-input 0.1.1 → 0.1.4

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
@@ -23,7 +23,7 @@ var dataAttr = (guard) => {
23
23
  return guard ? "" : void 0;
24
24
  };
25
25
  var ariaAttr = (guard) => {
26
- return guard ? true : void 0;
26
+ return guard ? "true" : void 0;
27
27
  };
28
28
  function nextTick(fn) {
29
29
  const set = /* @__PURE__ */ new Set();
@@ -38,6 +38,12 @@ function nextTick(fn) {
38
38
  });
39
39
  };
40
40
  }
41
+ function raf(fn) {
42
+ const id = globalThis.requestAnimationFrame(fn);
43
+ return function cleanup() {
44
+ globalThis.cancelAnimationFrame(id);
45
+ };
46
+ }
41
47
  function getNativeEvent(event) {
42
48
  var _a;
43
49
  return (_a = event.nativeEvent) != null ? _a : event;
@@ -72,6 +78,58 @@ function queryAll(root, selector) {
72
78
  var _a;
73
79
  return Array.from((_a = root == null ? void 0 : root.querySelectorAll(selector)) != null ? _a : []);
74
80
  }
81
+ function itemById(v, id) {
82
+ return v.find((node) => node.id === id);
83
+ }
84
+ function indexOfId(v, id) {
85
+ const item = itemById(v, id);
86
+ return item ? v.indexOf(item) : -1;
87
+ }
88
+ var getValueText = (item) => {
89
+ var _a, _b;
90
+ return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
91
+ };
92
+ var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
93
+ var wrap = (v, idx) => {
94
+ return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
95
+ };
96
+ function findByText(v, text, currentId) {
97
+ const index = currentId ? indexOfId(v, currentId) : -1;
98
+ let items = currentId ? wrap(v, index) : v;
99
+ const isSingleKey = text.length === 1;
100
+ if (isSingleKey) {
101
+ items = items.filter((item) => item.id !== currentId);
102
+ }
103
+ return items.find((item) => match(getValueText(item), text));
104
+ }
105
+ function findByTypeahead(_items, options) {
106
+ const { state: state2, activeId, key, timeout = 350 } = options;
107
+ const search = state2.keysSoFar + key;
108
+ const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
109
+ const query2 = isRepeated ? search[0] : search;
110
+ let items = _items.slice();
111
+ const next = findByText(items, query2, activeId);
112
+ function cleanup() {
113
+ clearTimeout(state2.timer);
114
+ state2.timer = -1;
115
+ }
116
+ function update(value) {
117
+ state2.keysSoFar = value;
118
+ cleanup();
119
+ if (value !== "") {
120
+ state2.timer = +setTimeout(() => {
121
+ update("");
122
+ cleanup();
123
+ }, timeout);
124
+ }
125
+ }
126
+ update(search);
127
+ return next;
128
+ }
129
+ findByTypeahead.defaultOptions = {
130
+ keysSoFar: "",
131
+ timer: -1
132
+ };
75
133
 
76
134
  // ../../types/dist/index.mjs
77
135
  function createNormalizer(fn) {
@@ -81,7 +139,7 @@ var normalizeProp = createNormalizer((v) => v);
81
139
 
82
140
  // ../../utilities/core/dist/index.mjs
83
141
  var fromLength = (length) => Array.from(Array(length).keys());
84
- var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey || v.shiftKey;
142
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
85
143
  function invariant(...a) {
86
144
  const m = a.length === 1 ? a[0] : a[1];
87
145
  const c = a.length === 2 ? a[0] : true;
@@ -96,6 +154,10 @@ var dom = {
96
154
  var _a;
97
155
  return (_a = ctx.doc) != null ? _a : document;
98
156
  },
157
+ getRootNode: (ctx) => {
158
+ var _a;
159
+ return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
160
+ },
99
161
  getRootId: (ctx) => {
100
162
  var _a, _b;
101
163
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `pin-input:${ctx.uid}`;
@@ -104,7 +166,7 @@ var dom = {
104
166
  var _a, _b, _c;
105
167
  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}`;
106
168
  },
107
- getRootEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getRootId(ctx)),
169
+ getRootEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getRootId(ctx)),
108
170
  getElements: (ctx) => {
109
171
  const ownerId = CSS.escape(dom.getRootId(ctx));
110
172
  const selector = `input[data-ownedby=${ownerId}]`;
@@ -137,10 +199,8 @@ function connect(state, send, normalize = normalizeProp) {
137
199
  send({ type: "SET_VALUE", value, index });
138
200
  },
139
201
  focus() {
140
- nextTick(() => {
141
- var _a;
142
- (_a = dom.getFirstInputEl(state.context)) == null ? void 0 : _a.focus();
143
- });
202
+ var _a;
203
+ (_a = dom.getFirstInputEl(state.context)) == null ? void 0 : _a.focus();
144
204
  },
145
205
  rootProps: normalize.element({
146
206
  dir: state.context.dir,
@@ -172,7 +232,7 @@ function connect(state, send, normalize = normalizeProp) {
172
232
  const evt = getNativeEvent(event);
173
233
  if (evt.isComposing)
174
234
  return;
175
- const value = event.target.value;
235
+ const { value } = event.currentTarget;
176
236
  if (evt.inputType === "insertFromPaste" || value.length > 2) {
177
237
  send({ type: "PASTE", value });
178
238
  event.preventDefault();
@@ -299,10 +359,6 @@ function machine(ctx = {}) {
299
359
  guard: and("isFinalValue", "isValidValue"),
300
360
  actions: "setFocusedValue"
301
361
  },
302
- {
303
- guard: and("hasValue", "isValidValue"),
304
- actions: ["replaceFocusedValue", "setNextFocusedIndex"]
305
- },
306
362
  {
307
363
  guard: "isValidValue",
308
364
  actions: ["setFocusedValue", "setNextFocusedIndex"]
@@ -360,6 +416,8 @@ function machine(ctx = {}) {
360
416
  setupDocument: (ctx2, evt) => {
361
417
  if (evt.doc)
362
418
  ctx2.doc = ref(evt.doc);
419
+ if (evt.root)
420
+ ctx2.rootNode = ref(evt.root);
363
421
  ctx2.uid = evt.id;
364
422
  },
365
423
  setupValue: (ctx2) => {
@@ -370,7 +428,7 @@ function machine(ctx = {}) {
370
428
  });
371
429
  },
372
430
  focusInput: (ctx2) => {
373
- nextTick(() => {
431
+ raf(() => {
374
432
  var _a;
375
433
  if (ctx2.focusedIndex === -1)
376
434
  return;
@@ -396,27 +454,23 @@ function machine(ctx = {}) {
396
454
  clearFocusedIndex: (ctx2) => {
397
455
  ctx2.focusedIndex = -1;
398
456
  },
399
- setValue: (ctx2, event) => {
400
- assign(ctx2, event.value);
401
- },
402
- setFocusedIndex: (ctx2, event) => {
403
- ctx2.focusedIndex = event.index;
457
+ setValue: (ctx2, evt) => {
458
+ assign(ctx2, evt.value);
404
459
  },
405
- setFocusedValue: (ctx2, event) => {
406
- ctx2.value[ctx2.focusedIndex] = event.value;
460
+ setFocusedIndex: (ctx2, evt) => {
461
+ ctx2.focusedIndex = evt.index;
407
462
  },
408
- replaceFocusedValue: (ctx2, evt) => {
409
- const val = ctx2.value[ctx2.focusedIndex];
410
- ctx2.value[ctx2.focusedIndex] = evt.value.replace(val, "").charAt(0);
463
+ setFocusedValue: (ctx2, evt) => {
464
+ ctx2.value[ctx2.focusedIndex] = lastChar(evt.value);
411
465
  },
412
466
  setPastedValue(ctx2, evt) {
413
- nextTick(() => {
414
- const value = evt.value.substr(0, ctx2.valueLength);
467
+ raf(() => {
468
+ const value = evt.value.substring(0, ctx2.valueLength);
415
469
  assign(ctx2, value.split("").filter(Boolean));
416
470
  });
417
471
  },
418
472
  setValueAtIndex: (ctx2, evt) => {
419
- ctx2.value[evt.index] = evt.value;
473
+ ctx2.value[evt.index] = lastChar(evt.value);
420
474
  },
421
475
  clearValue: (ctx2) => {
422
476
  assign(ctx2, "");
@@ -434,7 +488,7 @@ function machine(ctx = {}) {
434
488
  ctx2.focusedIndex = Math.max(ctx2.focusedIndex - 1, 0);
435
489
  },
436
490
  setLastValueFocusIndex: (ctx2) => {
437
- nextTick(() => {
491
+ raf(() => {
438
492
  ctx2.focusedIndex = Math.min(ctx2.filledValueLength, ctx2.valueLength - 1);
439
493
  });
440
494
  },
@@ -444,7 +498,7 @@ function machine(ctx = {}) {
444
498
  blurFocusedInputIfNeeded(ctx2) {
445
499
  if (!ctx2.blurOnComplete)
446
500
  return;
447
- nextTick(() => {
501
+ raf(() => {
448
502
  var _a;
449
503
  (_a = dom.getFocusedEl(ctx2)) == null ? void 0 : _a.blur();
450
504
  });
@@ -464,7 +518,8 @@ function isValidType(value, type) {
464
518
  return !!((_a = REGEX[type]) == null ? void 0 : _a.test(value));
465
519
  }
466
520
  function assign(ctx, value) {
467
- for (let i = 0; i < ctx.value.length; i++) {
521
+ const len = ctx.value.length;
522
+ for (let i = 0; i < len; i++) {
468
523
  if (Array.isArray(value)) {
469
524
  if (!value[i])
470
525
  continue;
@@ -474,6 +529,9 @@ function assign(ctx, value) {
474
529
  }
475
530
  }
476
531
  }
532
+ function lastChar(value) {
533
+ return value.charAt(value.length - 1);
534
+ }
477
535
  export {
478
536
  connect,
479
537
  machine