@zag-js/number-input 0.0.0-dev-20220514161650 → 0.0.0-dev-20220515163947

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
@@ -45,19 +45,9 @@ module.exports = __toCommonJS(src_exports);
45
45
  var dataAttr = (guard) => {
46
46
  return guard ? "" : void 0;
47
47
  };
48
- function nextTick(fn) {
49
- const set = /* @__PURE__ */ new Set();
50
- function raf2(fn2) {
51
- const id = globalThis.requestAnimationFrame(fn2);
52
- set.add(() => globalThis.cancelAnimationFrame(id));
53
- }
54
- raf2(() => raf2(fn));
55
- return function cleanup() {
56
- set.forEach(function(fn2) {
57
- fn2();
58
- });
59
- };
60
- }
48
+ var ariaAttr = (guard) => {
49
+ return guard ? "true" : void 0;
50
+ };
61
51
  function raf(fn) {
62
52
  const id = globalThis.requestAnimationFrame(fn);
63
53
  return function cleanup() {
@@ -261,40 +251,11 @@ findByTypeahead.defaultOptions = {
261
251
  };
262
252
 
263
253
  // ../../utilities/number/dist/index.mjs
264
- var __defProp2 = Object.defineProperty;
265
- var __defProps2 = Object.defineProperties;
266
- var __getOwnPropDescs2 = Object.getOwnPropertyDescriptors;
267
- var __getOwnPropSymbols2 = Object.getOwnPropertySymbols;
268
- var __hasOwnProp2 = Object.prototype.hasOwnProperty;
269
- var __propIsEnum2 = Object.prototype.propertyIsEnumerable;
270
254
  var __pow = Math.pow;
271
- var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
272
- var __spreadValues2 = (a, b) => {
273
- for (var prop in b || (b = {}))
274
- if (__hasOwnProp2.call(b, prop))
275
- __defNormalProp2(a, prop, b[prop]);
276
- if (__getOwnPropSymbols2)
277
- for (var prop of __getOwnPropSymbols2(b)) {
278
- if (__propIsEnum2.call(b, prop))
279
- __defNormalProp2(a, prop, b[prop]);
280
- }
281
- return a;
282
- };
283
- var __spreadProps2 = (a, b) => __defProps2(a, __getOwnPropDescs2(b));
284
- var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
285
- function formatter(n) {
286
- return parseFloat(nf.format(n));
287
- }
288
255
  function wrap2(num, max) {
289
256
  return (num % max + max) % max;
290
257
  }
291
- function round(v, t2) {
292
- let num = valueOf(v);
293
- const p = __pow(10, t2 != null ? t2 : 10);
294
- num = Math.round(num * p) / p;
295
- return t2 ? num.toFixed(t2) : v.toString();
296
- }
297
- function roundToPx(num) {
258
+ function roundToDevicePixel(num) {
298
259
  if (typeof window === "undefined")
299
260
  return Math.round(num);
300
261
  const dp = window.devicePixelRatio;
@@ -306,30 +267,28 @@ function clamp(v, o) {
306
267
  function countDecimals(value) {
307
268
  if (!Number.isFinite(value))
308
269
  return 0;
309
- let e = 1;
310
- let p = 0;
270
+ let e = 1, p = 0;
311
271
  while (Math.round(value * e) / e !== value) {
312
272
  e *= 10;
313
273
  p += 1;
314
274
  }
315
275
  return p;
316
276
  }
317
- var increment = (v, s) => formatter(valueOf(v) + s);
318
- var decrement = (v, s) => formatter(valueOf(v) - s);
319
- var multiply = (v, s) => formatter(valueOf(v) * s);
277
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
278
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
320
279
  function valueOf(v) {
321
280
  if (typeof v === "number")
322
281
  return v;
323
282
  const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
324
283
  return !Number.isNaN(num) ? num : 0;
325
284
  }
326
- function getMaxPrecision(o) {
327
- var _a;
328
- const stepPrecision = countDecimals(o.step);
329
- return Math.max(stepPrecision, (_a = o.precision) != null ? _a : 0);
330
- }
331
- function roundToPrecision(v, o) {
332
- return round(v, getMaxPrecision(__spreadProps2(__spreadValues2({}, o), { value: v })));
285
+ function formatDecimal(v, o) {
286
+ return new Intl.NumberFormat("en-US", {
287
+ useGrouping: false,
288
+ style: "decimal",
289
+ minimumFractionDigits: o.minFractionDigits,
290
+ maximumFractionDigits: o.maxFractionDigits
291
+ }).format(valueOf(v));
333
292
  }
334
293
  function isAtMax(v, o) {
335
294
  const val = valueOf(v);
@@ -343,6 +302,18 @@ function isWithinRange(v, o) {
343
302
  const val = valueOf(v);
344
303
  return val >= o.min && val <= o.max;
345
304
  }
305
+ function decimalOperation(a, op, b) {
306
+ let result = op === "+" ? a + b : a - b;
307
+ if (a % 1 !== 0 || b % 1 !== 0) {
308
+ const multiplier = __pow(10, Math.max(countDecimals(a), countDecimals(b)));
309
+ a = Math.round(a * multiplier);
310
+ b = Math.round(b * multiplier);
311
+ result = op === "+" ? a + b : a - b;
312
+ result /= multiplier;
313
+ }
314
+ return result;
315
+ }
316
+ var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
346
317
 
347
318
  // ../../utilities/rect/dist/index.mjs
348
319
  var isArray = (v) => Array.isArray(v);
@@ -360,6 +331,19 @@ function createNormalizer(fn) {
360
331
  }
361
332
  var normalizeProp = createNormalizer((v) => v);
362
333
 
334
+ // ../../utilities/core/dist/index.mjs
335
+ var pipe2 = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
336
+ var platform = (v) => isDom() && v.test(navigator.platform);
337
+ var ua = (v) => isDom() && v.test(navigator.userAgent);
338
+ var isDom = () => !!(typeof window !== "undefined");
339
+ var isMac = () => platform(/^Mac/);
340
+ var isIPhone = () => platform(/^iPhone/);
341
+ var isIPad = () => platform(/^iPad/) || isMac() && navigator.maxTouchPoints > 1;
342
+ var isIos = () => isIPhone() || isIPad();
343
+ var isSafari = () => ua(/^((?!chrome|android).)*safari/i);
344
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
345
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
346
+
363
347
  // src/number-input.dom.ts
364
348
  var dom = {
365
349
  getDoc: (ctx) => {
@@ -403,10 +387,10 @@ var dom = {
403
387
  getIncButtonEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getIncButtonId(ctx)),
404
388
  getDecButtonEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getDecButtonId(ctx)),
405
389
  getScrubberEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getScrubberId(ctx)),
406
- getCursorEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getCursorId(ctx)),
390
+ getCursorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getCursorId(ctx)),
407
391
  getMousementValue(ctx, event) {
408
- const x = roundToPx(event.movementX);
409
- const y = roundToPx(event.movementY);
392
+ const x = roundToDevicePixel(event.movementX);
393
+ const y = roundToDevicePixel(event.movementY);
410
394
  let hint = x > 0 ? "increment" : x < 0 ? "decrement" : null;
411
395
  if (ctx.isRtl && hint === "increment")
412
396
  hint = "decrement";
@@ -418,7 +402,7 @@ var dom = {
418
402
  };
419
403
  const win = dom.getWin(ctx);
420
404
  const width = win.innerWidth;
421
- const half = roundToPx(7.5);
405
+ const half = roundToDevicePixel(7.5);
422
406
  point.x = wrap2(point.x + half, width) - half;
423
407
  return { hint, point };
424
408
  },
@@ -449,14 +433,6 @@ var dom = {
449
433
  }
450
434
  };
451
435
 
452
- // ../../utilities/core/dist/index.mjs
453
- var pipe2 = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
454
- var ua = (v) => isDom() && v.test(navigator.userAgent);
455
- var isDom = () => !!(typeof window !== "undefined");
456
- var isSafari = () => ua(/^((?!chrome|android).)*safari/i);
457
- var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
458
- var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
459
-
460
436
  // src/number-input.utils.ts
461
437
  var utils = {
462
438
  isValidNumericEvent: (ctx, event) => {
@@ -475,18 +451,15 @@ var utils = {
475
451
  return value.split("").filter((_a = ctx.validateCharacter) != null ? _a : utils.isFloatingPoint).join("");
476
452
  },
477
453
  increment: (ctx, step) => {
478
- let value = increment(ctx.value, step != null ? step : ctx.step);
479
- value = clamp(value, ctx);
480
- return roundToPrecision(value, ctx);
454
+ const value = increment(ctx.value, step != null ? step : ctx.step);
455
+ return formatDecimal(clamp(value, ctx), ctx);
481
456
  },
482
457
  decrement: (ctx, step) => {
483
- let value = decrement(ctx.value, step != null ? step : ctx.step);
484
- value = clamp(value, ctx);
485
- return roundToPrecision(value, ctx);
458
+ const value = decrement(ctx.value, step != null ? step : ctx.step);
459
+ return formatDecimal(clamp(value, ctx), ctx);
486
460
  },
487
461
  clamp: (ctx) => {
488
- let value = clamp(ctx.value, ctx);
489
- return roundToPrecision(value, ctx);
462
+ return formatDecimal(clamp(ctx.value, ctx), ctx);
490
463
  },
491
464
  parse: (ctx, value) => {
492
465
  var _a, _b;
@@ -498,7 +471,7 @@ var utils = {
498
471
  return (_b = (_a = ctx.format) == null ? void 0 : _a.call(ctx, _val)) != null ? _b : _val;
499
472
  },
500
473
  round: (ctx) => {
501
- return roundToPrecision(ctx.value, ctx);
474
+ return formatDecimal(ctx.value, ctx);
502
475
  }
503
476
  };
504
477
 
@@ -519,7 +492,7 @@ function connect(state, send, normalize = normalizeProp) {
519
492
  send({ type: "SET_VALUE", value: value.toString() });
520
493
  },
521
494
  clearValue() {
522
- send({ type: "SET_VALUE", value: "" });
495
+ send("CLEAR_VALUE");
523
496
  },
524
497
  increment() {
525
498
  send("INCREMENT");
@@ -549,6 +522,14 @@ function connect(state, send, normalize = normalizeProp) {
549
522
  id: dom.getLabelId(state.context),
550
523
  htmlFor: dom.getInputId(state.context)
551
524
  }),
525
+ groupProps: normalize.element({
526
+ "data-part": "group",
527
+ role: "group",
528
+ "aria-disabled": isDisabled,
529
+ "data-disabled": dataAttr(isDisabled),
530
+ "data-invalid": dataAttr(isInvalid),
531
+ "aria-invalid": ariaAttr(state.context.invalid)
532
+ }),
552
533
  inputProps: normalize.input({
553
534
  "data-part": "input",
554
535
  name: state.context.name,
@@ -566,7 +547,7 @@ function connect(state, send, normalize = normalizeProp) {
566
547
  autoCorrect: "off",
567
548
  spellCheck: "false",
568
549
  type: "text",
569
- "aria-roledescription": "number field",
550
+ "aria-roledescription": !isIos() ? "number field" : void 0,
570
551
  "aria-valuemin": state.context.min,
571
552
  "aria-valuemax": state.context.max,
572
553
  "aria-valuenow": isNaN(state.context.valueAsNumber) ? void 0 : state.context.valueAsNumber,
@@ -590,7 +571,7 @@ function connect(state, send, normalize = normalizeProp) {
590
571
  if (!utils.isValidNumericEvent(state.context, event)) {
591
572
  event.preventDefault();
592
573
  }
593
- const step = multiply(getEventStep(event), state.context.step);
574
+ const step = getEventStep(event) * state.context.step;
594
575
  const keyMap = {
595
576
  ArrowUp() {
596
577
  send({ type: "ARROW_UP", step });
@@ -669,8 +650,8 @@ function connect(state, send, normalize = normalizeProp) {
669
650
  const evt = getNativeEvent(event);
670
651
  event.preventDefault();
671
652
  const point = getEventPoint(evt);
672
- point.x = point.x - roundToPx(7.5);
673
- point.y = point.y - roundToPx(7.5);
653
+ point.x = point.x - roundToDevicePixel(7.5);
654
+ point.y = point.y - roundToDevicePixel(7.5);
674
655
  send({ type: "PRESS_DOWN_SCRUBBER", point });
675
656
  },
676
657
  style: {
@@ -700,8 +681,6 @@ function machine(ctx = {}) {
700
681
  step: 1,
701
682
  min: Number.MIN_SAFE_INTEGER,
702
683
  max: Number.MAX_SAFE_INTEGER,
703
- precision: 0,
704
- inputSelection: null,
705
684
  scrubberCursorPoint: null,
706
685
  invalid: false
707
686
  }, ctx), {
@@ -735,8 +714,15 @@ function machine(ctx = {}) {
735
714
  SET_VALUE: {
736
715
  actions: ["setValue", "setHintToSet"]
737
716
  },
738
- INCREMENT: { actions: ["increment"] },
739
- DECREMENT: { actions: ["decrement"] }
717
+ CLEAR_VALUE: {
718
+ actions: ["clearValue"]
719
+ },
720
+ INCREMENT: {
721
+ actions: ["increment"]
722
+ },
723
+ DECREMENT: {
724
+ actions: ["decrement"]
725
+ }
740
726
  },
741
727
  states: {
742
728
  unknown: {
@@ -902,26 +888,23 @@ function machine(ctx = {}) {
902
888
  send("PRESS_UP");
903
889
  });
904
890
  },
905
- attachWheelListener(ctx2) {
906
- const cleanups = [];
907
- cleanups.push(nextTick(() => {
908
- const input = dom.getInputEl(ctx2);
909
- if (!input)
891
+ attachWheelListener(ctx2, _evt, { send }) {
892
+ const input = dom.getInputEl(ctx2);
893
+ if (!input)
894
+ return;
895
+ function onWheel(event) {
896
+ const isInputFocused = dom.getDoc(ctx2).activeElement === input;
897
+ if (!ctx2.allowMouseWheel || !isInputFocused)
910
898
  return;
911
- function onWheel(event) {
912
- const isInputFocused = dom.getDoc(ctx2).activeElement === input;
913
- if (!ctx2.allowMouseWheel || !isInputFocused)
914
- return;
915
- event.preventDefault();
916
- const dir = Math.sign(event.deltaY) * -1;
917
- if (dir === 1)
918
- ctx2.value = utils.increment(ctx2);
919
- else if (dir === -1)
920
- ctx2.value = utils.decrement(ctx2);
899
+ event.preventDefault();
900
+ const dir = Math.sign(event.deltaY) * -1;
901
+ if (dir === 1) {
902
+ send("INCREMENT");
903
+ } else if (dir === -1) {
904
+ send("DECREMENT");
921
905
  }
922
- cleanups.push(addDomEvent(input, "wheel", onWheel, { passive: false }));
923
- }));
924
- return () => cleanups.forEach((c) => c());
906
+ }
907
+ return addDomEvent(input, "wheel", onWheel, { passive: false });
925
908
  },
926
909
  activatePointerLock(ctx2) {
927
910
  if (isSafari() || !supportsPointerEvent())
@@ -936,7 +919,11 @@ function machine(ctx = {}) {
936
919
  const value = dom.getMousementValue(ctx2, event);
937
920
  if (!value.hint)
938
921
  return;
939
- send({ type: "POINTER_MOVE_SCRUBBER", hint: value.hint, point: value.point });
922
+ send({
923
+ type: "POINTER_MOVE_SCRUBBER",
924
+ hint: value.hint,
925
+ point: value.point
926
+ });
940
927
  }
941
928
  function onMouseup() {
942
929
  send("POINTER_UP_SCRUBBER");
@@ -968,7 +955,9 @@ function machine(ctx = {}) {
968
955
  ctx2.value = utils.clamp(ctx2);
969
956
  },
970
957
  roundValue(ctx2) {
971
- ctx2.value = utils.round(ctx2);
958
+ if (ctx2.value !== "") {
959
+ ctx2.value = utils.round(ctx2);
960
+ }
972
961
  },
973
962
  setValue(ctx2, evt) {
974
963
  var _a, _b;
@@ -995,14 +984,21 @@ function machine(ctx = {}) {
995
984
  },
996
985
  invokeOnChange(ctx2) {
997
986
  var _a;
998
- (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: ctx2.value, valueAsNumber: ctx2.valueAsNumber });
987
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, {
988
+ value: ctx2.value,
989
+ valueAsNumber: ctx2.valueAsNumber
990
+ });
999
991
  },
1000
992
  invokeOnInvalid(ctx2) {
1001
993
  var _a;
1002
994
  if (!ctx2.isOutOfRange)
1003
995
  return;
1004
996
  const reason = ctx2.valueAsNumber > ctx2.max ? "rangeOverflow" : "rangeUnderflow";
1005
- (_a = ctx2.onInvalid) == null ? void 0 : _a.call(ctx2, { reason, value: ctx2.formattedValue, valueAsNumber: ctx2.valueAsNumber });
997
+ (_a = ctx2.onInvalid) == null ? void 0 : _a.call(ctx2, {
998
+ reason,
999
+ value: ctx2.formattedValue,
1000
+ valueAsNumber: ctx2.valueAsNumber
1001
+ });
1006
1002
  },
1007
1003
  syncInputValue(ctx2) {
1008
1004
  const input = dom.getInputEl(ctx2);
@@ -1021,7 +1017,8 @@ function machine(ctx = {}) {
1021
1017
  const cursor = dom.getCursorEl(ctx2);
1022
1018
  if (!cursor || !ctx2.scrubberCursorPoint)
1023
1019
  return;
1024
- cursor.style.transform = `translate3d(${ctx2.scrubberCursorPoint.x}px, ${ctx2.scrubberCursorPoint.y}px, 0px)`;
1020
+ const { x, y } = ctx2.scrubberCursorPoint;
1021
+ cursor.style.transform = `translate3d(${x}px, ${y}px, 0px)`;
1025
1022
  },
1026
1023
  addCustomCursor(ctx2) {
1027
1024
  if (isSafari() || !supportsPointerEvent())
@@ -1029,11 +1026,10 @@ function machine(ctx = {}) {
1029
1026
  dom.createVirtualCursor(ctx2);
1030
1027
  },
1031
1028
  removeCustomCursor(ctx2) {
1029
+ var _a;
1032
1030
  if (isSafari() || !supportsPointerEvent())
1033
1031
  return;
1034
- const doc = dom.getDoc(ctx2);
1035
- const el = doc.getElementById(dom.getCursorId(ctx2));
1036
- el == null ? void 0 : el.remove();
1032
+ (_a = dom.getCursorEl(ctx2)) == null ? void 0 : _a.remove();
1037
1033
  },
1038
1034
  disableTextSelection(ctx2) {
1039
1035
  const doc = dom.getDoc(ctx2);