@zag-js/number-input 0.1.9 → 0.1.12

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
@@ -19,103 +19,95 @@ var __spreadValues = (a, b) => {
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
20
 
21
21
  // ../../utilities/dom/dist/index.mjs
22
+ var __defProp2 = Object.defineProperty;
23
+ var __getOwnPropSymbols2 = Object.getOwnPropertySymbols;
24
+ var __hasOwnProp2 = Object.prototype.hasOwnProperty;
25
+ var __propIsEnum2 = Object.prototype.propertyIsEnumerable;
26
+ var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
27
+ var __spreadValues2 = (a, b) => {
28
+ for (var prop in b || (b = {}))
29
+ if (__hasOwnProp2.call(b, prop))
30
+ __defNormalProp2(a, prop, b[prop]);
31
+ if (__getOwnPropSymbols2)
32
+ for (var prop of __getOwnPropSymbols2(b)) {
33
+ if (__propIsEnum2.call(b, prop))
34
+ __defNormalProp2(a, prop, b[prop]);
35
+ }
36
+ return a;
37
+ };
22
38
  var dataAttr = (guard) => {
23
39
  return guard ? "" : void 0;
24
40
  };
25
41
  var ariaAttr = (guard) => {
26
42
  return guard ? "true" : void 0;
27
43
  };
28
- function raf(fn) {
29
- const id = globalThis.requestAnimationFrame(fn);
30
- return function cleanup() {
31
- globalThis.cancelAnimationFrame(id);
32
- };
33
- }
34
- var isDom = () => typeof window !== "undefined";
35
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
36
- var isTouchDevice = isDom() && !!navigator.maxTouchPoints;
44
+ var MAX_Z_INDEX = 2147483647;
37
45
  var runIfFn = (v, ...a) => {
38
46
  const res = typeof v === "function" ? v(...a) : v;
39
47
  return res != null ? res : void 0;
40
48
  };
41
- var noop = () => {
49
+ var callAll = (...fns) => (...a) => {
50
+ fns.forEach(function(fn) {
51
+ fn == null ? void 0 : fn(...a);
52
+ });
42
53
  };
43
- var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
44
- function getListenerElements() {
45
- ;
46
- globalThis.__listenerElements__ = globalThis.__listenerElements__ || /* @__PURE__ */ new Map();
47
- return globalThis.__listenerElements__;
54
+ var isArray = (v) => Array.isArray(v);
55
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
56
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
57
+ var isDom = () => typeof window !== "undefined";
58
+ function getPlatform() {
59
+ var _a;
60
+ const agent = navigator.userAgentData;
61
+ return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
62
+ }
63
+ var pt = (v) => isDom() && v.test(getPlatform());
64
+ var vn = (v) => isDom() && v.test(navigator.vendor);
65
+ var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
66
+ var isMac = () => pt(/^Mac/) && !isTouchDevice;
67
+ var isSafari = () => isApple() && vn(/apple/i);
68
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
69
+ var isIos = () => isApple() && !isMac();
70
+ function isDocument(el) {
71
+ return el.nodeType === Node.DOCUMENT_NODE;
48
72
  }
49
- function getListenerCache() {
50
- ;
51
- globalThis.__listenerCache__ = globalThis.__listenerCache__ || /* @__PURE__ */ new Map();
52
- return globalThis.__listenerCache__;
73
+ function isWindow(value) {
74
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
53
75
  }
54
- function addGlobalEventListener(node, type, handler, options) {
76
+ function getDocument(el) {
55
77
  var _a;
56
- if (!node)
57
- return noop;
58
- const hash = JSON.stringify({ type, options });
59
- const listenerElements = getListenerElements();
60
- const listenerCache = getListenerCache();
61
- const group = listenerElements.get(node);
62
- if (!listenerElements.has(node)) {
63
- const group2 = /* @__PURE__ */ new Map([[hash, /* @__PURE__ */ new Set([handler])]]);
64
- listenerElements.set(node, group2);
65
- } else if (group == null ? void 0 : group.has(hash)) {
66
- (_a = group == null ? void 0 : group.get(hash)) == null ? void 0 : _a.add(handler);
67
- } else {
68
- group == null ? void 0 : group.set(hash, /* @__PURE__ */ new Set([handler]));
69
- }
70
- function attach(node2) {
71
- var _a2, _b;
72
- function listener(event) {
73
- var _a3;
74
- const group2 = listenerElements.get(node2);
75
- (_a3 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a3.forEach((fn) => fn(event));
76
- }
77
- if (!(listenerCache == null ? void 0 : listenerCache.has(node2))) {
78
- listenerCache.set(node2, /* @__PURE__ */ new Map([[hash, listener]]));
79
- node2.addEventListener(type, listener, options);
80
- return;
81
- }
82
- if (!((_a2 = listenerCache == null ? void 0 : listenerCache.get(node2)) == null ? void 0 : _a2.has(hash))) {
83
- (_b = listenerCache.get(node2)) == null ? void 0 : _b.set(hash, listener);
84
- node2.addEventListener(type, listener, options);
85
- }
86
- }
87
- attach(node);
88
- return function remove() {
89
- var _a2, _b, _c, _d;
90
- if (!listenerElements.has(node))
91
- return;
92
- const group2 = listenerElements.get(node);
93
- (_a2 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a2.delete(handler);
94
- if (((_b = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _b.size) === 0) {
95
- const listener = (_c = listenerCache.get(node)) == null ? void 0 : _c.get(hash);
96
- node.removeEventListener(type, listener, options);
97
- group2 == null ? void 0 : group2.delete(hash);
98
- (_d = listenerCache.get(node)) == null ? void 0 : _d.delete(hash);
99
- if ((group2 == null ? void 0 : group2.size) === 0) {
100
- listenerElements.delete(node);
101
- listenerCache.delete(node);
102
- }
103
- }
104
- };
78
+ if (isWindow(el))
79
+ return el.document;
80
+ if (isDocument(el))
81
+ return el;
82
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
105
83
  }
106
- var isRef = (v) => hasProp(v, "current");
107
- function addDomEvent(target, event, listener, options) {
108
- const node = isRef(target) ? target.current : runIfFn(target);
109
- return addGlobalEventListener(node, event, listener, options);
84
+ function defineDomHelpers(helpers) {
85
+ const dom2 = {
86
+ getRootNode: (ctx) => {
87
+ var _a, _b;
88
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
89
+ },
90
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
91
+ getWin: (ctx) => {
92
+ var _a;
93
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
94
+ },
95
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
96
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
97
+ };
98
+ return __spreadValues2(__spreadValues2({}, dom2), helpers);
110
99
  }
111
- var MAX_Z_INDEX = 2147483647;
112
100
  function getNativeEvent(e) {
113
101
  var _a;
114
102
  return (_a = e.nativeEvent) != null ? _a : e;
115
103
  }
104
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
105
+ var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
106
+ var isLeftClick = (v) => v.button === 0;
107
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
116
108
  function observeAttributes(node, attributes, fn) {
117
109
  if (!node)
118
- return noop;
110
+ return;
119
111
  const attrs = Array.isArray(attributes) ? attributes : [attributes];
120
112
  const win = node.ownerDocument.defaultView || window;
121
113
  const obs = new win.MutationObserver((changes) => {
@@ -128,6 +120,17 @@ function observeAttributes(node, attributes, fn) {
128
120
  obs.observe(node, { attributes: true, attributeFilter: attrs });
129
121
  return () => obs.disconnect();
130
122
  }
123
+ var fallback = {
124
+ pageX: 0,
125
+ pageY: 0,
126
+ clientX: 0,
127
+ clientY: 0
128
+ };
129
+ function getEventPoint(event, type = "page") {
130
+ var _a, _b;
131
+ const point = isTouchEvent(event) ? (_b = (_a = event.touches[0]) != null ? _a : event.changedTouches[0]) != null ? _b : fallback : event;
132
+ return { x: point[`${type}X`], y: point[`${type}Y`] };
133
+ }
131
134
  var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
132
135
  var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
133
136
  function getEventStep(event) {
@@ -139,29 +142,19 @@ function getEventStep(event) {
139
142
  return isSkipKey ? 10 : 1;
140
143
  }
141
144
  }
142
- function itemById(v, id) {
143
- return v.find((node) => node.id === id);
144
- }
145
- function indexOfId(v, id) {
146
- const item = itemById(v, id);
147
- return item ? v.indexOf(item) : -1;
145
+ var isRef = (v) => hasProp(v, "current");
146
+ function addDomEvent(target, eventName, handler, options) {
147
+ const node = isRef(target) ? target.current : runIfFn(target);
148
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
149
+ return () => {
150
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
151
+ };
148
152
  }
149
- var getValueText = (item) => {
150
- var _a, _b;
151
- return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
152
- };
153
- var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
154
- var wrap = (v, idx) => {
155
- return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
156
- };
157
- function findByText(v, text, currentId) {
158
- const index = currentId ? indexOfId(v, currentId) : -1;
159
- let items = currentId ? wrap(v, index) : v;
160
- const isSingleKey = text.length === 1;
161
- if (isSingleKey) {
162
- items = items.filter((item) => item.id !== currentId);
163
- }
164
- return items.find((item) => match(getValueText(item), text));
153
+ function raf(fn) {
154
+ const id = globalThis.requestAnimationFrame(fn);
155
+ return function cleanup() {
156
+ globalThis.cancelAnimationFrame(id);
157
+ };
165
158
  }
166
159
  function addPointerlockChangeListener(doc, fn) {
167
160
  return addDomEvent(doc, "pointerlockchange", fn, false);
@@ -195,7 +188,7 @@ function requestPointerLock(doc, handlers = {}) {
195
188
  if (!supported)
196
189
  return;
197
190
  body.requestPointerLock();
198
- const cleanup = pipe(addPointerlockChangeListener(doc, onPointerChange), addPointerlockErrorListener(doc, onPointerError));
191
+ const cleanup = callAll(addPointerlockChangeListener(doc, onPointerChange), addPointerlockErrorListener(doc, onPointerError));
199
192
  return function dispose() {
200
193
  if (!supported)
201
194
  return;
@@ -203,38 +196,10 @@ function requestPointerLock(doc, handlers = {}) {
203
196
  exit();
204
197
  };
205
198
  }
206
- function findByTypeahead(_items, options) {
207
- const { state: state2, activeId, key, timeout = 350 } = options;
208
- const search = state2.keysSoFar + key;
209
- const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
210
- const query2 = isRepeated ? search[0] : search;
211
- let items = _items.slice();
212
- const next = findByText(items, query2, activeId);
213
- function cleanup() {
214
- clearTimeout(state2.timer);
215
- state2.timer = -1;
216
- }
217
- function update(value) {
218
- state2.keysSoFar = value;
219
- cleanup();
220
- if (value !== "") {
221
- state2.timer = +setTimeout(() => {
222
- update("");
223
- cleanup();
224
- }, timeout);
225
- }
226
- }
227
- update(search);
228
- return next;
229
- }
230
- findByTypeahead.defaultOptions = {
231
- keysSoFar: "",
232
- timer: -1
233
- };
234
199
 
235
200
  // ../../utilities/number/dist/index.mjs
236
201
  var __pow = Math.pow;
237
- function wrap2(num, max) {
202
+ function wrap(num, max) {
238
203
  return (num % max + max) % max;
239
204
  }
240
205
  function roundToDevicePixel(num) {
@@ -297,91 +262,37 @@ function decimalOperation(a, op, b) {
297
262
  }
298
263
  var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
299
264
 
300
- // ../../utilities/rect/dist/index.mjs
301
- var isDom2 = () => typeof window !== "undefined";
302
- var isArray = (v) => Array.isArray(v);
303
- var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
304
- var hasProp2 = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
305
- var isTouchDevice2 = isDom2() && !!navigator.maxTouchPoints;
306
- var isTouchEvent = (v) => isObject(v) && hasProp2(v, "touches");
307
- var fallback = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
308
- function getEventPoint(e, t = "page") {
309
- const p = isTouchEvent(e) ? e.touches[0] || e.changedTouches[0] || fallback : e;
310
- return { x: p[`${t}X`], y: p[`${t}Y`] };
311
- }
312
-
313
- // ../../types/dist/index.mjs
314
- function createNormalizer(fn) {
315
- return new Proxy({}, {
316
- get() {
317
- return fn;
318
- }
319
- });
320
- }
321
- var normalizeProp = createNormalizer((v) => v);
322
-
323
- // ../../utilities/core/dist/index.mjs
324
- var isDom3 = () => typeof window !== "undefined";
325
- function getPlatform() {
326
- var _a;
327
- const agent = navigator.userAgentData;
328
- return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
329
- }
330
- var pt = (v) => isDom3() && v.test(getPlatform());
331
- var vn = (v) => isDom3() && v.test(navigator.vendor);
332
- var isTouchDevice3 = isDom3() && !!navigator.maxTouchPoints;
333
- var isMac = () => pt(/^Mac/) && !isTouchDevice3;
334
- var isSafari = () => isApple() && vn(/apple/i);
335
- var isApple = () => pt(/mac|iphone|ipad|ipod/i);
336
- var isIos = () => isApple() && !isMac();
337
- var supportsPointerEvent = () => isDom3() && window.onpointerdown === null;
338
- var isLeftClick = (v) => v.button === 0;
339
- var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
340
- var pipe2 = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
341
-
342
265
  // src/number-input.dom.ts
343
- var dom = {
344
- getDoc: (ctx) => {
345
- var _a;
346
- return (_a = ctx.doc) != null ? _a : document;
347
- },
348
- getWin: (ctx) => {
349
- var _a;
350
- return (_a = dom.getDoc(ctx).defaultView) != null ? _a : window;
351
- },
352
- getRootNode: (ctx) => {
353
- var _a;
354
- return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
355
- },
266
+ var dom = defineDomHelpers({
356
267
  getRootId: (ctx) => {
357
268
  var _a, _b;
358
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `number-input:${ctx.uid}`;
269
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `number-input:${ctx.id}`;
359
270
  },
360
271
  getInputId: (ctx) => {
361
272
  var _a, _b;
362
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.input) != null ? _b : `number-input:${ctx.uid}:input`;
273
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.input) != null ? _b : `number-input:${ctx.id}:input`;
363
274
  },
364
275
  getIncButtonId: (ctx) => {
365
276
  var _a, _b;
366
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.incBtn) != null ? _b : `number-input:${ctx.uid}:inc-btn`;
277
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.incBtn) != null ? _b : `number-input:${ctx.id}:inc-btn`;
367
278
  },
368
279
  getDecButtonId: (ctx) => {
369
280
  var _a, _b;
370
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.decBtn) != null ? _b : `number-input:${ctx.uid}:dec-btn`;
281
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.decBtn) != null ? _b : `number-input:${ctx.id}:dec-btn`;
371
282
  },
372
283
  getScrubberId: (ctx) => {
373
284
  var _a, _b;
374
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.scrubber) != null ? _b : `number-input:${ctx.uid}:scrubber`;
285
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.scrubber) != null ? _b : `number-input:${ctx.id}:scrubber`;
375
286
  },
376
- getCursorId: (ctx) => `number-input:${ctx.uid}:cursor`,
287
+ getCursorId: (ctx) => `number-input:${ctx.id}:cursor`,
377
288
  getLabelId: (ctx) => {
378
289
  var _a, _b;
379
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `number-input:${ctx.uid}:label`;
290
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `number-input:${ctx.id}:label`;
380
291
  },
381
- getInputEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getInputId(ctx)),
382
- getIncButtonEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getIncButtonId(ctx)),
383
- getDecButtonEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getDecButtonId(ctx)),
384
- getScrubberEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getScrubberId(ctx)),
292
+ getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)),
293
+ getIncButtonEl: (ctx) => dom.getById(ctx, dom.getIncButtonId(ctx)),
294
+ getDecButtonEl: (ctx) => dom.getById(ctx, dom.getDecButtonId(ctx)),
295
+ getScrubberEl: (ctx) => dom.getById(ctx, dom.getScrubberId(ctx)),
385
296
  getCursorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getCursorId(ctx)),
386
297
  getMousementValue(ctx, event) {
387
298
  const x = roundToDevicePixel(event.movementX);
@@ -398,7 +309,7 @@ var dom = {
398
309
  const win = dom.getWin(ctx);
399
310
  const width = win.innerWidth;
400
311
  const half = roundToDevicePixel(7.5);
401
- point.x = wrap2(point.x + half, width) - half;
312
+ point.x = wrap(point.x + half, width) - half;
402
313
  return { hint, point };
403
314
  },
404
315
  createVirtualCursor(ctx) {
@@ -426,7 +337,7 @@ var dom = {
426
337
  </svg>`;
427
338
  doc.body.appendChild(el);
428
339
  }
429
- };
340
+ });
430
341
 
431
342
  // src/number-input.utils.ts
432
343
  var utils = {
@@ -471,16 +382,18 @@ var utils = {
471
382
  };
472
383
 
473
384
  // src/number-input.connect.ts
474
- function connect(state, send, normalize = normalizeProp) {
385
+ function connect(state, send, normalize) {
475
386
  const isFocused = state.hasTag("focus");
476
387
  const isInvalid = state.context.isOutOfRange || !!state.context.invalid;
477
388
  const isDisabled = !!state.context.disabled;
389
+ const isValueEmpty = state.context.isValueEmpty;
478
390
  const isIncrementDisabled = isDisabled || !state.context.canIncrement;
479
391
  const isDecrementDisabled = isDisabled || !state.context.canDecrement;
480
392
  const messages = state.context.messages;
481
393
  return {
482
394
  isFocused,
483
395
  isInvalid,
396
+ isValueEmpty,
484
397
  value: state.context.formattedValue,
485
398
  valueAsNumber: state.context.valueAsNumber,
486
399
  setValue(value) {
@@ -659,14 +572,22 @@ function connect(state, send, normalize = normalizeProp) {
659
572
  }
660
573
 
661
574
  // src/number-input.machine.ts
662
- import { choose, createMachine, guards, ref } from "@zag-js/core";
575
+ import { choose, createMachine, guards } from "@zag-js/core";
576
+
577
+ // ../../utilities/core/dist/index.mjs
578
+ var callAll2 = (...fns) => (...a) => {
579
+ fns.forEach(function(fn) {
580
+ fn == null ? void 0 : fn(...a);
581
+ });
582
+ };
583
+
584
+ // src/number-input.machine.ts
663
585
  var { not, and } = guards;
664
- function machine(ctx = {}) {
586
+ function machine(ctx) {
665
587
  return createMachine({
666
588
  id: "number-input",
667
589
  initial: "unknown",
668
590
  context: __spreadProps(__spreadValues({
669
- uid: "",
670
591
  dir: "ltr",
671
592
  focusInputOnChange: true,
672
593
  clampValueOnBlur: true,
@@ -692,6 +613,7 @@ function machine(ctx = {}) {
692
613
  isAtMin: (ctx2) => isAtMin(ctx2.value, ctx2),
693
614
  isAtMax: (ctx2) => isAtMax(ctx2.value, ctx2),
694
615
  isOutOfRange: (ctx2) => !isWithinRange(ctx2.value, ctx2),
616
+ isValueEmpty: (ctx2) => ctx2.value === "",
695
617
  canIncrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMax,
696
618
  canDecrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMin,
697
619
  valueText: (ctx2) => {
@@ -726,7 +648,7 @@ function machine(ctx = {}) {
726
648
  on: {
727
649
  SETUP: {
728
650
  target: "idle",
729
- actions: ["setupDocument", "syncInputValue"]
651
+ actions: "syncInputValue"
730
652
  }
731
653
  }
732
654
  },
@@ -778,7 +700,7 @@ function machine(ctx = {}) {
778
700
  actions: ["clearValue", "clearHint"]
779
701
  },
780
702
  {
781
- guard: and("clampOnBlur", not("isInRange")),
703
+ guard: and("clampOnBlur", not("isInRange"), not("isEmptyValue")),
782
704
  target: "idle",
783
705
  actions: ["clampValue", "clearHint"]
784
706
  },
@@ -868,6 +790,7 @@ function machine(ctx = {}) {
868
790
  var _a;
869
791
  return ((_a = evt.hint) != null ? _a : ctx2.hint) === "decrement";
870
792
  },
793
+ isEmptyValue: (ctx2) => ctx2.isValueEmpty,
871
794
  isIncrementHint: (ctx2, evt) => {
872
795
  var _a;
873
796
  return ((_a = evt.hint) != null ? _a : ctx2.hint) === "increment";
@@ -927,17 +850,10 @@ function machine(ctx = {}) {
927
850
  function onMouseup() {
928
851
  send("POINTER_UP_SCRUBBER");
929
852
  }
930
- return pipe2(addDomEvent(doc, "mousemove", onMousemove, false), addDomEvent(doc, "mouseup", onMouseup, false));
853
+ return callAll2(addDomEvent(doc, "mousemove", onMousemove, false), addDomEvent(doc, "mouseup", onMouseup, false));
931
854
  }
932
855
  },
933
856
  actions: {
934
- setupDocument: (ctx2, evt) => {
935
- if (evt.doc)
936
- ctx2.doc = ref(evt.doc);
937
- if (evt.root)
938
- ctx2.rootNode = ref(evt.root);
939
- ctx2.uid = evt.id;
940
- },
941
857
  focusInput(ctx2) {
942
858
  if (!ctx2.focusInputOnChange)
943
859
  return;
@@ -961,7 +877,7 @@ function machine(ctx = {}) {
961
877
  setValue(ctx2, evt) {
962
878
  var _a, _b;
963
879
  const value = (_b = (_a = evt.target) == null ? void 0 : _a.value) != null ? _b : evt.value;
964
- ctx2.value = utils.sanitize(ctx2, utils.parse(ctx2, value));
880
+ ctx2.value = utils.sanitize(ctx2, utils.parse(ctx2, value.toString()));
965
881
  },
966
882
  clearValue(ctx2) {
967
883
  ctx2.value = "";
@@ -1050,4 +966,3 @@ export {
1050
966
  connect,
1051
967
  machine
1052
968
  };
1053
- //# sourceMappingURL=index.mjs.map
@@ -1,8 +1,9 @@
1
- import { PropTypes, ReactPropTypes } from "@zag-js/types";
2
- import { Send, State } from "./number-input.types";
3
- export declare function connect<T extends PropTypes = ReactPropTypes>(state: State, send: Send, normalize?: import("@zag-js/types").NormalizeProps): {
1
+ import type { NormalizeProps, PropTypes } from "@zag-js/types";
2
+ import type { Send, State } from "./number-input.types";
3
+ export declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
4
4
  isFocused: boolean;
5
5
  isInvalid: boolean;
6
+ isValueEmpty: boolean;
6
7
  value: string;
7
8
  valueAsNumber: number;
8
9
  setValue(value: string | number): void;
@@ -20,4 +21,3 @@ export declare function connect<T extends PropTypes = ReactPropTypes>(state: Sta
20
21
  incrementButtonProps: T["button"];
21
22
  scrubberProps: T["element"];
22
23
  };
23
- //# sourceMappingURL=number-input.connect.d.ts.map
@@ -1,8 +1,21 @@
1
- import { MachineContext as Ctx } from "./number-input.types";
1
+ import type { MachineContext as Ctx } from "./number-input.types";
2
2
  export declare const dom: {
3
- getDoc: (ctx: Ctx) => Document;
4
- getWin: (ctx: Ctx) => Window & typeof globalThis;
5
- getRootNode: (ctx: Ctx) => Document | ShadowRoot;
3
+ getRootNode: (ctx: {
4
+ getRootNode?: () => Node | Document | ShadowRoot;
5
+ }) => Document | ShadowRoot;
6
+ getDoc: (ctx: {
7
+ getRootNode?: () => Node | Document | ShadowRoot;
8
+ }) => Document;
9
+ getWin: (ctx: {
10
+ getRootNode?: () => Node | Document | ShadowRoot;
11
+ }) => Window & typeof globalThis;
12
+ getActiveElement: (ctx: {
13
+ getRootNode?: () => Node | Document | ShadowRoot;
14
+ }) => HTMLElement;
15
+ getById: <T_1 = HTMLElement>(ctx: {
16
+ getRootNode?: () => Node | Document | ShadowRoot;
17
+ }, id: string) => T_1;
18
+ } & {
6
19
  getRootId: (ctx: Ctx) => string;
7
20
  getInputId: (ctx: Ctx) => string;
8
21
  getIncButtonId: (ctx: Ctx) => string;
@@ -10,7 +23,7 @@ export declare const dom: {
10
23
  getScrubberId: (ctx: Ctx) => string;
11
24
  getCursorId: (ctx: Ctx) => string;
12
25
  getLabelId: (ctx: Ctx) => string;
13
- getInputEl: (ctx: Ctx) => HTMLInputElement;
26
+ getInputEl: (ctx: Ctx) => HTMLElement;
14
27
  getIncButtonEl: (ctx: Ctx) => HTMLButtonElement;
15
28
  getDecButtonEl: (ctx: Ctx) => HTMLButtonElement;
16
29
  getScrubberEl: (ctx: Ctx) => HTMLElement;
@@ -24,4 +37,3 @@ export declare const dom: {
24
37
  };
25
38
  createVirtualCursor(ctx: Ctx): void;
26
39
  };
27
- //# sourceMappingURL=number-input.dom.d.ts.map
@@ -1,3 +1,2 @@
1
1
  import type { MachineContext, MachineState, UserDefinedContext } from "./number-input.types";
2
- export declare function machine(ctx?: UserDefinedContext): import("@zag-js/core").Machine<MachineContext, MachineState, import("@zag-js/core").StateMachine.AnyEventObject>;
3
- //# sourceMappingURL=number-input.machine.d.ts.map
2
+ export declare function machine(ctx: UserDefinedContext): import("@zag-js/core").Machine<MachineContext, MachineState, import("@zag-js/core").StateMachine.AnyEventObject>;
@@ -1,7 +1,5 @@
1
1
  import type { StateMachine as S } from "@zag-js/core";
2
- import { FormatDecimalOptions } from "@zag-js/number-utils";
3
- import type { Point } from "@zag-js/rect-utils";
4
- import type { Context, DirectionProperty } from "@zag-js/types";
2
+ import type { CommonProperties, Context, DirectionProperty, RequiredBy } from "@zag-js/types";
5
3
  declare type ValidityState = "rangeUnderflow" | "rangeOverflow";
6
4
  declare type ElementIds = Partial<{
7
5
  root: string;
@@ -26,7 +24,7 @@ declare type IntlMessages = {
26
24
  */
27
25
  decrementLabel: string;
28
26
  };
29
- declare type PublicContext = DirectionProperty & FormatDecimalOptions & {
27
+ declare type PublicContext = DirectionProperty & CommonProperties & {
30
28
  /**
31
29
  * The ids of the elements in the number input. Useful for composition.
32
30
  */
@@ -127,8 +125,16 @@ declare type PublicContext = DirectionProperty & FormatDecimalOptions & {
127
125
  value: string;
128
126
  valueAsNumber: number;
129
127
  }) => void;
128
+ /**
129
+ * The minimum number of fraction digits to use. Possible values are from 0 to 20
130
+ */
131
+ minFractionDigits?: number;
132
+ /**
133
+ * The maximum number of fraction digits to use. Possible values are from 0 to 20;
134
+ */
135
+ maxFractionDigits?: number;
130
136
  };
131
- export declare type UserDefinedContext = Partial<PublicContext>;
137
+ export declare type UserDefinedContext = RequiredBy<PublicContext, "id">;
132
138
  declare type ComputedContext = Readonly<{
133
139
  /**
134
140
  * @computed
@@ -150,6 +156,11 @@ declare type ComputedContext = Readonly<{
150
156
  * Whether the value is out of the min/max range
151
157
  */
152
158
  isOutOfRange: boolean;
159
+ /**
160
+ * @computed
161
+ * Whether the value is empty
162
+ */
163
+ isValueEmpty: boolean;
153
164
  /**
154
165
  * @computed
155
166
  * Whether the increment button is enabled
@@ -186,7 +197,10 @@ declare type PrivateContext = Context<{
186
197
  * @internal
187
198
  * The scrubber cursor position
188
199
  */
189
- scrubberCursorPoint: Point | null;
200
+ scrubberCursorPoint: {
201
+ x: number;
202
+ y: number;
203
+ } | null;
190
204
  }>;
191
205
  export declare type MachineContext = PublicContext & PrivateContext & ComputedContext;
192
206
  export declare type MachineState = {
@@ -196,4 +210,3 @@ export declare type MachineState = {
196
210
  export declare type State = S.State<MachineContext, MachineState>;
197
211
  export declare type Send = S.Send<S.AnyEventObject>;
198
212
  export {};
199
- //# sourceMappingURL=number-input.types.d.ts.map
@@ -1,7 +1,7 @@
1
- import type { KeyboardEvent } from "react";
1
+ import type { JSX } from "@zag-js/types";
2
2
  import type { MachineContext as Ctx } from "./number-input.types";
3
3
  export declare const utils: {
4
- isValidNumericEvent: (ctx: Ctx, event: KeyboardEvent) => boolean;
4
+ isValidNumericEvent: (ctx: Ctx, event: JSX.KeyboardEvent) => boolean;
5
5
  isFloatingPoint: (v: string) => boolean;
6
6
  sanitize: (ctx: Ctx, value: string) => string;
7
7
  increment: (ctx: Ctx, step?: number) => string;
@@ -11,4 +11,3 @@ export declare const utils: {
11
11
  format: (ctx: Ctx, value: string | number) => string | number;
12
12
  round: (ctx: Ctx) => string;
13
13
  };
14
- //# sourceMappingURL=number-input.utils.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/number-input",
3
- "version": "0.1.9",
3
+ "version": "0.1.12",
4
4
  "description": "Core logic for the number-input widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -29,11 +29,12 @@
29
29
  "url": "https://github.com/chakra-ui/zag/issues"
30
30
  },
31
31
  "dependencies": {
32
- "@zag-js/core": "0.1.6",
33
- "@zag-js/dom-utils": "0.1.4",
32
+ "@zag-js/core": "0.1.8",
33
+ "@zag-js/types": "0.2.2"
34
+ },
35
+ "devDependencies": {
36
+ "@zag-js/dom-utils": "0.1.7",
34
37
  "@zag-js/number-utils": "0.1.2",
35
- "@zag-js/rect-utils": "0.1.2",
36
- "@zag-js/types": "0.1.2",
37
38
  "@zag-js/utils": "0.1.2"
38
39
  },
39
40
  "scripts": {