@zag-js/number-input 0.1.8 → 0.1.11

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.d.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  export { connect } from "./number-input.connect";
2
2
  export { machine } from "./number-input.machine";
3
3
  export type { UserDefinedContext as Context } from "./number-input.types";
4
- //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __defProps = Object.defineProperties;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -48,92 +49,43 @@ var dataAttr = (guard) => {
48
49
  var ariaAttr = (guard) => {
49
50
  return guard ? "true" : void 0;
50
51
  };
51
- function raf(fn) {
52
- const id = globalThis.requestAnimationFrame(fn);
53
- return function cleanup() {
54
- globalThis.cancelAnimationFrame(id);
55
- };
56
- }
57
- var noop = () => {
52
+ var MAX_Z_INDEX = 2147483647;
53
+ var runIfFn = (v, ...a) => {
54
+ const res = typeof v === "function" ? v(...a) : v;
55
+ return res != null ? res : void 0;
58
56
  };
59
- var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
60
- function getListenerElements() {
61
- ;
62
- globalThis.__listenerElements__ = globalThis.__listenerElements__ || /* @__PURE__ */ new Map();
63
- return globalThis.__listenerElements__;
64
- }
65
- function getListenerCache() {
66
- ;
67
- globalThis.__listenerCache__ = globalThis.__listenerCache__ || /* @__PURE__ */ new Map();
68
- return globalThis.__listenerCache__;
69
- }
70
- function globalEventBus(node, type, handler, options) {
57
+ var callAll = (...fns) => (...a) => {
58
+ fns.forEach(function(fn) {
59
+ fn == null ? void 0 : fn(...a);
60
+ });
61
+ };
62
+ var isArray = (v) => Array.isArray(v);
63
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
64
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
65
+ var isDom = () => typeof window !== "undefined";
66
+ function getPlatform() {
71
67
  var _a;
72
- if (!node)
73
- return noop;
74
- const hash = JSON.stringify({ type, options });
75
- const listenerElements = getListenerElements();
76
- const listenerCache = getListenerCache();
77
- const group = listenerElements.get(node);
78
- if (!listenerElements.has(node)) {
79
- const group2 = /* @__PURE__ */ new Map([[hash, /* @__PURE__ */ new Set([handler])]]);
80
- listenerElements.set(node, group2);
81
- } else if (group == null ? void 0 : group.has(hash)) {
82
- (_a = group == null ? void 0 : group.get(hash)) == null ? void 0 : _a.add(handler);
83
- } else {
84
- group == null ? void 0 : group.set(hash, /* @__PURE__ */ new Set([handler]));
85
- }
86
- function attach(node2) {
87
- var _a2, _b;
88
- function listener(event) {
89
- var _a3;
90
- const group2 = listenerElements.get(node2);
91
- (_a3 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a3.forEach((fn) => fn(event));
92
- }
93
- if (!(listenerCache == null ? void 0 : listenerCache.has(node2))) {
94
- listenerCache.set(node2, /* @__PURE__ */ new Map([[hash, listener]]));
95
- node2.addEventListener(type, listener, options);
96
- return;
97
- }
98
- if (!((_a2 = listenerCache == null ? void 0 : listenerCache.get(node2)) == null ? void 0 : _a2.has(hash))) {
99
- (_b = listenerCache.get(node2)) == null ? void 0 : _b.set(hash, listener);
100
- node2.addEventListener(type, listener, options);
101
- }
102
- }
103
- attach(node);
104
- return function remove() {
105
- var _a2, _b, _c, _d;
106
- if (!listenerElements.has(node))
107
- return;
108
- const group2 = listenerElements.get(node);
109
- (_a2 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a2.delete(handler);
110
- if (((_b = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _b.size) === 0) {
111
- const listener = (_c = listenerCache.get(node)) == null ? void 0 : _c.get(hash);
112
- node.removeEventListener(type, listener, options);
113
- group2 == null ? void 0 : group2.delete(hash);
114
- (_d = listenerCache.get(node)) == null ? void 0 : _d.delete(hash);
115
- if ((group2 == null ? void 0 : group2.size) === 0) {
116
- listenerElements.delete(node);
117
- listenerCache.delete(node);
118
- }
119
- }
120
- };
121
- }
122
- var t = (v) => Object.prototype.toString.call(v).slice(8, -1);
123
- var isRef = (v) => t(v) === "Object" && "current" in v;
124
- var runIfFn = (fn) => t(fn) === "Function" ? fn() : fn;
125
- function addDomEvent(target, event, listener, options) {
126
- const node = isRef(target) ? target.current : runIfFn(target);
127
- return globalEventBus(node, event, listener, options);
68
+ const agent = navigator.userAgentData;
69
+ return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
128
70
  }
129
- var MAX_Z_INDEX = 2147483647;
130
- function getNativeEvent(event) {
71
+ var pt = (v) => isDom() && v.test(getPlatform());
72
+ var vn = (v) => isDom() && v.test(navigator.vendor);
73
+ var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
74
+ var isMac = () => pt(/^Mac/) && !isTouchDevice;
75
+ var isSafari = () => isApple() && vn(/apple/i);
76
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
77
+ var isIos = () => isApple() && !isMac();
78
+ function getNativeEvent(e) {
131
79
  var _a;
132
- return (_a = event.nativeEvent) != null ? _a : event;
80
+ return (_a = e.nativeEvent) != null ? _a : e;
133
81
  }
82
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
83
+ var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
84
+ var isLeftClick = (v) => v.button === 0;
85
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
134
86
  function observeAttributes(node, attributes, fn) {
135
87
  if (!node)
136
- return noop;
88
+ return;
137
89
  const attrs = Array.isArray(attributes) ? attributes : [attributes];
138
90
  const win = node.ownerDocument.defaultView || window;
139
91
  const obs = new win.MutationObserver((changes) => {
@@ -146,6 +98,17 @@ function observeAttributes(node, attributes, fn) {
146
98
  obs.observe(node, { attributes: true, attributeFilter: attrs });
147
99
  return () => obs.disconnect();
148
100
  }
101
+ var fallback = {
102
+ pageX: 0,
103
+ pageY: 0,
104
+ clientX: 0,
105
+ clientY: 0
106
+ };
107
+ function getEventPoint(event, type = "page") {
108
+ var _a, _b;
109
+ const point = isTouchEvent(event) ? (_b = (_a = event.touches[0]) != null ? _a : event.changedTouches[0]) != null ? _b : fallback : event;
110
+ return { x: point[`${type}X`], y: point[`${type}Y`] };
111
+ }
149
112
  var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
150
113
  var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
151
114
  function getEventStep(event) {
@@ -157,29 +120,19 @@ function getEventStep(event) {
157
120
  return isSkipKey ? 10 : 1;
158
121
  }
159
122
  }
160
- function itemById(v, id) {
161
- return v.find((node) => node.id === id);
162
- }
163
- function indexOfId(v, id) {
164
- const item = itemById(v, id);
165
- return item ? v.indexOf(item) : -1;
123
+ var isRef = (v) => hasProp(v, "current");
124
+ function addDomEvent(target, eventName, handler, options) {
125
+ const node = isRef(target) ? target.current : runIfFn(target);
126
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
127
+ return () => {
128
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
129
+ };
166
130
  }
167
- var getValueText = (item) => {
168
- var _a, _b;
169
- return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
170
- };
171
- var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
172
- var wrap = (v, idx) => {
173
- return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
174
- };
175
- function findByText(v, text, currentId) {
176
- const index = currentId ? indexOfId(v, currentId) : -1;
177
- let items = currentId ? wrap(v, index) : v;
178
- const isSingleKey = text.length === 1;
179
- if (isSingleKey) {
180
- items = items.filter((item) => item.id !== currentId);
181
- }
182
- return items.find((item) => match(getValueText(item), text));
131
+ function raf(fn) {
132
+ const id = globalThis.requestAnimationFrame(fn);
133
+ return function cleanup() {
134
+ globalThis.cancelAnimationFrame(id);
135
+ };
183
136
  }
184
137
  function addPointerlockChangeListener(doc, fn) {
185
138
  return addDomEvent(doc, "pointerlockchange", fn, false);
@@ -213,7 +166,7 @@ function requestPointerLock(doc, handlers = {}) {
213
166
  if (!supported)
214
167
  return;
215
168
  body.requestPointerLock();
216
- const cleanup = pipe(addPointerlockChangeListener(doc, onPointerChange), addPointerlockErrorListener(doc, onPointerError));
169
+ const cleanup = callAll(addPointerlockChangeListener(doc, onPointerChange), addPointerlockErrorListener(doc, onPointerError));
217
170
  return function dispose() {
218
171
  if (!supported)
219
172
  return;
@@ -221,38 +174,10 @@ function requestPointerLock(doc, handlers = {}) {
221
174
  exit();
222
175
  };
223
176
  }
224
- function findByTypeahead(_items, options) {
225
- const { state: state2, activeId, key, timeout = 350 } = options;
226
- const search = state2.keysSoFar + key;
227
- const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
228
- const query2 = isRepeated ? search[0] : search;
229
- let items = _items.slice();
230
- const next = findByText(items, query2, activeId);
231
- function cleanup() {
232
- clearTimeout(state2.timer);
233
- state2.timer = -1;
234
- }
235
- function update(value) {
236
- state2.keysSoFar = value;
237
- cleanup();
238
- if (value !== "") {
239
- state2.timer = +setTimeout(() => {
240
- update("");
241
- cleanup();
242
- }, timeout);
243
- }
244
- }
245
- update(search);
246
- return next;
247
- }
248
- findByTypeahead.defaultOptions = {
249
- keysSoFar: "",
250
- timer: -1
251
- };
252
177
 
253
178
  // ../../utilities/number/dist/index.mjs
254
179
  var __pow = Math.pow;
255
- function wrap2(num, max) {
180
+ function wrap(num, max) {
256
181
  return (num % max + max) % max;
257
182
  }
258
183
  function roundToDevicePixel(num) {
@@ -315,40 +240,6 @@ function decimalOperation(a, op, b) {
315
240
  }
316
241
  var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
317
242
 
318
- // ../../utilities/rect/dist/index.mjs
319
- var isArray = (v) => Array.isArray(v);
320
- var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
321
- var isTouchEvent = (v) => isObject(v) && "touches" in v;
322
- var fallback = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
323
- function getEventPoint(e, t2 = "page") {
324
- const p = isTouchEvent(e) ? e.touches[0] || e.changedTouches[0] || fallback : e;
325
- return { x: p[`${t2}X`], y: p[`${t2}Y`] };
326
- }
327
-
328
- // ../../types/dist/index.mjs
329
- function createNormalizer(fn) {
330
- return new Proxy({}, {
331
- get() {
332
- return fn;
333
- }
334
- });
335
- }
336
- var normalizeProp = createNormalizer((v) => v);
337
-
338
- // ../../utilities/core/dist/index.mjs
339
- var pipe2 = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
340
- var platform = (v) => isDom() && v.test(navigator.platform);
341
- var ua = (v) => isDom() && v.test(navigator.userAgent);
342
- var isDom = () => !!(typeof window !== "undefined");
343
- var isMac = () => platform(/^Mac/);
344
- var isIPhone = () => platform(/^iPhone/);
345
- var isIPad = () => platform(/^iPad/) || isMac() && navigator.maxTouchPoints > 1;
346
- var isIos = () => isIPhone() || isIPad();
347
- var isSafari = () => ua(/^((?!chrome|android).)*safari/i);
348
- var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
349
- var isLeftClick = (v) => v.button === 0;
350
- var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
351
-
352
243
  // src/number-input.dom.ts
353
244
  var dom = {
354
245
  getDoc: (ctx) => {
@@ -408,7 +299,7 @@ var dom = {
408
299
  const win = dom.getWin(ctx);
409
300
  const width = win.innerWidth;
410
301
  const half = roundToDevicePixel(7.5);
411
- point.x = wrap2(point.x + half, width) - half;
302
+ point.x = wrap(point.x + half, width) - half;
412
303
  return { hint, point };
413
304
  },
414
305
  createVirtualCursor(ctx) {
@@ -481,16 +372,18 @@ var utils = {
481
372
  };
482
373
 
483
374
  // src/number-input.connect.ts
484
- function connect(state, send, normalize = normalizeProp) {
375
+ function connect(state, send, normalize) {
485
376
  const isFocused = state.hasTag("focus");
486
377
  const isInvalid = state.context.isOutOfRange || !!state.context.invalid;
487
378
  const isDisabled = !!state.context.disabled;
379
+ const isValueEmpty = state.context.isValueEmpty;
488
380
  const isIncrementDisabled = isDisabled || !state.context.canIncrement;
489
381
  const isDecrementDisabled = isDisabled || !state.context.canDecrement;
490
382
  const messages = state.context.messages;
491
383
  return {
492
384
  isFocused,
493
385
  isInvalid,
386
+ isValueEmpty,
494
387
  value: state.context.formattedValue,
495
388
  valueAsNumber: state.context.valueAsNumber,
496
389
  setValue(value) {
@@ -670,6 +563,15 @@ function connect(state, send, normalize = normalizeProp) {
670
563
 
671
564
  // src/number-input.machine.ts
672
565
  var import_core = require("@zag-js/core");
566
+
567
+ // ../../utilities/core/dist/index.mjs
568
+ var callAll2 = (...fns) => (...a) => {
569
+ fns.forEach(function(fn) {
570
+ fn == null ? void 0 : fn(...a);
571
+ });
572
+ };
573
+
574
+ // src/number-input.machine.ts
673
575
  var { not, and } = import_core.guards;
674
576
  function machine(ctx = {}) {
675
577
  return (0, import_core.createMachine)({
@@ -702,6 +604,7 @@ function machine(ctx = {}) {
702
604
  isAtMin: (ctx2) => isAtMin(ctx2.value, ctx2),
703
605
  isAtMax: (ctx2) => isAtMax(ctx2.value, ctx2),
704
606
  isOutOfRange: (ctx2) => !isWithinRange(ctx2.value, ctx2),
607
+ isValueEmpty: (ctx2) => ctx2.value === "",
705
608
  canIncrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMax,
706
609
  canDecrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMin,
707
610
  valueText: (ctx2) => {
@@ -788,7 +691,7 @@ function machine(ctx = {}) {
788
691
  actions: ["clearValue", "clearHint"]
789
692
  },
790
693
  {
791
- guard: and("clampOnBlur", not("isInRange")),
694
+ guard: and("clampOnBlur", not("isInRange"), not("isEmptyValue")),
792
695
  target: "idle",
793
696
  actions: ["clampValue", "clearHint"]
794
697
  },
@@ -878,6 +781,7 @@ function machine(ctx = {}) {
878
781
  var _a;
879
782
  return ((_a = evt.hint) != null ? _a : ctx2.hint) === "decrement";
880
783
  },
784
+ isEmptyValue: (ctx2) => ctx2.isValueEmpty,
881
785
  isIncrementHint: (ctx2, evt) => {
882
786
  var _a;
883
787
  return ((_a = evt.hint) != null ? _a : ctx2.hint) === "increment";
@@ -937,7 +841,7 @@ function machine(ctx = {}) {
937
841
  function onMouseup() {
938
842
  send("POINTER_UP_SCRUBBER");
939
843
  }
940
- return pipe2(addDomEvent(doc, "mousemove", onMousemove, false), addDomEvent(doc, "mouseup", onMouseup, false));
844
+ return callAll2(addDomEvent(doc, "mousemove", onMousemove, false), addDomEvent(doc, "mouseup", onMouseup, false));
941
845
  }
942
846
  },
943
847
  actions: {
@@ -971,7 +875,7 @@ function machine(ctx = {}) {
971
875
  setValue(ctx2, evt) {
972
876
  var _a, _b;
973
877
  const value = (_b = (_a = evt.target) == null ? void 0 : _a.value) != null ? _b : evt.value;
974
- ctx2.value = utils.sanitize(ctx2, utils.parse(ctx2, value));
878
+ ctx2.value = utils.sanitize(ctx2, utils.parse(ctx2, value.toString()));
975
879
  },
976
880
  clearValue(ctx2) {
977
881
  ctx2.value = "";
@@ -1056,4 +960,3 @@ function machine(ctx = {}) {
1056
960
  hookSync: true
1057
961
  });
1058
962
  }
1059
- //# sourceMappingURL=index.js.map