@zag-js/slider 0.2.6 → 0.2.8

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
@@ -43,10 +43,12 @@ var anatomy = (0, import_anatomy.createAnatomy)("slider").parts(
43
43
  );
44
44
  var parts = anatomy.build();
45
45
 
46
- // ../../utilities/dom/dist/index.mjs
46
+ // ../../utilities/dom/src/attrs.ts
47
47
  var dataAttr = (guard) => {
48
48
  return guard ? "" : void 0;
49
49
  };
50
+
51
+ // ../../utilities/core/src/functions.ts
50
52
  var runIfFn = (v, ...a) => {
51
53
  const res = typeof v === "function" ? v(...a) : v;
52
54
  return res != null ? res : void 0;
@@ -56,9 +58,22 @@ var callAll = (...fns) => (...a) => {
56
58
  fn == null ? void 0 : fn(...a);
57
59
  });
58
60
  };
61
+
62
+ // ../../utilities/core/src/guard.ts
59
63
  var isArray = (v) => Array.isArray(v);
60
64
  var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
61
65
  var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
66
+
67
+ // ../../utilities/core/src/object.ts
68
+ function compact(obj) {
69
+ if (obj === void 0)
70
+ return obj;
71
+ return Object.fromEntries(
72
+ Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
73
+ );
74
+ }
75
+
76
+ // ../../utilities/dom/src/platform.ts
62
77
  var isDom = () => typeof window !== "undefined";
