@zag-js/slider 0.1.4 → 0.1.7

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
@@ -42,19 +42,33 @@ function raf(fn) {
42
42
  globalThis.cancelAnimationFrame(id);
43
43
  };
44
44
  }
45
- var noop = () => {
46
- };
47
- var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
48
- var platform = (v) => isDom() && v.test(navigator.platform);
49
- var isDom = () => !!(typeof window !== "undefined");
50
- var isMac = () => platform(/^Mac/);
51
- var isIPhone = () => platform(/^iPhone/);
52
- var isIPad = () => platform(/^iPad/) || isMac() && navigator.maxTouchPoints > 1;
53
- var isIos = () => isIPhone() || isIPad();
45
+ var isDom = () => typeof window !== "undefined";
54
46
  var isArray = (v) => Array.isArray(v);
55
47
  var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
56
- var isMouseEvent = (v) => isObject(v) && "button" in v;
48
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
49
+ function getPlatform() {
50
+ var _a;
51
+ const agent = navigator.userAgentData;
52
+ return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
53
+ }
54
+ var pt = (v) => isDom() && v.test(getPlatform());
55
+ var isTouchDevice = isDom() && !!navigator.maxTouchPoints;
56
+ var isMac = () => pt(/^Mac/) && !isTouchDevice;
57
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
58
+ var isIos = () => isApple() && !isMac();
59
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
60
+ var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
61
+ var supportsMouseEvent = () => isDom() && window.onmousedown === null;
62
+ var isMouseEvent = (v) => isObject(v) && hasProp(v, "button");
63
+ var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
57
64
  var isLeftClick = (v) => v.button === 0;
