@zag-js/slider 0.1.3 → 0.1.6

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
@@ -188,10 +188,6 @@ function getOwnerWindow(el) {
188
188
  var _a;
189
189
  return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
190
190
  }
191
- function getNativeEvent(event) {
192
- var _a;
193
- return (_a = event.nativeEvent) != null ? _a : event;
194
- }
195
191
  function getDescriptor(el, options) {
196
192
  var _a;
197
193
  const { type, property } = options;
@@ -210,44 +206,50 @@ function dispatchInputValueEvent(el, value) {
210
206
  const event = new win.Event("input", { bubbles: true });
211
207
  el.dispatchEvent(event);
212
208
  }
213
- function trackInputPropertyMutation(el, options) {
214
- const { fn, property, type } = options;
215
- if (!fn || !el)
216
- return;
217
- const { get, set } = getDescriptor(el, { property, type });
218
- let run = true;
219
- Object.defineProperty(el, property, {
220
- get() {
221
- return get == null ? void 0 : get.call(this);
222
- },
223
- set(value) {
224
- if (run)
225
- fn(value);
226
- return set == null ? void 0 : set.call(this, value);
209
+ function getNativeEvent(event) {
210
+ var _a;
211
+ return (_a = event.nativeEvent) != null ? _a : event;
212
+ }
213
+ function observeAttributes(node, attributes, fn) {
214
+ if (!node)
215
+ return noop;
216
+ const attrs = Array.isArray(attributes) ? attributes : [attributes];
217
+ const win = node.ownerDocument.defaultView || window;
218
+ const obs = new win.MutationObserver((changes) => {
219
+ for (const change of changes) {
220
+ if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
221
+ fn(change);
222
+ }
227
223
  }
228
224
  });
229
- return function() {
230
- run = false;
231
- };
225
+ obs.observe(node, { attributes: true, attributeFilter: attrs });
226
+ return () => obs.disconnect();
232
227
  }
233
- function getClosestFormElement(el) {
228
+ function getClosestForm(el) {
234
229
  if (isFormElement(el))
235
230
  return el.form;
236
231
  else
237
232
  return el.closest("form");
238
233
  }
239
234
  function isFormElement(el) {
240
- return ["textarea", "input", "select", "button"].includes(el.localName);
235
+ return el.matches("textarea, input, select, button");
241
236
  }
242
237
  function trackFormReset(el, callback) {
243
238
  if (!el)
244
239
  return;
245
- const form = getClosestFormElement(el);
240
+ const form = getClosestForm(el);
246
241
  form == null ? void 0 : form.addEventListener("reset", callback, { passive: true });
247
242
  return () => {
248
243
  form == null ? void 0 : form.removeEventListener("reset", callback);
249
244
  };
250
245
  }
246
+ function trackFieldsetDisabled(el, callback) {
247
+ const fieldset = el == null ? void 0 : el.closest("fieldset");
248
+ if (!fieldset)
249
+ return;
250
+ callback(fieldset.disabled);
251
+ return observeAttributes(fieldset, ["disabled"], () => callback(fieldset.disabled));
252
+ }
251
253
  var rtlKeyMap = {
252
254
  ArrowLeft: "ArrowRight",
253
255
  ArrowRight: "ArrowLeft",
@@ -403,10 +405,6 @@ findByTypeahead.defaultOptions = {
403
405
 
404
406
  // ../../utilities/number/dist/index.mjs
405
407
  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
408
  function round(v, t2) {
411
409
  let num = valueOf(v);
412
410
  const p = __pow2(10, t2 != null ? t2 : 10);
@@ -421,17 +419,15 @@ function clamp(v, o) {
421
419
  function countDecimals(value) {
422
420
  if (!Number.isFinite(value))
423
421
  return 0;
424
- let e = 1;
425
- let p = 0;
422
+ let e = 1, p = 0;
426
423
  while (Math.round(value * e) / e !== value) {
427
424
  e *= 10;
428
425
  p += 1;
429
426
  }
430
427
  return p;
431
428
  }
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);
429
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
430
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
435
431
  function snapToStep(value, step) {
436
432
  const num = valueOf(value);
437
433
  const p = countDecimals(step);
@@ -444,6 +440,18 @@ function valueOf(v) {
444
440
  const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
445
441
  return !Number.isNaN(num) ? num : 0;
446
442
  }
443
+ function decimalOperation(a, op, b) {
444
+ let result = op === "+" ? a + b : a - b;
445
+ if (a % 1 !== 0 || b % 1 !== 0) {
446
+ const multiplier = __pow2(10, Math.max(countDecimals(a), countDecimals(b)));
447
+ a = Math.round(a * multiplier);
448
+ b = Math.round(b * multiplier);
449
+ result = op === "+" ? a + b : a - b;
450
+ result /= multiplier;
451
+ }
452
+ return result;
453
+ }
454
+ var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
447
455
  var transform = (a, b) => {
448
456
  const i = { min: a[0], max: a[1] };
449
457
  const o = { min: b[0], max: b[1] };
@@ -564,6 +572,10 @@ var dom = {
564
572
  var _a;
565
573
  return (_a = ctx.doc) != null ? _a : document;
566
574
  },
575
+ getRootNode: (ctx) => {
576
+ var _a;
577
+ return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
578
+ },
567
579
  getRootId: (ctx) => {
568
580
  var _a, _b;
569
581
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `slider:${ctx.uid}`;
@@ -594,9 +606,10 @@ var dom = {
594
606
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `slider:${ctx.uid}:label`;
595
607
  },
596
608
  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)),
609
+ getRootEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getRootId(ctx)),
610
+ getThumbEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getThumbId(ctx)),
611
+ getControlEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getControlId(ctx)),
612
+ getInputEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getInputId(ctx)),
600
613
  getControlStyle,
601
614
  getThumbStyle,
602
615
  getRangeStyle,
@@ -636,7 +649,11 @@ var dom = {
636
649
 
637
650
  // ../../types/dist/index.mjs
638
651
  function createNormalizer(fn) {
639
- return { button: fn, label: fn, input: fn, output: fn, element: fn };
652
+ return new Proxy({}, {
653
+ get() {
654
+ return fn;
655
+ }
656
+ });
640
657
  }
641
658
  var normalizeProp = createNormalizer((v) => v);
642
659
 
@@ -732,7 +749,7 @@ function connect(state2, send, normalize = normalizeProp) {
732
749
  onKeyDown(event) {
733
750
  if (!isInteractive)
734
751
  return;
735
- const step = multiply(getEventStep(event), state2.context.step);
752
+ const step = getEventStep(event) * state2.context.step;
736
753
  let prevent = true;
737
754
  const keyMap = {
738
755
  ArrowUp() {
@@ -886,7 +903,7 @@ function machine(ctx = {}) {
886
903
  watch: {
887
904
  value: ["invokeOnChange", "dispatchChangeEvent"]
888
905
  },
889
- activities: ["trackFormReset", "trackScriptedUpdate"],
906
+ activities: ["trackFormReset", "trackFieldsetDisabled"],
890
907
  on: {
891
908
  SET_VALUE: {
892
909
  actions: "setValue"
@@ -974,17 +991,12 @@ function machine(ctx = {}) {
974
991
  isVertical: (ctx2) => ctx2.isVertical
975
992
  },
976
993
  activities: {
977
- trackScriptedUpdate(ctx2, _, { send }) {
994
+ trackFieldsetDisabled(ctx2) {
978
995
  let cleanup;
979
996
  nextTick(() => {
980
- const el = dom.getInputEl(ctx2);
981
- if (!el)
982
- return;
983
- cleanup = trackInputPropertyMutation(el, {
984
- type: "input",
985
- property: "value",
986
- fn(value) {
987
- send({ type: "SET_VALUE", value: parseFloat(value) });
997
+ cleanup = trackFieldsetDisabled(dom.getRootEl(ctx2), (disabled) => {
998
+ if (disabled != ctx2.disabled) {
999
+ ctx2.disabled = disabled;
988
1000
  }
989
1001
  });
990
1002
  });
@@ -993,15 +1005,13 @@ function machine(ctx = {}) {
993
1005
  trackFormReset(ctx2) {
994
1006
  let cleanup;
995
1007
  nextTick(() => {
996
- const el = dom.getInputEl(ctx2);
997
- if (!el)
998
- return;
999
- cleanup = trackFormReset(el, () => {
1000
- if (ctx2.initialValue != null)
1008
+ cleanup = trackFormReset(dom.getInputEl(ctx2), () => {
1009
+ if (ctx2.initialValue != null) {
1001
1010
  ctx2.value = ctx2.initialValue;
1011
+ }
1002
1012
  });
1003
1013
  });
1004
- return cleanup;
1014
+ return () => cleanup == null ? void 0 : cleanup();
1005
1015
  },
1006
1016
  trackPointerMove(ctx2, _evt, { send }) {
1007
1017
  return trackPointerMove({
@@ -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) {
@@ -1055,7 +1067,7 @@ function machine(ctx = {}) {
1055
1067
  ctx2.value = utils.clamp(ctx2, value);
1056
1068
  },
1057
1069
  focusThumb(ctx2) {
1058
- nextTick(() => {
1070
+ raf(() => {
1059
1071
  var _a;
1060
1072
  return (_a = dom.getThumbEl(ctx2)) == null ? void 0 : _a.focus();
1061
1073
  });