@zag-js/slider 0.1.3 → 0.1.4

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
@@ -403,10 +403,6 @@ findByTypeahead.defaultOptions = {
403
403
 
404
404
  // ../../utilities/number/dist/index.mjs
405
405
  var __pow2 = Math.pow;
406
- var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
407
- function formatter(n) {
408
- return parseFloat(nf.format(n));
409
- }
410
406
  function round(v, t2) {
411
407
  let num = valueOf(v);
412
408
  const p = __pow2(10, t2 != null ? t2 : 10);
@@ -421,17 +417,15 @@ function clamp(v, o) {
421
417
  function countDecimals(value) {
422
418
  if (!Number.isFinite(value))
423
419
  return 0;
424
- let e = 1;
425
- let p = 0;
420
+ let e = 1, p = 0;
426
421
  while (Math.round(value * e) / e !== value) {
427
422
  e *= 10;
428
423
  p += 1;
429
424
  }
430
425
  return p;
431
426
  }
432
- var increment = (v, s) => formatter(valueOf(v) + s);
433
- var decrement = (v, s) => formatter(valueOf(v) - s);
434
- var multiply = (v, s) => formatter(valueOf(v) * s);
427
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
428
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
435
429
  function snapToStep(value, step) {
436
430
  const num = valueOf(value);
437
431
  const p = countDecimals(step);
@@ -444,6 +438,18 @@ function valueOf(v) {
444
438
  const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
445
439
  return !Number.isNaN(num) ? num : 0;
446
440
  }
441
+ function decimalOperation(a, op, b) {
442
+ let result = op === "+" ? a + b : a - b;
443
+ if (a % 1 !== 0 || b % 1 !== 0) {
444
+ const multiplier = __pow2(10, Math.max(countDecimals(a), countDecimals(b)));
445
+ a = Math.round(a * multiplier);
446
+ b = Math.round(b * multiplier);
447
+ result = op === "+" ? a + b : a - b;
448
+ result /= multiplier;
449
+ }
450
+ return result;
451
+ }
452
+ var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
447
453
  var transform = (a, b) => {
448
454
  const i = { min: a[0], max: a[1] };
449
455
  const o = { min: b[0], max: b[1] };
@@ -564,6 +570,10 @@ var dom = {
564
570
  var _a;
565
571
  return (_a = ctx.doc) != null ? _a : document;
566
572
  },
573
+ getRootNode: (ctx) => {
574
+ var _a;
575
+ return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
576
+ },
567
577
  getRootId: (ctx) => {
568
578
  var _a, _b;
569
579
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `slider:${ctx.uid}`;
@@ -594,9 +604,9 @@ var dom = {
594
604
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `slider:${ctx.uid}:label`;
595
605
  },
596
606
  getMarkerId: (ctx, value) => `slider:${ctx.uid}:marker:${value}`,
597
- getThumbEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getThumbId(ctx)),
598
- getControlEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getControlId(ctx)),
599
- getInputEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getInputId(ctx)),
607
+ getThumbEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getThumbId(ctx)),
608
+ getControlEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getControlId(ctx)),
609
+ getInputEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getInputId(ctx)),
600
610
  getControlStyle,
601
611
  getThumbStyle,
602
612
  getRangeStyle,
@@ -732,7 +742,7 @@ function connect(state2, send, normalize = normalizeProp) {
732
742
  onKeyDown(event) {
733
743
  if (!isInteractive)
734
744
  return;
735
- const step = multiply(getEventStep(event), state2.context.step);
745
+ const step = getEventStep(event) * state2.context.step;
736
746
  let prevent = true;
737
747
  const keyMap = {
738
748
  ArrowUp() {
@@ -1019,6 +1029,8 @@ function machine(ctx = {}) {
1019
1029
  setupDocument(ctx2, evt) {
1020
1030
  if (evt.doc)
1021
1031
  ctx2.doc = ref(evt.doc);
1032
+ if (evt.root)
1033
+ ctx2.rootNode = ref(evt.root);
1022
1034
  ctx2.uid = evt.id;
1023
1035
  },
1024
1036
  checkValue(ctx2) {