@zag-js/number-input 0.0.0-dev-20220513103837 → 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) => {
@@ -370,6 +354,10 @@ var dom = {
370
354
  var _a;
371
355
  return (_a = dom.getDoc(ctx).defaultView) != null ? _a : window;
372
356
  },
357
+ getRootNode: (ctx) => {
358
+ var _a;
359
+ return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
360
+ },
373
361
  getRootId: (ctx) => {
374
362
  var _a, _b;
375
363
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `number-input:${ctx.uid}`;
@@ -395,14 +383,14 @@ var dom = {
395
383
  var _a, _b;
396
384
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `number-input:${ctx.uid}:label`;
397
385
  },
398
- getInputEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getInputId(ctx)),
399
- getIncButtonEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getIncButtonId(ctx)),
400
- getDecButtonEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getDecButtonId(ctx)),
401
- getScrubberEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getScrubberId(ctx)),
386
+ getInputEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getInputId(ctx)),
387
+ getIncButtonEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getIncButtonId(ctx)),
388
+ getDecButtonEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getDecButtonId(ctx)),
389
+ getScrubberEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getScrubberId(ctx)),
402
390
  getCursorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getCursorId(ctx)),
403
391
  getMousementValue(ctx, event) {
404
- const x = roundToPx(event.movementX);
405
- const y = roundToPx(event.movementY);
392
+ const x = roundToDevicePixel(event.movementX);
393
+ const y = roundToDevicePixel(event.movementY);
406
394
  let hint = x > 0 ? "increment" : x < 0 ? "decrement" : null;
407
395
  if (ctx.isRtl && hint === "increment")
408
396
  hint = "decrement";
@@ -414,7 +402,7 @@ var dom = {
414
402
  };
415
403
  const win = dom.getWin(ctx);
416
404
  const width = win.innerWidth;
417
- const half = roundToPx(7.5);
405
+ const half = roundToDevicePixel(7.5);
418
406
  point.x = wrap2(point.x + half, width) - half;
419
407
  return { hint, point };
420
408
  },
@@ -445,14 +433,6 @@ var dom = {
445
433
  }
446
434
  };
447
435
 
448
- // ../../utilities/core/dist/index.mjs
449
- var pipe2 = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
450
- var ua = (v) => isDom() && v.test(navigator.userAgent);
451
- var isDom = () => !!(typeof window !== "undefined");
452
- var isSafari = () => ua(/^((?!chrome|android).)*safari/i);
453
- var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
454
- var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
455
-
456
436
  // src/number-input.utils.ts