63
78
  function getPlatform() {
64
79
  var _a;
@@ -70,6 +85,8 @@ var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
70
85
  var isMac = () => pt(/^Mac/) && !isTouchDevice;
71
86
  var isApple = () => pt(/mac|iphone|ipad|ipod/i);
72
87
  var isIos = () => isApple() && !isMac();
88
+
89
+ // ../../utilities/dom/src/query.ts
73
90
  function isDocument(el) {
74
91
  return el.nodeType === Node.DOCUMENT_NODE;
75
92
  }
@@ -84,6 +101,10 @@ function getDocument(el) {
84
101
  return el;
85
102
  return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
86
103
  }
104
+ function getWindow(el) {
105
+ var _a;
106
+ return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
107
+ }
87
108
  function defineDomHelpers(helpers) {
88
109
  const dom2 = {
89
110
  getRootNode: (ctx) => {
@@ -96,31 +117,15 @@ function defineDomHelpers(helpers) {
96
117
  return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
97
118
  },
98
119
  getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
99
- getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
100
- createEmitter: (ctx, target) => {
101
- const win = dom2.getWin(ctx);
102
- return function emit(evt, detail, options) {
103
- const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
104
- const eventName = `zag:${evt}`;
105
- const init = { bubbles, cancelable, composed, detail };
106
- const event = new win.CustomEvent(eventName, init);
107
- target.dispatchEvent(event);
108
- };
109
- },
110
- createListener: (target) => {
111
- return function listen(evt, handler) {
112
- const eventName = `zag:${evt}`;
113
- const listener = (e) => handler(e);
114
- target.addEventListener(eventName, listener);
115
- return () => target.removeEventListener(eventName, listener);
116
- };
117
- }
120
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
118
121
  };
119
122
  return {
120
123
  ...dom2,
121
124
  ...helpers
122
125
  };
123
126
  }
127
+
128
+ // ../../utilities/dom/src/event.ts
124
129
  function getNativeEvent(e) {
125
130
  var _a;
126
131
  return (_a = e.nativeEvent) != null ? _a : e;
@@ -132,6 +137,8 @@ var isMouseEvent = (v) => isObject(v) && hasProp(v, "button");
132
137
  var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
133
138
  var isLeftClick = (v) => v.button === 0;
134
139
  var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
140
+
141
+ // ../../utilities/dom/src/get-element-offset.ts
135
142
  function getElementOffset(element) {
136
143
  let left = 0;
137
144
  let top = 0;
@@ -154,6 +161,8 @@ function getElementOffset(element) {
154
161
  left
155
162
  };
156
163
  }
164
+
165
+ // ../../utilities/dom/src/get-event-point.ts
157
166
  var fallback = {
158
167
  pageX: 0,
159
168
  pageY: 0,
@@ -165,12 +174,16 @@ function getEventPoint(event, type = "page") {
165
174
  const point = isTouchEvent(event) ? (_b = (_a = event.touches[0]) != null ? _a : event.changedTouches[0]) != null ? _b : fallback : event;
166
175
  return { x: point[`${type}X`], y: point[`${type}Y`] };
167
176
  }
177
+
178
+ // ../../utilities/dom/src/get-point-relative-to-element.ts
168
179
  function getPointRelativeToNode(point, element) {
169
180
  const offset = getElementOffset(element);
170
181
  const x = point.x - offset.left;
171
182
  const y = point.y - offset.top;
172
183
  return { x, y };
173
184
  }
185
+
186
+ // ../../utilities/dom/src/keyboard-event.ts
174
187
  var rtlKeyMap = {
175
188
  ArrowLeft: "ArrowRight",
176
189
  ArrowRight: "ArrowLeft"
@@ -206,6 +219,8 @@ function getEventStep(event) {
206
219
  return isSkipKey ? 10 : 1;
207
220
  }
208
221
  }
222
+
223
+ // ../../utilities/dom/src/listener.ts
209
224
  var isRef = (v) => hasProp(v, "current");
210
225
  var fallback2 = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
211
226
  function extractInfo(event, type = "page") {
@@ -270,6 +285,25 @@ function getEventName(evt) {
270
285
  return mouseEventNames[evt];
271
286
  return evt;
272
287
  }
288
+
289
+ // ../../utilities/dom/src/mutation-observer.ts
290
+ function observeAttributes(node, attributes, fn) {
291
+ if (!node)
292
+ return;
293
+ const attrs = Array.isArray(attributes) ? attributes : [attributes];
294
+ const win = node.ownerDocument.defaultView || window;
295
+ const obs = new win.MutationObserver((changes) => {
296
+ for (const change of changes) {
297
+ if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
298
+ fn(change);
299
+ }
300
+ }
301
+ });
302
+ obs.observe(node, { attributes: true, attributeFilter: attrs });
303
+ return () => obs.disconnect();
304
+ }
305
+
306
+ // ../../utilities/dom/src/next-tick.ts
273
307
  function nextTick(fn) {
274
308
  const set = /* @__PURE__ */ new Set();
275
309
  function raf2(fn2) {
@@ -289,6 +323,8 @@ function raf(fn) {
289
323
  globalThis.cancelAnimationFrame(id);
290
324
  };
291
325
  }
326
+
327
+ // ../../utilities/dom/src/text-selection.ts
292
328
  var state = "default";
293
329
  var savedUserSelect = "";
294
330
  var modifiedElementMap = /* @__PURE__ */ new WeakMap();
@@ -336,6 +372,8 @@ function restoreTextSelection({ target, doc } = {}) {
336
372
  }
337
373
  }
338
374
  }
375
+
376
+ // ../../utilities/dom/src/pointer-event.ts
339
377
  var THRESHOLD = 5;
340
378
  function trackPointerMove(doc, opts) {
341
379
  const { onPointerMove, onPointerUp } = opts;
@@ -359,85 +397,10 @@ function trackPointerMove(doc, opts) {
359
397
  );
360
398
  }
361
399
 
362
- // ../../utilities/number/dist/index.mjs
363
- function round(v, t) {
364
- let num = valueOf(v);
365
- const p = 10 ** (t != null ? t : 10);
366
- num = Math.round(num * p) / p;
367
- return t ? num.toFixed(t) : v.toString();
368
- }
369
- var valueToPercent = (v, r) => (valueOf(v) - r.min) * 100 / (r.max - r.min);
370
- var percentToValue = (v, r) => r.min + (r.max - r.min) * valueOf(v);
371
- function clamp(v, o) {
372
- return Math.min(Math.max(valueOf(v), o.min), o.max);
373
- }
374
- function countDecimals(value) {
375
- if (!Number.isFinite(value))
376
- return 0;
377
- let e = 1, p = 0;
378
- while (Math.round(value * e) / e !== value) {
379
- e *= 10;
380
- p += 1;
381
- }
382
- return p;
383
- }
384
- var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
385
- var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
386
- function snapToStep(value, step) {
387
- const num = valueOf(value);
388
- const p = countDecimals(step);
389
- const v = Math.round(num / step) * step;
390
- return round(v, p);
391
- }
392
- function valueOf(v) {
393
- if (typeof v === "number")
394
- return v;
395
- const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
396
- return !Number.isNaN(num) ? num : 0;
397
- }
398
- function decimalOperation(a, op, b) {
399
- let result = op === "+" ? a + b : a - b;
400
- if (a % 1 !== 0 || b % 1 !== 0) {
401
- const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
402
- a = Math.round(a * multiplier);
403
- b = Math.round(b * multiplier);
404
- result = op === "+" ? a + b : a - b;
405
- result /= multiplier;
406
- }
407
- return result;
408
- }
409
- var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
410
- var transform = (a, b) => {
411
- const i = { min: a[0], max: a[1] };
412
- const o = { min: b[0], max: b[1] };
413
- return (v) => {
414
- if (i.min === i.max || o.min === o.max)
415
- return o.min;
416
- const ratio = (o.max - o.min) / (i.max - i.min);
417
- return o.min + ratio * (v - i.min);
418
- };
419
- };
400
+ // src/slider.connect.ts
401
+ var import_numeric_range3 = require("@zag-js/numeric-range");
420
402
 
421
- // ../../utilities/form-utils/dist/index.mjs
422
- function getWindow(el) {
423
- var _a;
424
- return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
425
- }
426
- function observeAttributes(node, attributes, fn) {
427
- if (!node)
428
- return;
429
- const attrs = Array.isArray(attributes) ? attributes : [attributes];
430
- const win = node.ownerDocument.defaultView || window;
431
- const obs = new win.MutationObserver((changes) => {
432
- for (const change of changes) {
433
- if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
434
- fn(change);
435
- }
436
- }
437
- });
438
- obs.observe(node, { attributes: true, attributeFilter: attrs });
439
- return () => obs.disconnect();
440
- }
403
+ // ../../utilities/form-utils/src/input-event.ts
441
404
  function getDescriptor(el, options) {
442
405
  var _a;
443
406
  const { type, property = "value" } = options;
@@ -456,6 +419,8 @@ function dispatchInputValueEvent(el, value) {
456
419
  const event = new win.Event("input", { bubbles: true });
457
420
  el.dispatchEvent(event);
458
421
  }
422
+
423
+ // ../../utilities/form-utils/src/form.ts
459
424
  function getClosestForm(el) {
460
425
  if (isFormElement(el))
461
426
  return el.form;
@@ -497,46 +462,57 @@ function trackFormControl(el, options) {
497
462
  };
498
463
  }
499
464
 
465
+ // src/slider.dom.ts
466
+ var import_numeric_range2 = require("@zag-js/numeric-range");
467
+
500
468
  // src/slider.style.ts
469
+ var import_numeric_range = require("@zag-js/numeric-range");
501
470
  function getVerticalThumbOffset(ctx) {
502
471
  var _a;
503
472
  const { height = 0 } = (_a = ctx.thumbSize) != null ? _a : {};
504
- const getValue = transform([ctx.min, ctx.max], [-height / 2, height / 2]);
473
+ const getValue = (0, import_numeric_range.getValueTransformer)([ctx.min, ctx.max], [-height / 2, height / 2]);
505
474
  return parseFloat(getValue(ctx.value).toFixed(2));
506
475
  }
507
476
  function getHorizontalThumbOffset(ctx) {
508
477
  var _a;
509
478
  const { width = 0 } = (_a = ctx.thumbSize) != null ? _a : {};
510
479
  if (ctx.isRtl) {
511
- const getValue2 = transform([ctx.max, ctx.min], [-width * 1.5, -width / 2]);
480
+ const getValue2 = (0, import_numeric_range.getValueTransformer)([ctx.max, ctx.min], [-width * 1.5, -width / 2]);
512
481
  return -1 * parseFloat(getValue2(ctx.value).toFixed(2));
513
482
  }
514
- const getValue = transform([ctx.min, ctx.max], [-width / 2, width / 2]);
483
+ const getValue = (0, import_numeric_range.getValueTransformer)([ctx.min, ctx.max], [-width / 2, width / 2]);
515
484
  return parseFloat(getValue(ctx.value).toFixed(2));
516
485
  }
517
486
  function getThumbOffset(ctx) {
518
- const percent = valueToPercent(ctx.value, ctx);
519
- if (ctx.thumbAlignment === "center")
487
+ const percent = (0, import_numeric_range.getValuePercent)(ctx.value, ctx.min, ctx.max) * 100;
488
+ if (ctx.thumbAlignment === "center") {
520
489
  return `${percent}%`;
490
+ }
521
491
  const offset = ctx.isVertical ? getVerticalThumbOffset(ctx) : getHorizontalThumbOffset(ctx);
522
492
  return `calc(${percent}% - ${offset}px)`;
523
493
  }
494
+ function getVisibility(ctx) {
495
+ let visibility = "visible";
496
+ if (ctx.thumbAlignment === "contain" && !ctx.hasMeasuredThumbSize) {
497
+ visibility = "hidden";
498
+ }
499
+ return visibility;
500
+ }
524
501
  function getThumbStyle(ctx) {
525
502
  const placementProp = ctx.isVertical ? "bottom" : ctx.isRtl ? "right" : "left";
526
503
  return {
527
- visibility: ctx.hasMeasuredThumbSize ? "visible" : "hidden",
504
+ visibility: getVisibility(ctx),
528
505
  position: "absolute",
529
506
  transform: "var(--slider-thumb-transform)",
530
507
  [placementProp]: "var(--slider-thumb-offset)"
531
508
  };
532
509
  }
533
510
  function getRangeOffsets(ctx) {
534
- const percent = valueToPercent(ctx.value, ctx);
535
511
  let start = "0%";
536
- let end = `${100 - percent}%`;
512
+ let end = `${100 - ctx.valuePercent}%`;
537
513
  if (ctx.origin === "center") {
538
- const isNegative = percent < 50;
539
- start = isNegative ? `${percent}%` : "50%";
514
+ const isNegative = ctx.valuePercent < 50;
515
+ start = isNegative ? `${ctx.valuePercent}%` : "50%";
540
516
  end = isNegative ? "50%" : end;
541
517
  }
542
518
  return { start, end };
@@ -575,7 +551,7 @@ function getMarkerStyle(ctx, percent) {
575
551
  return {
576
552
  position: "absolute",
577
553
  pointerEvents: "none",
578
- [ctx.isHorizontal ? "left" : "bottom"]: `${ctx.isRtl ? 100 - percent : percent}%`
554
+ [ctx.isHorizontal ? "left" : "bottom"]: `${(ctx.isRtl ? 1 - percent : percent) * 100}%`
579
555
  };
580
556
  }
581
557
  function getLabelStyle() {
@@ -603,28 +579,6 @@ var styles = {
603
579
  getMarkerGroupStyle
604
580
  };
605
581
 
606
- // src/slider.utils.ts
607
- var utils = {
608
- fromPercent(ctx, percent) {
609
- percent = clamp(percent, { min: 0, max: 1 });
610
- return parseFloat(snapToStep(percentToValue(percent, ctx), ctx.step));
611
- },
612
- clamp(ctx, value) {
613
- return clamp(value, ctx);
614
- },
615
- convert(ctx, value) {
616
- return clamp(parseFloat(snapToStep(value, ctx.step)), ctx);
617
- },
618
- decrement(ctx, step) {
619
- let value = decrement(ctx.value, step != null ? step : ctx.step);
620
- return utils.convert(ctx, value);
621
- },
622
- increment(ctx, step) {
623
- let value = increment(ctx.value, step != null ? step : ctx.step);
624
- return utils.convert(ctx, value);
625
- }
626
- };
627
-
628
582
  // src/slider.dom.ts
629
583
  var dom = defineDomHelpers({
630
584
  ...styles,
@@ -675,7 +629,7 @@ var dom = defineDomHelpers({
675
629
  } else {
676
630
  percent = 1 - percentY;
677
631
  }
678
- return utils.fromPercent(ctx, percent);
632
+ return (0, import_numeric_range2.getPercentValue)(percent, ctx.min, ctx.max, ctx.step);
679
633
  },
680
634
  dispatchChangeEvent(ctx) {
681
635
  const input = dom.getHiddenInputEl(ctx);
@@ -700,12 +654,15 @@ function connect(state2, send, normalize) {
700
654
  isFocused,
701
655
  isDragging,
702
656
  value: state2.context.value,
703
- percent: valueToPercent(state2.context.value, state2.context),
657
+ percent: (0, import_numeric_range3.getValuePercent)(state2.context.value, state2.context.min, state2.context.max),
704
658
  setValue(value) {
705
659
  send({ type: "SET_VALUE", value });
706
660
  },
707
661
  getPercentValue(percent) {
708
- return percentToValue(percent, state2.context);
662
+ return (0, import_numeric_range3.getPercentValue)(percent, state2.context.min, state2.context.max, state2.context.step);
663
+ },
664
+ getValuePercent(value) {
665
+ return (0, import_numeric_range3.getValuePercent)(value, state2.context.min, state2.context.max);
709
666
  },
710
667
  focus() {
711
668
  var _a2;
@@ -881,7 +838,7 @@ function connect(state2, send, normalize) {
881
838
  style: dom.getMarkerGroupStyle()
882
839
  }),
883
840
  getMarkerProps({ value }) {
884
- const percent = valueToPercent(value, state2.context);
841
+ const percent = this.getValuePercent(value);
885
842
  const style = dom.getMarkerStyle(state2.context, percent);
886
843
  const markerState = value > state2.context.value ? "over-value" : value < state2.context.value ? "under-value" : "at-value";
887
844
  return normalize.element({
@@ -901,16 +858,31 @@ function connect(state2, send, normalize) {
901
858
  // src/slider.machine.ts
902
859
  var import_core = require("@zag-js/core");
903
860
  var import_element_size = require("@zag-js/element-size");
861
+ var import_numeric_range5 = require("@zag-js/numeric-range");
904
862
 
905
- // ../../utilities/core/dist/index.mjs
906
- var isArray2 = (v) => Array.isArray(v);
907
- var isObject2 = (v) => !(v == null || typeof v !== "object" || isArray2(v));
908
- function compact(obj) {
909
- if (obj === void 0)
910
- return obj;
911
- return Object.fromEntries(
912
- Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject2(value) ? compact(value) : value])
913
- );
863
+ // src/slider.utils.ts
864
+ var import_numeric_range4 = require("@zag-js/numeric-range");
865
+ function constrainValue(ctx, value) {
866
+ const snapValue = (0, import_numeric_range4.snapValueToStep)(value, ctx.min, ctx.max, ctx.step);
867
+ return (0, import_numeric_range4.clampValue)(snapValue, ctx.min, ctx.max);
868
+ }
869
+ function decrement(ctx, step) {
870
+ const index = 0;
871
+ const values = (0, import_numeric_range4.getPreviousStepValue)(index, {
872
+ ...ctx,
873
+ step: step != null ? step : ctx.step,
874
+ values: [ctx.value]
875
+ });
876
+ return values[index];
877
+ }
878
+ function increment(ctx, step) {
879
+ const index = 0;
880
+ const values = (0, import_numeric_range4.getNextStepValue)(index, {
881
+ ...ctx,
882
+ step: step != null ? step : ctx.step,
883
+ values: [ctx.value]
884
+ });
885
+ return values[index];
914
886
  }
915
887
 
916
888
  // src/slider.machine.ts
@@ -940,7 +912,8 @@ function machine(userContext) {
940
912
  isVertical: (ctx2) => ctx2.orientation === "vertical",
941
913
  isRtl: (ctx2) => ctx2.orientation === "horizontal" && ctx2.dir === "rtl",
942
914
  isInteractive: (ctx2) => !(ctx2.disabled || ctx2.readOnly),
943
- hasMeasuredThumbSize: (ctx2) => ctx2.thumbSize !== null
915
+ hasMeasuredThumbSize: (ctx2) => ctx2.thumbSize !== null,
916
+ valuePercent: (ctx2) => 100 * (0, import_numeric_range5.getValuePercent)(ctx2.value, ctx2.min, ctx2.max)
944
917
  },
945
918
  watch: {
946
919
  value: ["invokeOnChange", "dispatchChangeEvent"]
@@ -1067,7 +1040,7 @@ function machine(userContext) {
1067
1040
  },
1068
1041
  actions: {
1069
1042
  checkValue(ctx2) {
1070
- const value = utils.convert(ctx2, ctx2.value);
1043
+ const value = constrainValue(ctx2, ctx2.value);
1071
1044
  ctx2.value = value;
1072
1045
  ctx2.initialValue = value;
1073
1046
  },
@@ -1090,7 +1063,7 @@ function machine(userContext) {
1090
1063
  const value = dom.getValueFromPoint(ctx2, evt.point);
1091
1064
  if (value == null)
1092
1065
  return;
1093
- ctx2.value = utils.clamp(ctx2, value);
1066
+ ctx2.value = (0, import_numeric_range5.clampValue)(value, ctx2.min, ctx2.max);
1094
1067
  },
1095
1068
  focusThumb(ctx2) {
1096
1069
  raf(() => {
@@ -1099,10 +1072,10 @@ function machine(userContext) {
1099
1072
  });
1100
1073
  },
1101
1074
  decrement(ctx2, evt) {
1102
- ctx2.value = utils.decrement(ctx2, evt.step);
1075
+ ctx2.value = decrement(ctx2, evt.step);
1103
1076
  },
1104
1077
  increment(ctx2, evt) {
1105
- ctx2.value = utils.increment(ctx2, evt.step);
1078
+ ctx2.value = increment(ctx2, evt.step);
1106
1079
  },
1107
1080
  setToMin(ctx2) {
1108
1081
  ctx2.value = ctx2.min;
@@ -1111,7 +1084,7 @@ function machine(userContext) {
1111
1084
  ctx2.value = ctx2.max;
1112
1085
  },
1113
1086
  setValue(ctx2, evt) {
1114
- ctx2.value = utils.convert(ctx2, evt.value);
1087
+ ctx2.value = constrainValue(ctx2, evt.value);
1115
1088
  }
1116
1089
  }
1117
1090
  }