@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.mjs CHANGED
@@ -35,6 +35,12 @@ function nextTick(fn) {
35
35
  });
36
36
  };
37
37
  }
38
+ function raf(fn) {
39
+ const id = globalThis.requestAnimationFrame(fn);
40
+ return function cleanup() {
41
+ globalThis.cancelAnimationFrame(id);
42
+ };
43
+ }
38
44
  var noop = () => {
39
45
  };
40
46
  var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
@@ -252,7 +258,7 @@ var __spreadValues2 = (a, b) => {
252
258
  return a;
253
259
  };
254
260
  var __spreadProps2 = (a, b) => __defProps2(a, __getOwnPropDescs2(b));
255
- var nf = new Intl.NumberFormat("en-US", { style: "decimal" });
261
+ var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
256
262
  function formatter(n) {
257
263
  return parseFloat(nf.format(n));
258
264
  }
@@ -274,9 +280,16 @@ function roundToPx(num) {
274
280
  function clamp(v, o) {
275
281
  return Math.min(Math.max(valueOf(v), o.min), o.max);
276
282
  }
277
- function countDecimals(v) {
278
- var _a, _b;
279
- return (_b = (_a = nf.formatToParts(v).find((p) => p.type === "fraction")) == null ? void 0 : _a.value.length) != null ? _b : 0;
283
+ function countDecimals(value) {
284
+ if (!Number.isFinite(value))
285
+ return 0;
286
+ let e = 1;
287
+ let p = 0;
288
+ while (Math.round(value * e) / e !== value) {
289
+ e *= 10;
290
+ p += 1;
291
+ }
292
+ return p;
280
293
  }
281
294
  var increment = (v, s) => formatter(valueOf(v) + s);
282
295
  var decrement = (v, s) => formatter(valueOf(v) - s);
@@ -290,9 +303,7 @@ function valueOf(v) {
290
303
  function getMaxPrecision(o) {
291
304
  var _a;
292
305
  const stepPrecision = countDecimals(o.step);
293
- const val = valueOf(o.value);
294
- const valuePrecision = Number.isNaN(val) ? stepPrecision : Math.max(countDecimals(val), stepPrecision);
295
- return Math.max(valuePrecision, (_a = o.precision) != null ? _a : 0);
306
+ return Math.max(stepPrecision, (_a = o.precision) != null ? _a : 0);
296
307
  }
297
308
  function roundToPrecision(v, o) {
298
309
  return round(v, getMaxPrecision(__spreadProps2(__spreadValues2({}, o), { value: v })));
@@ -496,10 +507,8 @@ function connect(state, send, normalize = normalizeProp) {
496
507
  send({ type: "SET_VALUE", value: state.context.min });
497
508
  },
498
509
  focus() {
499
- nextTick(() => {
500
- var _a;
501
- (_a = dom.getInputEl(state.context)) == null ? void 0 : _a.focus();
502
- });
510
+ var _a;
511
+ (_a = dom.getInputEl(state.context)) == null ? void 0 : _a.focus();
503
512
  },
504
513
  rootProps: normalize.element({
505
514
  id: dom.getRootId(state.context),
@@ -750,7 +759,7 @@ function machine(ctx = {}) {
750
759
  actions: "setToMax"
751
760
  },
752
761
  CHANGE: {
753
- actions: ["setValue", "setSelectionRange", "setHint"]
762
+ actions: ["setValue", "setHint"]
754
763
  },
755
764
  BLUR: [
756
765
  {
@@ -764,6 +773,7 @@ function machine(ctx = {}) {
764
773
  actions: ["clampValue", "clearHint"]
765
774
  },
766
775
  {
776
+ target: "idle",
767
777
  actions: ["roundValue"]
768
778
  }
769
779
  ]
@@ -784,7 +794,7 @@ function machine(ctx = {}) {
784
794
  on: {
785
795
  PRESS_UP: {
786
796
  target: "focused",
787
- actions: ["clearHint", "restoreSelection"]
797
+ actions: "clearHint"
788
798
  }
789
799
  }
790
800
  },
@@ -806,7 +816,7 @@ function machine(ctx = {}) {
806
816
  on: {
807
817
  PRESS_UP: {
808
818
  target: "focused",
809
- actions: ["clearHint", "restoreSelection"]
819
+ actions: "clearHint"
810
820
  }
811
821
  }
812
822
  },
@@ -918,7 +928,7 @@ function machine(ctx = {}) {
918
928
  if (!ctx2.focusInputOnChange)
919
929
  return;
920
930
  const input = dom.getInputEl(ctx2);
921
- nextTick(() => input == null ? void 0 : input.focus());
931
+ raf(() => input == null ? void 0 : input.focus());
922
932
  },
923
933
  increment(ctx2, evt) {
924
934
  ctx2.value = utils.increment(ctx2, evt.step);
@@ -955,20 +965,6 @@ function machine(ctx = {}) {
955
965
  setHintToSet(ctx2) {
956
966
  ctx2.hint = "set";
957
967
  },
958
- setSelectionRange(ctx2, evt) {
959
- ctx2.inputSelection = {
960
- start: evt.target.selectionStart,
961
- end: evt.target.selectionEnd
962
- };
963
- },
964
- restoreSelection(ctx2) {
965
- var _a, _b, _c;
966
- const input = dom.getInputEl(ctx2);
967
- if (!input || !ctx2.inputSelection)
968
- return;
969
- input.selectionStart = (_b = ctx2.inputSelection.start) != null ? _b : (_a = input.value) == null ? void 0 : _a.length;
970
- input.selectionEnd = (_c = ctx2.inputSelection.end) != null ? _c : input.selectionStart;
971
- },
972
968
  invokeOnChange(ctx2) {
973
969
  var _a;
974
970
  (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: ctx2.value, valueAsNumber: ctx2.valueAsNumber });