457
437
  var utils = {
458
438
  isValidNumericEvent: (ctx, event) => {
@@ -471,18 +451,15 @@ var utils = {
471
451
  return value.split("").filter((_a = ctx.validateCharacter) != null ? _a : utils.isFloatingPoint).join("");
472
452
  },
473
453
  increment: (ctx, step) => {
474
- let value = increment(ctx.value, step != null ? step : ctx.step);
475
- value = clamp(value, ctx);
476
- return roundToPrecision(value, ctx);
454
+ const value = increment(ctx.value, step != null ? step : ctx.step);
455
+ return formatDecimal(clamp(value, ctx), ctx);
477
456
  },
478
457
  decrement: (ctx, step) => {
479
- let value = decrement(ctx.value, step != null ? step : ctx.step);
480
- value = clamp(value, ctx);
481
- return roundToPrecision(value, ctx);
458
+ const value = decrement(ctx.value, step != null ? step : ctx.step);
459
+ return formatDecimal(clamp(value, ctx), ctx);
482
460
  },
483
461
  clamp: (ctx) => {
484
- let value = clamp(ctx.value, ctx);
485
- return roundToPrecision(value, ctx);
462
+ return formatDecimal(clamp(ctx.value, ctx), ctx);
486
463
  },
487
464
  parse: (ctx, value) => {
488
465
  var _a, _b;
@@ -494,7 +471,7 @@ var utils = {
494
471
  return (_b = (_a = ctx.format) == null ? void 0 : _a.call(ctx, _val)) != null ? _b : _val;
495
472
  },
496
473
  round: (ctx) => {
497
- return roundToPrecision(ctx.value, ctx);
474
+ return formatDecimal(ctx.value, ctx);
498
475
  }
499
476
  };
500
477
 
@@ -515,7 +492,7 @@ function connect(state, send, normalize = normalizeProp) {
515
492
  send({ type: "SET_VALUE", value: value.toString() });
516
493
  },
517
494
  clearValue() {
518
- send({ type: "SET_VALUE", value: "" });
495
+ send("CLEAR_VALUE");
519
496
  },
520
497
  increment() {
521
498
  send("INCREMENT");
@@ -545,6 +522,14 @@ function connect(state, send, normalize = normalizeProp) {
545
522
  id: dom.getLabelId(state.context),
546
523
  htmlFor: dom.getInputId(state.context)
547
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
+ }),
548
533
  inputProps: normalize.input({
549
534
  "data-part": "input",
550
535
  name: state.context.name,
@@ -562,7 +547,7 @@ function connect(state, send, normalize = normalizeProp) {
562
547
  autoCorrect: "off",
563
548
  spellCheck: "false",
564
549
  type: "text",
565
- "aria-roledescription": "number field",
550
+ "aria-roledescription": !isIos() ? "number field" : void 0,
566
551
  "aria-valuemin": state.context.min,
567
552
  "aria-valuemax": state.context.max,
568
553
  "aria-valuenow": isNaN(state.context.valueAsNumber) ? void 0 : state.context.valueAsNumber,
@@ -586,7 +571,7 @@ function connect(state, send, normalize = normalizeProp) {
586
571
  if (!utils.isValidNumericEvent(state.context, event)) {
587
572
  event.preventDefault();
588
573
  }
589
- const step = multiply(getEventStep(event), state.context.step);
574
+ const step = getEventStep(event) * state.context.step;
590
575
  const keyMap = {
591
576
  ArrowUp() {
592
577
  send({ type: "ARROW_UP", step });
@@ -665,8 +650,8 @@ function connect(state, send, normalize = normalizeProp) {
665
650
  const evt = getNativeEvent(event);
666
651
  event.preventDefault();
667
652
  const point = getEventPoint(evt);
668
- point.x = point.x - roundToPx(7.5);
669
- point.y = point.y - roundToPx(7.5);
653
+ point.x = point.x - roundToDevicePixel(7.5);
654
+ point.y = point.y - roundToDevicePixel(7.5);
670
655
  send({ type: "PRESS_DOWN_SCRUBBER", point });
671
656
  },
672
657
  style: {
@@ -696,8 +681,6 @@ function machine(ctx = {}) {
696
681
  step: 1,
697
682
  min: Number.MIN_SAFE_INTEGER,
698
683
  max: Number.MAX_SAFE_INTEGER,
699
- precision: 0,
700
- inputSelection: null,
701
684
  scrubberCursorPoint: null,
702
685
  invalid: false
703
686
  }, ctx), {
@@ -731,8 +714,15 @@ function machine(ctx = {}) {
731
714
  SET_VALUE: {
732
715
  actions: ["setValue", "setHintToSet"]
733
716
  },
734
- INCREMENT: { actions: ["increment"] },
735
- DECREMENT: { actions: ["decrement"] }
717
+ CLEAR_VALUE: {
718
+ actions: ["clearValue"]
719
+ },
720
+ INCREMENT: {
721
+ actions: ["increment"]
722
+ },
723
+ DECREMENT: {
724
+ actions: ["decrement"]
725
+ }
736
726
  },
737
727
  states: {
738
728
  unknown: {
@@ -898,26 +888,23 @@ function machine(ctx = {}) {
898
888
  send("PRESS_UP");
899
889
  });
900
890
  },
901
- attachWheelListener(ctx2) {
902
- const cleanups = [];
903
- cleanups.push(nextTick(() => {
904
- const input = dom.getInputEl(ctx2);
905
- 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)
906
898
  return;
907
- function onWheel(event) {
908
- const isInputFocused = dom.getDoc(ctx2).activeElement === input;
909
- if (!ctx2.allowMouseWheel || !isInputFocused)
910
- return;
911
- event.preventDefault();
912
- const dir = Math.sign(event.deltaY) * -1;
913
- if (dir === 1)
914
- ctx2.value = utils.increment(ctx2);
915
- else if (dir === -1)
916
- 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");
917
905
  }
918
- cleanups.push(addDomEvent(input, "wheel", onWheel, { passive: false }));
919
- }));
920
- return () => cleanups.forEach((c) => c());
906
+ }
907
+ return addDomEvent(input, "wheel", onWheel, { passive: false });
921
908
  },
922
909
  activatePointerLock(ctx2) {
923
910
  if (isSafari() || !supportsPointerEvent())
@@ -932,7 +919,11 @@ function machine(ctx = {}) {
932
919
  const value = dom.getMousementValue(ctx2, event);
933
920
  if (!value.hint)
934
921
  return;
935
- 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
+ });
936
927
  }
937
928
  function onMouseup() {
938
929
  send("POINTER_UP_SCRUBBER");
@@ -944,6 +935,8 @@ function machine(ctx = {}) {
944
935
  setupDocument: (ctx2, evt) => {
945
936
  if (evt.doc)
946
937
  ctx2.doc = (0, import_core.ref)(evt.doc);
938
+ if (evt.root)
939
+ ctx2.rootNode = (0, import_core.ref)(evt.root);
947
940
  ctx2.uid = evt.id;
948
941
  },
949
942
  focusInput(ctx2) {
@@ -962,7 +955,9 @@ function machine(ctx = {}) {
962
955
  ctx2.value = utils.clamp(ctx2);
963
956
  },
964
957
  roundValue(ctx2) {
965
- ctx2.value = utils.round(ctx2);
958
+ if (ctx2.value !== "") {
959
+ ctx2.value = utils.round(ctx2);
960
+ }
966
961
  },
967
962
  setValue(ctx2, evt) {
968
963
  var _a, _b;
@@ -989,14 +984,21 @@ function machine(ctx = {}) {
989
984
  },
990
985
  invokeOnChange(ctx2) {
991
986
  var _a;
992
- (_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
+ });
993
991
  },
994
992
  invokeOnInvalid(ctx2) {
995
993
  var _a;
996
994
  if (!ctx2.isOutOfRange)
997
995
  return;
998
996
  const reason = ctx2.valueAsNumber > ctx2.max ? "rangeOverflow" : "rangeUnderflow";
999
- (_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
+ });
1000
1002
  },
1001
1003
  syncInputValue(ctx2) {
1002
1004
  const input = dom.getInputEl(ctx2);
@@ -1015,7 +1017,8 @@ function machine(ctx = {}) {
1015
1017
  const cursor = dom.getCursorEl(ctx2);
1016
1018
  if (!cursor || !ctx2.scrubberCursorPoint)
1017
1019
  return;
1018
- 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)`;
1019
1022
  },
1020
1023
  addCustomCursor(ctx2) {
1021
1024
  if (isSafari() || !supportsPointerEvent())
@@ -1023,11 +1026,10 @@ function machine(ctx = {}) {
1023
1026
  dom.createVirtualCursor(ctx2);
1024
1027
  },
1025
1028
  removeCustomCursor(ctx2) {
1029
+ var _a;
1026
1030
  if (isSafari() || !supportsPointerEvent())
1027
1031
  return;
1028
- const doc = dom.getDoc(ctx2);
1029
- const el = doc.getElementById(dom.getCursorId(ctx2));
1030
- el == null ? void 0 : el.remove();
1032
+ (_a = dom.getCursorEl(ctx2)) == null ? void 0 : _a.remove();
1031
1033
  },
1032
1034
  disableTextSelection(ctx2) {
1033
1035
  const doc = dom.getDoc(ctx2);