@zag-js/number-input 0.0.0-dev-20220508172505 → 0.0.0-dev-20220509213421

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
@@ -58,6 +58,12 @@ function nextTick(fn) {
58
58
  });
59
59
  };
60
60
  }
61
+ function raf(fn) {
62
+ const id = globalThis.requestAnimationFrame(fn);
63
+ return function cleanup() {
64
+ globalThis.cancelAnimationFrame(id);
65
+ };
66
+ }
61
67
  var noop = () => {
62
68
  };
63
69
  var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
@@ -275,7 +281,7 @@ var __spreadValues2 = (a, b) => {
275
281
  return a;
276
282
  };
277
283
  var __spreadProps2 = (a, b) => __defProps2(a, __getOwnPropDescs2(b));
278
- var nf = new Intl.NumberFormat("en-US", { style: "decimal" });
284
+ var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
279
285
  function formatter(n) {
280
286
  return parseFloat(nf.format(n));
281
287
  }
@@ -297,9 +303,16 @@ function roundToPx(num) {
297
303
  function clamp(v, o) {
298
304
  return Math.min(Math.max(valueOf(v), o.min), o.max);
299
305
  }
300
- function countDecimals(v) {
301
- var _a, _b;
302
- return (_b = (_a = nf.formatToParts(v).find((p) => p.type === "fraction")) == null ? void 0 : _a.value.length) != null ? _b : 0;
306
+ function countDecimals(value) {
307
+ if (!Number.isFinite(value))
308
+ return 0;
309
+ let e = 1;
310
+ let p = 0;
311
+ while (Math.round(value * e) / e !== value) {
312
+ e *= 10;
313
+ p += 1;
314
+ }
315
+ return p;
303
316
  }
304
317
  var increment = (v, s) => formatter(valueOf(v) + s);
305
318
  var decrement = (v, s) => formatter(valueOf(v) - s);
@@ -313,9 +326,7 @@ function valueOf(v) {
313
326
  function getMaxPrecision(o) {
314
327
  var _a;
315
328
  const stepPrecision = countDecimals(o.step);
316
- const val = valueOf(o.value);
317
- const valuePrecision = Number.isNaN(val) ? stepPrecision : Math.max(countDecimals(val), stepPrecision);
318
- return Math.max(valuePrecision, (_a = o.precision) != null ? _a : 0);
329
+ return Math.max(stepPrecision, (_a = o.precision) != null ? _a : 0);
319
330
  }
320
331
  function roundToPrecision(v, o) {
321
332
  return round(v, getMaxPrecision(__spreadProps2(__spreadValues2({}, o), { value: v })));
@@ -519,10 +530,8 @@ function connect(state, send, normalize = normalizeProp) {
519
530
  send({ type: "SET_VALUE", value: state.context.min });
520
531
  },
521
532
  focus() {
522
- nextTick(() => {
523
- var _a;
524
- (_a = dom.getInputEl(state.context)) == null ? void 0 : _a.focus();
525
- });
533
+ var _a;
534
+ (_a = dom.getInputEl(state.context)) == null ? void 0 : _a.focus();
526
535
  },
527
536
  rootProps: normalize.element({
528
537
  id: dom.getRootId(state.context),
@@ -773,7 +782,7 @@ function machine(ctx = {}) {
773
782
  actions: "setToMax"
774
783
  },
775
784
  CHANGE: {
776
- actions: ["setValue", "setSelectionRange", "setHint"]
785
+ actions: ["setValue", "setHint"]
777
786
  },
778
787
  BLUR: [
779
788
  {
@@ -787,6 +796,7 @@ function machine(ctx = {}) {
787
796
  actions: ["clampValue", "clearHint"]
788
797
  },
789
798
  {
799
+ target: "idle",
790
800
  actions: ["roundValue"]
791
801
  }
792
802
  ]
@@ -807,7 +817,7 @@ function machine(ctx = {}) {
807
817
  on: {
808
818
  PRESS_UP: {
809
819
  target: "focused",
810
- actions: ["clearHint", "restoreSelection"]
820
+ actions: "clearHint"
811
821
  }
812
822
  }
813
823
  },
@@ -829,7 +839,7 @@ function machine(ctx = {}) {
829
839
  on: {
830
840
  PRESS_UP: {
831
841
  target: "focused",
832
- actions: ["clearHint", "restoreSelection"]
842
+ actions: "clearHint"
833
843
  }
834
844
  }
835
845
  },
@@ -941,7 +951,7 @@ function machine(ctx = {}) {
941
951
  if (!ctx2.focusInputOnChange)
942
952
  return;
943
953
  const input = dom.getInputEl(ctx2);
944
- nextTick(() => input == null ? void 0 : input.focus());
954
+ raf(() => input == null ? void 0 : input.focus());
945
955
  },
946
956
  increment(ctx2, evt) {
947
957
  ctx2.value = utils.increment(ctx2, evt.step);
@@ -978,20 +988,6 @@ function machine(ctx = {}) {
978
988
  setHintToSet(ctx2) {
979
989
  ctx2.hint = "set";
980
990
  },
981
- setSelectionRange(ctx2, evt) {
982
- ctx2.inputSelection = {
983
- start: evt.target.selectionStart,
984
- end: evt.target.selectionEnd
985
- };
986
- },
987
- restoreSelection(ctx2) {
988
- var _a, _b, _c;
989
- const input = dom.getInputEl(ctx2);
990
- if (!input || !ctx2.inputSelection)
991
- return;
992
- input.selectionStart = (_b = ctx2.inputSelection.start) != null ? _b : (_a = input.value) == null ? void 0 : _a.length;
993
- input.selectionEnd = (_c = ctx2.inputSelection.end) != null ? _c : input.selectionStart;
994
- },
995
991
  invokeOnChange(ctx2) {
996
992
  var _a;
997
993
  (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: ctx2.value, valueAsNumber: ctx2.valueAsNumber });