@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.js CHANGED
@@ -212,10 +212,6 @@ function getOwnerWindow(el) {
212
212
  var _a;
213
213
  return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
214
214
  }
215
- function getNativeEvent(event) {
216
- var _a;
217
- return (_a = event.nativeEvent) != null ? _a : event;
218
- }
219
215
  function getDescriptor(el, options) {
220
216
  var _a;
221
217
  const { type, property } = options;
@@ -234,44 +230,50 @@ function dispatchInputValueEvent(el, value) {
234
230
  const event = new win.Event("input", { bubbles: true });
235
231
  el.dispatchEvent(event);
236
232
  }
237
- function trackInputPropertyMutation(el, options) {
238
- const { fn, property, type } = options;
239
- if (!fn || !el)
240
- return;
241
- const { get, set } = getDescriptor(el, { property, type });
242
- let run = true;
243
- Object.defineProperty(el, property, {
244
- get() {
245
- return get == null ? void 0 : get.call(this);
246
- },
247
- set(value) {
248
- if (run)
249
- fn(value);
250
- return set == null ? void 0 : set.call(this, value);
233
+ function getNativeEvent(event) {
234
+ var _a;
235
+ return (_a = event.nativeEvent) != null ? _a : event;
236
+ }
237
+ function observeAttributes(node, attributes, fn) {
238
+ if (!node)
239
+ return noop;
240
+ const attrs = Array.isArray(attributes) ? attributes : [attributes];
241
+ const win = node.ownerDocument.defaultView || window;
242
+ const obs = new win.MutationObserver((changes) => {
243
+ for (const change of changes) {
244
+ if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
245
+ fn(change);
246
+ }
251
247
  }
252
248
  });
253
- return function() {
254
- run = false;
255
- };
249
+ obs.observe(node, { attributes: true, attributeFilter: attrs });
250
+ return () => obs.disconnect();
256
251
  }
257
- function getClosestFormElement(el) {
252
+ function getClosestForm(el) {
258
253
  if (isFormElement(el))
259
254
  return el.form;
260
255
  else
261
256
  return el.closest("form");
262
257
  }
263
258
  function isFormElement(el) {
264
- return ["textarea", "input", "select", "button"].includes(el.localName);
259
+ return el.matches("textarea, input, select, button");
265
260
  }
266
261
  function trackFormReset(el, callback) {
267
262
  if (!el)
268
263
  return;
269
- const form = getClosestFormElement(el);
264
+ const form = getClosestForm(el);
270
265
  form == null ? void 0 : form.addEventListener("reset", callback, { passive: true });
271
266
  return () => {
272
267
  form == null ? void 0 : form.removeEventListener("reset", callback);
273
268
  };
274
269
  }
270
+ function trackFieldsetDisabled(el, callback) {
271
+ const fieldset = el == null ? void 0 : el.closest("fieldset");
272
+ if (!fieldset)
273
+ return;
274
+ callback(fieldset.disabled);
275
+ return observeAttributes(fieldset, ["disabled"], () => callback(fieldset.disabled));
276
+ }
275
277
  var rtlKeyMap = {
276
278
  ArrowLeft: "ArrowRight",
277
279
  ArrowRight: "ArrowLeft",
@@ -427,10 +429,6 @@ findByTypeahead.defaultOptions = {
427
429
 
428
430
  // ../../utilities/number/dist/index.mjs
429
431
  var __pow2 = Math.pow;
430
- var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
431
- function formatter(n) {
432
- return parseFloat(nf.format(n));
433
- }
434
432
  function round(v, t2) {
435
433
  let num = valueOf(v);
436
434
  const p = __pow2(10, t2 != null ? t2 : 10);
@@ -445,17 +443,15 @@ function clamp(v, o) {
445
443
  function countDecimals(value) {
446
444
  if (!Number.isFinite(value))
447
445
  return 0;
448
- let e = 1;
449
- let p = 0;
446
+ let e = 1, p = 0;
450
447
  while (Math.round(value * e) / e !== value) {
451
448
  e *= 10;
452
449
  p += 1;
453
450
  }
454
451
  return p;
455
452
  }
456
- var increment = (v, s) => formatter(valueOf(v) + s);
457
- var decrement = (v, s) => formatter(valueOf(v) - s);
458
- var multiply = (v, s) => formatter(valueOf(v) * s);
453
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
454
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
459
455
  function snapToStep(value, step) {
460
456
  const num = valueOf(value);
461
457
  const p = countDecimals(step);
@@ -468,6 +464,18 @@ function valueOf(v) {
468
464
  const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
469
465
  return !Number.isNaN(num) ? num : 0;
470
466
  }
467
+ function decimalOperation(a, op, b) {
468
+ let result = op === "+" ? a + b : a - b;
469
+ if (a % 1 !== 0 || b % 1 !== 0) {
470
+ const multiplier = __pow2(10, Math.max(countDecimals(a), countDecimals(b)));
471
+ a = Math.round(a * multiplier);
472
+ b = Math.round(b * multiplier);
473
+ result = op === "+" ? a + b : a - b;
474
+ result /= multiplier;
475
+ }
476
+ return result;
477
+ }
478
+ var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
471
479
  var transform = (a, b) => {
472
480
  const i = { min: a[0], max: a[1] };
473
481
  const o = { min: b[0], max: b[1] };
@@ -588,6 +596,10 @@ var dom = {
588
596
  var _a;
589
597
  return (_a = ctx.doc) != null ? _a : document;
590
598
  },
599
+ getRootNode: (ctx) => {
600
+ var _a;
601
+ return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
602
+ },
591
603
  getRootId: (ctx) => {
592
604
  var _a, _b;
593
605
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `slider:${ctx.uid}`;
@@ -618,9 +630,10 @@ var dom = {
618
630
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `slider:${ctx.uid}:label`;
619
631
  },
620
632
  getMarkerId: (ctx, value) => `slider:${ctx.uid}:marker:${value}`,
621
- getThumbEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getThumbId(ctx)),
622
- getControlEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getControlId(ctx)),
623
- getInputEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getInputId(ctx)),
633
+ getRootEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getRootId(ctx)),
634
+ getThumbEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getThumbId(ctx)),
635
+ getControlEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getControlId(ctx)),
636
+ getInputEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getInputId(ctx)),
624
637
  getControlStyle,
625
638
  getThumbStyle,
626
639
  getRangeStyle,
@@ -660,7 +673,11 @@ var dom = {
660
673
 
661
674
  // ../../types/dist/index.mjs
662
675
  function createNormalizer(fn) {
663
- return { button: fn, label: fn, input: fn, output: fn, element: fn };
676
+ return new Proxy({}, {
677
+ get() {
678
+ return fn;
679
+ }
680
+ });
664
681
  }
665
682
  var normalizeProp = createNormalizer((v) => v);
666
683
 
@@ -756,7 +773,7 @@ function connect(state2, send, normalize = normalizeProp) {
756
773
  onKeyDown(event) {
757
774
  if (!isInteractive)
758
775
  return;
759
- const step = multiply(getEventStep(event), state2.context.step);
776
+ const step = getEventStep(event) * state2.context.step;
760
777
  let prevent = true;
761
778
  const keyMap = {
762
779
  ArrowUp() {
@@ -910,7 +927,7 @@ function machine(ctx = {}) {
910
927
  watch: {
911
928
  value: ["invokeOnChange", "dispatchChangeEvent"]
912
929
  },
913
- activities: ["trackFormReset", "trackScriptedUpdate"],
930
+ activities: ["trackFormReset", "trackFieldsetDisabled"],
914
931
  on: {
915
932
  SET_VALUE: {
916
933
  actions: "setValue"
@@ -998,17 +1015,12 @@ function machine(ctx = {}) {
998
1015
  isVertical: (ctx2) => ctx2.isVertical
999
1016
  },
1000
1017
  activities: {
1001
- trackScriptedUpdate(ctx2, _, { send }) {
1018
+ trackFieldsetDisabled(ctx2) {
1002
1019
  let cleanup;
1003
1020
  nextTick(() => {
1004
- const el = dom.getInputEl(ctx2);
1005
- if (!el)
1006
- return;
1007
- cleanup = trackInputPropertyMutation(el, {
1008
- type: "input",
1009
- property: "value",
1010
- fn(value) {
1011
- send({ type: "SET_VALUE", value: parseFloat(value) });
1021
+ cleanup = trackFieldsetDisabled(dom.getRootEl(ctx2), (disabled) => {
1022
+ if (disabled != ctx2.disabled) {
1023
+ ctx2.disabled = disabled;
1012
1024
  }
1013
1025
  });
1014
1026
  });
@@ -1017,15 +1029,13 @@ function machine(ctx = {}) {
1017
1029
  trackFormReset(ctx2) {
1018
1030
  let cleanup;
1019
1031
  nextTick(() => {
1020
- const el = dom.getInputEl(ctx2);
1021
- if (!el)
1022
- return;
1023
- cleanup = trackFormReset(el, () => {
1024
- if (ctx2.initialValue != null)
1032
+ cleanup = trackFormReset(dom.getInputEl(ctx2), () => {
1033
+ if (ctx2.initialValue != null) {
1025
1034
  ctx2.value = ctx2.initialValue;
1035
+ }
1026
1036
  });
1027
1037
  });
1028
- return cleanup;
1038
+ return () => cleanup == null ? void 0 : cleanup();
1029
1039
  },
1030
1040
  trackPointerMove(ctx2, _evt, { send }) {
1031
1041
  return trackPointerMove({
@@ -1043,6 +1053,8 @@ function machine(ctx = {}) {
1043
1053
  setupDocument(ctx2, evt) {
1044
1054
  if (evt.doc)
1045
1055
  ctx2.doc = (0, import_core.ref)(evt.doc);
1056
+ if (evt.root)
1057
+ ctx2.rootNode = (0, import_core.ref)(evt.root);
1046
1058
  ctx2.uid = evt.id;
1047
1059
  },
1048
1060
  checkValue(ctx2) {
@@ -1079,7 +1091,7 @@ function machine(ctx = {}) {
1079
1091
  ctx2.value = utils.clamp(ctx2, value);
1080
1092
  },
1081
1093
  focusThumb(ctx2) {
1082
- nextTick(() => {
1094
+ raf(() => {
1083
1095
  var _a;
1084
1096
  return (_a = dom.getThumbEl(ctx2)) == null ? void 0 : _a.focus();
1085
1097
  });