65
+ var runIfFn = (v, ...a) => {
66
+ const res = typeof v === "function" ? v(...a) : v;
67
+ return res != null ? res : void 0;
68
+ };
69
+ var noop = () => {
70
+ };
71
+ var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
58
72
  function getListenerElements() {
59
73
  ;
60
74
  globalThis.__listenerElements__ = globalThis.__listenerElements__ || /* @__PURE__ */ new Map();
@@ -65,7 +79,7 @@ function getListenerCache() {
65
79
  globalThis.__listenerCache__ = globalThis.__listenerCache__ || /* @__PURE__ */ new Map();
66
80
  return globalThis.__listenerCache__;
67
81
  }
68
- function globalEventBus(node, type, handler, options) {
82
+ function addGlobalEventListener(node, type, handler, options) {
69
83
  var _a;
70
84
  if (!node)
71
85
  return noop;
@@ -117,10 +131,7 @@ function globalEventBus(node, type, handler, options) {
117
131
  }
118
132
  };
119
133
  }
120
- var t = (v) => Object.prototype.toString.call(v).slice(8, -1);
121
- var isRef = (v) => t(v) === "Object" && "current" in v;
122
- var runIfFn = (fn) => t(fn) === "Function" ? fn() : fn;
123
- var isTouchEvent = (v) => t(v) === "Object" && !!v.touches;
134
+ var isRef = (v) => hasProp(v, "current");
124
135
  var fallback = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
125
136
  function extractInfo(event, type = "page") {
126
137
  const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback : event;
@@ -133,7 +144,7 @@ function extractInfo(event, type = "page") {
133
144
  }
134
145
  function addDomEvent(target, event, listener, options) {
135
146
  const node = isRef(target) ? target.current : runIfFn(target);
136
- return globalEventBus(node, event, listener, options);
147
+ return addGlobalEventListener(node, event, listener, options);
137
148
  }
138
149
  function addPointerEvent(target, event, listener, options) {
139
150
  var _a;
@@ -156,9 +167,6 @@ function filterPrimaryPointer(fn) {
156
167
  fn(event);
157
168
  };
158
169
  }
159
- var supportsPointerEvent = () => typeof window !== "undefined" && window.onpointerdown === null;
160
- var supportsTouchEvent = () => typeof window !== "undefined" && window.ontouchstart === null;
161
- var supportsMouseEvent = () => typeof window !== "undefined" && window.onmousedown === null;
162
170
  var mouseEventNames = {
163
171
  pointerdown: "mousedown",
164
172
  pointermove: "mousemove",
@@ -188,66 +196,68 @@ function getOwnerWindow(el) {
188
196
  var _a;
189
197
  return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
190
198
  }
191
- function getNativeEvent(event) {
199
+ function getNativeEvent(e) {
192
200
  var _a;
193
- return (_a = event.nativeEvent) != null ? _a : event;
194
- }
195
- function getDescriptor(el, options) {
196
- var _a;
197
- const { type, property } = options;
198
- const win = getOwnerWindow(el);
199
- const _type = type === "input" ? "HTMLInputElement" : "HTMLTextAreaElement";
200
- const proto = win[_type].prototype;
201
- return (_a = Object.getOwnPropertyDescriptor(proto, property)) != null ? _a : {};
201
+ return (_a = e.nativeEvent) != null ? _a : e;
202
202
  }
203
- function dispatchInputValueEvent(el, value) {
204
- var _a;
205
- const win = getOwnerWindow(el);
206
- if (!(el instanceof win.HTMLInputElement))
207
- return;
208
- const desc = getDescriptor(el, { type: "input", property: "value" });
209
- (_a = desc.set) == null ? void 0 : _a.call(el, value);
210
- const event = new win.Event("input", { bubbles: true });
211
- el.dispatchEvent(event);
212
- }
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);
203
+ function observeAttributes(node, attributes, fn) {
204
+ if (!node)
205
+ return noop;
206
+ const attrs = Array.isArray(attributes) ? attributes : [attributes];
207
+ const win = node.ownerDocument.defaultView || window;
208
+ const obs = new win.MutationObserver((changes) => {
209
+ for (const change of changes) {
210
+ if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
211
+ fn(change);
212
+ }
227
213
  }
228
214
  });
229
- return function() {
230
- run = false;
231
- };
215
+ obs.observe(node, { attributes: true, attributeFilter: attrs });
216
+ return () => obs.disconnect();
232
217
  }
233
- function getClosestFormElement(el) {
218
+ function getClosestForm(el) {
234
219
  if (isFormElement(el))
235
220
  return el.form;
236
221
  else
237
222
  return el.closest("form");
238
223
  }
239
224
  function isFormElement(el) {
240
- return ["textarea", "input", "select", "button"].includes(el.localName);
225
+ return el.matches("textarea, input, select, button");
241
226
  }
242
227
  function trackFormReset(el, callback) {
243
228
  if (!el)
244
229
  return;
245
- const form = getClosestFormElement(el);
230
+ const form = getClosestForm(el);
246
231
  form == null ? void 0 : form.addEventListener("reset", callback, { passive: true });
247
232
  return () => {
248
233
  form == null ? void 0 : form.removeEventListener("reset", callback);
249
234
  };
250
235
  }
236
+ function trackFieldsetDisabled(el, callback) {
237
+ const fieldset = el == null ? void 0 : el.closest("fieldset");
238
+ if (!fieldset)
239
+ return;
240
+ callback(fieldset.disabled);
241
+ return observeAttributes(fieldset, ["disabled"], () => callback(fieldset.disabled));
242
+ }
243
+ function getDescriptor(el, options) {
244
+ var _a;
245
+ const { type, property } = options;
246
+ const win = getOwnerWindow(el);
247
+ const _type = type === "input" ? "HTMLInputElement" : "HTMLTextAreaElement";
248
+ const proto = win[_type].prototype;
249
+ return (_a = Object.getOwnPropertyDescriptor(proto, property)) != null ? _a : {};
250
+ }
251
+ function dispatchInputValueEvent(el, value) {
252
+ var _a;
253
+ const win = getOwnerWindow(el);
254
+ if (!(el instanceof win.HTMLInputElement))
255
+ return;
256
+ const desc = getDescriptor(el, { type: "input", property: "value" });
257
+ (_a = desc.set) == null ? void 0 : _a.call(el, value);
258
+ const event = new win.Event("input", { bubbles: true });
259
+ el.dispatchEvent(event);
260
+ }
251
261
  var rtlKeyMap = {
252
262
  ArrowLeft: "ArrowRight",
253
263
  ArrowRight: "ArrowLeft",
@@ -403,11 +413,11 @@ findByTypeahead.defaultOptions = {
403
413
 
404
414
  // ../../utilities/number/dist/index.mjs
405
415
  var __pow2 = Math.pow;
406
- function round(v, t2) {
416
+ function round(v, t) {
407
417
  let num = valueOf(v);
408
- const p = __pow2(10, t2 != null ? t2 : 10);
418
+ const p = __pow2(10, t != null ? t : 10);
409
419
  num = Math.round(num * p) / p;
410
- return t2 ? num.toFixed(t2) : v.toString();
420
+ return t ? num.toFixed(t) : v.toString();
411
421
  }
412
422
  var valueToPercent = (v, r) => (valueOf(v) - r.min) * 100 / (r.max - r.min);
413
423
  var percentToValue = (v, r) => r.min + (r.max - r.min) * valueOf(v);
@@ -462,13 +472,16 @@ var transform = (a, b) => {
462
472
  };
463
473
 
464
474
  // ../../utilities/rect/dist/index.mjs
475
+ var isDom2 = () => typeof window !== "undefined";
465
476
  var isArray2 = (v) => Array.isArray(v);
466
477
  var isObject2 = (v) => !(v == null || typeof v !== "object" || isArray2(v));
467
- var isTouchEvent2 = (v) => isObject2(v) && "touches" in v;
478
+ var hasProp2 = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
479
+ var isTouchDevice2 = isDom2() && !!navigator.maxTouchPoints;
480
+ var isTouchEvent2 = (v) => isObject2(v) && hasProp2(v, "touches");
468
481
  var fallback2 = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
469
- function getEventPoint(e, t2 = "page") {
482
+ function getEventPoint(e, t = "page") {
470
483
  const p = isTouchEvent2(e) ? e.touches[0] || e.changedTouches[0] || fallback2 : e;
471
- return { x: p[`${t2}X`], y: p[`${t2}Y`] };
484
+ return { x: p[`${t}X`], y: p[`${t}Y`] };
472
485
  }
473
486
  function relativeToNode(p, el) {
474
487
  const dx = p.x - el.offsetLeft - el.clientLeft + el.scrollLeft;
@@ -604,6 +617,7 @@ var dom = {
604
617
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `slider:${ctx.uid}:label`;
605
618
  },
606
619
  getMarkerId: (ctx, value) => `slider:${ctx.uid}:marker:${value}`,
620
+ getRootEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getRootId(ctx)),
607
621
  getThumbEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getThumbId(ctx)),
608
622
  getControlEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getControlId(ctx)),
609
623
  getInputEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getInputId(ctx)),
@@ -646,11 +660,17 @@ var dom = {
646
660
 
647
661
  // ../../types/dist/index.mjs
648
662
  function createNormalizer(fn) {
649
- return { button: fn, label: fn, input: fn, output: fn, element: fn };
663
+ return new Proxy({}, {
664
+ get() {
665
+ return fn;
666
+ }
667
+ });
650
668
  }
651
669
  var normalizeProp = createNormalizer((v) => v);
652
670
 
653
671
  // ../../utilities/core/dist/index.mjs
672
+ var isDom3 = () => typeof window !== "undefined";
673
+ var isTouchDevice3 = isDom3() && !!navigator.maxTouchPoints;
654
674
  var isLeftClick2 = (v) => v.button === 0;
655
675
  var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
656
676
 
@@ -896,7 +916,7 @@ function machine(ctx = {}) {
896
916
  watch: {
897
917
  value: ["invokeOnChange", "dispatchChangeEvent"]
898
918
  },
899
- activities: ["trackFormReset", "trackScriptedUpdate"],
919
+ activities: ["trackFormReset", "trackFieldsetDisabled"],
900
920
  on: {
901
921
  SET_VALUE: {
902
922
  actions: "setValue"
@@ -984,17 +1004,12 @@ function machine(ctx = {}) {
984
1004
  isVertical: (ctx2) => ctx2.isVertical
985
1005
  },
986
1006
  activities: {
987
- trackScriptedUpdate(ctx2, _, { send }) {
1007
+ trackFieldsetDisabled(ctx2) {
988
1008
  let cleanup;
989
1009
  nextTick(() => {
990
- const el = dom.getInputEl(ctx2);
991
- if (!el)
992
- return;
993
- cleanup = trackInputPropertyMutation(el, {
994
- type: "input",
995
- property: "value",
996
- fn(value) {
997
- send({ type: "SET_VALUE", value: parseFloat(value) });
1010
+ cleanup = trackFieldsetDisabled(dom.getRootEl(ctx2), (disabled) => {
1011
+ if (disabled != ctx2.disabled) {
1012
+ ctx2.disabled = disabled;
998
1013
  }
999
1014
  });
1000
1015
  });
@@ -1003,15 +1018,13 @@ function machine(ctx = {}) {
1003
1018
  trackFormReset(ctx2) {
1004
1019
  let cleanup;
1005
1020
  nextTick(() => {
1006
- const el = dom.getInputEl(ctx2);
1007
- if (!el)
1008
- return;
1009
- cleanup = trackFormReset(el, () => {
1010
- if (ctx2.initialValue != null)
1021
+ cleanup = trackFormReset(dom.getInputEl(ctx2), () => {
1022
+ if (ctx2.initialValue != null) {
1011
1023
  ctx2.value = ctx2.initialValue;
1024
+ }
1012
1025
  });
1013
1026
  });
1014
- return cleanup;
1027
+ return () => cleanup == null ? void 0 : cleanup();
1015
1028
  },
1016
1029
  trackPointerMove(ctx2, _evt, { send }) {
1017
1030
  return trackPointerMove({
@@ -1067,7 +1080,7 @@ function machine(ctx = {}) {
1067
1080
  ctx2.value = utils.clamp(ctx2, value);
1068
1081
  },
1069
1082
  focusThumb(ctx2) {
1070
- nextTick(() => {
1083
+ raf(() => {
1071
1084
  var _a;
1072
1085
  return (_a = dom.getThumbEl(ctx2)) == null ? void 0 : _a.focus();
1073
1086
  });